mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Add unit tests for Transaction
This commit is contained in:
parent
9cbb309d6b
commit
323bbccd50
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
136
framework/db/test/com/cloud/utils/db/TestTransaction.java
Normal file
136
framework/db/test/com/cloud/utils/db/TestTransaction.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
18
framework/db/test/db.properties
Normal file
18
framework/db/test/db.properties
Normal file
@ -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
|
||||
48
utils/test/com/cloud/utils/exception/ExceptionUtilTest.java
Normal file
48
utils/test/com/cloud/utils/exception/ExceptionUtilTest.java
Normal file
@ -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 ) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user