netty5/testsuite-osgi/src/test/java/io/netty/osgitests/OsgiBundleTest.java
Robert Varga fa64625522 Add a pax-exam based OSGi test suite
Motivation:

Release 4.0.25 was not usable in OSGi environments due to a simple typo.
An automated test could have caught the problem even before it was
committed.

Modifications:

This patch introduces a new artifact, osgitests, which pulls in all
production artifacts (which we want to be checked for OSGi compliance).
It contains only a single unit test, which runs a pax-exam container
with felix OSGi.

At initialization time, it scans all the artifact's dependencies,
looking for things belonging to io.netty group. The container is
configured to deploy those artifacts as bundles and fail if any bundle
is found to be unresolved. It performs a final check to see if any
bundles were tested this way, to make sure the mechanism is not
completely broken.

We are using wrappedBundle(), as two of our third-party dependencies do
not export packages correctly -- this masks the problem, assuming that
whoever deploys our artifacts depending on them will figure out how to
OSGify them.

Result:

Simple typos and other bundle manifest errors should be caught during
test phase of every build.
2015-02-18 16:00:15 +01:00

110 lines
3.8 KiB
Java

/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.osgitests;
import static org.junit.Assert.assertFalse;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
@RunWith(PaxExam.class)
public class OsgiBundleTest {
private static final Pattern SLASH = Pattern.compile("/", Pattern.LITERAL);
private static final String DEPENCIES_LINE = "# dependencies";
private static final String GROUP = "io.netty";
private static final Collection<String> BUNDLES;
static {
final Set<String> artifacts = new HashSet<String>();
final File f = new File("target/classes/META-INF/maven/dependencies.properties");
try {
final BufferedReader r = new BufferedReader(new FileReader(f));
try {
boolean haveDeps = false;
while (true) {
final String line = r.readLine();
if (line == null) {
// End-of-file
break;
}
// We need to ignore any lines up to the dependencies
// line, otherwise we would include ourselves.
if (DEPENCIES_LINE.equals(line)) {
haveDeps = true;
} else if (haveDeps && line.startsWith(GROUP)) {
final String[] split = SLASH.split(line);
if (split.length > 1) {
artifacts.add(split[1]);
}
}
}
} finally {
r.close();
}
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
BUNDLES = artifacts;
}
@Configuration
public final Option[] config() {
final Collection<Option> options = new ArrayList<Option>();
options.add(systemProperty("pax.exam.osgi.unresolved.fail").value("true"));
options.addAll(Arrays.asList(junitBundles()));
options.add(mavenBundle("com.barchart.udt", "barchart-udt-bundle").versionAsInProject());
options.add(wrappedBundle(mavenBundle("com.twitter", "hpack").versionAsInProject()));
options.add(wrappedBundle(mavenBundle("org.rxtx", "rxtx").versionAsInProject()));
for (String name : BUNDLES) {
options.add(mavenBundle(GROUP, name).versionAsInProject());
}
return options.toArray(new Option[0]);
}
@Test
public void testResolvedBundles() {
// No-op, as we just want the bundles to be resolved. Just check if we tested something
assertFalse("At least one bundle needs to be tested", BUNDLES.isEmpty());
}
}