KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > test > XmlScribeTest


1 package org.sapia.util.xml.idefix.test;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import junit.framework.TestCase;
7 import junit.textui.TestRunner;
8 import org.sapia.util.xml.Attribute;
9 import org.sapia.util.xml.idefix.XmlScribe;
10
11 /**
12  *
13  *
14  * @author Jean-Cedric Desrochers
15  *
16  * <dl>
17  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class XmlScribeTest extends TestCase {
23
24   private XmlScribe _theScribe;
25
26   public static void main(String JavaDoc[] args) {
27     TestRunner.run(XmlScribeTest.class);
28   }
29
30   public XmlScribeTest(String JavaDoc aName) {
31     super(aName);
32     _theScribe = new XmlScribe("UTF-8");
33   }
34
35   public void testInvalidStreamEncoding() throws Exception JavaDoc {
36     try {
37       _theScribe.setStreamEncoding("UTF-256");
38       _theScribe.composeXmlDeclaration(null, new ByteArrayOutputStream JavaDoc());
39       fail("XMLBuffer should not encode using the invalid UTF-256 encoding!");
40     } catch (IOException JavaDoc ioe) {
41       // success
42
} finally {
43       _theScribe.setStreamEncoding("UTF-8");
44     }
45   }
46
47
48   public void testXmlDeclaration_Buffer() throws Exception JavaDoc {
49     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
50     _theScribe.composeXmlDeclaration(null, aBuffer);
51     assertEquals("<?xml version=\"1.0\" ?>", aBuffer.toString());
52
53     aBuffer = new StringBuffer JavaDoc();
54     _theScribe.composeXmlDeclaration("", aBuffer);
55     assertEquals("<?xml version=\"1.0\" ?>", aBuffer.toString());
56
57     aBuffer = new StringBuffer JavaDoc();
58     _theScribe.composeXmlDeclaration("UTF-8", aBuffer);
59     assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", aBuffer.toString());
60   }
61
62
63   public void testXmlDeclaration_Streaming() throws Exception JavaDoc {
64     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
65     _theScribe.composeXmlDeclaration(null, aStream);
66     assertEquals("<?xml version=\"1.0\" ?>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
67
68     aStream = new ByteArrayOutputStream JavaDoc();
69     _theScribe.composeXmlDeclaration("", aStream);
70     assertEquals("<?xml version=\"1.0\" ?>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
71
72     aStream = new ByteArrayOutputStream JavaDoc();
73     _theScribe.composeXmlDeclaration("UTF-8", aStream);
74     assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
75   }
76
77
78   public void testUnqualifiedEmptyElement_Buffer() throws Exception JavaDoc {
79     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
80     _theScribe.composeStartingElement(null, "foo", true, aBuffer);
81     assertEquals("<foo />", aBuffer.toString());
82
83     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
84     aList.add(new Attribute("bar", "value"));
85     aBuffer = new StringBuffer JavaDoc();
86     _theScribe.composeStartingElement(null, "foo", aList, true, aBuffer);
87     assertEquals("<foo bar=\"value\" />", aBuffer.toString());
88   }
89
90
91   public void testUnqualifiedEmptyElement_Streaming() throws Exception JavaDoc {
92     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
93     _theScribe.composeStartingElement(null, "foo", true, aStream);
94     assertEquals("<foo />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
95
96     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
97     aList.add(new Attribute("bar", "value"));
98     aStream = new ByteArrayOutputStream JavaDoc();
99     _theScribe.composeStartingElement(null, "foo", aList, true, aStream);
100     assertEquals("<foo bar=\"value\" />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
101   }
102
103
104   public void testQualifiedEmptyElement_Buffer() throws Exception JavaDoc {
105     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
106     _theScribe.composeStartingElement("A", "foo", true, aBuffer);
107     assertEquals("<A:foo />", aBuffer.toString());
108
109     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
110     aList.add(new Attribute("bar", "value"));
111     aBuffer = new StringBuffer JavaDoc();
112     _theScribe.composeStartingElement("B", "foo", aList, true, aBuffer);
113     assertEquals("<B:foo bar=\"value\" />", aBuffer.toString());
114   }
115
116
117   public void testQualifiedEmptyElement_Streaming() throws Exception JavaDoc {
118     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
119     _theScribe.composeStartingElement("A", "foo", true, aStream);
120     assertEquals("<A:foo />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
121
122     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
123     aList.add(new Attribute("bar", "value"));
124     aStream = new ByteArrayOutputStream JavaDoc();
125     _theScribe.composeStartingElement("B", "foo", aList, true, aStream);
126     assertEquals("<B:foo bar=\"value\" />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
127   }
128
129
130   public void testUnqualifiedStartingElement_Buffer() throws Exception JavaDoc {
131     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
132     _theScribe.composeStartingElement(null, "foo", false, aBuffer);
133     assertEquals("<foo>", aBuffer.toString());
134
135     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
136     aList.add(new Attribute("bar", "value"));
137     aBuffer = new StringBuffer JavaDoc();
138     _theScribe.composeStartingElement(null, "foo", aList, false, aBuffer);
139     assertEquals("<foo bar=\"value\">", aBuffer.toString());
140   }
141
142
143   public void testUnqualifiedStartingElement_Streaming() throws Exception JavaDoc {
144     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
145     _theScribe.composeStartingElement(null, "foo", false, aStream);
146     assertEquals("<foo>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
147
148     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
149     aList.add(new Attribute("bar", "value"));
150     aStream = new ByteArrayOutputStream JavaDoc();
151     _theScribe.composeStartingElement(null, "foo", aList, false, aStream);
152     assertEquals("<foo bar=\"value\">", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
153   }
154
155
156   public void testQualifiedStartingElement_Buffer() throws Exception JavaDoc {
157     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
158     _theScribe.composeStartingElement("A", "foo", false, aBuffer);
159     assertEquals("<A:foo>", aBuffer.toString());
160
161     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
162     aList.add(new Attribute("bar", "value"));
163     aBuffer = new StringBuffer JavaDoc();
164     _theScribe.composeStartingElement("B", "foo", aList, false, aBuffer);
165     assertEquals("<B:foo bar=\"value\">", aBuffer.toString());
166   }
167
168
169   public void testQualifiedStartingElement_Streaming() throws Exception JavaDoc {
170     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
171     _theScribe.composeStartingElement("A", "foo", false, aStream);
172     assertEquals("<A:foo>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
173
174     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
175     aList.add(new Attribute("bar", "value"));
176     aStream = new ByteArrayOutputStream JavaDoc();
177     _theScribe.composeStartingElement("B", "foo", aList, false, aStream);
178     assertEquals("<B:foo bar=\"value\">", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
179   }
180
181
182   public void testUnqualifiedEndingElement_Buffer() throws Exception JavaDoc {
183     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
184     _theScribe.composeEndingElement("", "foo", aBuffer);
185     assertEquals("</foo>", aBuffer.toString());
186
187     aBuffer = new StringBuffer JavaDoc();
188     _theScribe.composeEndingElement(null, "foo", aBuffer);
189     assertEquals("</foo>", aBuffer.toString());
190   }
191
192
193   public void testUnqualifiedEndingElement_Streaming() throws Exception JavaDoc {
194     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
195     _theScribe.composeEndingElement("", "foo", aStream);
196     assertEquals("</foo>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
197
198     aStream = new ByteArrayOutputStream JavaDoc();
199     _theScribe.composeEndingElement(null, "foo", aStream);
200     assertEquals("</foo>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
201   }
202
203
204   public void testQualifiedEndingElement_Buffer() throws Exception JavaDoc {
205     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
206     _theScribe.composeEndingElement("A", "foo", aBuffer);
207     assertEquals("</A:foo>", aBuffer.toString());
208   }
209
210
211   public void testQualifiedEndingElement_Streaming() throws Exception JavaDoc {
212     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
213     _theScribe.composeEndingElement("A", "foo", aStream);
214     assertEquals("</A:foo>", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
215   }
216
217
218   public void testQualifiedAttribute_Buffer() throws Exception JavaDoc {
219     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
220     aList.add(new Attribute("Z", "bar","value"));
221     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
222     _theScribe.composeStartingElement("C", "foo", aList, true, aBuffer);
223     assertEquals("<C:foo Z:bar=\"value\" />", aBuffer.toString());
224   }
225
226
227   public void testQualifiedAttribute_Streaming() throws Exception JavaDoc {
228     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
229     aList.add(new Attribute("Z", "bar","value"));
230     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
231     _theScribe.composeStartingElement("C", "foo", aList, true, aStream);
232     assertEquals("<C:foo Z:bar=\"value\" />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
233   }
234
235
236   public void testXMLReservedCharacters_Buffer() throws Exception JavaDoc {
237     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
238     aList.add(new Attribute("bar","<\"value\" & \'value\'>"));
239     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
240     _theScribe.composeStartingElement("C", "foo", aList, true, aBuffer);
241     assertEquals("<C:foo bar=\"&lt;&quot;value&quot; &amp; &apos;value&apos;&gt;\" />", aBuffer.toString());
242   }
243
244
245   public void testXMLReservedCharacters_Streaming() throws Exception JavaDoc {
246     ArrayList JavaDoc aList = new ArrayList JavaDoc(1);
247     aList.add(new Attribute("bar","<\"value\" & \'value\'>"));
248     ByteArrayOutputStream JavaDoc aStream = new ByteArrayOutputStream JavaDoc();
249     _theScribe.composeStartingElement("C", "foo", aList, true, aStream);
250     assertEquals("<C:foo bar=\"&lt;&quot;value&quot; &amp; &apos;value&apos;&gt;\" />", new String JavaDoc(aStream.toByteArray(), "UTF-8"));
251   }
252 }
Popular Tags