Changeset 7262380c0bc232cba2cbdf8d19313ca8e4d5d268
- Timestamp:
- 02/17/08 17:20:32
(8 months ago)
- Author:
- Christopher Jung <bktheg@web.de>
- git-committer:
- Christopher Jung <bktheg@web.de> 1203265232 +0100
- git-parent:
[a55e1ef5522b0215c6e961eebb95e1005be1be0c]
- git-author:
- Christopher Jung <bktheg@web.de> 1203265232 +0100
- Message:
Unterstuetzung fuer Junit4-Testcases mit DB-Tests verbessert
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| rd193343 |
r7262380 |
|
| 19 | 19 | package net.driftingsouls.ds2.server; |
|---|
| 20 | 20 | |
|---|
| | 21 | import java.sql.Connection; |
|---|
| | 22 | import java.sql.ResultSet; |
|---|
| | 23 | import java.sql.SQLException; |
|---|
| | 24 | import java.sql.Statement; |
|---|
| | 25 | |
|---|
| 21 | 26 | import net.driftingsouls.ds2.server.framework.BasicContext; |
|---|
| 22 | 27 | import net.driftingsouls.ds2.server.framework.CmdLineRequest; |
|---|
| 23 | | import net.driftingsouls.ds2.server.framework.Configuration; |
|---|
| 24 | 28 | import net.driftingsouls.ds2.server.framework.SimpleResponse; |
|---|
| 25 | 29 | |
|---|
| 26 | | import org.dbunit.DBTestCase; |
|---|
| 27 | | import org.dbunit.PropertiesBasedJdbcDatabaseTester; |
|---|
| 28 | | import org.dbunit.operation.DatabaseOperation; |
|---|
| | 30 | import org.junit.After; |
|---|
| | 31 | import org.junit.Before; |
|---|
| 29 | 32 | |
|---|
| 30 | 33 | /** |
|---|
| … | … | |
| 33 | 36 | * |
|---|
| 34 | 37 | */ |
|---|
| 35 | | public abstract class DriftingSoulsDBTestCase extends DBTestCase { |
|---|
| | 38 | public abstract class DriftingSoulsDBTestCase implements DBTestable { |
|---|
| 36 | 39 | protected BasicContext context; |
|---|
| | 40 | protected DBTestCaseAdapter dbTester; |
|---|
| 37 | 41 | |
|---|
| 38 | 42 | /** |
|---|
| … | … | |
| 40 | 44 | */ |
|---|
| 41 | 45 | public DriftingSoulsDBTestCase() { |
|---|
| 42 | | try { |
|---|
| 43 | | Configuration.init("./test/cfg/"); |
|---|
| 44 | | |
|---|
| 45 | | System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver" ); |
|---|
| 46 | | System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, |
|---|
| 47 | | "jdbc:mysql://"+Configuration.getSetting("db_server")+"/"+Configuration.getSetting("db_database") ); |
|---|
| 48 | | System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, Configuration.getSetting("db_user") ); |
|---|
| 49 | | System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, Configuration.getSetting("db_password") ); |
|---|
| 50 | | } |
|---|
| 51 | | catch( Exception e ) { |
|---|
| 52 | | if( e instanceof RuntimeException ) { |
|---|
| 53 | | throw (RuntimeException)e; |
|---|
| 54 | | } |
|---|
| 55 | | |
|---|
| 56 | | throw new RuntimeException("Fehler beim Initalisieren des Testcases", e); |
|---|
| 57 | | } |
|---|
| | 46 | dbTester = new DBTestCaseAdapter(this); |
|---|
| 58 | 47 | } |
|---|
| 59 | 48 | |
|---|
| 60 | | @Override |
|---|
| 61 | | protected DatabaseOperation getTearDownOperation() throws Exception { |
|---|
| 62 | | return DatabaseOperation.DELETE_ALL; |
|---|
| 63 | | } |
|---|
| 64 | | |
|---|
| 65 | | @Override |
|---|
| 66 | | protected void setUp() throws Exception { |
|---|
| | 49 | /** |
|---|
| | 50 | * SetUp-Methode |
|---|
| | 51 | * @throws Exception |
|---|
| | 52 | */ |
|---|
| | 53 | @Before |
|---|
| | 54 | public void setUp() throws Exception { |
|---|
| 67 | 55 | SimpleResponse response = new SimpleResponse(); |
|---|
| 68 | 56 | CmdLineRequest request = new CmdLineRequest(new String[0]); |
|---|
| 69 | 57 | this.context = new BasicContext(request, response); |
|---|
| 70 | 58 | |
|---|
| 71 | | super.setUp(); |
|---|
| | 59 | Connection con = this.dbTester.getConnection().getConnection(); |
|---|
| | 60 | Statement stmt = con.createStatement(); |
|---|
| | 61 | stmt.executeUpdate("INSERT INTO config VALUES ('1', '', '', '')"); |
|---|
| | 62 | stmt.close(); |
|---|
| | 63 | |
|---|
| | 64 | this.dbTester.setUp(); |
|---|
| 72 | 65 | } |
|---|
| 73 | 66 | |
|---|
| 74 | | @Override |
|---|
| 75 | | protected void tearDown() throws Exception { |
|---|
| | 67 | /** |
|---|
| | 68 | * TearDown-Methode |
|---|
| | 69 | * @throws Exception |
|---|
| | 70 | */ |
|---|
| | 71 | @After |
|---|
| | 72 | public void tearDown() throws Exception { |
|---|
| 76 | 73 | this.context.free(); |
|---|
| 77 | 74 | |
|---|
| 78 | | super.tearDown(); |
|---|
| | 75 | try { |
|---|
| | 76 | Connection con = this.dbTester.getConnection().getConnection(); |
|---|
| | 77 | Statement stmt = con.createStatement(); |
|---|
| | 78 | stmt.executeUpdate("SET FOREIGN_KEY_CHECKS=0"); |
|---|
| | 79 | ResultSet result = stmt.executeQuery("SHOW TABLES"); |
|---|
| | 80 | while( result.next() ) { |
|---|
| | 81 | String table = result.getString(1); |
|---|
| | 82 | Statement stmt2 = con.createStatement(); |
|---|
| | 83 | stmt2.executeUpdate("DELETE FROM "+table); |
|---|
| | 84 | stmt2.close(); |
|---|
| | 85 | } |
|---|
| | 86 | result.close(); |
|---|
| | 87 | stmt.executeUpdate("SET FOREIGN_KEY_CHECKS=1"); |
|---|
| | 88 | stmt.close(); |
|---|
| | 89 | } |
|---|
| | 90 | catch( SQLException e ) { |
|---|
| | 91 | e.printStackTrace(); |
|---|
| | 92 | } |
|---|
| | 93 | |
|---|
| | 94 | this.dbTester.tearDown(); |
|---|
| 79 | 95 | } |
|---|
| 80 | 96 | } |
|---|
| r1f46047 |
r7262380 |
|
| 39 | 39 | |
|---|
| 40 | 40 | @Override |
|---|
| 41 | | protected void setUp() throws Exception { |
|---|
| | 41 | public void setUp() throws Exception { |
|---|
| 42 | 42 | super.setUp(); |
|---|
| 43 | 43 | |
|---|
| … | … | |
| 45 | 45 | } |
|---|
| 46 | 46 | |
|---|
| 47 | | @Override |
|---|
| 48 | | protected IDataSet getDataSet() throws Exception { |
|---|
| | 47 | public IDataSet getDataSet() throws Exception { |
|---|
| 49 | 48 | return new FlatXmlDataSet(ShipUtilsTest.class.getResourceAsStream("ShipUtilsTest.xml")); |
|---|
| 50 | 49 | } |
|---|
| r81cff56 |
r7262380 |
|
| 59 | 59 | |
|---|
| 60 | 60 | @Override |
|---|
| 61 | | protected void setUp() throws Exception { |
|---|
| | 61 | public void setUp() throws Exception { |
|---|
| 62 | 62 | super.setUp(); |
|---|
| 63 | 63 | |
|---|
| … | … | |
| 108 | 108 | } |
|---|
| 109 | 109 | |
|---|
| 110 | | @Override |
|---|
| 111 | | protected IDataSet getDataSet() throws Exception { |
|---|
| | 110 | public IDataSet getDataSet() throws Exception { |
|---|
| 112 | 111 | return new FlatXmlDataSet(DeutTransporterTest.class.getResourceAsStream("DeutTransporterTest.xml")); |
|---|
| 113 | 112 | } |
|---|