aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1732308 - /aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java
Date Thu, 25 Feb 2016 14:08:14 GMT
Author: timothyjward
Date: Thu Feb 25 14:08:14 2016
New Revision: 1732308

URL: http://svn.apache.org/viewvc?rev=1732308&view=rev
Log:
[tx-control] Testing of nested transactions

Modified:
    aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java

Modified: aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java?rev=1732308&r1=1732307&r2=1732308&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java
(original)
+++ aries/trunk/tx-control/tx-control-itests/src/test/java/org/apache/aries/tx/control/itests/SimpleTransactionTest.java
Thu Feb 25 14:08:14 2016
@@ -29,6 +29,7 @@ import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.Arrays;
 import java.util.Properties;
 
 import javax.inject.Inject;
@@ -120,6 +121,105 @@ public class SimpleTransactionTest exten
 		}));
 	}
 
+	@Test
+	public void testNestedTx() {
+		txControl.required(() -> {
+			connection.createStatement()
+				.execute("Insert into TEST_TABLE values ( 'Hello World!' )");
+			
+			txControl.requiresNew(() -> connection.createStatement()
+					.execute("Insert into TEST_TABLE values ( 'Hello Nested World!' )"));
+			
+			return null;
+		});
+		
+		String[] results = txControl.notSupported(() -> {
+				Statement s = connection.createStatement();
+				
+				ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
+				rs.next();
+				int count = rs.getInt(1);
+				
+				rs = s.executeQuery("Select message from TEST_TABLE ORDER BY message");
+				
+				String[] result = new String[2];
+				rs.next();
+				result[0] = "" + count + ": " + rs.getString(1);
+				rs.next();
+				result[1] = "" + count + ": " + rs.getString(1);
+				return result;
+			});
+		
+		System.out.println(Arrays.toString(results));
+		
+		assertEquals("2: Hello Nested World!", results[0]);
+		assertEquals("2: Hello World!", results[1]);
+	}
+	
+	@Test
+	public void testNestedTxOuterRollback() {
+		txControl.required(() -> {
+				// This will not end up in the database
+				connection.createStatement()
+					.execute("Insert into TEST_TABLE values ( 'Hello World!' )");
+				
+				// This should only apply to the current transaction level
+				txControl.setRollbackOnly();
+			
+				// This nested transaction will commit
+				txControl.requiresNew(() -> connection.createStatement()
+						.execute("Insert into TEST_TABLE values ( 'Hello Nested World!' )"));
+			
+				return null;
+			});
+		
+		assertEquals("1: Hello Nested World!", txControl.notSupported(() -> {
+				Statement s = connection.createStatement();
+				
+				ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
+				rs.next();
+				int count = rs.getInt(1);
+				
+				rs = s.executeQuery("Select message from TEST_TABLE ORDER BY message");
+				
+				rs.next();
+				return "" + count + ": " + rs.getString(1);
+			}));
+	}
+
+	@Test
+	public void testNestedTxInnerRollback() {
+		txControl.required(() -> {
+			// This will end up in the database
+			connection.createStatement()
+				.execute("Insert into TEST_TABLE values ( 'Hello World!' )");
+			
+						
+			// This nested transaction not commit
+			txControl.requiresNew(() -> {
+					connection.createStatement()
+						.execute("Insert into TEST_TABLE values ( 'Hello Nested World!' )");
+					txControl.setRollbackOnly();
+					return null;
+				});
+			
+			return null;
+		});
+		
+		assertEquals("1: Hello World!", txControl.notSupported(() -> {
+				Statement s = connection.createStatement();
+				
+				ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
+				rs.next();
+				int count = rs.getInt(1);
+				
+				rs = s.executeQuery("Select message from TEST_TABLE ORDER BY message");
+				
+				rs.next();
+				return "" + count + ": " + rs.getString(1);
+			}));
+	}
+
 	@Configuration
 	public Option[] configuration() {
 		String localRepo = System.getProperty("maven.repo.local");



Mime
View raw message