1 21 22 package org.dbunit.dataset.xml; 23 24 import junit.framework.TestCase; 25 import org.dbunit.dataset.Column; 26 import org.dbunit.dataset.DefaultDataSet; 27 import org.dbunit.dataset.DefaultTable; 28 import org.dbunit.dataset.datatype.DataType; 29 import org.dbunit.database.IDatabaseConnection; 30 31 import java.io.StringWriter ; 32 import java.io.File ; 33 import java.io.FileWriter ; 34 import java.io.FileOutputStream ; 35 36 42 public class FlatXmlWriterTest extends TestCase 43 { 44 public FlatXmlWriterTest(String name) 45 { 46 super(name); 47 } 48 49 public void testWrite() throws Exception 50 { 51 String expectedOutput = 52 "<dataset>\n" + 53 " <TABLE1 COL0=\"t1v1\" COL1=\"t1v2\"/>\n" + 54 " <TABLE2 COL0=\"t2v1\" COL1=\"t2v2\"/>\n" + 55 "</dataset>\n"; 56 57 String col0 = "COL0"; 58 String col1 = "COL1"; 59 Column[] columns = new Column[]{ 60 new Column(col0, DataType.UNKNOWN), 61 new Column(col1, DataType.UNKNOWN) 62 }; 63 64 DefaultTable table1 = new DefaultTable("TABLE1", columns); 65 table1.addRow(); 66 table1.setValue(0, col0, "t1v1"); 67 table1.setValue(0, col1, "t1v2"); 68 69 DefaultTable table2 = new DefaultTable("TABLE2", columns); 70 table2.addRow(); 71 table2.setValue(0, col0, "t2v1"); 72 table2.setValue(0, col1, "t2v2"); 73 74 StringWriter stringWriter = new StringWriter (); 75 FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter); 76 xmlWriter.write(new DefaultDataSet(table1, table2)); 77 78 String actualOutput = stringWriter.toString(); 79 assertEquals("output", expectedOutput, actualOutput); 80 } 81 82 public void testWriteWithDocType() throws Exception 83 { 84 String expectedOutput = 85 "<!DOCTYPE dataset SYSTEM \"dataset.dtd\">\n" + 86 "<dataset>\n" + 87 " <TABLE1 COL0=\"v1\" COL1=\"v2\"/>\n" + 88 "</dataset>\n"; 89 90 String col0 = "COL0"; 91 String col1 = "COL1"; 92 Column[] columns = new Column[]{ 93 new Column(col0, DataType.UNKNOWN), 94 new Column(col1, DataType.UNKNOWN) 95 }; 96 97 DefaultTable table1 = new DefaultTable("TABLE1", columns); 98 table1.addRow(); 99 table1.setValue(0, col0, "v1"); 100 table1.setValue(0, col1, "v2"); 101 102 StringWriter stringWriter = new StringWriter (); 103 FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter); 104 xmlWriter.setDocType("dataset.dtd"); 105 xmlWriter.write(new DefaultDataSet(table1)); 106 107 String actualOutput = stringWriter.toString(); 108 assertEquals("output", expectedOutput, actualOutput); 109 } 110 111 public void testWriteExcludeEmptyTable() throws Exception 112 { 113 String expectedOutput = 114 "<dataset>\n" + 115 " <TEST_TABLE COL0=\"value\"/>\n" + 116 "</dataset>\n"; 117 118 String col0 = "COL0"; 119 Column[] columns = new Column[]{ 120 new Column(col0, DataType.UNKNOWN), 121 }; 122 123 DefaultTable table1 = new DefaultTable("TEST_TABLE", columns); 124 table1.addRow(); 125 table1.setValue(0, col0, "value"); 126 DefaultTable table2 = new DefaultTable("EMPTY_TABLE", columns); 127 128 StringWriter stringWriter = new StringWriter (); 129 FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter); 130 datasetWriter.setIncludeEmptyTable(false); 131 datasetWriter.write(new DefaultDataSet(table1, table2)); 132 133 String actualOutput = stringWriter.toString(); 134 assertEquals("output", expectedOutput, actualOutput); 135 } 136 137 public void testWriteIncludeEmptyTable() throws Exception 138 { 139 String expectedOutput = 140 "<dataset>\n" + 141 " <TEST_TABLE COL0=\"value\"/>\n" + 142 " <EMPTY_TABLE/>\n" + 143 "</dataset>\n"; 144 145 String col0 = "COL0"; 146 Column[] columns = new Column[]{ 147 new Column(col0, DataType.UNKNOWN), 148 }; 149 150 DefaultTable table1 = new DefaultTable("TEST_TABLE", columns); 151 table1.addRow(); 152 table1.setValue(0, col0, "value"); 153 DefaultTable table2 = new DefaultTable("EMPTY_TABLE", columns); 154 155 StringWriter stringWriter = new StringWriter (); 156 FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter); 157 datasetWriter.setIncludeEmptyTable(true); 158 datasetWriter.write(new DefaultDataSet(table1, table2)); 159 160 String actualOutput = stringWriter.toString(); 161 assertEquals("output", expectedOutput, actualOutput); 162 } 163 164 public void testWriteNullValue() throws Exception 165 { 166 String expectedOutput = 167 "<dataset>\n" + 168 " <TEST_TABLE COL0=\"c0r0\" COL1=\"c1r0\"/>\n" + 169 " <TEST_TABLE COL0=\"c0r1\"/>\n" + 170 "</dataset>\n"; 171 172 String col0 = "COL0"; 173 String col1 = "COL1"; 174 Column[] columns = new Column[]{ 175 new Column(col0, DataType.UNKNOWN), 176 new Column(col1, DataType.UNKNOWN) 177 }; 178 179 DefaultTable table = new DefaultTable("TEST_TABLE", columns); 180 table.addRow(); 181 table.setValue(0, col0, "c0r0"); 182 table.setValue(0, col1, "c1r0"); 183 table.addRow(); 184 table.setValue(1, col0, "c0r1"); 185 table.setValue(1, col1, null); 186 187 StringWriter stringWriter = new StringWriter (); 188 FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter); 189 xmlWriter.write(new DefaultDataSet(table)); 190 191 String actualOutput = stringWriter.toString(); 192 assertEquals("output", expectedOutput, actualOutput); 193 } 194 } 195 | Popular Tags |