Java tutorial
/* * Copyright (c) 2011 NTT DATA Corporation * * Licensed 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 jp.terasoluna.fw.batch.util; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.StringStartsWith.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionException; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.TransactionSystemException; import jp.terasoluna.fw.batch.exception.IllegalClassTypeException; import jp.terasoluna.fw.logger.TLogger; import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLoggerFactory; /** * * */ public class BatchUtilTest { // private Log log = LogFactory.getLog(BatchUtilTest.class); private Log log = TLogger.getLogger(BatchUtilTest.class); private static TestLogger logger = TestLoggerFactory.getTestLogger(BatchUtilTest.class); @Before public void setUp() throws Exception { logger.clear(); } /** * testBatchUtil001 * @throws Exception */ @Test public void testBatchUtil001() throws Exception { BatchUtil bu = new BatchUtil(); assertNotNull(bu); } /** * ???<br> * <br> * ?<br> * ??????????????? <br> * ?<br> * ?????????<br> * @throws Exception */ @Test public void testCat01() throws Exception { // String[] args = { "test1", "test2" }; // String result = BatchUtil.cat((Object[]) args); // ? assertEquals("test1test2", result); } /** * ???<br> * <br> * ?<br> * null????null?????? <br> * ?<br> * null?????<br> * @throws Exception */ @Test public void testCat02() throws Exception { // String[] args = null; // String result = BatchUtil.cat((Object[]) args); // ? assertNull(result); } /** * ???<br> * <br> * ?<br> * ??????????? <br> * ?<br> * ?????<br> * @throws Exception */ @Test public void testCat03() throws Exception { // String[] args = { "" }; // String result = BatchUtil.cat((Object[]) args); // ? assertEquals("", result); } /** * ???<br> * <br> * ?<br> * 3?????????null???????null?????????????? <br> * ?<br> * null??????????<br> * @throws Exception */ @Test public void testCat04() throws Exception { // String[] args = { "test1", null, "test2" }; // String result = BatchUtil.cat((Object[]) args); // ? assertEquals("test1test2", result); } /** * ????<br> * <br> * ?<br> * ?????????????<br> * ?<br> * ?????????<br> * @throws Exception */ @Test public void testGetInfoLogStartMsg01() throws Exception { // String arg = "test1"; // String result = BatchUtil.getInfoLogStartMsg(arg); // ? assertEquals("[test1] ?", result); } /** * ????<br> * <br> * ?<br> * ?null????????????<br> * ?<br> * ??????????<br> * @throws Exception */ @Test public void testGetInfoLogStartMsg02() throws Exception { // String arg = null; // String result = BatchUtil.getInfoLogStartMsg(arg); // ? assertEquals("[] ?", result); } /** * ????<br> * <br> * ?<br> * ?????????????<br> * ?<br> * ??????????<br> * @throws Exception */ @Test public void testGetInfoLogStartMsg03() throws Exception { // String arg = ""; // String result = BatchUtil.getInfoLogStartMsg(arg); // ? assertEquals("[] ?", result); } /** * ????<br> * <br> * ?<br> * ?????????????<br> * ?<br> * ?????????<br> * @throws Exception */ @Test public void testGetInfoLogEndMsg01() throws Exception { // String arg = "test1"; // String result = BatchUtil.getInfoLogEndMsg(arg); // ? assertEquals("[test1] ?", result); } /** * ????<br> * <br> * ?<br> * ?????????????<br> * ?<br> * ??????????<br> * @throws Exception */ @Test public void testGetInfoLogEndMsg02() throws Exception { // String arg = null; // String result = BatchUtil.getInfoLogEndMsg(arg); // ? assertEquals("[] ?", result); } /** * ????<br> * <br> * ?<br> * ?????????????<br> * ?<br> * ??????????<br> * @throws Exception */ @Test public void testGetInfoLogEndMsg03() throws Exception { // String arg = ""; // String result = BatchUtil.getInfoLogEndMsg(arg); // ? assertEquals("[] ?", result); } /** * testGetTransactionDefinition01 * @throws Exception */ @Test public void testGetTransactionDefinition01() throws Exception { // TransactionDefinition result = BatchUtil.getTransactionDefinition(); // ? assertNotNull(result); } /** * testGetTransactionDefinition02 * @throws Exception */ @Test public void testGetTransactionDefinition02() throws Exception { // TransactionDefinition result = BatchUtil.getTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT, false); // ? assertNotNull(result); } /** * testChangeListToArray01 * @throws Exception */ @Test public void testChangeListToArray01() throws Exception { // List<String> list = new ArrayList<String>(); list.add("test1"); list.add("test2"); list.add("test3"); // String[] result = BatchUtil.changeListToArray(list, String.class); // ? assertEquals("test1", result[0]); assertEquals("test2", result[1]); assertEquals("test3", result[2]); } /** * testChangeListToArray02 * @throws Exception */ @Test public void testChangeListToArray02() throws Exception { // List<String> list = new ArrayList<String>(); list.add("test1"); list.add("test2"); list.add("test3"); try { // BatchUtil.changeListToArray(list, null); // ? fail(); } catch (IllegalClassTypeException e) { assertNotNull(e); } } /** * testChangeListToArray03 * @throws Exception */ @Test public void testChangeListToArray03() throws Exception { // List<String> list = new ArrayList<String>(); list.add("test1"); list.add("test2"); list.add("test3"); try { // BatchUtil.changeListToArray(list, Integer.class); // ? fail(); } catch (IllegalClassTypeException e) { assertNotNull(e); } } /** * testGetProperties01 * @throws Exception */ @Test public void testGetProperties01() throws Exception { // List<String> result = BatchUtil.getProperties("messages", "errors"); // ? assertEquals(26, result.size()); } /** * testStartTransaction01 * @throws Exception */ @Test public void testStartTransaction01() throws Exception { // PlatformTransactionManager tran = null; // TransactionStatus result = BatchUtil.startTransaction(tran); // ? assertNull(result); } /** * testStartTransaction02 * @throws Exception */ @Test public void testStartTransaction02() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); // TransactionStatus result = BatchUtil.startTransaction(tran); // ? assertNotNull(result); } /** * testStartTransaction03 * @throws Exception */ @Test public void testStartTransaction03() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); // TransactionStatus result = BatchUtil.startTransaction(tran, log); // ? assertNotNull(result); } /** * testStartTransaction04 * @throws Exception */ @Test public void testStartTransaction04() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); // TransactionStatus result = BatchUtil.startTransaction(tran, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT, false); // ? assertNotNull(result); } /** * testStartTransaction05 * @throws Exception */ @Test public void testStartTransaction05() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); // TransactionStatus result = BatchUtil.startTransaction(tran, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT, false, log); // ? assertNotNull(result); } /** * testStartTransaction06 * @throws Exception */ @Test public void testStartTransaction06() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = BatchUtil.startTransaction(tran, def); // ? assertNotNull(result); } /** * testStartTransaction07 * @throws Exception */ @Test public void testStartTransaction07() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = BatchUtil.startTransaction(tran, def, log); // ? assertNotNull(result); } @Test public void testStartTransaction08() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); // TransactionStatus result = BatchUtil.startTransaction(tran, null, log); // ? assertNotNull(result); } /** * testStartTransactions01 * @throws Exception */ @Test public void testStartTransactions01() throws Exception { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); tranMap.put("tran", tran); // Map<String, TransactionStatus> result = BatchUtil.startTransactions(def, tranMap); // ? assertNotNull(result); } /** * testStartTransactions02 * @throws Exception */ @Test public void testStartTransactions02() throws Exception { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); tranMap.put("tran", tran); // Map<String, TransactionStatus> result = BatchUtil.startTransactions(def, tranMap, log); // ? assertNotNull(result); } /** * testStartTransactions03 * @throws Exception */ @Test public void testStartTransactions03() throws Exception {// // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new LinkedHashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub3 tran1 = new PlatformTransactionManagerStub3(); tran1.setFailStartTx(true); tranMap.put("tran1", tran1); PlatformTransactionManagerStub3 tran2 = new PlatformTransactionManagerStub3(); tranMap.put("tran2", tran2); PlatformTransactionManagerStub3 tran3 = new PlatformTransactionManagerStub3(); tranMap.put("tran3", tran3); PlatformTransactionManagerStub3 tran4 = new PlatformTransactionManagerStub3(); tranMap.put("tran4", tran4); // try { BatchUtil.startTransactions(def, tranMap, log); fail(); } catch (TransactionException e) { // ? assertTrue(tran1.wasCalledGetTransaction()); assertFalse(tran2.wasCalledGetTransaction()); assertFalse(tran3.wasCalledGetTransaction()); assertFalse(tran4.wasCalledGetTransaction()); assertFalse(tran1.wasCalledRollback()); assertFalse(tran2.wasCalledRollback()); assertFalse(tran3.wasCalledRollback()); assertFalse(tran4.wasCalledRollback()); assertThat(logger.getLoggingEvents().get(2).getMessage(), is(startsWith("[EAL025048] Failed to start transaction."))); } } /** * testStartTransactions04 * @throws Exception */ @Test public void testStartTransactions04() throws Exception { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new LinkedHashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub3 tran1 = new PlatformTransactionManagerStub3(); tranMap.put("tran1", tran1); PlatformTransactionManagerStub3 tran2 = new PlatformTransactionManagerStub3(); tran2.setFailStartTx(true); tranMap.put("tran2", tran2); PlatformTransactionManagerStub3 tran3 = new PlatformTransactionManagerStub3(); tranMap.put("tran3", tran3); PlatformTransactionManagerStub3 tran4 = new PlatformTransactionManagerStub3(); tranMap.put("tran4", tran4); // try { BatchUtil.startTransactions(def, tranMap, log); fail(); } catch (TransactionException e) { // ? assertTrue(tran1.wasCalledGetTransaction()); assertTrue(tran2.wasCalledGetTransaction()); assertFalse(tran3.wasCalledGetTransaction()); assertFalse(tran4.wasCalledGetTransaction()); assertTrue(tran1.wasCalledRollback()); assertFalse(tran2.wasCalledRollback()); assertFalse(tran3.wasCalledRollback()); assertFalse(tran4.wasCalledRollback()); assertThat(logger.getLoggingEvents().get(5).getMessage(), is(startsWith("[EAL025048] Failed to start transaction."))); } } /** * testStartTransactions05 * @throws Exception */ @Test public void testStartTransactions05() throws Exception { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new LinkedHashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub3 tran1 = new PlatformTransactionManagerStub3(); tranMap.put("tran1", tran1); PlatformTransactionManagerStub3 tran2 = new PlatformTransactionManagerStub3(); tranMap.put("tran2", tran2); PlatformTransactionManagerStub3 tran3 = new PlatformTransactionManagerStub3(); tranMap.put("tran3", tran3); PlatformTransactionManagerStub3 tran4 = new PlatformTransactionManagerStub3(); tran4.setFailStartTx(true); tranMap.put("tran4", tran4); // try { BatchUtil.startTransactions(def, tranMap, log); fail(); } catch (TransactionException e) { // ? assertTrue(tran1.wasCalledGetTransaction()); assertTrue(tran2.wasCalledGetTransaction()); assertTrue(tran3.wasCalledGetTransaction()); assertTrue(tran4.wasCalledGetTransaction()); assertTrue(tran1.wasCalledRollback()); assertTrue(tran2.wasCalledRollback()); assertTrue(tran3.wasCalledRollback()); assertFalse(tran4.wasCalledRollback()); assertThat(logger.getLoggingEvents().get(11).getMessage(), is(startsWith("[EAL025048] Failed to start transaction."))); } } /** * testStartTransactions06 * @throws Exception */ @Test public void testStartTransactions06() throws Exception { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new LinkedHashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub3 tran1 = new PlatformTransactionManagerStub3(); tranMap.put("tran1", tran1); PlatformTransactionManagerStub3 tran2 = new PlatformTransactionManagerStub3(); tranMap.put("tran2", tran2); PlatformTransactionManagerStub3 tran3 = new PlatformTransactionManagerStub3(); tranMap.put("tran3", tran3); PlatformTransactionManagerStub3 tran4 = new PlatformTransactionManagerStub3(); tranMap.put("tran4", tran4); // Map<?, ?> statMap = BatchUtil.startTransactions(def, tranMap, log); // ? assertTrue(statMap instanceof LinkedHashMap); assertTrue(tran1.wasCalledGetTransaction()); assertTrue(tran2.wasCalledGetTransaction()); assertTrue(tran3.wasCalledGetTransaction()); assertTrue(tran4.wasCalledGetTransaction()); assertFalse(tran1.wasCalledRollback()); assertFalse(tran2.wasCalledRollback()); assertFalse(tran3.wasCalledRollback()); assertFalse(tran4.wasCalledRollback()); } /** * testCommitTransaction01 * @throws Exception */ @Test public void testCommitTransaction01() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.commitTransaction(tran, stat); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testCommitTransaction02 * @throws Exception */ @Test public void testCommitTransaction02() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.commitTransaction(tran, stat, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testCommitTransactions01 * @throws Exception */ @Test public void testCommitTransactions01() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); tranMap.put("tran", tran); Map<String, TransactionStatus> statMap = new HashMap<String, TransactionStatus>(); // try { BatchUtil.commitTransactions(tranMap, statMap); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testCommitTransactions02 * @throws Exception */ @Test public void testCommitTransactions02() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); tranMap.put("tran", tran); Map<String, TransactionStatus> statMap = new HashMap<String, TransactionStatus>(); statMap.put("tran", new TransactionStatusStub()); // try { BatchUtil.commitTransactions(tranMap, statMap, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testCommitTransactions03 * @throws Exception */ @Test public void testCommitTransaction03() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub4 tran = new PlatformTransactionManagerStub4(); tranMap.put("tran1", tran); tranMap.put("tran2", tran); tranMap.put("tran3", tran); tranMap.put("tran4", tran); Map<String, TransactionStatus> statMap = new LinkedHashMap<String, TransactionStatus>(); TransactionStatusStub status1 = new TransactionStatusStub(); status1.setName("stat1"); TransactionStatusStub status2 = new TransactionStatusStub(); status2.setName("stat2"); TransactionStatusStub status3 = new TransactionStatusStub(); status3.setName("stat3"); TransactionStatusStub status4 = new TransactionStatusStub(); status4.setName("stat4"); // LinkedHashMap??transactionStatus statMap.put("tran1", status1); statMap.put("tran2", status2); statMap.put("tran3", status3); statMap.put("tran4", status4); // BatchUtil.commitTransactions(tranMap, statMap, log); // ? List<TransactionStatus> statusList = tran.getStatusList(); // statMap?put??????? assertEquals("stat4", ((TransactionStatusStub) statusList.get(0)).getName()); assertEquals("stat3", ((TransactionStatusStub) statusList.get(1)).getName()); assertEquals("stat2", ((TransactionStatusStub) statusList.get(2)).getName()); assertEquals("stat1", ((TransactionStatusStub) statusList.get(3)).getName()); } /** * testEndTransaction01 * @throws Exception */ @Test public void testEndTransaction01() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.endTransaction(tran, stat); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testEndTransaction02 * @throws Exception */ @Test public void testEndTransaction02() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.endTransaction(tran, stat, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testEndTransactions01 * @throws Exception */ @Test public void testEndTransactions01() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); tranMap.put("tran", tran); Map<String, TransactionStatus> statMap = new HashMap<String, TransactionStatus>(); statMap.put("tran", new TransactionStatusStub()); // boolean result = false; try { result = BatchUtil.endTransactions(tranMap, statMap); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertTrue(result); } /** * testEndTransactions02 * @throws Exception */ @Test public void testEndTransactions02() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); PlatformTransactionManager tran2 = new PlatformTransactionManagerStub2(); tranMap.put("tran", tran); tranMap.put("tran2", tran2); Map<String, TransactionStatus> statMap = new HashMap<String, TransactionStatus>(); statMap.put("tran", new TransactionStatusStub()); statMap.put("tran2", new TransactionStatusStub()); // boolean result = true; try { result = BatchUtil.endTransactions(tranMap, statMap, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertFalse(result); assertThat(logger.getLoggingEvents().get(3).getMessage(), is(startsWith("[EAL025045] Failed to rollback transaction."))); } /** * testEndTransactions03 * @throws Exception */ @Test public void testEndTransactions03() throws Exception { // Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManagerStub4 tran = new PlatformTransactionManagerStub4(); tranMap.put("tran1", tran); tranMap.put("tran2", tran); tranMap.put("tran3", tran); tranMap.put("tran4", tran); // TransactionStatus??????????tranMap?put????? Map<String, TransactionStatus> statMap = new LinkedHashMap<String, TransactionStatus>(); TransactionStatusStub stat1 = new TransactionStatusStub(); stat1.setName("stat1"); stat1.setRollbackOnly(); TransactionStatusStub stat2 = new TransactionStatusStub(); stat2.setName("stat2"); stat2.setRollbackOnly(); TransactionStatusStub stat3 = new TransactionStatusStub(); stat3.setName("stat3"); stat3.setRollbackOnly(); TransactionStatusStub stat4 = new TransactionStatusStub(); stat4.setName("stat4"); stat4.setRollbackOnly(); statMap.put("tran1", stat1); statMap.put("tran2", stat2); statMap.put("tran3", stat3); statMap.put("tran4", stat4); // boolean result = BatchUtil.endTransactions(tranMap, statMap, log); // ? assertTrue(result); List<TransactionStatus> statList = tran.getStatusList(); // statMap?put???????? assertEquals("stat4", ((TransactionStatusStub) statList.get(0)).getName()); assertEquals("stat3", ((TransactionStatusStub) statList.get(1)).getName()); assertEquals("stat2", ((TransactionStatusStub) statList.get(2)).getName()); assertEquals("stat1", ((TransactionStatusStub) statList.get(3)).getName()); } /** * testSetSavepoint01 * @throws Exception */ @Test public void testSetSavepoint01() throws Exception { // // Object result = null; try { result = BatchUtil.setSavepoint(new TransactionStatusStub()); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testSetSavepoint02 * @throws Exception */ @Test public void testSetSavepoint02() throws Exception { // // Object result = null; try { result = BatchUtil.setSavepoint(new TransactionStatusStub(), log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testReleaseSavepoint01 * @throws Exception */ @Test public void testReleaseSavepoint01() throws Exception { // // try { BatchUtil.releaseSavepoint(new TransactionStatusStub(), new Object()); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testReleaseSavepoint02 * @throws Exception */ @Test public void testReleaseSavepoint02() throws Exception { // // try { BatchUtil.releaseSavepoint(new TransactionStatusStub(), new Object(), log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testRollbackSavepoint01 * @throws Exception */ @Test public void testRollbackSavepoint01() throws Exception { // // try { BatchUtil.rollbackSavepoint(new TransactionStatusStub(), new Object()); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testRollbackSavepoint02 * @throws Exception */ @Test public void testRollbackSavepoint02() throws Exception { // // try { BatchUtil.rollbackSavepoint(new TransactionStatusStub(), new Object(), log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testRollbackTransaction01 * @throws Exception */ @Test public void testRollbackTransaction01() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.rollbackTransaction(tran, stat); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testRollbackTransaction02 * @throws Exception */ @Test public void testRollbackTransaction02() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // try { BatchUtil.rollbackTransaction(tran, stat, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? } /** * testCommitRestartTransaction01 * @throws Exception */ @Test public void testCommitRestartTransaction01() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // TransactionStatus result = null; try { result = BatchUtil.commitRestartTransaction(tran, stat); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testCommitRestartTransaction02 * @throws Exception */ @Test public void testCommitRestartTransaction02() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // TransactionStatus result = null; try { result = BatchUtil.commitRestartTransaction(tran, stat, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testCommitRestartTransaction03 * @throws Exception */ @Test public void testCommitRestartTransaction03() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = null; try { result = BatchUtil.commitRestartTransaction(tran, stat, def); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testCommitRestartTransaction04 * @throws Exception */ @Test public void testCommitRestartTransaction04() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = null; try { result = BatchUtil.commitRestartTransaction(tran, stat, def, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testRollbackRestartTransaction001 * @throws Exception */ @Test public void testRollbackRestartTransaction001() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // TransactionStatus result = null; try { result = BatchUtil.rollbackRestartTransaction(tran, stat); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testRollbackRestartTransaction002 * @throws Exception */ @Test public void testRollbackRestartTransaction002() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); // TransactionStatus result = null; try { result = BatchUtil.rollbackRestartTransaction(tran, stat, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testRollbackRestartTransaction003 * @throws Exception */ @Test public void testRollbackRestartTransaction003() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = null; try { result = BatchUtil.rollbackRestartTransaction(tran, stat, def); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testRollbackRestartTransaction004 * @throws Exception */ @Test public void testRollbackRestartTransaction004() throws Exception { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionStatus stat = new TransactionStatusStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); // TransactionStatus result = null; try { result = BatchUtil.rollbackRestartTransaction(tran, stat, def, log); } catch (Exception e) { e.printStackTrace(); fail(); return; } // ? assertNotNull(result); } /** * testGetMemoryInfo001 * @throws Exception */ @Test public void testGetMemoryInfo001() throws Exception { String info = BatchUtil.getMemoryInfo(); assertNotNull(info); assertTrue(info.startsWith("Java memory info : ")); } /** * ??? * * <pre> * ?? * {@code TLogger}?{@code commons.logging.Log}??{@code BatchUtil}????? * ? * ??{@code debug()}?????????? * </pre> */ public void testSwitchLogger01() { // PlatformTransactionManager tran = new PlatformTransactionManagerStub(); TransactionDefinition def = BatchUtil.getTransactionDefinition(); Log mockCommonsLog = LogFactory.getLog(BatchUtilTest.class); TLogger mockLog = (TLogger) spy(log); mockCommonsLog = spy(mockCommonsLog); // BatchUtil.startTransaction(tran, def, mockCommonsLog); BatchUtil.startTransaction(tran, def, mockLog); // ? verify(mockCommonsLog, times(3)).debug(any()); verify(mockLog, times(2)).debug(any(String.class), any()); verify(mockLog).debug(any(String.class), any(), any(), any(), any(), any()); } /** * ??? * * <pre> * ?? * {@code TLogger}?{@code commons.logging.Log}??{@code BatchUtil}????? * ? * ??{@code error()}?????????? * </pre> */ public void testSwitchLogger02() { // TransactionDefinition def = BatchUtil.getTransactionDefinition(); Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>(); PlatformTransactionManager tran = new PlatformTransactionManagerStub(); Log mockCommonsLog = LogFactory.getLog(BatchUtilTest.class); TLogger mockLog = (TLogger) spy(log); mockCommonsLog = spy(mockCommonsLog); tran = spy(tran); tranMap.put("tran", tran); when(tran.getTransaction(def)).thenThrow(new TransactionSystemException("test exception")); // try { BatchUtil.startTransactions(def, tranMap, mockLog); fail(); } catch (TransactionException e) { assertTrue(e instanceof TransactionException); } try { BatchUtil.startTransactions(def, tranMap, mockCommonsLog); fail(); } catch (TransactionException e) { assertEquals("test exception", e.getMessage()); } // ? verify(mockCommonsLog).error(any()); verify(mockLog).error(any(String.class), any(), any(String.class)); } }