Created
January 17, 2012 22:38
-
-
Save froop/1629470 to your computer and use it in GitHub Desktop.
[Java][JUnit] DbUnit例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.dbunit.*; | |
public class DbUnitSampleTest extends DatabaseTestCase { | |
@Override | |
protected IDatabaseConnection getConnection() throws Exception { | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection jdbcConn = DriverManager.getConnection( | |
"jdbc:mysql://localhost:3306/sample1", "user", "pass"); | |
return new DatabaseConnection(jdbcConn); | |
} | |
@Override | |
protected IDataSet getDataSet() throws Exception { | |
return createDataSet(new File("test/dbunit", "prepare.xml")); | |
} | |
private IDataSet createDataSet(File file) throws Exception { | |
return new FlatXmlDataSetBuilder().build(file); | |
} | |
@Test | |
public void testInsert() throws Exception { | |
new DbUnitSample().insert(); | |
IDataSet expected = createDataSet(new File("test/dbunit", "expected.xml")); | |
assertEqualsTable(expected, "sample1_1"); | |
} | |
private void assertEqualsTable(IDataSet expectedDataSet, String tableName) | |
throws Exception { | |
ITable expectedTable = expectedDataSet.getTable(tableName); | |
IDataSet databaseDataSet = getConnection().createDataSet(); | |
ITable actualTable = databaseDataSet.getTable(tableName); | |
Assertion.assertEquals(expectedTable, actualTable); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DtdCreate { | |
public static void main(String[] args) throws Exception { | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection jdbcConn = DriverManager.getConnection( | |
"jdbc:mysql://localhost:3306/sample1", "user", "pass"); | |
IDatabaseConnection connection = new DatabaseConnection(jdbcConn); | |
FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream( | |
"sample1.dtd")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE dataset SYSTEM "sample1.dtd"> | |
<dataset> | |
<sample1_1 col1="1" col2="abc" /> | |
<sample1_1 col1="2" col2="あいう" /> | |
</dataset> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!ELEMENT dataset ( | |
sample1_1*, | |
sample1_2*)> | |
<!ELEMENT sample1_1 EMPTY> | |
<!ATTLIST sample1_1 | |
col1 CDATA #REQUIRED | |
col2 CDATA #IMPLIED | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment