KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > xml > v1 > XMLUtilUTest


1 /*
2  * @(#)XMLUtilUTest.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.xml.v1;
28
29 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34
35 /**
36  * Tests the XMLUtil class.
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @version $Date: 2003/11/24 22:33:36 $
40  * @since 06-Mar-2002
41  */

42 public class XMLUtilUTest extends TestCase
43 {
44     //-------------------------------------------------------------------------
45
// Standard JUnit Class-specific declarations
46
private static final Class JavaDoc THIS_CLASS = XMLUtilUTest.class;
47     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
48     
49     public XMLUtilUTest( String JavaDoc name )
50     {
51         super( name );
52     }
53
54
55
56     //-------------------------------------------------------------------------
57
// Tests
58

59     
60     public void testConstruction()
61     {
62         assertNotNull(
63             "getInstance() must never return null.",
64             XMLUtil.getInstance() );
65     }
66     
67     
68     public void testUTFConvert1()
69     {
70         // can work with nulls
71
assertNull(
72             "Null conversion didn't work right.",
73             XMLUtil.getInstance().utf2xml( null ) );
74     }
75     
76     
77     public void testUTFConvert2()
78     {
79         // test non-escaped characters
80
String JavaDoc noescapes =
81             "abcdefghijklmnopqrstuvwxyz "+
82             "ABCDEFGHIJKLMNOPQRSTUVWXYZ "+
83             "()=+-_!@#$%^*{}[]\\|;:/?.,\n\t\r";
84         String JavaDoc val = XMLUtil.getInstance().utf2xml( noescapes );
85         DOC.getLog().debug( "expected '"+noescapes+"'" );
86         DOC.getLog().debug( "received '"+val+"'" );
87         assertEquals(
88             noescapes,
89             val );
90     }
91     
92     
93     public void testUTFConvert2a()
94     {
95         // we've changed the non-escaped character translation such that
96
// it's compatible with UTF-8 encoding. Probably shouldn't have,
97
// but here we are.
98
String JavaDoc noescapes = "" +
99             ((char)0xD7FF) + ((char)0xE000) + ((char)0xFFFD);
100         String JavaDoc real = "&#55295;&#57344;&#65533;";
101         String JavaDoc val = XMLUtil.getInstance().utf2xml( noescapes );
102         DOC.getLog().debug( "expected '"+noescapes+"'" );
103         DOC.getLog().debug( "received '"+val+"'" );
104         assertEquals(
105             real,
106             val );
107     }
108     
109     
110     public void testUTFConvert3()
111     {
112         assertEquals(
113             "&lt;",
114             XMLUtil.getInstance().utf2xml( "<" ) );
115     }
116     
117     
118     public void testUTFConvert4()
119     {
120         assertEquals(
121             "&gt;",
122             XMLUtil.getInstance().utf2xml( ">" ) );
123     }
124     
125     
126     public void testUTFConvert5()
127     {
128         assertEquals(
129             "&amp;",
130             XMLUtil.getInstance().utf2xml( "&" ) );
131     }
132     
133     
134     public void testUTFConvert6()
135     {
136         assertEquals(
137             "&quot;",
138             XMLUtil.getInstance().utf2xml( "\"" ) );
139     }
140     
141     
142     public void testUTFConvert7()
143     {
144         assertEquals(
145             "&apos;",
146             XMLUtil.getInstance().utf2xml( "'" ) );
147     }
148     
149     
150     public void testUTFConvert8()
151     {
152         assertEquals(
153             "&#2;",
154             XMLUtil.getInstance().utf2xml( ""+((char)2) ) );
155     }
156     
157     
158     public void testUTFConvert9()
159     {
160         assertEquals(
161             "a&lt;b",
162             XMLUtil.getInstance().utf2xml( "a<b" ) );
163     }
164     
165     
166     public void testUTFConvert10()
167     {
168         assertEquals(
169             "a&gt;b",
170             XMLUtil.getInstance().utf2xml( "a>b" ) );
171     }
172     
173     
174     public void testUTFConvert11()
175     {
176         assertEquals(
177             "&amp;a&lt;",
178             XMLUtil.getInstance().utf2xml( "&a<" ) );
179     }
180     
181     
182     public void testUTFConvert12()
183     {
184         assertEquals(
185             "a&quot;b&gt;c",
186             XMLUtil.getInstance().utf2xml( "a\"b>c" ) );
187     }
188     
189     
190     public void testUTFConvert13()
191     {
192         assertEquals(
193             "&apos;abc&gt;d",
194             XMLUtil.getInstance().utf2xml( "'abc>d" ) );
195     }
196     
197     
198     public void testUTFConvert14()
199     {
200         assertEquals(
201             "aaa&#2;b&#3;cfed asdf;lkj qwepiouf &amp;amp;f3df",
202             XMLUtil.getInstance().utf2xml( "aaa"+((char)2)+
203                 'b' + ((char)3) + "cfed asdf;lkj qwepiouf &amp;f3df") );
204     }
205     
206     
207     public void testUTFAppend1()
208     {
209         try
210         {
211             XMLUtil.getInstance().utf2xml( null, null );
212             fail( "did not throw an IllegalArgumentException." );
213         }
214         catch (IllegalArgumentException JavaDoc iae)
215         {
216             // test text?
217
}
218     }
219     
220     
221     public void testUTFAppend2()
222     {
223         try
224         {
225             XMLUtil.getInstance().utf2xml( "abc", null );
226             fail( "did not throw an IllegalArgumentException." );
227         }
228         catch (IllegalArgumentException JavaDoc iae)
229         {
230             // test text?
231
}
232     }
233     
234     
235     public void testUTFAppend3()
236     {
237         // can work with nulls
238
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
239         XMLUtil.getInstance().utf2xml( null, sb );
240         assertEquals(
241             "Null conversion did not work.",
242             "null",
243             sb.toString() );
244     }
245     
246     
247     
248     
249     //-------------------------------------------------------------------------
250
// Standard JUnit declarations
251

252     
253     public static Test suite()
254     {
255         TestSuite suite = new TestSuite( THIS_CLASS );
256         
257         return suite;
258     }
259     
260     public static void main( String JavaDoc[] args )
261     {
262         String JavaDoc[] name = { THIS_CLASS.getName() };
263         
264         // junit.textui.TestRunner.main( name );
265
// junit.swingui.TestRunner.main( name );
266

267         junit.textui.TestRunner.main( name );
268     }
269     
270     
271     /**
272      *
273      * @exception Exception thrown under any exceptional condition.
274      */

275     protected void setUp() throws Exception JavaDoc
276     {
277         super.setUp();
278         
279         // set ourself up
280
}
281     
282     
283     /**
284      *
285      * @exception Exception thrown under any exceptional condition.
286      */

287     protected void tearDown() throws Exception JavaDoc
288     {
289         // tear ourself down
290

291         
292         super.tearDown();
293     }
294 }
295
296
Popular Tags