1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.database.statement;
23
24 import junit.framework.TestCase;
25 import org.dbunit.dataset.datatype.DataType;
26
27
28
29
30
31
32 public class BatchStatementDecoratorTest extends TestCase
33 {
34 public BatchStatementDecoratorTest(String s)
35 {
36 super(s);
37 }
38
39 public void testAddBatch() throws Exception
40 {
41 String template = "START VAL0 = ?, VAL1 = ?, VAL2 = ? END";
42 String expected = "START VAL0 = NULL, VAL1 = 'value', VAL2 = 1234 END";
43 Object[] values = new Object[]{null, "value", new Integer(1234)};
44
45 MockBatchStatement mockStatement = new MockBatchStatement();
46 mockStatement.addExpectedBatchString(expected);
47 mockStatement.setExpectedExecuteBatchCalls(1);
48 mockStatement.setExpectedClearBatchCalls(1);
49 mockStatement.setExpectedCloseCalls(1);
50
51 IPreparedBatchStatement preparedStatement =
52 new BatchStatementDecorator(template, mockStatement);
53
54 for (int i = 0; i < values.length; i++)
55 {
56 Object value = values[i];
57 preparedStatement.addValue(value, DataType.forObject(value));
58 }
59 preparedStatement.addBatch();
60 assertEquals("execute result", 1, preparedStatement.executeBatch());
61 preparedStatement.clearBatch();
62 preparedStatement.close();
63 mockStatement.verify();
64 }
65
66 public void testMultipleAddBatch() throws Exception
67 {
68 String template = "I am ?";
69 String[] expected = {"I am 'Manuel'", "I am 'not here'", "I am 'fine'"};
70 String[] values = {"Manuel", "not here", "fine"};
71
72 MockBatchStatement mockStatement = new MockBatchStatement();
73 mockStatement.addExpectedBatchStrings(expected);
74 mockStatement.setExpectedExecuteBatchCalls(1);
75 mockStatement.setExpectedClearBatchCalls(1);
76 mockStatement.setExpectedCloseCalls(1);
77
78 IPreparedBatchStatement preparedStatement =
79 new BatchStatementDecorator(template, mockStatement);
80
81 for (int i = 0; i < values.length; i++)
82 {
83 Object value = values[i];
84 preparedStatement.addValue(value, DataType.VARCHAR);
85 preparedStatement.addBatch();
86 }
87 assertEquals("execute result", values.length,
88 preparedStatement.executeBatch());
89 mockStatement.clearBatch();
90 mockStatement.close();
91 mockStatement.verify();
92 }
93
94 }
95
96
97