Search in sources :

Example 56 with CheckService

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

the class TestRequires method testExceptionInsideTransactionWithCallback.

@Test
public void testExceptionInsideTransactionWithCallback() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
    ComponentInstance prov = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.handler.transaction.components.FooImpl");
    ComponentInstance under = ipojoHelper.createComponentInstance("requires-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);
    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());
    try {
        // Throw a rollback exception.
        t.commit();
    } catch (RollbackException e) {
    // Expected
    } catch (Throwable e) {
        // Unexpected
        Assert.fail(e.getMessage());
    }
    Assert.assertNotNull(cs.getLastRolledBack());
    Assert.assertNull(cs.getLastCommitted());
    Assert.assertEquals(0, cs.getNumberOfCommit());
    Assert.assertEquals(1, cs.getNumberOfRollback());
    Assert.assertSame(t, cs.getLastRolledBack());
}
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 57 with CheckService

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

the class TestRequires 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("requires-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.assertNotNull(cs.getLastCommitted());
    Assert.assertEquals(1, 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 58 with CheckService

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

the class TestRequires method testExceptionInsideTransaction.

@Test(expected = RollbackException.class)
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("requires-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());
    // Throw a rollback exception.
    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 59 with CheckService

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

the class TestRequires 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("requires-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 60 with CheckService

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

the class TestRequiresNew method testExceptionOutsideTransaction.

@Test
public void testExceptionOutsideTransaction() {
    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);
    try {
        // throws an exception on rollback
        ((CheckService) osgiHelper.getServiceObject(ref)).doSomethingBad();
        Assert.fail("Exception expected on rollback");
    } catch (IllegalStateException e) {
    // Ok.
    } catch (Exception e) {
        Assert.fail("IllegalStateException expected on 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)

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