Test Internal Class Using Pax Exam

282 Views Asked by At

Currently I have test class for testing internal class using Pax-Exam 5.

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
   probe.setHeader("Fragment-Host", "com.mycompany.abc");
   return probe;
}

The reason I use Fragment-Host is to avoid exporting internal package for bundle com.mycompany.abc. But I get this error

org.ops4j.pax.exam.TestContainerException: org.osgi.framework.BundleException: Invalid operation on a fragment.
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:135)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:140)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.installProbe(NativeTestContainer.java:428)
    at org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactor.setUp(EagerSingleStagedReactor.java:68)
    at org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactor.beforeClass(EagerSingleStagedReactor.java:106)
    at org.ops4j.pax.exam.spi.reactors.ReactorManager.beforeClass(ReactorManager.java:400)
    at org.ops4j.pax.exam.junit.DriverExtension.beforeClassBlock(DriverExtension.java:130)
    at org.ops4j.pax.exam.junit.ExtensibleRunner$1.evaluate(ExtensibleRunner.java:53)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.ops4j.pax.exam.junit.PaxExam.run(PaxExam.java:78)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.osgi.framework.BundleException: Invalid operation on a fragment.
    at org.eclipse.osgi.container.Module.checkFragment(Module.java:520)
    at org.eclipse.osgi.container.Module.start(Module.java:408)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:428)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:447)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:131)
... 15 more

Is there any other solution?

Thanks

1

There are 1 best solutions below

0
On

Pax Exam supports provisioning of fragments because you can call .noStart() on the provisioning Option to tell Pax Exam that the bundle/fragment must not be started:

CoreOptions.mavenBundle("gid", "aid", "version").noStart();

But what you are trying to achieve is different, you are trying to make your probe bundle into a fragment of the tested bundle.

You may still achieve your goal without having to make the probe a fragment as follows:

  1. Use TinyBundles to create a fragment that exports the internal package(s) of the test bundle
  2. Pass the fragment to Pax Exam as an url() option with .noStart()

see the following class for an example:

https://github.com/ops4j/org.ops4j.pax.exam2/blob/exam-reactor-3.2.0/itest/osgi/src/it/regression-multi/src/test/java/org/ops4j/pax/exam/regression/multi/fragment/FragmentTest.java#L61

In other words, rather than making the probe a fragment of the host, create on the fly an empty fragment just to export the packages of the host for the benefit of the probe.

Disclaimer: I did not try this, it's just an hypothesis.