Resolving OSGi tests when upgrading from Groovy 2.4 to 2.5 due to "Fragment bundles can not be started"?

148 Views Asked by At

This is a follow-up question to How to upgrade groovy-all from 2.4 to 2.5 when running in OSGi?. The groovy-all artifact is no longer available as a jar file in version 2.5 in Groovy so the suggestion in the answer above was to instead use the individual jar files. I have a test case using Pax Exam that looks like this:

@RunWith(PaxExam.class)
public class XmlPathOSGiITest {

    @Configuration
    public static Option[] configure() {
        return new Option[]
                {
                        mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.hamcrest", "1.3_1"),
                        junitBundles(),
                        systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
                        systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"),

                        /* Transitive dependencies needed in the Pax Exam container.
                        Some of these need to be wrapped because they are not available as OSGi bundles */
                        mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
                        wrappedBundle(mavenBundle().groupId("org.ccil.cowan.tagsoup").artifactId("tagsoup").versionAsInProject()),
                        wrappedBundle(mavenBundle("javax.xml.bind", "jaxb-api").versionAsInProject()),
                        wrappedBundle(mavenBundle("javax.activation", "activation").version("1.1.1")),
                        wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-all").version("2.4.12")),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpclient").versionAsInProject()),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpmime").versionAsInProject()),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpcore").versionAsInProject()),

                        /* Rest Assured dependencies needed in the Pax Exam container to be able to execute the tests below */
                        mavenBundle("io.rest-assured", "json-path").versionAsInProject(),
                        mavenBundle("io.rest-assured", "xml-path").versionAsInProject(),
                        mavenBundle("io.rest-assured", "rest-assured").versionAsInProject(),
                        mavenBundle("io.rest-assured", "rest-assured-common").versionAsInProject()
                };
    }

    @Test
    public void getUUIDParsesAStringResultToUUID() {
        final String UUID_XML = "<some>\n" +
                "  <thing id=\"1\">db24eeeb-7fe5-41d3-8f06-986b793ecc91</thing>\n" +
                "  <thing id=\"2\">d69ded28-d75c-460f-9cbe-1412c60ed4cc</thing>\n" +
                "</some>";

        final UUID uuid = from(UUID_XML).getUUID("some.thing[0]");

        assertThat(uuid, Matchers.equalTo(UUID.fromString("db24eeeb-7fe5-41d3-8f06-986b793ecc91")));
    }
}

where the point of interest is:

wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-all").version("2.4.12")),

Now I want to upgrade to Groovy 2.5.6 so I replace the line above with:

wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy").version("2.5.6")),
wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-json").version("2.5.6")),
wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-xml").version("2.5.6")),

But now when I re-run my test I get the following error:

org.osgi.framework.BundleException: Fragment bundles can not be started.

    at org.apache.felix.framework.Felix.startBundle(Felix.java:2144)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
    at org.ops4j.pax.swissbox.framework.RemoteFrameworkImpl.startBundle(RemoteFrameworkImpl.java:178)

How can I resolve this?

1

There are 1 best solutions below

4
On

You need to check after re-run your bundle if its fragmented:

private static boolean isBundleFragment(final Bundle bundle) {
    boolean frament = false;
    final Dictionary<String, String> dictionary = bundle.getHeaders();

    if (dictionary.get("Fragment-Host") != null) {
        frament = true;
    }
    return frament;
}

When it happens you need to stop the bundle first:

if (!isBundleFragment(bundle)) {
    bundle.stop();
}

Then start your bundle or update it. Hope it helps!