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 static org.assertj.core.api.Assertions.assertThat;
24  
25  import java.io.BufferedReader;
26  import java.io.ByteArrayOutputStream;
27  import java.io.File;
28  import java.io.Writer;
29  import java.nio.charset.StandardCharsets;
30  import java.nio.file.Files;
31  
32  import org.dbunit.DatabaseEnvironment;
33  import org.dbunit.database.IDatabaseConnection;
34  import org.dbunit.dataset.AbstractDataSetTest;
35  import org.dbunit.dataset.DataSetBuilder;
36  import org.dbunit.dataset.FilteredDataSet;
37  import org.dbunit.dataset.IDataSet;
38  import org.dbunit.testutil.FileAsserts;
39  import org.dbunit.testutil.TestUtils;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * @author Manuel Laflamme
44   * @version $Revision$
45   * @since Apr 4, 2002
46   */
47  class FlatDtdDataSetIT extends AbstractDataSetTest
48  {
49      private static final String DTD_FILE = "dtd/flatDtdDataSetTest.dtd";
50      private static final String DUPLICATE_FILE =
51              "dtd/flatDtdDataSetDuplicateTest.dtd";
52      private static final String DUPLICATE_MULTIPLE_CASE_FILE =
53              "dtd/flatDtdDataSetDuplicateMultipleCaseTest.dtd";
54  
55      ////////////////////////////////////////////////////////////////////////////
56      // AbstractDataSetTest class
57  
58      private File getFile(final String fileName) throws Exception
59      {
60          return TestUtils
61                  .getFileForDatabaseEnvironment(TestUtils.getFileName(fileName));
62      }
63  
64      @Override
65      protected IDataSet createDataSet() throws Exception
66      {
67          return new FlatDtdDataSet(TestUtils.getFileReader(DTD_FILE));
68      }
69  
70      @Override
71      protected IDataSet createDuplicateDataSet() throws Exception
72      {
73          return new FlatDtdDataSet(TestUtils.getFileReader(DUPLICATE_FILE));
74      }
75  
76      @Override
77      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception
78      {
79          return new FlatDtdDataSet(
80                  TestUtils.getFileReader(DUPLICATE_MULTIPLE_CASE_FILE));
81      }
82  
83      @Override
84      protected int[] getExpectedDuplicateRows()
85      {
86          return new int[] {0, 0, 0};
87      }
88  
89      ////////////////////////////////////////////////////////////////////////////
90      // Test methods
91  
92      @Test
93      void testWriteFromDtd_withValidDtd_writesEquivalentDtdFile() throws Exception
94      {
95          final IDataSet dataSet =
96                  new FlatDtdDataSet(TestUtils.getFileReader(DTD_FILE));
97  
98          final File tempFile = Files.createTempFile("flatXmlDocType", ".dtd").toFile();
99  
100         try
101         {
102             final Writer out = Files.newBufferedWriter(tempFile.toPath(), StandardCharsets.UTF_8);
103 
104             try
105             {
106                 // write DTD in temp file
107                 FlatDtdDataSet.write(dataSet, out);
108             } finally
109             {
110                 out.close();
111             }
112 
113             FileAsserts.assertEquals(
114                     new BufferedReader(TestUtils.getFileReader(DTD_FILE)),
115                     new BufferedReader(Files.newBufferedReader(tempFile.toPath(), StandardCharsets.UTF_8)));
116         } finally
117         {
118             tempFile.delete();
119         }
120 
121     }
122 
123     @Test
124     void testWriteFromDatabase_withDatabaseDataSet_writesEquivalentDtdFile() throws Exception
125     {
126         final IDatabaseConnection connection =
127                 DatabaseEnvironment.getInstance().getConnection();
128         final IDataSet dataSet = connection.createDataSet();
129 
130         final File tempFile = Files.createTempFile("flatXmlDocType", ".dtd").toFile();
131 
132         try
133         {
134             final Writer out = Files.newBufferedWriter(tempFile.toPath(), StandardCharsets.UTF_8);
135 
136             try
137             {
138                 // write DTD in temp file
139                 final String[] tableNames = getExpectedNames();
140                 FlatDtdDataSet.write(new FilteredDataSet(tableNames, dataSet),
141                         out);
142             } finally
143             {
144                 out.close();
145             }
146 
147             FileAsserts.assertEquals(
148                     new BufferedReader(Files.newBufferedReader(getFile(DTD_FILE).toPath(), StandardCharsets.UTF_8)),
149                     new BufferedReader(Files.newBufferedReader(tempFile.toPath(), StandardCharsets.UTF_8)));
150         } finally
151         {
152             tempFile.delete();
153         }
154     }
155 
156     @Test
157     void testWrite_withNonAsciiTableName_writesUtf8Bytes() throws Exception
158     {
159         final IDataSet dataSet = new DataSetBuilder().table("café_TABLE")
160                 .columns("ID").row(1).build();
161 
162         final ByteArrayOutputStream out = new ByteArrayOutputStream();
163         FlatDtdDataSet.write(dataSet, out);
164 
165         final String actualUtf8 =
166                 new String(out.toByteArray(), StandardCharsets.UTF_8);
167         assertThat(actualUtf8)
168                 .as("The DTD output stream must be encoded as UTF-8,"
169                         + " matching what the InputStream constructor's SAX"
170                         + " parsing assumes, regardless of the platform"
171                         + " default charset.")
172                 .contains("café_TABLE");
173     }
174 }