View Javadoc
1   package org.dbunit.dataset.xml;
2   
3   import junit.framework.TestCase;
4   import org.dbunit.dataset.Column;
5   import org.dbunit.dataset.DefaultDataSet;
6   import org.dbunit.dataset.DefaultTable;
7   import org.dbunit.dataset.datatype.DataType;
8   
9   import java.io.StringWriter;
10  
11  /**
12   * <p> Copyright (c) 2003 OZ.COM.  All Rights Reserved. </p>
13   * 
14   * @author manuel.laflamme
15   * @since Jan 26, 2004
16   */
17  public class FlatDtdWriterTest extends TestCase
18  {
19      public void testWriteSequenceModel() throws Exception
20      {
21          String expectedOutput =
22                  "<!ELEMENT dataset (\n" +
23                  "    TABLE1*,\n" +
24                  "    TABLE2*)>\n" +
25                  "\n" +
26                  "<!ELEMENT TABLE1 EMPTY>\n" +
27                  "<!ATTLIST TABLE1\n" +
28                  "    COL0 CDATA #IMPLIED\n" +
29                  "    COL1 CDATA #IMPLIED\n" +
30                  "    COL2 CDATA #REQUIRED\n" +
31                  "    COL3 CDATA #IMPLIED\n" + // Has default value
32                  ">\n" +
33                  "\n" +
34                  "<!ELEMENT TABLE2 EMPTY>\n" +
35                  "<!ATTLIST TABLE2\n" +
36                  "    COL0 CDATA #IMPLIED\n" +
37                  "    COL1 CDATA #IMPLIED\n" +
38                  "    COL2 CDATA #REQUIRED\n" +
39                  "    COL3 CDATA #IMPLIED\n" + // Has default value
40                  ">\n" +
41                  "\n";
42  
43          Column[] columns = new Column[]{
44              new Column("COL0", DataType.UNKNOWN, Column.NULLABLE),
45              new Column("COL1", DataType.UNKNOWN, Column.NULLABLE_UNKNOWN),
46              new Column("COL2", DataType.UNKNOWN, Column.NO_NULLS),
47              new Column("COL3", DataType.UNKNOWN, DataType.UNKNOWN.toString(), Column.NO_NULLS, "default"),
48          };
49  
50          DefaultTable table1 = new DefaultTable("TABLE1", columns);
51          DefaultTable table2 = new DefaultTable("TABLE2", columns);
52  
53          StringWriter stringWriter = new StringWriter();
54          FlatDtdWriter dtdWriter = new FlatDtdWriter(stringWriter);
55          dtdWriter.write(new DefaultDataSet(table1, table2));
56  
57          String actualOutput = stringWriter.toString();
58          assertEquals("output", expectedOutput, actualOutput);
59      }
60  
61      public void testWriteChoiceModel() throws Exception
62      {
63          String expectedOutput =
64                  "<!ELEMENT dataset (\n" +
65                  "   (TABLE1|\n" +
66                  "    TABLE2)*)>\n" +
67                  "\n" +
68                  "<!ELEMENT TABLE1 EMPTY>\n" +
69                  "<!ATTLIST TABLE1\n" +
70                  "    COL0 CDATA #IMPLIED\n" +
71                  "    COL1 CDATA #IMPLIED\n" +
72                  "    COL2 CDATA #REQUIRED\n" +
73                  ">\n" +
74                  "\n" +
75                  "<!ELEMENT TABLE2 EMPTY>\n" +
76                  "<!ATTLIST TABLE2\n" +
77                  "    COL0 CDATA #IMPLIED\n" +
78                  "    COL1 CDATA #IMPLIED\n" +
79                  "    COL2 CDATA #REQUIRED\n" +
80                  ">\n" +
81                  "\n";
82  
83          Column[] columns = new Column[]{
84              new Column("COL0", DataType.UNKNOWN, Column.NULLABLE),
85              new Column("COL1", DataType.UNKNOWN, Column.NULLABLE_UNKNOWN),
86              new Column("COL2", DataType.UNKNOWN, Column.NO_NULLS),
87          };
88  
89          DefaultTable table1 = new DefaultTable("TABLE1", columns);
90          DefaultTable table2 = new DefaultTable("TABLE2", columns);
91  
92          StringWriter stringWriter = new StringWriter();
93          FlatDtdWriter dtdWriter = new FlatDtdWriter(stringWriter);
94          dtdWriter.setContentModel(FlatDtdWriter.CHOICE);
95          dtdWriter.write(new DefaultDataSet(table1, table2));
96  
97          String actualOutput = stringWriter.toString();
98          assertEquals("output", expectedOutput, actualOutput);
99      }
100 
101     
102     public void testWriteChoiceModel_NoInputColumns() throws Exception
103     {
104         String expectedOutput =
105                 "<!ELEMENT dataset (\n" +
106                 "   (TABLE1|\n" +
107                 "    TABLE2)*)>\n" +
108                 "\n" +
109                 "<!ELEMENT TABLE1 EMPTY>\n" +
110                 "<!ATTLIST TABLE1\n" +
111                 ">\n" +
112                 "\n" +
113                 "<!ELEMENT TABLE2 EMPTY>\n" +
114                 "<!ATTLIST TABLE2\n" +
115                 ">\n" +
116                 "\n";
117 
118         Column[] columns = new Column[0];
119 
120         DefaultTable table1 = new DefaultTable("TABLE1", columns);
121         DefaultTable table2 = new DefaultTable("TABLE2", columns);
122 
123         StringWriter stringWriter = new StringWriter();
124         FlatDtdWriter dtdWriter = new FlatDtdWriter(stringWriter);
125         dtdWriter.setContentModel(FlatDtdWriter.CHOICE);
126         dtdWriter.write(new DefaultDataSet(table1, table2));
127 
128         String actualOutput = stringWriter.toString();
129         assertEquals("output", expectedOutput, actualOutput);
130     }
131 
132 }