1 21 22 package org.webdocwf.util.loader.test; 23 24 import java.sql.Connection ; 25 import org.webdocwf.util.loader.test.DatabaseOperation; 26 import org.webdocwf.util.loader.test.LoaderOperation; 27 import org.webdocwf.util.loader.Loader; 28 29 import junit.framework.TestCase; 30 31 37 public abstract class LoaderTestCase 38 extends TestCase { 39 40 public LoaderTestCase(String name) { 41 super(name); 42 } 43 44 47 protected abstract Connection getConnection() throws Exception ; 48 49 52 53 protected String getDatabaseName() throws Exception { 54 return "LoaderTest"; 55 } 56 57 60 protected abstract Loader getLoader() throws Exception ; 61 62 66 protected void closeConnection(Connection connection) throws Exception { 67 connection.close(); 68 } 69 70 75 protected DatabaseOperation[] getSetUpOperation() throws Exception { 76 DatabaseOperation[] dbOperation = new DatabaseOperation[2]; 77 dbOperation[0] = new CreateDatabaseOperation(getDatabaseName()); 78 dbOperation[1] = new LoaderOperation(getLoader()); 79 80 return dbOperation; 81 } 82 83 88 protected DatabaseOperation[] getTearDownOperation() throws Exception { 89 DatabaseOperation[] dbOperation = new DatabaseOperation[1]; 90 dbOperation[0] = new DropDatabaseOperation(getDatabaseName()); 91 92 return dbOperation; 93 } 94 95 private void executeOperation(DatabaseOperation dbOperation) throws Exception { 96 if (dbOperation != null) { 97 if (!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.NONE)) { 98 if (!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.LOADER)) { 99 Connection conn = getConnection(); 100 try { 101 if (conn != null) 102 dbOperation.execute(conn); 103 } 104 finally { 105 if (conn != null) 106 closeConnection(conn); 107 } 108 } else 109 ( (LoaderOperation)dbOperation).execute(); 110 } 111 } 112 } 113 114 protected void setUp() throws Exception { 115 super.setUp(); 116 117 for (int i = 0; i < getSetUpOperation().length; i++) 118 executeOperation(getSetUpOperation()[i]); 119 } 120 121 protected void tearDown() throws Exception { 122 super.tearDown(); 123 124 for (int i = 0; i < getTearDownOperation().length; i++) 125 executeOperation(getTearDownOperation()[i]); 126 } 127 } 128 | Popular Tags |