Search in sources :

Example 11 with ManagedServiceTestActivator

use of org.apache.felix.cm.integration.helper.ManagedServiceTestActivator in project felix by apache.

the class ConfigurationBindingTest method test_configuration_unbound_on_uninstall_with_cm_restart.

@Test
public void test_configuration_unbound_on_uninstall_with_cm_restart() throws BundleException {
    final String pid = "test_configuration_unbound_on_uninstall_with_cm_restart";
    configure(pid);
    final Bundle cmBundle = getCmBundle();
    // ensure configuration is unbound
    final Configuration beforeInstall = getConfiguration(pid);
    TestCase.assertNull(beforeInstall.getBundleLocation());
    bundle = installBundle(pid);
    // ensure no configuration bound before start
    final Configuration beforeStart = getConfiguration(pid);
    TestCase.assertNull(beforeInstall.getBundleLocation());
    TestCase.assertNull(beforeStart.getBundleLocation());
    bundle.start();
    final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
    TestCase.assertNotNull("IOActivator not started !!", tester);
    // give cm time for distribution
    delay();
    // assert activater has configuration
    TestCase.assertNotNull("Expect Properties after Service Registration", tester.props);
    TestCase.assertEquals("Expect a single update call", 1, tester.numManagedServiceUpdatedCalls);
    // ensure a freshly retrieved object also has the location
    final Configuration beforeStop = getConfiguration(pid);
    TestCase.assertEquals(beforeStop.getBundleLocation(), bundle.getLocation());
    // check whether bundle context is set on first configuration
    TestCase.assertEquals(beforeInstall.getBundleLocation(), bundle.getLocation());
    TestCase.assertEquals(beforeStart.getBundleLocation(), bundle.getLocation());
    bundle.stop();
    // ensure configuration still bound
    TestCase.assertEquals(beforeInstall.getBundleLocation(), bundle.getLocation());
    TestCase.assertEquals(beforeStart.getBundleLocation(), bundle.getLocation());
    TestCase.assertEquals(beforeStop.getBundleLocation(), bundle.getLocation());
    // ensure a freshly retrieved object also has the location
    final Configuration beforeUninstall = getConfiguration(pid);
    TestCase.assertEquals(beforeUninstall.getBundleLocation(), bundle.getLocation());
    // stop cm bundle now before uninstalling configured bundle
    cmBundle.stop();
    delay();
    // assert configuration admin service is gone
    TestCase.assertNull(configAdminTracker.getService());
    // uninstall bundle while configuration admin is stopped
    bundle.uninstall();
    bundle = null;
    // start cm bundle again after uninstallation
    cmBundle.start();
    delay();
    // ensure a freshly retrieved object also does not have the location
    // FELIX-1484: this test fails due to bundle location not verified
    // at first configuration access
    final Configuration atEnd = getConfiguration(pid);
    TestCase.assertNull(atEnd.getBundleLocation());
    // remove the configuration for good
    deleteConfig(pid);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ManagedServiceTestActivator(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator) Test(org.junit.Test)

Example 12 with ManagedServiceTestActivator

use of org.apache.felix.cm.integration.helper.ManagedServiceTestActivator in project felix by apache.

the class ConfigurationBindingTest method test_static_binding.

@Test
public void test_static_binding() throws BundleException {
    final String pid = "test_static_binding";
    // install a bundle to get a location for binding
    bundle = installBundle(pid);
    final String location = bundle.getLocation();
    // create and statically bind the configuration
    configure(pid);
    final Configuration config = getConfiguration(pid);
    TestCase.assertEquals(pid, config.getPid());
    TestCase.assertNull(config.getBundleLocation());
    config.setBundleLocation(location);
    TestCase.assertEquals(location, config.getBundleLocation());
    // ensure configuration is settled before starting the bundle
    delay();
    configListener.assertEvents(ConfigurationEvent.CM_UPDATED, 1);
    // start the bundle
    bundle.start();
    delay();
    TestCase.assertEquals(location, config.getBundleLocation());
    configListener.assertEvents(ConfigurationEvent.CM_LOCATION_CHANGED, 1);
    // assert the configuration is supplied
    final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
    TestCase.assertNotNull("Activator not started !!", tester);
    TestCase.assertNotNull("Expect Properties after Service Registration", tester.props);
    TestCase.assertEquals("Expect a single update call", 1, tester.numManagedServiceUpdatedCalls);
    // remove the static binding and assert bound (again)
    config.setBundleLocation(null);
    delay();
    TestCase.assertEquals(location, config.getBundleLocation());
    configListener.assertEvents(ConfigurationEvent.CM_LOCATION_CHANGED, 2);
    // uninstall bundle and assert configuration unbound
    bundle.uninstall();
    bundle = null;
    delay();
    TestCase.assertNull(config.getBundleLocation());
    configListener.assertEvents(ConfigurationEvent.CM_LOCATION_CHANGED, 1);
}
Also used : Configuration(org.osgi.service.cm.Configuration) ManagedServiceTestActivator(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator) Test(org.junit.Test)

Example 13 with ManagedServiceTestActivator

use of org.apache.felix.cm.integration.helper.ManagedServiceTestActivator in project felix by apache.

the class ConfigurationBindingTest method test_switch_static_binding.

@Test
public void test_switch_static_binding() throws BundleException {
    // 1. create config with pid and locationA
    // 2. update config with properties
    final String pid = "test_switch_static_binding";
    final String locationA = "test:location/A/" + pid;
    final Configuration config = configure(pid, locationA, true);
    // 3. register ManagedService ms1 with pid from said locationA
    final Bundle bundleA = installBundle(pid, ManagedServiceTestActivator.class, locationA);
    bundleA.start();
    delay();
    // ==> configuration supplied to the service ms1
    final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
    TestCase.assertNotNull(testerA1.props);
    TestCase.assertEquals(1, testerA1.numManagedServiceUpdatedCalls);
    // 4. register ManagedService ms2 with pid from locationB
    final String locationB = "test:location/B/" + pid;
    final Bundle bundleB = installBundle(pid, ManagedServiceTestActivator2.class, locationB);
    bundleB.start();
    delay();
    // ==> invisible configuration supplied as null to service ms2
    final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
    TestCase.assertNull(testerB1.props);
    TestCase.assertEquals(1, testerB1.numManagedServiceUpdatedCalls);
    // 5. Call Configuration.setBundleLocation( "locationB" )
    config.setBundleLocation(locationB);
    delay();
    // ==> configuration is bound to locationB
    TestCase.assertEquals(locationB, config.getBundleLocation());
    // ==> configuration removed from service ms1
    TestCase.assertNull(testerA1.props);
    TestCase.assertEquals(2, testerA1.numManagedServiceUpdatedCalls);
    // ==> configuration supplied to the service ms2
    TestCase.assertNotNull(testerB1.props);
    TestCase.assertEquals(2, testerB1.numManagedServiceUpdatedCalls);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ManagedServiceTestActivator(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator) ManagedServiceTestActivator2(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator2) Test(org.junit.Test)

Example 14 with ManagedServiceTestActivator

use of org.apache.felix.cm.integration.helper.ManagedServiceTestActivator in project felix by apache.

the class MultiValuePIDTest method test_multi_value_pid_array.

@Test
public void test_multi_value_pid_array() throws BundleException {
    final String pid1 = "test.pid.1";
    final String pid2 = "test.pid.2";
    configure(pid1);
    configure(pid2);
    final Configuration config1 = getConfiguration(pid1);
    TestCase.assertEquals(pid1, config1.getPid());
    TestCase.assertNull(config1.getBundleLocation());
    final Configuration config2 = getConfiguration(pid2);
    TestCase.assertEquals(pid2, config2.getPid());
    TestCase.assertNull(config2.getBundleLocation());
    // multi-pid with array
    bundle = installBundle(pid1 + "," + pid2);
    bundle.start();
    // give cm time for distribution
    delay();
    final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
    TestCase.assertNotNull("Activator not started !!", tester);
    // assert activater has configuration (two calls, one per pid)
    TestCase.assertNotNull("Expect Properties after Service Registration", tester.props);
    TestCase.assertEquals("Expect a single update call", 2, tester.numManagedServiceUpdatedCalls);
    TestCase.assertEquals(bundle.getLocation(), config1.getBundleLocation());
    TestCase.assertEquals(bundle.getLocation(), config2.getBundleLocation());
    bundle.uninstall();
    bundle = null;
    delay();
    TestCase.assertNull(config1.getBundleLocation());
    TestCase.assertNull(config2.getBundleLocation());
    // remove the configuration for good
    deleteConfig(pid1);
    deleteConfig(pid2);
}
Also used : Configuration(org.osgi.service.cm.Configuration) ManagedServiceTestActivator(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator) Test(org.junit.Test)

Example 15 with ManagedServiceTestActivator

use of org.apache.felix.cm.integration.helper.ManagedServiceTestActivator in project felix by apache.

the class TargetedPidTest method test_pid_with_pipe.

@Test
public void test_pid_with_pipe() throws BundleException {
    final String pid0 = "test_targeted";
    final String pid1 = String.format("%s|%s", pid0, ManagedServiceTestActivator.class.getName());
    try {
        // start the bundle and assert this
        bundle = installBundle(pid1);
        configure(pid0);
        configure(pid1);
        bundle.start();
        final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
        TestCase.assertNotNull("Activator not started !!", tester);
        // give cm time for distribution
        delay();
        // assert activater has configuration
        int callCount = 0;
        TestCase.assertNotNull("Expect Properties after update", tester.props);
        TestCase.assertEquals("Expect PID", pid1, tester.props.get(Constants.SERVICE_PID));
        TestCase.assertEquals("Expect calls", ++callCount, tester.numManagedServiceUpdatedCalls);
        // delete pid1 - don't expect pid0 is assigned
        deleteConfig(pid1);
        delay();
        // final delete
        TestCase.assertNull("Expect no Properties after delete", tester.props);
        TestCase.assertEquals("Expect calls", ++callCount, tester.numManagedServiceUpdatedCalls);
        // cleanup
        bundle.uninstall();
        bundle = null;
    } finally {
        // remove the configuration for good
        deleteConfig(pid0);
        deleteConfig(pid1);
    }
}
Also used : ManagedServiceTestActivator(org.apache.felix.cm.integration.helper.ManagedServiceTestActivator) Test(org.junit.Test)

Aggregations

ManagedServiceTestActivator (org.apache.felix.cm.integration.helper.ManagedServiceTestActivator)26 Test (org.junit.Test)26 Configuration (org.osgi.service.cm.Configuration)20 Bundle (org.osgi.framework.Bundle)8 ManagedServiceTestActivator2 (org.apache.felix.cm.integration.helper.ManagedServiceTestActivator2)5 MultiManagedServiceTestActivator (org.apache.felix.cm.integration.helper.MultiManagedServiceTestActivator)2 Hashtable (java.util.Hashtable)1