001: /* Copyright 2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021: package nu.xom.tests;
022:
023: import junit.framework.ComparisonFailure;
024: import nu.xom.Attribute;
025: import nu.xom.Comment;
026: import nu.xom.Element;
027: import nu.xom.Namespace;
028: import nu.xom.Node;
029: import nu.xom.Text;
030:
031: /**
032: * <p>
033: * Unit tests for XOMTestCase. Added after the first bug discovered in
034: * XOM 1.0 showed up in XOMTestCase.
035: * </p>
036: *
037: * @author Elliotte Rusty Harold
038: * @version 1.1a2
039: *
040: */
041: public class XOMTestCaseTest extends XOMTestCase {
042:
043: public XOMTestCaseTest(String name) {
044: super (name);
045: }
046:
047: public void testNullCheck() {
048:
049: Text t = new Text("");
050: try {
051: assertEquals(t, null);
052: fail("Allowed comparison with null");
053: } catch (ComparisonFailure ex) {
054: assertNotNull(ex.getMessage());
055: }
056:
057: try {
058: assertEquals(null, t);
059: fail("Allowed comparison with null");
060: } catch (ComparisonFailure ex) {
061: assertNotNull(ex.getMessage());
062: }
063:
064: }
065:
066: public void testNamespaceEqualsItself() {
067: Namespace ns = new Namespace("pre", "http://www.example.org",
068: null);
069: assertEquals(ns, ns);
070: }
071:
072: public void testCompareMismatchedTypes() {
073:
074: Node n1 = new Text("");
075: Node n2 = new Attribute("name", "value");
076:
077: try {
078: assertEquals(n1, n2);
079: fail("Text equals Attribute?!");
080: } catch (ComparisonFailure ex) {
081: assertNotNull(ex.getMessage());
082: }
083:
084: try {
085: assertEquals(n2, n1);
086: fail("Text equals Attribute?!");
087: } catch (ComparisonFailure ex) {
088: assertNotNull(ex.getMessage());
089: }
090:
091: }
092:
093: public void testCompareMismatchedNullNodeTypes() {
094:
095: Node n1 = new Text("");
096: Node n2 = null;
097:
098: try {
099: assertEquals(n1, n2);
100: fail("Text equals null?!");
101: } catch (ComparisonFailure ex) {
102: assertNotNull(ex.getMessage());
103: }
104:
105: }
106:
107: public void testCompareAttributesAsNodes() {
108:
109: Node a1 = new Attribute("test", "value");
110: Node a2 = a1.copy();
111: assertEquals(a1, a2);
112:
113: }
114:
115: public void testCombineTextNodes() {
116:
117: Element e1 = new Element("test");
118: e1.appendChild("1");
119: e1.appendChild("2");
120: Element e2 = new Element("test");
121: e2.appendChild("12");
122: assertEquals(e1, e2);
123: assertEquals(2, e1.getChildCount());
124:
125: }
126:
127: public void testTrickyCombineTextNodes() {
128:
129: Element e1 = new Element("test");
130: e1.appendChild("12");
131: e1.appendChild("3");
132: Element e2 = new Element("test");
133: e2.appendChild("1");
134: e2.appendChild("23");
135: assertEquals(e1, e2);
136: assertEquals(2, e1.getChildCount());
137:
138: }
139:
140: public void testCombineThreeTextNodes() {
141:
142: Element e1 = new Element("test");
143: e1.appendChild("1");
144: e1.appendChild("2");
145: e1.appendChild("3");
146: Element e2 = new Element("test");
147: e2.appendChild("123");
148: assertEquals(e1, e2);
149:
150: }
151:
152: public void testCombineThreeTextNodes2() {
153:
154: Element e1 = new Element("test");
155: e1.appendChild("\n");
156: e1.appendChild(new Element("p"));
157: e1.appendChild("1");
158: e1.appendChild("2");
159: e1.appendChild("3");
160: Element e2 = new Element("test");
161: e2.appendChild("\n");
162: e2.appendChild(new Element("p"));
163: e2.appendChild("123");
164: assertEquals(e2, e1);
165:
166: }
167:
168: public void testUnequalElements() {
169:
170: Element e1 = new Element("test");
171: e1.appendChild("1");
172: e1.appendChild(new Element("b"));
173: e1.appendChild("3");
174: Element e2 = new Element("test");
175: e2.appendChild("1");
176: e2.appendChild(new Element("c"));
177: e2.appendChild("3");
178: try {
179: assertEquals(e1, e2);
180: fail("Unequal elements compared equal");
181: } catch (ComparisonFailure success) {
182: assertNotNull(success.getMessage());
183: }
184:
185: }
186:
187: public void testCompareXMLBaseAttributes() {
188:
189: Node a1 = new Attribute("xml:base", Namespace.XML_NAMESPACE,
190: "value.xml");
191: Node a2 = new Attribute("xml:base", Namespace.XML_NAMESPACE,
192: "./value.xml");
193: assertEquals(a1, a2);
194:
195: }
196:
197: public void testCompareChildren() {
198:
199: Element e1 = new Element("e");
200: Element e2 = new Element("e");
201: e1.appendChild(new Comment("a"));
202: e2.appendChild(new Comment("b"));
203: try {
204: assertEquals("BOO!", e1, e2);
205: fail("didn't check children");
206: } catch (ComparisonFailure ex) {
207: assertTrue(ex.getMessage().indexOf("BOO!") >= 0);
208: }
209:
210: }
211:
212: }
|