You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by da...@apache.org on 2013/10/23 21:43:56 UTC
[39/47] git commit: updated refs/heads/master to c910196
Add unit tests for Transaction
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/323bbccd
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/323bbccd
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/323bbccd
Branch: refs/heads/master
Commit: 323bbccd50c3008ced5f4ab1e0ba1be8e0499a62
Parents: 9cbb309
Author: Darren Shepherd <da...@gmail.com>
Authored: Thu Oct 17 16:07:11 2013 -0700
Committer: Darren Shepherd <da...@gmail.com>
Committed: Thu Oct 17 16:07:11 2013 -0700
----------------------------------------------------------------------
.../db/src/com/cloud/utils/db/Transaction.java | 7 +-
.../com/cloud/utils/db/TransactionLegacy.java | 26 +++-
.../com/cloud/utils/db/TestTransaction.java | 136 +++++++++++++++++++
framework/db/test/db.properties | 18 +++
.../utils/exception/ExceptionUtilTest.java | 48 +++++++
5 files changed, 230 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/323bbccd/framework/db/src/com/cloud/utils/db/Transaction.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/Transaction.java b/framework/db/src/com/cloud/utils/db/Transaction.java
index 4f8e5c7..d65b87e 100755
--- a/framework/db/src/com/cloud/utils/db/Transaction.java
+++ b/framework/db/src/com/cloud/utils/db/Transaction.java
@@ -27,7 +27,12 @@ public class Transaction {
public static <T> T execute(TransactionCallback<T> callback) {
String name = "tx-" + counter.incrementAndGet();
- TransactionLegacy txn = TransactionLegacy.open(name);
+ short databaseId = TransactionLegacy.CLOUD_DB;
+ TransactionLegacy currentTxn = TransactionLegacy.currentTxn(false);
+ if ( currentTxn != null ) {
+ databaseId = currentTxn.getDatabaseId();
+ }
+ TransactionLegacy txn = TransactionLegacy.open(name, databaseId, false);
try {
txn.start();
T result = callback.doInTransaction(STATUS);
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/323bbccd/framework/db/src/com/cloud/utils/db/TransactionLegacy.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/TransactionLegacy.java b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java
index b191491..ffa0670 100755
--- a/framework/db/src/com/cloud/utils/db/TransactionLegacy.java
+++ b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java
@@ -117,10 +117,16 @@ public class TransactionLegacy {
private TransactionLegacy _prev = null;
public static TransactionLegacy currentTxn() {
+ return currentTxn(true);
+ }
+
+ protected static TransactionLegacy currentTxn(boolean check) {
TransactionLegacy txn = tls.get();
- assert txn != null : "No Transaction on stack. Did you mark the method with @DB?";
-
- assert checkAnnotation(3, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn;
+ if (check) {
+ assert txn != null : "No Transaction on stack. Did you mark the method with @DB?";
+
+ assert checkAnnotation(4, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn;
+ }
return txn;
}
@@ -397,6 +403,10 @@ public class TransactionLegacy {
return lockMaster.release(name);
}
+ /**
+ * @deprecated Use {@link Transaction} for new code
+ */
+ @Deprecated
public void start() {
if (s_logger.isTraceEnabled()) {
s_logger.trace("txn: start requested by: " + buildName());
@@ -1170,5 +1180,13 @@ public class TransactionLegacy {
return new PoolingDataSource(
/* connectionPool */poolableConnectionFactory.getPool());
}
-
+
+ /**
+ * Used for unit testing primarily
+ *
+ * @param conn
+ */
+ protected void setConnection(Connection conn) {
+ this._conn = conn;
+ }
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/323bbccd/framework/db/test/com/cloud/utils/db/TestTransaction.java
----------------------------------------------------------------------
diff --git a/framework/db/test/com/cloud/utils/db/TestTransaction.java b/framework/db/test/com/cloud/utils/db/TestTransaction.java
new file mode 100644
index 0000000..ea277c7
--- /dev/null
+++ b/framework/db/test/com/cloud/utils/db/TestTransaction.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.cloud.utils.db;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.io.FileNotFoundException;
+import java.sql.Connection;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestTransaction {
+
+ TransactionLegacy txn;
+ Connection conn;
+
+ @Before
+ public void setup() {
+ setup(TransactionLegacy.CLOUD_DB);
+ }
+
+ public void setup(short db) {
+ txn = TransactionLegacy.open(db);
+ conn = Mockito.mock(Connection.class);
+ txn.setConnection(conn);
+ }
+
+ @After
+ public void after() {
+ TransactionLegacy.currentTxn().close();
+ }
+
+ @Test
+ public void testCommit() throws Exception {
+ assertEquals(42L, Transaction.execute(new TransactionCallback<Object>() {
+ @Override
+ public Object doInTransaction(TransactionStatus status) {
+ return 42L;
+ }
+ }));
+
+ verify(conn).setAutoCommit(false);
+ verify(conn, times(1)).commit();
+ verify(conn, times(0)).rollback();
+ verify(conn, times(1)).close();
+ }
+
+ @Test
+ public void testRollback() throws Exception {
+ try {
+ Transaction.execute(new TransactionCallback<Object>() {
+ @Override
+ public Object doInTransaction(TransactionStatus status) {
+ throw new RuntimeException("Panic!");
+ }
+ });
+ fail();
+ } catch (RuntimeException e) {
+ assertEquals("Panic!", e.getMessage());
+ }
+
+ verify(conn).setAutoCommit(false);
+ verify(conn, times(0)).commit();
+ verify(conn, times(1)).rollback();
+ verify(conn, times(1)).close();
+ }
+
+ @Test
+ public void testRollbackWithException() throws Exception {
+ try {
+ Transaction.executeWithException(new TransactionCallbackWithException<Object>() {
+ @Override
+ public Object doInTransaction(TransactionStatus status) throws FileNotFoundException {
+ assertEquals(TransactionLegacy.CLOUD_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue());
+
+ throw new FileNotFoundException("Panic!");
+ }
+ }, FileNotFoundException.class);
+ fail();
+ } catch (FileNotFoundException e) {
+ assertEquals("Panic!", e.getMessage());
+ }
+
+ verify(conn).setAutoCommit(false);
+ verify(conn, times(0)).commit();
+ verify(conn, times(1)).rollback();
+ verify(conn, times(1)).close();
+ }
+
+ @Test
+ public void testOtherdatabaseRollback() throws Exception {
+ after();
+ setup(TransactionLegacy.AWSAPI_DB);
+
+ try {
+ Transaction.execute(new TransactionCallbackNoReturn() {
+ @Override
+ public void doInTransactionWithoutResult(TransactionStatus status) {
+ assertEquals(TransactionLegacy.AWSAPI_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue());
+
+ throw new RuntimeException("Panic!");
+ }
+ });
+ fail();
+ } catch (RuntimeException e) {
+ assertEquals("Panic!", e.getMessage());
+ }
+
+
+ verify(conn).setAutoCommit(false);
+ verify(conn, times(0)).commit();
+ verify(conn, times(1)).rollback();
+ verify(conn, times(1)).close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/323bbccd/framework/db/test/db.properties
----------------------------------------------------------------------
diff --git a/framework/db/test/db.properties b/framework/db/test/db.properties
new file mode 100644
index 0000000..cc1215f
--- /dev/null
+++ b/framework/db/test/db.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Just here to make the unit test not blow up
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/323bbccd/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java
new file mode 100644
index 0000000..33fca37
--- /dev/null
+++ b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.cloud.utils.exception;
+
+import static org.junit.Assert.*;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class ExceptionUtilTest {
+
+ @Test
+ public void test() throws Exception {
+ FileNotFoundException fnfe = new FileNotFoundException();
+ try {
+ ExceptionUtil.rethrow(fnfe, IOException.class);
+ fail();
+ } catch (IOException e) {
+ }
+
+ ExceptionUtil.rethrow(fnfe, ClassNotFoundException.class);
+
+ try {
+ ExceptionUtil.rethrow(fnfe, FileNotFoundException.class);
+ fail();
+ } catch ( FileNotFoundException e ) {
+ }
+ }
+
+}