Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakub Holy
committed
Apr 8, 2011
1 parent
98ef073
commit c32264c
Showing
7 changed files
with
180 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
target | ||
testData/testDB | ||
derby.log |
This file contains 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
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
src/main/java/iterate/jz2011/codeinjection/javaproxy/Main.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package iterate.jz2011.codeinjection.javaproxy; | ||
|
||
import java.sql.BatchUpdateException; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import net.jakubholy.testing.dbunit.embeddeddb.EmbeddedDbTester; | ||
|
||
public class Main { | ||
|
||
private static final int BATCH_SIZE = 2; | ||
|
||
private static final String ILLEGAL_VALUE = "Only values of 10 or less characters are allowed"; | ||
|
||
private static EmbeddedDbTester testDb = new EmbeddedDbTester(); | ||
|
||
private void failingJdbcBatchInsert(Connection connection, Map<Integer, String> data) throws SQLException { | ||
|
||
PreparedStatement rawPrepStmt = connection.prepareStatement("INSERT INTO my_test_schema.my_test_table (id,some_text) VALUES (?,?)"); | ||
PreparedStatement loggingPrepStmt = LoggingStatementDecorator.createProxy(rawPrepStmt); | ||
|
||
int batchCounter = 0; | ||
for (Entry<Integer, String> row : data.entrySet()) { | ||
loggingPrepStmt.setInt(1, row.getKey()); | ||
loggingPrepStmt.setString(2, row.getValue()); | ||
loggingPrepStmt.addBatch(); | ||
++batchCounter; | ||
|
||
if (batchCounter % BATCH_SIZE == 0) | ||
loggingPrepStmt.executeBatch(); | ||
} | ||
|
||
// Execute remaining batches if any | ||
if (batchCounter % BATCH_SIZE != 0) | ||
loggingPrepStmt.executeBatch(); | ||
|
||
} | ||
|
||
/** | ||
* Initialize DB, perform updates. | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
|
||
// Create the DB from the DDL | ||
try { | ||
net.jakubholy.testing.dbunit.embeddeddb.DatabaseCreator.createAndInitializeTestDb(); | ||
} catch (BatchUpdateException e) {/* perhaps the DB exists already*/} | ||
|
||
// Initialize DB connection, clear existing data from a previous run | ||
testDb.onSetup(); | ||
|
||
// Prepare & insert data! | ||
@SuppressWarnings("serial") | ||
Map<Integer, String> data = new HashMap<Integer, String>() {{ | ||
// Batch 0 | ||
put(100, "ok value 1"); | ||
put(200, "ok value 2"); | ||
// Batch 1 | ||
put(300, "ok value 3"); | ||
put(300, ILLEGAL_VALUE); | ||
// Batch 2 | ||
put(400, "ok value 4"); | ||
}}; | ||
|
||
new Main().failingJdbcBatchInsert(testDb.getSqlConnection(), data); | ||
|
||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- DDL file defining the schemas and tables to create in the test database. | ||
-- Read and executed by net.jakubholy.testing.dbunit.DatabaseCreator#createDbSchemaFromDdl(Connection) | ||
-- see net.jakubholy.testing.dbunit.DatabaseCreator#main. | ||
|
||
-- Replace the text below with whatever you need. | ||
create schema my_test_schema; | ||
|
||
create table my_test_schema.my_test_table ( | ||
id int primary key | ||
, some_text varchar(10)); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!--DOCTYPE dataset SYSTEM 'testData/dbunit-test_data_set.dtd'--> | ||
<!-- | ||
See DbUnit's XmlDataSet. | ||
Database definition for AbstractEmbeddedDbTestCase | ||
- uses DbUnit to load the data into an embedded derby DB - | ||
To connect to the DB: user sa, psw emty, url | ||
jdbc:derby:<absolute path to this project>\testData\testDB | ||
Note: Only 1 connection to the Derby DB at a time is allowed!!!! | ||
IMPORTANT | ||
========= | ||
1) You must have <table> entry for any table you want DbUnit to clear for you. | ||
Even if you don't want DbUnit to insert any data you must still declare at least | ||
<table name="yourSchema.yourTable" /> for the table yourSchema.yourTable to be emptied during test setUp(). | ||
2) When using a column that contains XML value, you may use <![CDATA[ to supply the value. | ||
But there should be no space within '<value><![CDATA[<?xml ...' otherwise you may get Xerces' | ||
exception 'The processing instruction target matching "[xX][mM][lL]" is not allowed.' | ||
ABOUT FILE FORMAT | ||
================= | ||
This format is better than flat data set because it describes explicitly the | ||
columns though it's also more verbose. | ||
Also, thanks to setting the DbUnit feature QUALIFIED_TABLE_NAMES to true in the test, | ||
we can use fully qualified table names and thus have tables in different schemas. | ||
HOW TO CREATE | ||
============= | ||
To create a DbUnit data set like this you've several options: | ||
1. Write it manually. | ||
2. Use DbUnit itself to connect to a DB, load a data set and store it into a XmlDataSet. | ||
3. Use the Eclipse plugin QuantumDB and its ability to export table into DbUnit data set format. | ||
4. Use Jailer (http://jailer.sourceforge.net/home.htm) - it can export also only a sample of | ||
a DB while preserving referential integrity. | ||
--> | ||
<dataset> | ||
|
||
<!-- | ||
######### TABLES TO CLEAR WITHOUT INSERTING ANY DATA DURING TestCase#setUp() ######### | ||
Provide names of the tables whose content shall be dropped prior to running a test method. | ||
You don't need to list its columns. | ||
--> | ||
<table name="my_test_schema.my_test_table" /> | ||
|
||
</dataset> |