You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/03/28 18:20:03 UTC
svn commit: r642300 - in /geronimo/sandbox/concurrent:
concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/
concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/
geronimo-concurrent-core/src/ma...
Author: gawor
Date: Fri Mar 28 10:19:51 2008
New Revision: 642300
URL: http://svn.apache.org/viewvc?rev=642300&view=rev
Log:
basic test for transaction handling
Modified:
geronimo/sandbox/concurrent/concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/ContextServiceTest.java
geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/BasicTask.java
geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/ContextServiceServlet.java
geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java
Modified: geronimo/sandbox/concurrent/concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/ContextServiceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/ContextServiceTest.java?rev=642300&r1=642299&r2=642300&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/ContextServiceTest.java (original)
+++ geronimo/sandbox/concurrent/concurrent-tests/concurrent-ear/src/test/java/org/apache/geronimo/concurrent/test/ContextServiceTest.java Fri Mar 28 10:19:51 2008
@@ -37,6 +37,16 @@
}
@Test
+ public void testTransactionDemarcation() throws Exception {
+ invokeTest("testTransactionDemarcation");
+ }
+
+ @Test
+ public void testUseParentTransaction() throws Exception {
+ invokeTest("testUseParentTransaction");
+ }
+
+ @Test
public void test() throws Exception {
invokeTest("testContextService");
}
Modified: geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/BasicTask.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/BasicTask.java?rev=642300&r1=642299&r2=642300&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/BasicTask.java (original)
+++ geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/BasicTask.java Fri Mar 28 10:19:51 2008
@@ -21,6 +21,8 @@
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
+import javax.transaction.NotSupportedException;
+import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.testng.Assert;
@@ -29,6 +31,7 @@
private ClassLoader expectedClassLoader;
private boolean testEJBSecurity;
+ private boolean testTransaction;
public BasicTask(ClassLoader expectedClassLoader) {
this.expectedClassLoader = expectedClassLoader;
@@ -38,6 +41,10 @@
this.testEJBSecurity = testEjbSecurity;
}
+ public void setTestTransaction(boolean testTransaction) {
+ this.testTransaction = testTransaction;
+ }
+
public Object execute() throws Exception {
System.out.println("Task starting");
@@ -85,9 +92,16 @@
UserTransaction userTransaction =
(UserTransaction)ctx.lookup("java:comp/UserTransaction");
Assert.assertNotNull(userTransaction);
+ if (this.testTransaction) {
+ userTransaction.begin();
+ }
} catch (NamingException e) {
Assert.fail("UserTransaction JNDI lookup failed", e);
- }
+ } catch (NotSupportedException e) {
+ Assert.fail("Transaction failed", e);
+ } catch (SystemException e) {
+ Assert.fail("Transaction failed", e);
+ }
}
private void testEJB(InitialContext ctx) {
Modified: geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/ContextServiceServlet.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/ContextServiceServlet.java?rev=642300&r1=642299&r2=642300&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/ContextServiceServlet.java (original)
+++ geronimo/sandbox/concurrent/concurrent-tests/concurrent-war/src/main/java/org/apache/geronimo/concurrent/test/ContextServiceServlet.java Fri Mar 28 10:19:51 2008
@@ -21,16 +21,22 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
import javax.annotation.Resource;
+import javax.naming.InitialContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
+import javax.transaction.Status;
+import javax.transaction.UserTransaction;
import javax.util.concurrent.ContextService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.testng.Assert;
public class ContextServiceServlet extends HttpServlet {
@@ -101,6 +107,45 @@
}
}
+ public void testTransactionDemarcation() throws Exception {
+ InitialContext ctx = new InitialContext();
+ UserTransaction userTransaction =
+ (UserTransaction)ctx.lookup("java:comp/UserTransaction");
+ userTransaction.begin();
+ Assert.assertEquals(Status.STATUS_ACTIVE, userTransaction.getStatus());
+
+ // run the task on the same thread
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ BasicTaskRunnable runnable = new BasicTaskRunnable(classLoader);
+ runnable.setTestTransaction(true);
+
+ Runnable task = (Runnable)contextService.createContextObject(runnable, new Class [] {Runnable.class});
+ task.run();
+
+ Assert.assertEquals(Status.STATUS_ACTIVE, userTransaction.getStatus());
+ userTransaction.commit();
+ }
+
+ public void testUseParentTransaction() throws Exception {
+ InitialContext ctx = new InitialContext();
+ UserTransaction userTransaction =
+ (UserTransaction)ctx.lookup("java:comp/UserTransaction");
+ Assert.assertEquals(Status.STATUS_NO_TRANSACTION, userTransaction.getStatus());
+
+ // run the task on the same thread
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ BasicTaskRunnable runnable = new BasicTaskRunnable(classLoader);
+ runnable.setTestTransaction(true);
+
+ Map<String, String> props = new HashMap<String, String>();
+ props.put(ContextService.USE_PARENT_TRANSACTION, "true");
+ Runnable task = (Runnable)contextService.createContextObject(runnable, new Class [] {Runnable.class}, props);
+ task.run();
+
+ Assert.assertEquals(Status.STATUS_ACTIVE, userTransaction.getStatus());
+ userTransaction.commit();
+ }
+
public void testContextService() throws Exception {
TestRunnable testRunnable = new TestRunnable(2 * 1000);
Modified: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java?rev=642300&r1=642299&r2=642300&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java Fri Mar 28 10:19:51 2008
@@ -46,7 +46,13 @@
private boolean isUseParentTransaction(Map<String, Object> threadContext) {
Object useParentTransaction =
threadContext.get(ContextService.USE_PARENT_TRANSACTION);
- return Boolean.TRUE.equals(useParentTransaction);
+ if (useParentTransaction instanceof String) {
+ return Boolean.valueOf((String)useParentTransaction).booleanValue();
+ } else if (useParentTransaction instanceof Boolean) {
+ return Boolean.TRUE.equals(useParentTransaction);
+ } else {
+ return false;
+ }
}
public void setContext(Map<String, Object> threadContext) {