[jboss-svn-commits] JBL Code SVN: r37223 - in labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src: main/java/org/jboss/narayana/quickstarts/servlet and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 4 08:49:32 EDT 2011


Author: tomjenkinson
Date: 2011-07-04 08:49:31 -0400 (Mon, 04 Jul 2011)
New Revision: 37223

Modified:
   labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/ejb/Customer.java
   labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/servlet/SimpleServlet.java
   labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/test/java/TestSimpleServlet.java
Log:
JBTM-854 Updated to show how a TXOJ can be rolled back by the wrapping transaction in the case of a JPA constraint violation

Modified: labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/ejb/Customer.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/ejb/Customer.java	2011-07-04 12:35:57 UTC (rev 37222)
+++ labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/ejb/Customer.java	2011-07-04 12:49:31 UTC (rev 37223)
@@ -22,6 +22,7 @@
 
 import java.io.Serializable;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
@@ -32,6 +33,9 @@
 	@GeneratedValue
 	private int id;
 
+	@Column(unique = true, nullable = false)
+	private String name;
+
 	public int getId() {
 		return id;
 	}
@@ -47,6 +51,4 @@
 	public void setName(String name) {
 		this.name = name;
 	}
-
-	private String name;
 }

Modified: labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/servlet/SimpleServlet.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/servlet/SimpleServlet.java	2011-07-04 12:35:57 UTC (rev 37222)
+++ labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/main/java/org/jboss/narayana/quickstarts/servlet/SimpleServlet.java	2011-07-04 12:49:31 UTC (rev 37223)
@@ -90,7 +90,7 @@
 			toWrite.append("<p>" + simpleEJB.listIds() + "</p>");
 			tx.commit();
 		} catch (Throwable e) {
-			throw new RuntimeException(e);
+			toWrite.append("FAILED: " + e.toString());
 		}
 		return toWrite.toString();
 	}
@@ -103,12 +103,12 @@
 			UserTransaction tx = (UserTransaction) new InitialContext()
 					.lookup("java:comp/UserTransaction");
 			tx.begin();
+			atomicObject.incr(1);
 			simpleEJB.createCustomer(name);
-			atomicObject.incr(1);
 			toWrite.append("<p>Created: " + name + "</p>");
 			tx.commit();
 		} catch (Throwable e) {
-			throw new RuntimeException(e);
+			toWrite.append("FAILED: " + e.toString());
 		}
 		return toWrite.toString();
 	}
@@ -121,11 +121,11 @@
 			UserTransaction tx = (UserTransaction) new InitialContext()
 					.lookup("java:comp/UserTransaction");
 			tx.begin();
-			toWrite.append("<p>Customers created this run: " + atomicObject.get()
-					+ "</p>");
+			toWrite.append("<p>Customers created this run: "
+					+ atomicObject.get() + "</p>");
 			tx.commit();
 		} catch (Throwable e) {
-			throw new RuntimeException(e);
+			toWrite.append("FAILED: " + e.toString());
 		}
 		return toWrite.toString();
 	}

Modified: labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/test/java/TestSimpleServlet.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/test/java/TestSimpleServlet.java	2011-07-04 12:35:57 UTC (rev 37222)
+++ labs/jbosstm/trunk/ArjunaJTA/quickstarts/integration/src/test/java/TestSimpleServlet.java	2011-07-04 12:49:31 UTC (rev 37223)
@@ -18,14 +18,11 @@
  * (C) 2011,
  * @author JBoss, by Red Hat.
  */
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
 
 import javax.ejb.EJB;
-import javax.naming.InitialContext;
-import javax.transaction.UserTransaction;
 
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
@@ -42,8 +39,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import com.arjuna.ats.arjuna.common.Uid;
-
 @RunWith(Arquillian.class)
 public class TestSimpleServlet {
 	@EJB(lookup = "java:module/SimpleEJBImpl")
@@ -72,9 +67,9 @@
 	}
 
 	@Test
-	public void checkThatDoubleCallIncreasesListSize()
-			throws SecurityException, NoSuchFieldException,
-			IllegalArgumentException, IllegalAccessException {
+	public void checkListCustomers() throws SecurityException,
+			NoSuchFieldException, IllegalArgumentException,
+			IllegalAccessException {
 
 		// Declare the servlet outside the container
 		SimpleServlet servlet = new SimpleServlet();
@@ -83,69 +78,60 @@
 		field.set(servlet, simpleEJB);
 		field.setAccessible(false);
 
-		servlet.createCustomer("tom");
-
+		// Create a customer
+		servlet.createCustomer("checkThatDoubleCallIncreasesListSize");
 		String firstList = servlet.listCustomers();
-
-		servlet.createCustomer("tom");
-
+		// Create a different customer
+		servlet.createCustomer("checkThatDoubleCallIncreasesListSize2");
 		String secondList = servlet.listCustomers();
 
-		System.out.println(firstList);
-		System.out.println(secondList);
-
+		// Check that the list size increased
 		assertTrue(firstList.length() < secondList.length());
 	}
 
 	@Test
-	public void testTxoj() throws Exception {
-		UserTransaction tx = (UserTransaction) new InitialContext()
-				.lookup("java:comp/UserTransaction");
+	public void testCustomerCount() throws Exception {
+		// Declare the servlet outside the container
+		SimpleServlet servlet = new SimpleServlet();
+		Field field = servlet.getClass().getDeclaredField("simpleEJB");
+		field.setAccessible(true);
+		field.set(servlet, simpleEJB);
+		field.setAccessible(false);
 
-		AtomicObject foo = new AtomicObject();
-		Uid u = foo.get_uid();
+		String toLookFor = "<p>Customers created this run: ";
+		String response = null;
+		int indexOf = -1;
+		String customersCreated = null;
+		int size = 0;
+		int newSize = -1;
 
-		tx.begin();
+		// Create a new customer
+		servlet.createCustomer("testCustomerCount");
 
-		foo.set(2);
+		// Get the initial number of customers
+		response = servlet.getCustomerCount();
+		indexOf = response.indexOf(toLookFor);
+		customersCreated = response.substring(indexOf + toLookFor.length(),
+				response.indexOf("</p>", indexOf));
+		newSize = Integer.parseInt(customersCreated);
+		assertTrue(newSize == size + 1);
+		size = newSize;
 
-		tx.commit();
+		// Create a new customer
+		servlet.createCustomer("testCustomerCount2");
 
-		int finalVal = foo.get();
-
-		assertEquals(2, finalVal);
-
-		foo = new AtomicObject(u);
-
-		tx.begin();
-
-		foo.set(4);
-
-		tx.commit();
-
-		finalVal = foo.get();
-
-		assertEquals(4, finalVal);
-
-		foo = new AtomicObject(u);
-
-		finalVal = foo.get();
-
-		assertEquals(4, finalVal);
-
-		tx.begin();
-
-		foo.set(10);
-
-		tx.rollback();
-
-		finalVal = foo.get();
-
-		assertEquals(4, finalVal);
+		// Check that one extra customer was created
+		response = servlet.getCustomerCount();
+		indexOf = response.indexOf(toLookFor);
+		customersCreated = response.substring(indexOf + toLookFor.length(),
+				response.indexOf("</p>", indexOf));
+		newSize = Integer.parseInt(customersCreated);
+		assertTrue(newSize == size + 1);
+		size = newSize;
 	}
 
 	@Test
-	public void testServlet() throws Exception {
+	public void testCustomerCountInPresenceOfRollback() throws Exception {
 		// Declare the servlet outside the container
 		SimpleServlet servlet = new SimpleServlet();
 		Field field = servlet.getClass().getDeclaredField("simpleEJB");
@@ -157,23 +143,43 @@
 		String response = null;
 		int indexOf = -1;
 		String customersCreated = null;
+		int size = 0;
+		int newSize = -1;
 
+		// Create a new customer
+		servlet.createCustomer("testCustomerCountInPresenceOfRollback");
+
 		// Get the initial number of customers
 		response = servlet.getCustomerCount();
 		indexOf = response.indexOf(toLookFor);
 		customersCreated = response.substring(indexOf + toLookFor.length(),
 				response.indexOf("</p>", indexOf));
-		int initialSize = Integer.parseInt(customersCreated);
+		newSize = Integer.parseInt(customersCreated);
+		assertTrue(newSize == size + 1);
+		size = newSize;
 
 		// Create a new customer
-		servlet.createCustomer("tom");
+		servlet.createCustomer("testCustomerCountInPresenceOfRollback");
 
+		// Check that no extra customers were created
+		response = servlet.getCustomerCount();
+		indexOf = response.indexOf(toLookFor);
+		customersCreated = response.substring(indexOf + toLookFor.length(),
+				response.indexOf("</p>", indexOf));
+		newSize = Integer.parseInt(customersCreated);
+		assertTrue(newSize == size);
+		size = newSize;
+
+		// Create a new customer
+		servlet.createCustomer("testCustomerCountInPresenceOfRollback2");
+
 		// Check that one extra customer was created
 		response = servlet.getCustomerCount();
 		indexOf = response.indexOf(toLookFor);
 		customersCreated = response.substring(indexOf + toLookFor.length(),
 				response.indexOf("</p>", indexOf));
-		int newSize = Integer.parseInt(customersCreated);
-		assertTrue(newSize == initialSize + 1);
+		newSize = Integer.parseInt(customersCreated);
+		assertTrue(newSize == size + 1);
+		size = newSize;
 	}
 }
\ No newline at end of file



More information about the jboss-svn-commits mailing list