View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2008, 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.util.xml;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.StringWriter;
25  import java.io.Writer;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * @author gommma
31   * @author Last changed by: $Author$
32   * @version $Revision$ $Date$
33   * @since 2.3.0
34   */
35  public class XmlWriterTest extends TestCase 
36  {
37  
38  	public void testLiterallyFalse() throws Exception
39  	{
40  		String text = "text1\ntext2\rtext3";
41  		String expectedXml = "<COLUMN1 ATTR=\"" + text + "\">" + text + "</COLUMN1>\n";
42          Writer writer = new StringWriter();
43          XmlWriter xmlWriter = new XmlWriter(writer);
44  		xmlWriter.writeElement("COLUMN1");
45  		xmlWriter.writeAttribute("ATTR", text);
46  		xmlWriter.writeText(text);
47  		xmlWriter.endElement();
48  		xmlWriter.close();
49  		String actualXml = writer.toString();
50  		assertEquals(expectedXml, actualXml);
51  	}
52  	
53  	public void testLiterallyTrue() throws Exception
54  	{
55  		String expectedText = "text1&#xA;text2&#xD;text3";
56  		String expectedXml = "<COLUMN1 ATTR=\"" + expectedText + "\">" + expectedText + "</COLUMN1>\n";
57  
58  		boolean literally = true;
59  		String text = "text1\ntext2\rtext3";
60          Writer writer = new StringWriter();
61          XmlWriter xmlWriter = new XmlWriter(writer);
62  		xmlWriter.writeElement("COLUMN1");
63  		xmlWriter.writeAttribute("ATTR", text, literally);
64  		xmlWriter.writeText(text, literally);
65  		xmlWriter.endElement();
66  		xmlWriter.close();
67  		String actualXml = writer.toString();
68  		assertEquals(expectedXml, actualXml);
69  	}
70  	
71  	public void testWriteAttributesAfterText() throws Exception
72  	{
73  		String text = "bla";
74          Writer writer = new StringWriter();
75          XmlWriter xmlWriter = new XmlWriter(writer);
76  		xmlWriter.writeElement("COLUMN1");
77  		xmlWriter.writeText(text);
78  		try {
79  			xmlWriter.writeAttribute("ATTR", text);
80  			fail("Should not be able to add attributes afterwards with the current XmlWriter implementation (which could be better...)");
81  		}
82  		catch(IllegalStateException expected) {
83  			// all right
84  		}
85  	}
86  	
87  	public void testWriteNestedCDATAWithoutSurrounder() throws Exception
88  	{
89  	    String text = "<![CDATA[Text that itself is in a CDATA section]]>";
90          Writer writer = new StringWriter();
91          XmlWriter xmlWriter = new XmlWriter(writer);
92          xmlWriter.writeElement("COLUMN1");
93          xmlWriter.writeCData(text);
94          xmlWriter.endElement();
95          xmlWriter.close();
96          String actualXml = writer.toString();
97          
98          // Input should be equal to output because the text already starts with a CDATA section
99          assertEquals("<COLUMN1>" + text + "</COLUMN1>\n", actualXml);
100 	}
101 
102 	public void testWriteNestedCDATAWithSurrounder() throws Exception
103 	{
104 	    String text = "<myXmlText>"+XmlWriter.CDATA_START+"Text that itself is in a CDATA section"+XmlWriter.CDATA_END+"</myXmlText>";
105         String expectedResultText = "<myXmlText>"+XmlWriter.CDATA_START+"Text that itself is in a CDATA section]]"+XmlWriter.CDATA_END+XmlWriter.CDATA_START+"></myXmlText>";
106 	    Writer writer = new StringWriter();
107 	    XmlWriter xmlWriter = new XmlWriter(writer);
108 	    xmlWriter.writeElement("COLUMN1");
109 	    xmlWriter.writeCData(text);
110 	    xmlWriter.endElement();
111 	    xmlWriter.close();
112 	    String actualXml = writer.toString();
113 
114 	    String expectedXml = "<COLUMN1>" + XmlWriter.CDATA_START + expectedResultText + XmlWriter.CDATA_END + "</COLUMN1>\n";
115 	    assertEquals(expectedXml, actualXml);
116 	}
117 
118 	   public void testOutputStreamWithNullEncoding() throws Exception
119 	    {
120 	        ByteArrayOutputStream out = new ByteArrayOutputStream();
121 	        // Use a different encoding than the default
122 	        XmlWriter xmlWriter = new XmlWriter(out, null);
123 	        xmlWriter.writeDeclaration();
124 	        xmlWriter.writeEmptyElement("COLUMN1");
125 	        xmlWriter.close();
126 	        
127 	           String expected = "<?xml version='1.0' encoding='UTF-8'?>\n" +
128 	           		"<COLUMN1/>\n";
129 	        assertEquals(expected, out.toString("UTF-8"));
130 	    }
131 
132        public void testOutputStreamWithNonDefaultEncoding() throws Exception
133        {
134            ByteArrayOutputStream out = new ByteArrayOutputStream();
135            // Use a different encoding than the default
136            XmlWriter xmlWriter = new XmlWriter(out, "ISO-8859-1");
137            xmlWriter.writeDeclaration();
138            xmlWriter.writeEmptyElement("COLUMN1");
139            xmlWriter.close();
140            
141            String expected = "<?xml version='1.0' encoding='ISO-8859-1'?>\n" +
142            		"<COLUMN1/>\n";
143            assertEquals(expected, out.toString("ISO-8859-1"));
144        }
145 	   
146 	public void testEncodedXmlChar() throws Exception
147 	{
148 		String expectedText = "\u00AEtext1&#xA;text2&#xD;text3\u00AE";
149 		String expectedXml = "<COLUMN1 ATTR=\"" + expectedText + "\">" + expectedText + "</COLUMN1>\n";
150 
151 		boolean literally = true;
152         StringBuffer textBuilder = new StringBuffer();
153         String registeredSymbol = new String(new char[] { 0xAE });
154         textBuilder.append(registeredSymbol);
155         textBuilder.append("text1\ntext2\rtext3");
156         textBuilder.append(registeredSymbol);
157         String text = textBuilder.toString();
158         Writer writer = new StringWriter();
159         XmlWriter xmlWriter = new XmlWriter(writer);
160 		xmlWriter.writeElement("COLUMN1");
161 		xmlWriter.writeAttribute("ATTR", text, literally);
162 		xmlWriter.writeText(text, literally);
163 		xmlWriter.endElement();
164 		xmlWriter.close();
165 		String actualXml = writer.toString();
166 		assertEquals(expectedXml, actualXml);
167 	}
168 
169 
170 	public void testNonAsciiValidXmlCharactersInAttributeValue() throws Exception
171 	{
172 		String expectedText = "привет";
173 		String expectedXml = "<COLUMN1 ATTR=\"" + expectedText + "\"/>\n";
174 
175 		boolean literally = true;
176 		Writer writer = new StringWriter();
177 		XmlWriter xmlWriter = new XmlWriter(writer);
178 		xmlWriter.writeElement("COLUMN1");
179 		xmlWriter.writeAttribute("ATTR", expectedText, literally);
180 		xmlWriter.endElement();
181 		xmlWriter.close();
182 
183 		String actualXml = writer.toString();
184 		assertEquals(expectedXml, actualXml);
185 	}
186 }