Search in sources :

Example 76 with ComponentInstance

use of org.apache.felix.ipojo.ComponentInstance in project felix by apache.

the class TestSupported method testExpectedExceptionOutsideTransactionWithCallback.

@Test(expected = UnsupportedOperationException.class)
public void testExpectedExceptionOutsideTransactionWithCallback() {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("supported-cb");
    Assert.assertEquals(ComponentInstance.VALID, prov.getState());
    Assert.assertEquals(ComponentInstance.VALID, under.getState());
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), under.getInstanceName());
    Assert.assertNotNull(ref);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref);
    cs.doSomethingBad2();
    Assert.assertNull(cs.getLastRolledBack());
    Assert.assertNull(cs.getLastCommitted());
    Assert.assertEquals(0, cs.getNumberOfCommit());
    Assert.assertEquals(0, cs.getNumberOfRollback());
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.transaction.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 77 with ComponentInstance

use of org.apache.felix.ipojo.ComponentInstance in project felix by apache.

the class TestSupported method testExceptionInsideTransactionRB.

@Test
public void testExceptionInsideTransactionRB() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("supported-ok");
    Assert.assertEquals(ComponentInstance.VALID, prov.getState());
    Assert.assertEquals(ComponentInstance.VALID, under.getState());
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), under.getInstanceName());
    Assert.assertNotNull(ref);
    osgiHelper.waitForService(TransactionManager.class.getName(), null, 5000);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref);
    TransactionManager tm = (TransactionManager) osgiHelper.getServiceObject(TransactionManager.class.getName(), null);
    tm.begin();
    Transaction t = tm.getTransaction();
    try {
        cs.doSomethingBad();
        Assert.fail("NullPointerException expected");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof NullPointerException);
    }
    Transaction t2 = cs.getCurrentTransaction();
    Assert.assertSame(t2, t);
    Assert.assertEquals(Status.STATUS_MARKED_ROLLBACK, t.getStatus());
    t.rollback();
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.transaction.services.CheckService) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 78 with ComponentInstance

use of org.apache.felix.ipojo.ComponentInstance in project felix by apache.

the class TimeoutTest method testDelay.

@Test
public void testDelay() {
    String prov = "provider";
    ComponentInstance provider = ipojoHelper.createComponentInstance("FooProvider", prov);
    String un = "under-1";
    ComponentInstance under = ipojoHelper.createComponentInstance("CheckServiceProviderTimeout", un);
    ServiceReference ref_fs = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov);
    assertNotNull("Check foo availability", ref_fs);
    ServiceReference ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability", ref_cs);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(ref_cs);
    assertTrue("Check invocation", cs.check());
    // Stop the provider.
    provider.stop();
    assertNull("No FooService", osgiHelper.getServiceReference(FooService.class.getName(), null));
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 2", ref_cs);
    long begin = System.currentTimeMillis();
    DelayedProvider dp = new DelayedProvider(provider, 200);
    dp.start();
    cs = (CheckService) osgiHelper.getRawServiceObject(ref_cs);
    assertTrue("Check invocation - 2", cs.check());
    long end = System.currentTimeMillis();
    assertTrue("Assert delay (" + (end - begin) + ")", (end - begin) >= 200);
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 3", ref_cs);
    cs = (CheckService) osgiHelper.getRawServiceObject(ref_cs);
    assertTrue("Check invocation - 3", cs.check());
    provider.stop();
    provider.dispose();
    under.stop();
    under.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.test.dependencies.timeout.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.test.dependencies.timeout.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 79 with ComponentInstance

use of org.apache.felix.ipojo.ComponentInstance in project felix by apache.

the class TestCallbacks method testWithPostUnregistrationOnly.

@Test
public void testWithPostUnregistrationOnly() {
    ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Callbacks-unreg-only");
    // Controller set to true.
    osgiHelper.waitForService(FooService.class.getName(), null, 5000);
    osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
    CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
    assertNotNull(check);
    Integer reg = (Integer) check.getProps().get("registered");
    Integer unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(0), reg);
    assertEquals(new Integer(0), unreg);
    ci.stop();
    reg = (Integer) check.getProps().get("registered");
    unreg = (Integer) check.getProps().get("unregistered");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertEquals(new Integer(0), reg);
    assertEquals(new Integer(1), unreg);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Test(org.junit.Test)

Example 80 with ComponentInstance

use of org.apache.felix.ipojo.ComponentInstance in project felix by apache.

the class TestCallbacks method testWithTwoPairsOfCallbacks.

@Test
public void testWithTwoPairsOfCallbacks() {
    ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Callbacks-both-2");
    // Controller set to true.
    osgiHelper.waitForService(FooService.class.getName(), null, 5000);
    osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
    CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
    assertNotNull(check);
    Integer reg = (Integer) check.getProps().get("registered");
    Integer unreg = (Integer) check.getProps().get("unregistered");
    Integer reg2 = (Integer) check.getProps().get("registered2");
    Integer unreg2 = (Integer) check.getProps().get("unregistered2");
    assertNotNull(reg);
    assertNotNull(unreg);
    assertNotNull(reg2);
    assertNotNull(unreg2);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(0), unreg);
    assertEquals(new Integer(1), reg2);
    assertEquals(new Integer(0), unreg2);
    ci.stop();
    reg = (Integer) check.getProps().get("registered");
    unreg = (Integer) check.getProps().get("unregistered");
    reg2 = (Integer) check.getProps().get("registered2");
    unreg2 = (Integer) check.getProps().get("unregistered2");
    assertNotNull(reg2);
    assertNotNull(unreg2);
    assertEquals(new Integer(1), reg);
    assertEquals(new Integer(1), unreg);
    assertEquals(new Integer(1), reg2);
    assertEquals(new Integer(1), unreg2);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Test(org.junit.Test)

Aggregations

ComponentInstance (org.apache.felix.ipojo.ComponentInstance)373 Test (org.junit.Test)360 ServiceReference (org.osgi.framework.ServiceReference)215 Properties (java.util.Properties)142 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65 Factory (org.apache.felix.ipojo.Factory)53 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)50 ServiceContext (org.apache.felix.ipojo.ServiceContext)47 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)39 IOException (java.io.IOException)29 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)22 BundleContext (org.osgi.framework.BundleContext)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 Dictionary (java.util.Dictionary)12 Hashtable (java.util.Hashtable)12 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)10 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)9