View Javadoc
1 /* 2 3 Loader - tool for transfering data from one JDBC source to another and 4 doing transformations during copy. 5 6 Copyright (C) 2002 Together 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 /* 23 * DatabaseTestCase.java Aug 29, 2002 24 * Sinisa Milosevic sinisami@eunet.yu 25 26 * 27 */ 28 29 package org.webdocwf.util.loader.test; 30 31 import java.sql.Connection; 32 import org.webdocwf.util.loader.test.DatabaseOperation; 33 import org.webdocwf.util.loader.test.LoaderOperation; 34 import org.webdocwf.util.loader.Loader; 35 36 import junit.framework.TestCase; 37 38 /*** 39 * Basic class for using Loader as a test case (extends Junit Test class) 40 * 41 * @author Sinisa Milosevic 42 * @version $Revision: 1.1 $ 43 */ 44 public abstract class LoaderTestCase extends TestCase 45 { 46 47 public LoaderTestCase(String name) 48 { 49 super(name); 50 } 51 52 /*** 53 * Returns the test database connection. 54 */ 55 protected abstract Connection getConnection() throws Exception; 56 57 /*** 58 * Returns the name of test database. 59 */ 60 61 protected String getDatabaseName() throws Exception 62 { 63 return "LoaderTest"; 64 } 65 66 /*** 67 * Returns the test Loader class (loaderjob). 68 */ 69 protected abstract Loader getLoader() throws Exception; 70 71 /*** 72 * Close the specified connection. Override this method of you want to 73 * keep your connection alive between tests. 74 */ 75 protected void closeConnection(Connection connection) throws Exception 76 { 77 connection.close(); 78 } 79 80 /*** 81 * Returns the database operations executed in test setup. First operation will be 82 * executed dbOperation[0], then dbOperation[1]... 83 * Override this method of you want to change database operations before the start test. 84 */ 85 protected DatabaseOperation[] getSetUpOperation() throws Exception 86 { 87 DatabaseOperation[] dbOperation = new DatabaseOperation[2]; 88 dbOperation[0]=new CreateDatabaseOperation(getDatabaseName()); 89 dbOperation[1]=new LoaderOperation(getLoader()); 90 91 return dbOperation; 92 } 93 94 /*** 95 * Returns the database operation executed in test cleanup. 96 * First operation will be executed dbOperation[0], then dbOperation[1]... 97 * Override this method of you want to change database operations after the test execution. 98 */ 99 protected DatabaseOperation[] getTearDownOperation() throws Exception 100 { 101 DatabaseOperation[] dbOperation = new DatabaseOperation[1]; 102 dbOperation[0]=new DropDatabaseOperation(getDatabaseName()); 103 104 return dbOperation; 105 } 106 107 private void executeOperation(DatabaseOperation dbOperation) throws Exception 108 { 109 if(dbOperation!=null) { 110 if (!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.NONE)) 111 { 112 113 Connection conn = getConnection(); 114 if(!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.LOADER)) { 115 try 116 { 117 if(conn!=null) 118 dbOperation.execute(conn); 119 } 120 finally 121 { 122 if(conn!=null) 123 closeConnection(conn); 124 } 125 } 126 else 127 ((LoaderOperation)dbOperation).execute(); 128 } 129 } 130 } 131 132 133 134 protected void setUp() throws Exception 135 { 136 super.setUp(); 137 138 for(int i=0; i<getSetUpOperation().length; i++) 139 executeOperation(getSetUpOperation()[i]); 140 } 141 142 protected void tearDown() throws Exception 143 { 144 super.tearDown(); 145 146 for(int i=0; i<getTearDownOperation().length; i++) 147 executeOperation(getTearDownOperation()[i]); 148 } 149 } 150 151 152 153 154

This page automatically generated by Maven