1 21 22 package org.dbunit; 23 24 import junit.framework.TestCase; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.operation.DatabaseOperation; 28 29 34 public abstract class DatabaseTestCase extends TestCase 35 { 36 public DatabaseTestCase() 37 { 38 } 39 40 public DatabaseTestCase(String name) 41 { 42 super(name); 43 } 44 45 48 protected abstract IDatabaseConnection getConnection() throws Exception ; 49 50 53 protected abstract IDataSet getDataSet() throws Exception ; 54 55 59 protected void closeConnection(IDatabaseConnection connection) throws Exception 60 { 61 connection.close(); 62 } 63 64 67 protected DatabaseOperation getSetUpOperation() throws Exception 68 { 69 return DatabaseOperation.CLEAN_INSERT; 70 } 71 72 75 protected DatabaseOperation getTearDownOperation() throws Exception 76 { 77 return DatabaseOperation.NONE; 78 } 79 80 private void executeOperation(DatabaseOperation operation) throws Exception 81 { 82 if (operation != DatabaseOperation.NONE) 83 { 84 IDatabaseConnection connection = getConnection(); 85 try 86 { 87 operation.execute(connection, getDataSet()); 88 } 89 finally 90 { 91 closeConnection(connection); 92 } 93 } 94 } 95 96 97 100 protected void setUp() throws Exception 101 { 102 super.setUp(); 103 104 executeOperation(getSetUpOperation()); 105 } 106 107 protected void tearDown() throws Exception 108 { 109 super.tearDown(); 110 111 executeOperation(getTearDownOperation()); 112 } 113 } 114 115 116 117 118 119 | Popular Tags |