use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.
the class TestSupported method testExpectedExceptionInsideTransactionWithCallback.
@Test
public void testExpectedExceptionInsideTransactionWithCallback() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
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);
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.assertSame(t2, t);
Assert.assertEquals(Status.STATUS_ACTIVE, t.getStatus());
t.commit();
Assert.assertNull(cs.getLastRolledBack());
Assert.assertNotNull(cs.getLastCommitted());
Assert.assertEquals(1, cs.getNumberOfCommit());
Assert.assertEquals(0, cs.getNumberOfRollback());
Assert.assertSame(t, cs.getLastCommitted());
}
use of org.apache.felix.ipojo.handler.transaction.services.CheckService 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());
}
use of org.apache.felix.ipojo.handler.transaction.services.CheckService 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();
}
use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.
the class TestInvalidation method testInvalidation.
@Test
public void testInvalidation() throws NotSupportedException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
final 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);
Thread thread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
prov.dispose();
}
});
thread.start();
tm.begin();
Transaction t = tm.getTransaction();
// 5s, so prov should be disposed during this time and under becomes invalid
cs.doSomethingLong();
Assert.assertEquals(ComponentInstance.INVALID, under.getState());
Assert.assertEquals(Status.STATUS_MARKED_ROLLBACK, t.getStatus());
t.rollback();
}
use of org.apache.felix.ipojo.handler.transaction.services.CheckService in project felix by apache.
the class TestMandatory 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("mandatory-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());
}
Aggregations