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.ext.mssql;
23  
24  import java.io.FileReader;
25  import java.io.Reader;
26  
27  import org.dbunit.AbstractDatabaseIT;
28  import org.dbunit.Assertion;
29  import org.dbunit.TestFeature;
30  import org.dbunit.dataset.DataSetUtils;
31  import org.dbunit.dataset.ForwardOnlyDataSet;
32  import org.dbunit.dataset.IDataSet;
33  import org.dbunit.dataset.ITable;
34  import org.dbunit.dataset.LowerCaseDataSet;
35  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
36  import org.dbunit.dataset.xml.XmlDataSet;
37  import org.dbunit.operation.DatabaseOperation;
38  import org.dbunit.testutil.TestUtils;
39  
40  /**
41   * @author Manuel Laflamme
42   * @author Eric Pugh
43   * @version $Revision$
44   * @since Feb 19, 2002
45   */
46  public class InsertIdentityOperationIT extends AbstractDatabaseIT
47  {
48      public InsertIdentityOperationIT(String s)
49      {
50          super(s);
51      }
52      
53      protected boolean runTest(String testName) {
54        return environmentHasFeature(TestFeature.INSERT_IDENTITY);
55      }
56  
57      public void testExecuteXML() throws Exception
58      {
59          Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTest.xml");
60          IDataSet dataSet = new XmlDataSet(in);
61  
62          testExecute(dataSet);
63      }
64  
65      public void testExecuteFlatXML() throws Exception
66      {
67          Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTestFlat.xml");
68          IDataSet dataSet = new FlatXmlDataSetBuilder().build(in);
69  
70          testExecute(dataSet);
71      }
72  
73      public void testExecuteLowerCase() throws Exception
74      {
75          Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTestFlat.xml");
76          IDataSet dataSet = new LowerCaseDataSet(new FlatXmlDataSetBuilder().build(in));
77  
78          testExecute(dataSet);
79      }
80  
81      public void testExecuteForwardOnly() throws Exception
82      {
83          Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTestFlat.xml");
84          IDataSet dataSet = new ForwardOnlyDataSet(new FlatXmlDataSetBuilder().build(in));
85  
86          testExecute(dataSet);
87      }
88  
89      private void testExecute(IDataSet dataSet) throws Exception
90      {
91          ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
92  //        InsertIdentityOperation.CLEAN_INSERT.execute(_connection, dataSet);
93          InsertIdentityOperation.INSERT.execute(_connection, dataSet);
94          ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
95  
96          assertEquals("table count", tablesBefore.length, tablesAfter.length);
97  
98          // Verify tables after
99          for (int i = 0; i < tablesAfter.length; i++)
100         {
101             ITable tableBefore = tablesBefore[i];
102             ITable tableAfter = tablesAfter[i];
103 
104             String name = tableAfter.getTableMetaData().getTableName();
105             if (name.startsWith("IDENTITY"))
106             {
107                 assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
108                 if (dataSet instanceof ForwardOnlyDataSet)
109                 {
110                     assertTrue(name, tableAfter.getRowCount() > 0);
111                 }
112                 else
113                 {
114                     Assertion.assertEquals(dataSet.getTable(name), tableAfter);
115                 }
116             }
117             else
118             {
119                 // Other tables should have not been affected
120                 Assertion.assertEquals(tableBefore, tableAfter);
121             }
122         }
123     }
124 
125     /* test case was added to validate the bug that tables with Identity columns that are not
126     one of the primary keys are able to figure out if an IDENTITY_INSERT is needed.
127     Thanks to Gaetano Di Gregorio for finding the bug.
128     */
129     public void testIdentityInsertNoPK() throws Exception
130     {
131         Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTestNoPK.xml");
132         IDataSet xmlDataSet = new FlatXmlDataSetBuilder().build(in);
133 
134         ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
135         InsertIdentityOperation.CLEAN_INSERT.execute(_connection, xmlDataSet);
136         ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
137 
138         // Verify tables after
139         for (int i = 0; i < tablesAfter.length; i++)
140         {
141             ITable tableBefore = tablesBefore[i];
142             ITable tableAfter = tablesAfter[i];
143 
144             String name = tableAfter.getTableMetaData().getTableName();
145             if (name.equals("TEST_IDENTITY_NOT_PK"))
146             {
147                 assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
148                 Assertion.assertEquals(xmlDataSet.getTable(name), tableAfter);
149             }
150             else
151             {
152                 // Other tables should have not been affected
153                 Assertion.assertEquals(tableBefore, tableAfter);
154             }
155         }
156     }
157     
158     public void testSetCustomIdentityColumnFilter() throws Exception
159     {
160         _connection.getConfig().setProperty(InsertIdentityOperation.PROPERTY_IDENTITY_COLUMN_FILTER, InsertIdentityOperation.IDENTITY_FILTER_EXTENDED);
161         try {
162             IDataSet dataSet = _connection.createDataSet();
163             ITable table = dataSet.getTable("IDENTITY_TABLE");
164             
165             InsertIdentityOperation op = new InsertIdentityOperation(DatabaseOperation.INSERT);
166             boolean hasIdentityColumn = op.hasIdentityColumn(table.getTableMetaData(), _connection);
167             assertTrue("Identity column not recognized", hasIdentityColumn);
168         }
169         finally{
170             // Reset property
171             _connection.getConfig().setProperty(InsertIdentityOperation.PROPERTY_IDENTITY_COLUMN_FILTER, null);
172         }
173     }
174 }
175 
176 
177 
178 
179 
180 
181 
182 
183 
184