KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ext > mssql > InsertIdentityOperationTest


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 org.dbunit.AbstractDatabaseTest;
25 import org.dbunit.Assertion;
26 import org.dbunit.dataset.*;
27 import org.dbunit.dataset.xml.FlatXmlDataSet;
28 import org.dbunit.dataset.xml.XmlDataSet;
29
30 import java.io.FileReader JavaDoc;
31 import java.io.Reader JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @author Eric Pugh
36  * @version $Revision: 1.3 $
37  * @since Feb 19, 2002
38  */

39 public class InsertIdentityOperationTest extends AbstractDatabaseTest
40 {
41     public InsertIdentityOperationTest(String JavaDoc s)
42     {
43         super(s);
44     }
45
46     public void testExecuteXML() throws Exception JavaDoc
47     {
48         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertIdentityOperationTest.xml");
49         IDataSet dataSet = new XmlDataSet(in);
50
51         testExecute(dataSet);
52     }
53
54     public void testExecuteFlatXML() throws Exception JavaDoc
55     {
56         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertIdentityOperationTestFlat.xml");
57         IDataSet dataSet = new FlatXmlDataSet(in);
58
59         testExecute(dataSet);
60     }
61
62     public void testExecuteLowerCase() throws Exception JavaDoc
63     {
64         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertIdentityOperationTestFlat.xml");
65         IDataSet dataSet = new LowerCaseDataSet(new FlatXmlDataSet(in));
66
67         testExecute(dataSet);
68     }
69
70     public void testExecuteForwardOnly() throws Exception JavaDoc
71     {
72         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertIdentityOperationTestFlat.xml");
73         IDataSet dataSet = new ForwardOnlyDataSet(new FlatXmlDataSet(in));
74
75         testExecute(dataSet);
76     }
77
78     private void testExecute(IDataSet dataSet) throws Exception JavaDoc
79     {
80         ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
81 // InsertIdentityOperation.CLEAN_INSERT.execute(_connection, dataSet);
82
InsertIdentityOperation.INSERT.execute(_connection, dataSet);
83         ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
84
85         assertEquals("table count", tablesBefore.length, tablesAfter.length);
86
87         // Verify tables after
88
for (int i = 0; i < tablesAfter.length; i++)
89         {
90             ITable tableBefore = tablesBefore[i];
91             ITable tableAfter = tablesAfter[i];
92
93             String JavaDoc name = tableAfter.getTableMetaData().getTableName();
94             if (name.startsWith("IDENTITY"))
95             {
96                 assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
97                 if (dataSet instanceof ForwardOnlyDataSet)
98                 {
99                     assertTrue(name, tableAfter.getRowCount() > 0);
100                 }
101                 else
102                 {
103                     Assertion.assertEquals(dataSet.getTable(name), tableAfter);
104                 }
105             }
106             else
107             {
108                 // Other tables should have not been affected
109
Assertion.assertEquals(tableBefore, tableAfter);
110             }
111         }
112     }
113
114     /* test case was added to validate the bug that tables with Identity columns that are not
115     one of the primary keys are able to figure out if an IDENTITY_INSERT is needed.
116     Thanks to Gaetano Di Gregorio for finding the bug.
117     */

118     public void testIdentityInsertNoPK() throws Exception JavaDoc
119     {
120         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertIdentityOperationTestNoPK.xml");
121         IDataSet xmlDataSet = new FlatXmlDataSet(in);
122
123         ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
124         InsertIdentityOperation.CLEAN_INSERT.execute(_connection, xmlDataSet);
125         ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
126
127         // Verify tables after
128
for (int i = 0; i < tablesAfter.length; i++)
129         {
130             ITable tableBefore = tablesBefore[i];
131             ITable tableAfter = tablesAfter[i];
132
133             String JavaDoc name = tableAfter.getTableMetaData().getTableName();
134             if (name.equals("TEST_IDENTITY_NOT_PK"))
135             {
136                 assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
137                 Assertion.assertEquals(xmlDataSet.getTable(name), tableAfter);
138             }
139             else
140             {
141                 // Other tables should have not been affected
142
Assertion.assertEquals(tableBefore, tableAfter);
143             }
144         }
145     }
146 }
147
148
149
150
151
152
153
154
155
156
157
Popular Tags