Java – using OSGi declarative services in the context of JUnit testing

I'm trying to figure out how to use JUnit to implement multi - bundle integration testing in OSGi

By integration testing, I mean instantiating a subset of bundles to automatically verify the functions in the subsystem

We are running equinox and using eclipse as the tool chain Eclipse provides the "run as JUnit plug-in" option, which enables the OSGi framework to start and instantiate the configuration package, so I guess this is the path to follow, but I haven't found a way to inject DS references into my tests I've seen the use of servicetracker as a programming way to access different service packages, but the purpose of this is to make DS, isn't it?

I've just started using OSGi, so I think I just want some puzzles. Let me put my multi bundle tests together

Any ideas?

Thank you, Gerard

*Editing: Solutions*

After further studying this problem, I finally figured out how to use the JUnit plug-in function to put this multi bundle integration test in place:

For dynamic service injection to work, a service definition file must be created, in which the injected dependencies must be declared, because it is usually completed when using DS This file is usually located in the OSGi - inf / directory For example, osgi-inf / service In XML

service. XML must declare the dependencies required for this test, but does not provide its own services:

service.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="MyTest" activate="startup" deactivate="shutdown">

   <implementation class="com.test.functionaltest.MyTester"/>
   <reference name="OtherService" interface="com.product.service.FooService" policy="static" cardinality="1..1" bind="onServiceUp" unbind="onServiceDown"/>

</scr:component>

This instructs DS to inject dependencies on fooservice using the declared onserviceup method Onservicedown must be called in the OSGi shutdown phase after running the test

com. test. functionaltest. Mytester contains the test methods to be executed, following typical JUnit practices

Here, this is all by the book However, if JUnit runs, NullPointerException will be thrown when accessing the reference of fooservice The reason is that the OSGi framework is in competition with the JUnit test run environment. Usually, the JUnit test run will win and execute the test before injecting the reference of the required service

To solve this situation, you need to make JUnit test wait for OSGi to run I solve this problem by using countdownlatch, which is initialized to the number of dependent services required in the test Then, each dependency injection method will count down, and when they are completed, the test will begin The code is as follows:

private static CountDownLatch dependencyLatch = new CountDownLatch(1);// 1 = number of dependencies required    
static FooService  fooService = null;   
public void onFooServiceUp(FooService service) {
  fooService = service;
  dependencyLatch.countDown();
}

Note that fooservice references need to be static to allow service references to be shared between OSGi and JUnit execution contexts Countdownlatch provides an advanced synchronization mechanism for safely publishing this shared reference

Then, you should add a dependency check before the test is executed:

@Before
public void dependencyCheck() {
  // Wait for OSGi dependencies
    try {
      dependencyLatch.await(10,TimeUnit.SECONDS); 
      // Dependencies fulfilled
    } catch (InterruptedException ex)  {
      fail("OSGi dependencies unfulfilled");
    }
}

In this way, JUnit framework waits for OSGi DS service to inject dependency, or fails after timeout

It took me some time to fully understand this I hope I can save some headaches for fellow programmers in the future

Solution

*Editing: Solutions*

After further study of the problem, or failure after timeout

It took me some time to fully understand this I hope I can save some headaches for fellow programmers in the future

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>