View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * @author Manuel Laflamme
29   * @version $Revision$
30   * @since Mar 16, 2002
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