Search in sources :

Example 26 with CheckService

use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.

the class TestRequiresNew method testExpectedExceptionInsideTransaction.

@Test
public void testExpectedExceptionInsideTransaction() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requiresnew-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.doSomethingBad2();
        Assert.fail("UnsupportedOperationException expected");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
    Transaction t2 = cs.getCurrentTransaction();
    Assert.assertNotSame(t2, t);
    Assert.assertEquals(Status.STATUS_NO_TRANSACTION, t2.getStatus());
    Assert.assertEquals(Status.STATUS_ACTIVE, t.getStatus());
    t.commit();
}
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 27 with CheckService

use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.

the class TestRequiresNew method testOkInsideTransaction.

@Test
public void testOkInsideTransaction() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requiresnew-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();
    cs.doSomethingGood();
    Transaction t2 = cs.getCurrentTransaction();
    // Two different transactions
    Assert.assertNotSame(t2, t);
    // Check that t2 is commited => no transaction
    Assert.assertEquals(Status.STATUS_NO_TRANSACTION, t2.getStatus());
    t.commit();
}
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 28 with CheckService

use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.

the class TestRequiresNew method testOkOutsideTransaction.

@Test
public void testOkOutsideTransaction() {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requiresnew-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);
    // Ok
    ((CheckService) osgiHelper.getServiceObject(ref)).doSomethingGood();
}
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 29 with CheckService

use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.

the class TestRequiresNew method testExpectedExceptionOutsideTransaction.

@Test(expected = UnsupportedOperationException.class)
public void testExpectedExceptionOutsideTransaction() {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requiresnew-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);
    ((CheckService) osgiHelper.getServiceObject(ref)).doSomethingBad2();
}
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 30 with CheckService

use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.

the class TestRequiresNew method testExceptionInsideTransaction.

@Test
public void testExceptionInsideTransaction() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requiresnew-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();
        // throws an exception on rollback
        Assert.fail("IllegalStateException expected");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalStateException);
    }
    Transaction t2 = cs.getCurrentTransaction();
    Assert.assertNotSame(t2, t);
    // Check that t2 is rolledback
    Assert.assertEquals(Status.STATUS_NO_TRANSACTION, t2.getStatus());
    Assert.assertEquals(Status.STATUS_ACTIVE, t.getStatus());
    t.commit();
}
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)

Aggregations

ComponentInstance (org.apache.felix.ipojo.ComponentInstance)65 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65 Test (org.junit.Test)65 ServiceReference (org.osgi.framework.ServiceReference)65 IOException (java.io.IOException)23 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)1 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1 Bundle (org.osgi.framework.Bundle)1