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  package org.dbunit.dataset.xml;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.FileWriter;
27  import java.io.Writer;
28  
29  import org.dbunit.DatabaseEnvironment;
30  import org.dbunit.database.IDatabaseConnection;
31  import org.dbunit.dataset.AbstractDataSetTest;
32  import org.dbunit.dataset.FilteredDataSet;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.testutil.FileAsserts;
35  import org.dbunit.testutil.TestUtils;
36  
37  /**
38   * @author Manuel Laflamme
39   * @version $Revision$
40   * @since Apr 4, 2002
41   */
42  public class FlatDtdDataSetIT extends AbstractDataSetTest
43  {
44      private static final String DTD_FILE =
45          "dtd/flatDtdDataSetTest.dtd";
46      private static final String DUPLICATE_FILE =
47          "dtd/flatDtdDataSetDuplicateTest.dtd";
48      private static final String DUPLICATE_MULTIPLE_CASE_FILE =
49          "dtd/flatDtdDataSetDuplicateMultipleCaseTest.dtd";
50  
51      public FlatDtdDataSetIT(String s)
52      {
53          super(s);
54      }
55  
56      ////////////////////////////////////////////////////////////////////////////
57      // AbstractDataSetTest class
58  
59      private File getFile(String fileName) throws Exception
60      {
61          return TestUtils.getFileForDatabaseEnvironment(TestUtils.getFileName(fileName));
62      }
63  
64      protected IDataSet createDataSet() throws Exception
65      {
66          return new FlatDtdDataSet(TestUtils.getFileReader(DTD_FILE));
67      }
68  
69      protected IDataSet createDuplicateDataSet() throws Exception
70      {
71          return new FlatDtdDataSet(TestUtils.getFileReader(DUPLICATE_FILE));
72      }
73  
74      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 
75      {
76          return new FlatDtdDataSet(TestUtils.getFileReader(DUPLICATE_MULTIPLE_CASE_FILE));
77      }
78  
79      protected int[] getExpectedDuplicateRows()
80      {
81          return new int[] {0, 0, 0};
82      }
83  
84      ////////////////////////////////////////////////////////////////////////////
85      // Test methods
86  
87      public void testWriteFromDtd() throws Exception
88      {
89          IDataSet dataSet = new FlatDtdDataSet(TestUtils.getFileReader(DTD_FILE));
90  
91          File tempFile = File.createTempFile("flatXmlDocType", ".dtd");
92  
93          try
94          {
95              Writer out = new FileWriter(tempFile);
96  
97              try
98              {
99                  // write DTD in temp file
100                 FlatDtdDataSet.write(dataSet, out);
101             }
102             finally
103             {
104                 out.close();
105             }
106 
107             FileAsserts.assertEquals(
108                     new BufferedReader(TestUtils.getFileReader(DTD_FILE)),
109                     new BufferedReader(new FileReader(tempFile)));
110         }
111         finally
112         {
113             tempFile.delete();
114         }
115 
116     }
117 
118     public void testWriteFromDatabase() throws Exception
119     {
120         IDatabaseConnection connection =
121                 DatabaseEnvironment.getInstance().getConnection();
122         IDataSet dataSet = connection.createDataSet();
123 
124         File tempFile = File.createTempFile("flatXmlDocType", ".dtd");
125 
126         try
127         {
128             Writer out = new FileWriter(tempFile);
129 
130             try
131             {
132                 // write DTD in temp file
133                 String[] tableNames = getExpectedNames();
134                 FlatDtdDataSet.write(new FilteredDataSet(
135                         tableNames, dataSet), out);
136             }
137             finally
138             {
139                 out.close();
140             }
141 
142             FileAsserts.assertEquals(
143                     new BufferedReader(new FileReader(getFile(DTD_FILE))),
144                     new BufferedReader(new FileReader(tempFile)));
145         }
146         finally
147         {
148             tempFile.delete();
149         }
150     }
151 
152 
153 }
154 
155 
156 
157 
158