001: /*
002: * @(#)XMLUtilUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.xml.v1;
028:
029: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: /**
035: * Tests the XMLUtil class.
036: *
037: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
038: * @version $Date: 2003/11/24 22:33:36 $
039: * @since 06-Mar-2002
040: */
041: public class XMLUtilUTest extends TestCase {
042: //-------------------------------------------------------------------------
043: // Standard JUnit Class-specific declarations
044: private static final Class THIS_CLASS = XMLUtilUTest.class;
045: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
046:
047: public XMLUtilUTest(String name) {
048: super (name);
049: }
050:
051: //-------------------------------------------------------------------------
052: // Tests
053:
054: public void testConstruction() {
055: assertNotNull("getInstance() must never return null.", XMLUtil
056: .getInstance());
057: }
058:
059: public void testUTFConvert1() {
060: // can work with nulls
061: assertNull("Null conversion didn't work right.", XMLUtil
062: .getInstance().utf2xml(null));
063: }
064:
065: public void testUTFConvert2() {
066: // test non-escaped characters
067: String noescapes = "abcdefghijklmnopqrstuvwxyz "
068: + "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
069: + "()=+-_!@#$%^*{}[]\\|;:/?.,\n\t\r";
070: String val = XMLUtil.getInstance().utf2xml(noescapes);
071: DOC.getLog().debug("expected '" + noescapes + "'");
072: DOC.getLog().debug("received '" + val + "'");
073: assertEquals(noescapes, val);
074: }
075:
076: public void testUTFConvert2a() {
077: // we've changed the non-escaped character translation such that
078: // it's compatible with UTF-8 encoding. Probably shouldn't have,
079: // but here we are.
080: String noescapes = "" + ((char) 0xD7FF) + ((char) 0xE000)
081: + ((char) 0xFFFD);
082: String real = "퟿�";
083: String val = XMLUtil.getInstance().utf2xml(noescapes);
084: DOC.getLog().debug("expected '" + noescapes + "'");
085: DOC.getLog().debug("received '" + val + "'");
086: assertEquals(real, val);
087: }
088:
089: public void testUTFConvert3() {
090: assertEquals("<", XMLUtil.getInstance().utf2xml("<"));
091: }
092:
093: public void testUTFConvert4() {
094: assertEquals(">", XMLUtil.getInstance().utf2xml(">"));
095: }
096:
097: public void testUTFConvert5() {
098: assertEquals("&", XMLUtil.getInstance().utf2xml("&"));
099: }
100:
101: public void testUTFConvert6() {
102: assertEquals(""", XMLUtil.getInstance().utf2xml("\""));
103: }
104:
105: public void testUTFConvert7() {
106: assertEquals("'", XMLUtil.getInstance().utf2xml("'"));
107: }
108:
109: public void testUTFConvert8() {
110: assertEquals("", XMLUtil.getInstance().utf2xml(
111: "" + ((char) 2)));
112: }
113:
114: public void testUTFConvert9() {
115: assertEquals("a<b", XMLUtil.getInstance().utf2xml("a<b"));
116: }
117:
118: public void testUTFConvert10() {
119: assertEquals("a>b", XMLUtil.getInstance().utf2xml("a>b"));
120: }
121:
122: public void testUTFConvert11() {
123: assertEquals("&a<", XMLUtil.getInstance().utf2xml("&a<"));
124: }
125:
126: public void testUTFConvert12() {
127: assertEquals("a"b>c", XMLUtil.getInstance().utf2xml(
128: "a\"b>c"));
129: }
130:
131: public void testUTFConvert13() {
132: assertEquals("'abc>d", XMLUtil.getInstance().utf2xml(
133: "'abc>d"));
134: }
135:
136: public void testUTFConvert14() {
137: assertEquals(
138: "aaabcfed asdf;lkj qwepiouf &amp;f3df",
139: XMLUtil.getInstance().utf2xml(
140: "aaa" + ((char) 2) + 'b' + ((char) 3)
141: + "cfed asdf;lkj qwepiouf &f3df"));
142: }
143:
144: public void testUTFAppend1() {
145: try {
146: XMLUtil.getInstance().utf2xml(null, null);
147: fail("did not throw an IllegalArgumentException.");
148: } catch (IllegalArgumentException iae) {
149: // test text?
150: }
151: }
152:
153: public void testUTFAppend2() {
154: try {
155: XMLUtil.getInstance().utf2xml("abc", null);
156: fail("did not throw an IllegalArgumentException.");
157: } catch (IllegalArgumentException iae) {
158: // test text?
159: }
160: }
161:
162: public void testUTFAppend3() {
163: // can work with nulls
164: StringBuffer sb = new StringBuffer();
165: XMLUtil.getInstance().utf2xml(null, sb);
166: assertEquals("Null conversion did not work.", "null", sb
167: .toString());
168: }
169:
170: //-------------------------------------------------------------------------
171: // Standard JUnit declarations
172:
173: public static Test suite() {
174: TestSuite suite = new TestSuite(THIS_CLASS);
175:
176: return suite;
177: }
178:
179: public static void main(String[] args) {
180: String[] name = { THIS_CLASS.getName() };
181:
182: // junit.textui.TestRunner.main( name );
183: // junit.swingui.TestRunner.main( name );
184:
185: junit.textui.TestRunner.main(name);
186: }
187:
188: /**
189: *
190: * @exception Exception thrown under any exceptional condition.
191: */
192: protected void setUp() throws Exception {
193: super .setUp();
194:
195: // set ourself up
196: }
197:
198: /**
199: *
200: * @exception Exception thrown under any exceptional condition.
201: */
202: protected void tearDown() throws Exception {
203: // tear ourself down
204:
205: super.tearDown();
206: }
207: }
|