Pages

Subscribe:

Ads 468x60px

Thursday, October 15, 2015

OSGi Unit Test Framework for Carbon 5


Introduction
In the current carbon kernel there is no way to write test cases in order to test the OSGi services and functions. This is purely because of the bundles needs to be installed and run on a OSGi  container. Pax- Exam [1] framework provides the underneath infrastructure to compose unit test cases which needs to be run.

How Pax-Exam Works
Pax exam starts up the OSGi framework, (which is equinox for the carbon kernel), with minimum level of bundles which is necessary for the pax-exam operations. Then the Pax Exam user can provision their bundles to the OSGi container. Refer [2] for the provisioned bundles list [2]. This way when the Pax-Exam boots up these provisioned bundles will be installed to the container

How to write Unit test case

Now the Pax-Exam have booted up the OSGi framework with the necessary bundles for your test environment. You have to use the  @RunWith(PaxExam.class) annotation to hook Pax Exam into Testng and let it do its magic of setting up a test container with an OSGi framework and your bundles to run the tests.

[3] is a sample test case added. In the sample you can see that it is very similar to a normal testng test case and containing some annotations which relates to pax-exam.

Using Dependency Injection your test method can access the BundleContext of the probe bundle or any service obtained from the OSGi service registry.

    @Inject
    private BundleContext bundleContext;


or else you can directly access the OSGi services using the following

    @Inject
    private SomeSampleOsgiService service;


If you need any special bundles other than defined in [2], you can always override the config method in your test case class. But make sure that testng bundle must be installed.

@Configuration
    public Option[] config() {
        return options(
mavenBundle().artifactId("testng").groupId("org.testng").versionAsInProject(),
            mavenBundle("sample", "org.wso2.carbon", "1.0.0-SNAPSHOT")
            );
    }

[1].https://ops4j1.jira.com/wiki/display/PAXEXAM4/Getting+Started+with+OSGi+Tests
[2].https://github.com/wso2/carbon4-kernel/blob/c5-pax-exam-work/tests/osgi-tests/src/test/java/org/wso2/carbon/osgi/config/CarbonOSGiConfiguration.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright 2015 WSO2, Inc. http://www.wso2.org
 *
 * Licensed 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 org.wso2.carbon.osgi;

import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.ops4j.pax.exam.testng.listener.PaxExam;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import javax.inject.Inject;

/**
 * Base OSGi class to test the OSGi status of the org.wso2.carbon.core bundle
 */
@Listeners(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class BaseOSGiTest {

    @Inject
    private BundleContext bundleContext;

    @Test
    public void testBundleContextStatus() {
        Assert.assertNotNull(bundleContext, "Bundle Context is null");
    }

    @Test
    public void testCarbonCoreBundleStatus() {

        Bundle coreBundle = null;
        for (Bundle bundle : bundleContext.getBundles()) {
            if (bundle.getSymbolicName().equals("org.wso2.carbon.core")) {
                coreBundle = bundle;
                break;
            }
        }
        Assert.assertNotNull(coreBundle, "Carbon Core bundle not found");
        Assert.assertEquals(coreBundle.getState(), Bundle.ACTIVE, "Carbon Core Bundle is not activated");
    }
}



No comments:

Post a Comment