001: /* Copyright 2002-2004 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:
022: package nu.xom.tests;
023:
024: import nu.xom.Element;
025: import nu.xom.IllegalDataException;
026: import nu.xom.IllegalTargetException;
027: import nu.xom.ProcessingInstruction;
028:
029: /**
030: * <p>
031: * Unit tests for the <code>ProcessingInstruction</code> class.
032: * </p>
033: *
034: * @author Elliotte Rusty Harold
035: * @version 1.0
036: *
037: */
038: public class ProcessingInstructionTest extends XOMTestCase {
039:
040: public ProcessingInstructionTest(String name) {
041: super (name);
042: }
043:
044: private ProcessingInstruction pi;
045:
046: protected void setUp() {
047: pi = new ProcessingInstruction("test", "test");
048: }
049:
050: public void testToXML() {
051: assertEquals("<?test test?>", pi.toXML());
052: }
053:
054: public void testToString() {
055: assertEquals(
056: "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"test\"]",
057: pi.toString());
058: }
059:
060: public void testToStringWithLineFeed() {
061:
062: ProcessingInstruction p = new ProcessingInstruction("test",
063: "content\ncontent");
064: assertEquals(
065: "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"content\\ncontent\"]",
066: p.toString());
067:
068: }
069:
070: public void testToStringWithLotsOfData() {
071:
072: ProcessingInstruction p = new ProcessingInstruction("target",
073: "content content 012345678901234567890123456789012345678901234567890123456789");
074: String s = p.toString();
075: assertTrue(s.endsWith("...\"]"));
076:
077: }
078:
079: public void testConstructor() {
080:
081: assertEquals("test", pi.getValue());
082: assertEquals("test", pi.getTarget());
083:
084: try {
085: new ProcessingInstruction("test:test", "test");
086: fail("Processing instruction targets cannot contain colons");
087: } catch (IllegalTargetException success) {
088: assertNotNull(success.getMessage());
089: assertEquals("test:test", success.getData());
090: }
091:
092: try {
093: new ProcessingInstruction("", "test");
094: fail("Processing instruction targets cannot be empty");
095: } catch (IllegalTargetException success) {
096: assertNotNull(success.getMessage());
097: assertEquals("", success.getData());
098: }
099:
100: try {
101: new ProcessingInstruction(null, "test");
102: fail("Processing instruction targets cannot be empty");
103: } catch (IllegalTargetException success) {
104: assertNotNull(success.getMessage());
105: assertNull(success.getData());
106: }
107:
108: try {
109: new ProcessingInstruction("12345", "test");
110: fail("Processing instruction targets must be NCNames");
111: } catch (IllegalTargetException success) {
112: assertEquals("12345", success.getData());
113: }
114:
115: // test empty data allowed
116: pi = new ProcessingInstruction("test", "");
117: assertEquals("", pi.getValue());
118: assertEquals("<?test?>", pi.toXML());
119:
120: }
121:
122: public void testSetTarget() {
123:
124: try {
125: pi.setTarget("test:test");
126: fail("Processing instruction targets cannot contain colons");
127: } catch (IllegalTargetException success) {
128: assertNotNull(success.getMessage());
129: assertEquals("test:test", success.getData());
130: }
131:
132: try {
133: pi.setTarget("");
134: fail("Processing instruction targets cannot be empty");
135: } catch (IllegalTargetException success) {
136: assertNotNull(success.getMessage());
137: assertEquals("", success.getData());
138: }
139:
140: try {
141: pi.setTarget(null);
142: fail("Processing instruction targets cannot be empty");
143: } catch (IllegalTargetException success) {
144: assertNotNull(success.getMessage());
145: assertNull(success.getData());
146: }
147:
148: try {
149: pi.setTarget("12345");
150: fail("Processing instruction targets must be NCNames");
151: } catch (IllegalTargetException success) {
152: assertEquals("12345", success.getData());
153: }
154:
155: pi.setTarget("testing123");
156: assertEquals("testing123", pi.getTarget());
157:
158: }
159:
160: public void testCopyConstructor() {
161:
162: ProcessingInstruction instruction1 = new ProcessingInstruction(
163: "target", "data");
164: ProcessingInstruction instruction2 = new ProcessingInstruction(
165: instruction1);
166:
167: assertEquals(instruction1.getTarget(), instruction2.getTarget());
168: assertEquals(instruction1.getValue(), instruction2.getValue());
169: assertEquals(instruction1.toXML(), instruction2.toXML());
170:
171: }
172:
173: public void testSetValue() {
174:
175: try {
176: pi.setValue("kjsahdj ?>");
177: fail("Should raise an IllegalDataException");
178: } catch (IllegalDataException success) {
179: assertEquals("kjsahdj ?>", success.getData());
180: assertNotNull(success.getMessage());
181: }
182:
183: try {
184: pi.setValue("?>");
185: fail("Should raise an IllegalDataException");
186: } catch (IllegalDataException success) {
187: assertEquals("?>", success.getData());
188: assertNotNull(success.getMessage());
189: }
190:
191: try {
192: pi.setValue("kjsahdj ?> skhskjlhd");
193: fail("Should raise an IllegalDataException");
194: } catch (IllegalDataException success) {
195: assertEquals("kjsahdj ?> skhskjlhd", success.getData());
196: assertNotNull(success.getMessage());
197: }
198:
199: try {
200: pi.setValue(null);
201: fail("Allowed null data");
202: } catch (IllegalDataException success) {
203: assertNull(success.getData());
204: assertNotNull(success.getMessage());
205: }
206:
207: // These should all work
208: String[] testData = { "<html></html>", "name=value",
209: "name='value'", "name=\"value\"",
210: "salkdhsalkjhdkjsadhkj sadhsajkdh", "<?", "? >", "--" };
211: for (int i = 0; i < testData.length; i++) {
212: pi.setValue(testData[i]);
213: assertEquals(testData[i], pi.getValue());
214: }
215:
216: }
217:
218: public void testNames() {
219: assertEquals("test", pi.getTarget());
220: }
221:
222: public void testEquals() {
223:
224: ProcessingInstruction pi1 = new ProcessingInstruction("test",
225: "afaf");
226: ProcessingInstruction pi2 = new ProcessingInstruction("test",
227: "afaf");
228: ProcessingInstruction pi3 = new ProcessingInstruction(
229: "tegggst", "afaf");
230: ProcessingInstruction pi4 = new ProcessingInstruction("test",
231: "1234");
232:
233: assertEquals(pi1, pi1);
234: assertEquals(pi1.hashCode(), pi1.hashCode());
235: assertTrue(!pi1.equals(pi2));
236: assertTrue(!pi1.equals(pi3));
237: assertTrue(!pi3.equals(pi4));
238: assertTrue(!pi2.equals(pi4));
239: assertTrue(!pi2.equals(pi3));
240:
241: }
242:
243: public void testCopy() {
244:
245: Element test = new Element("test");
246: test.appendChild(pi);
247: ProcessingInstruction c2 = (ProcessingInstruction) pi.copy();
248:
249: assertEquals(pi, c2);
250: assertEquals(pi.getValue(), c2.getValue());
251: assertTrue(!pi.equals(c2));
252: assertNull(c2.getParent());
253:
254: }
255:
256: // Check passing in a string with correct surrogate pairs
257: public void testCorrectSurrogates() {
258:
259: String goodString = "test: \uD8F5\uDF80 ";
260: pi.setValue(goodString);
261: assertEquals(goodString, pi.getValue());
262:
263: }
264:
265: // Check passing in a string with broken surrogate pairs
266: public void testSurrogates() {
267:
268: try {
269: pi.setValue("test \uD8F5\uD8F5 test");
270: fail("Allowed two high halves");
271: } catch (IllegalDataException success) {
272: assertEquals("test \uD8F5\uD8F5 test", success.getData());
273: assertNotNull(success.getMessage());
274: }
275:
276: try {
277: pi.setValue("test \uDF80\uDF80 test");
278: fail("Allowed two low halves");
279: } catch (IllegalDataException success) {
280: assertEquals("test \uDF80\uDF80 test", success.getData());
281: assertNotNull(success.getMessage());
282: }
283:
284: try {
285: pi.setValue("test \uD8F5 \uDF80 test");
286: fail("Allowed two halves split by space");
287: } catch (IllegalDataException success) {
288: assertEquals("test \uD8F5 \uDF80 test", success.getData());
289: assertNotNull(success.getMessage());
290: }
291:
292: try {
293: pi.setValue("test \uDF80\uD8F5 test");
294: fail("Allowed reversed pair");
295: } catch (IllegalDataException success) {
296: assertEquals("test \uDF80\uD8F5 test", success.getData());
297: assertNotNull(success.getMessage());
298: }
299:
300: }
301:
302: public void testLeafNode() {
303:
304: assertEquals(0, pi.getChildCount());
305: try {
306: pi.getChild(0);
307: fail("Didn't throw IndexOutofBoundsException");
308: } catch (IndexOutOfBoundsException success) {
309: assertNotNull(success.getMessage());
310: }
311:
312: assertNull(pi.getParent());
313:
314: Element element = new Element("test");
315: element.appendChild(pi);
316: assertEquals(element, pi.getParent());
317: assertEquals(pi, element.getChild(0));
318:
319: element.removeChild(pi);
320: assertEquals(0, element.getChildCount());
321:
322: }
323:
324: // This is a problem becuase it cannot be serialized
325: // since character and entity references aren't
326: // recognized in comment data
327: public void testCarriageReturnInProcessingInstructionData() {
328:
329: try {
330: new ProcessingInstruction("target", "data\rdata");
331: fail("Allowed carriage return in processing instruction data");
332: } catch (IllegalDataException success) {
333: assertEquals("data\rdata", success.getData());
334: assertNotNull(success.getMessage());
335: }
336:
337: }
338:
339: public void testAllowReservedCharactersInData() {
340: ProcessingInstruction pi = new ProcessingInstruction("target",
341: "<test>&&greater;");
342: String xml = pi.toXML();
343: assertEquals("<?target <test>&&greater;?>", xml);
344: }
345:
346: // This can't be round-tripped
347: public void testNoInitialWhiteSpace() {
348:
349: try {
350: new ProcessingInstruction("target", " initial spaces");
351: fail("allowed processing instruction data with leading space");
352: } catch (IllegalDataException success) {
353: assertEquals(" initial spaces", success.getData());
354: assertNotNull(success.getMessage());
355: }
356:
357: try {
358: new ProcessingInstruction("target", "\tinitial tab");
359: fail("allowed processing instruction data with leading space");
360: } catch (IllegalDataException success) {
361: assertEquals("\tinitial tab", success.getData());
362: assertNotNull(success.getMessage());
363: }
364:
365: try {
366: new ProcessingInstruction("target", "\ninitial linefeed");
367: fail("allowed processing instruction data with leading space");
368: } catch (IllegalDataException success) {
369: assertEquals("\ninitial linefeed", success.getData());
370: assertNotNull(success.getMessage());
371: }
372:
373: try {
374: new ProcessingInstruction("target",
375: "\r initial carriage return");
376: fail("allowed processing instruction data with leading space");
377: } catch (IllegalDataException success) {
378: assertEquals("\r initial carriage return", success
379: .getData());
380: assertNotNull(success.getMessage());
381: }
382:
383: }
384:
385: public void testNoXMLTargets() {
386:
387: try {
388: new ProcessingInstruction("xml", "data");
389: fail("allowed processing instruction with target xml");
390: } catch (IllegalTargetException success) {
391: assertEquals("xml", success.getData());
392: assertNotNull(success.getMessage());
393: }
394:
395: try {
396: new ProcessingInstruction("XML", "data");
397: fail("allowed processing instruction with target XML");
398: } catch (IllegalTargetException success) {
399: assertEquals("XML", success.getData());
400: assertNotNull(success.getMessage());
401: }
402:
403: try {
404: new ProcessingInstruction("Xml", "data");
405: fail("allowed processing instruction with target Xml");
406: } catch (IllegalTargetException success) {
407: assertEquals("Xml", success.getData());
408: assertNotNull(success.getMessage());
409: }
410:
411: }
412:
413: public void testColonsNotAllowedInTargets() {
414:
415: try {
416: new ProcessingInstruction("pre:target", "data");
417: fail("allowed processing instruction with target that uses a prefixed name");
418: } catch (IllegalTargetException success) {
419: assertEquals("pre:target", success.getData());
420: assertNotNull(success.getMessage());
421: }
422:
423: try {
424: new ProcessingInstruction("pre:", "data");
425: fail("allowed processing instruction with trailing colon in target");
426: } catch (IllegalTargetException success) {
427: assertEquals("pre:", success.getData());
428: assertNotNull(success.getMessage());
429: }
430:
431: try {
432: new ProcessingInstruction(":target", "data");
433: fail("allowed processing instruction with initial colon in target");
434: } catch (IllegalTargetException success) {
435: assertEquals(":target", success.getData());
436: assertNotNull(success.getMessage());
437: }
438:
439: }
440:
441: }
|