001: /*
002: * XML 2 Java Binding (X2JB) - the excellent Java tool.
003: * Copyright 2007, by Richard Opalka.
004: *
005: * This is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as
007: * published by the Free Software Foundation; either version 2.1 of
008: * the License, or (at your option) any later version.
009: *
010: * This software is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this software; if not see the FSF site:
017: * http://www.fsf.org/ and search for the LGPL License document there.
018: */
019: package test.org.x2jb.bind;
020:
021: import junit.framework.TestCase;
022: import org.x2jb.bind.BindingException;
023: import org.x2jb.bind.XML2Java;
024: import org.w3c.dom.Element;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.DocumentBuilder;
027: import java.math.BigDecimal;
028: import java.math.BigInteger;
029: import test.sample.valid.*;
030: import test.sample.invalid.*;
031:
032: /**
033: * XML2Java Binding Java Tool Test
034: *
035: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
036: * @version 1.0
037: */
038: public class XML2JavaTest extends TestCase {
039:
040: private DocumentBuilder builder;
041:
042: public XML2JavaTest(String s) {
043: super (s);
044: }
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: DocumentBuilderFactory dbf = DocumentBuilderFactory
049: .newInstance();
050: dbf.setNamespaceAware(true);
051: dbf.setExpandEntityReferences(true);
052: dbf.setIgnoringComments(true);
053: this .builder = dbf.newDocumentBuilder();
054: }
055:
056: protected void tearDown() throws Exception {
057: this .builder = null;
058: super .tearDown();
059: }
060:
061: public void testNullReturnValues() throws Exception {
062: ElementOrAttributeNotPresent cfg = (ElementOrAttributeNotPresent) XML2Java
063: .bind(getRootElement("/resources/correct/test1.xml"),
064: ElementOrAttributeNotPresent.class);
065:
066: assertNull("Expected null", cfg.getNoSuchAttribute());
067: assertNull("Expected null", cfg.getNoSuchElement());
068: assertTrue("Expected zero length array", cfg
069: .getNoSuchElementAsArray().length == 0);
070: }
071:
072: public void testAllSupportedSimpleTypes() throws Exception {
073: AllSupportedSimpleTypes cfg = (AllSupportedSimpleTypes) XML2Java
074: .bind(getRootElement("/resources/correct/test5.xml"),
075: AllSupportedSimpleTypes.class);
076:
077: assertEquals("a00", cfg.getAttributeAsString(), "1");
078: assertEquals("a10", cfg.getAttributeAsBigDecimal(),
079: new BigDecimal("1"));
080: assertEquals("a20", cfg.getAttributeAsBigInteger(),
081: new BigInteger("1"));
082: assertEquals("a30", cfg.getAttributeAsByte(), new Byte("1"));
083: assertEquals("a40", cfg.getAttributeAsCharacter(),
084: new Character('1'));
085: assertEquals("a50", cfg.getAttributeAsDouble(), new Double("1"));
086: assertEquals("a60", cfg.getAttributeAsFloat(), new Float("1"));
087: assertEquals("a70", cfg.getAttributeAsInteger(), new Integer(
088: "1"));
089: assertEquals("a80", cfg.getAttributeAsLong(), new Long("1"));
090: assertEquals("a90", cfg.getAttributeAsShort(), new Short("1"));
091: assertTrue("a31",
092: cfg.getAttributeAsPrimitiveByte() == ((byte) 1));
093: assertTrue("a41",
094: cfg.getAttributeAsPrimitiveCharacter() == ((char) '1'));
095: assertTrue("a51",
096: cfg.getAttributeAsPrimitiveDouble() == ((double) 1));
097: assertTrue("a61",
098: cfg.getAttributeAsPrimitiveFloat() == ((float) 1));
099: assertTrue("a71",
100: cfg.getAttributeAsPrimitiveInteger() == ((int) 1));
101: assertTrue("a81",
102: cfg.getAttributeAsPrimitiveLong() == ((long) 1));
103: assertTrue("a91",
104: cfg.getAttributeAsPrimitiveShort() == ((short) 1));
105:
106: assertEquals("e00", cfg.getElementAsString(), "2");
107: assertEquals("e10", cfg.getElementAsBigDecimal(),
108: new BigDecimal("2"));
109: assertEquals("e20", cfg.getElementAsBigInteger(),
110: new BigInteger("2"));
111: assertEquals("e30", cfg.getElementAsByte(), new Byte("2"));
112: assertEquals("e40", cfg.getElementAsCharacter(), new Character(
113: '2'));
114: assertEquals("e50", cfg.getElementAsDouble(), new Double("2"));
115: assertEquals("e60", cfg.getElementAsFloat(), new Float("2"));
116: assertEquals("e70", cfg.getElementAsInteger(), new Integer("2"));
117: assertEquals("e80", cfg.getElementAsLong(), new Long("2"));
118: assertEquals("e90", cfg.getElementAsShort(), new Short("2"));
119: assertTrue("e31", cfg.getElementAsPrimitiveByte() == ((byte) 2));
120: assertTrue("e41",
121: cfg.getElementAsPrimitiveCharacter() == ((char) '2'));
122: assertTrue("e51",
123: cfg.getElementAsPrimitiveDouble() == ((double) 2));
124: assertTrue("e61",
125: cfg.getElementAsPrimitiveFloat() == ((float) 2));
126: assertTrue("e71",
127: cfg.getElementAsPrimitiveInteger() == ((int) 2));
128: assertTrue("e81", cfg.getElementAsPrimitiveLong() == ((long) 2));
129: assertTrue("e91",
130: cfg.getElementAsPrimitiveShort() == ((short) 2));
131: }
132:
133: public void testAllSupportedSimpleTypesWrongValue()
134: throws Exception {
135: runNegativeTest("/resources/correct/test5.xml",
136: AllSupportedSimpleTypesWrongAttributeValue.class,
137: new String[] { "Incorrect value", "abc" });
138: runNegativeTest("/resources/correct/test5.xml",
139: AllSupportedSimpleTypesWrongElementValue.class,
140: new String[] { "Incorrect value", "abc" });
141: }
142:
143: public void testDefaultValues() throws Exception {
144: DefaultValues cfg = (DefaultValues) XML2Java.bind(
145: getRootElement("/resources/correct/test5.xml"),
146: DefaultValues.class);
147:
148: assertTrue("b00", cfg.getDefaultStringArray().length == 0);
149: assertTrue("b10", cfg.getDefaultBigDecimalArray().length == 0);
150: assertTrue("b20", cfg.getDefaultBigIntegerArray().length == 0);
151: assertTrue("b30", cfg.getDefaultByteArray().length == 0);
152: assertTrue("b40", cfg.getDefaultCharacterArray().length == 0);
153: assertTrue("b50", cfg.getDefaultDoubleArray().length == 0);
154: assertTrue("b60", cfg.getDefaultFloatArray().length == 0);
155: assertTrue("b70", cfg.getDefaultIntegerArray().length == 0);
156: assertTrue("b80", cfg.getDefaultLongArray().length == 0);
157: assertTrue("b90", cfg.getDefaultShortArray().length == 0);
158: assertTrue("b31",
159: cfg.getDefaultPrimitiveByteArray().length == 0);
160: assertTrue("b41",
161: cfg.getDefaultPrimitiveCharacterArray().length == 0);
162: assertTrue("b51",
163: cfg.getDefaultPrimitiveDoubleArray().length == 0);
164: assertTrue("b61",
165: cfg.getDefaultPrimitiveFloatArray().length == 0);
166: assertTrue("b71",
167: cfg.getDefaultPrimitiveIntegerArray().length == 0);
168: assertTrue("b81",
169: cfg.getDefaultPrimitiveLongArray().length == 0);
170: assertTrue("b91",
171: cfg.getDefaultPrimitiveShortArray().length == 0);
172: assertTrue("b01", cfg.getDefaultUnusedIfaceArray().length == 0);
173:
174: assertTrue("a00", cfg.getAttributeDefaultAsString() == null);
175: assertTrue("a10", cfg.getAttributeDefaultAsBigDecimal() == null);
176: assertTrue("a20", cfg.getAttributeDefaultAsBigInteger() == null);
177: assertTrue("a30", cfg.getAttributeDefaultAsByte() == null);
178: assertTrue("a40", cfg.getAttributeDefaultAsCharacter() == null);
179: assertTrue("a50", cfg.getAttributeDefaultAsDouble() == null);
180: assertTrue("a60", cfg.getAttributeDefaultAsFloat() == null);
181: assertTrue("a70", cfg.getAttributeDefaultAsInteger() == null);
182: assertTrue("a80", cfg.getAttributeDefaultAsLong() == null);
183: assertTrue("a90", cfg.getAttributeDefaultAsShort() == null);
184: assertTrue("a31",
185: cfg.getAttributeDefaultAsPrimitiveByte() == ((byte) 0));
186: assertTrue(
187: "a41",
188: cfg.getAttributeDefaultAsPrimitiveCharacter() == ((char) 0));
189: assertTrue(
190: "a51",
191: cfg.getAttributeDefaultAsPrimitiveDouble() == ((double) 0.0));
192: assertTrue(
193: "a61",
194: cfg.getAttributeDefaultAsPrimitiveFloat() == ((float) 0.0));
195: assertTrue(
196: "a71",
197: cfg.getAttributeDefaultAsPrimitiveInteger() == ((int) 0));
198: assertTrue("a81",
199: cfg.getAttributeDefaultAsPrimitiveLong() == ((long) 0));
200: assertTrue(
201: "a91",
202: cfg.getAttributeDefaultAsPrimitiveShort() == ((short) 0));
203:
204: assertTrue("e00", cfg.getElementDefaultAsString() == null);
205: assertTrue("e10", cfg.getElementDefaultAsBigDecimal() == null);
206: assertTrue("e20", cfg.getElementDefaultAsBigInteger() == null);
207: assertTrue("e30", cfg.getElementDefaultAsByte() == null);
208: assertTrue("e40", cfg.getElementDefaultAsCharacter() == null);
209: assertTrue("e50", cfg.getElementDefaultAsDouble() == null);
210: assertTrue("e60", cfg.getElementDefaultAsFloat() == null);
211: assertTrue("e70", cfg.getElementDefaultAsInteger() == null);
212: assertTrue("e80", cfg.getElementDefaultAsLong() == null);
213: assertTrue("e90", cfg.getElementDefaultAsShort() == null);
214: assertTrue("e31",
215: cfg.getElementDefaultAsPrimitiveByte() == ((byte) 0));
216: assertTrue(
217: "e41",
218: cfg.getElementDefaultAsPrimitiveCharacter() == ((char) 0));
219: assertTrue(
220: "e51",
221: cfg.getElementDefaultAsPrimitiveDouble() == ((double) 0.0));
222: assertTrue(
223: "e61",
224: cfg.getElementDefaultAsPrimitiveFloat() == ((float) 0.0));
225: assertTrue("e71",
226: cfg.getElementDefaultAsPrimitiveInteger() == ((int) 0));
227: assertTrue("e81",
228: cfg.getElementDefaultAsPrimitiveLong() == ((long) 0));
229: assertTrue("e91",
230: cfg.getElementDefaultAsPrimitiveShort() == ((short) 0));
231: }
232:
233: public void testDefaultValuesEnduranceTest() throws Exception {
234: Element rootElement = getRootElement("/resources/correct/test5.xml");
235: for (int i = 0; i < 10000; i++) {
236: XML2Java.bind(rootElement, DefaultValues.class);
237: }
238: }
239:
240: public void test1() throws Exception {
241: Config1 cfg = (Config1) XML2Java.bind(
242: getRootElement("/resources/correct/test1.xml"),
243: Config1.class);
244:
245: assertTrue("F1", cfg.getAttributeXyz().equals("asdf"));
246: assertTrue("F2", cfg.getElementTest().equals("TEST"));
247: assertTrue("F41", cfg.getElementTestArray()[0]
248: .equals(new Character('A')));
249: assertTrue("F42", cfg.getElementTestArray()[1]
250: .equals(new Character('S')));
251: assertTrue("F43", cfg.getElementTestArray()[2]
252: .equals(new Character('D')));
253: assertTrue("F44", cfg.getElementTestArray()[3]
254: .equals(new Character('F')));
255:
256: Config1.Inner1 inner = cfg.getElementInner();
257: assertTrue("F4", inner.getAttributeAsdf().equals("xyz"));
258: assertTrue("F5", inner.getElementTest2().equals("TEST_INNER"));
259: assertTrue("F61", inner.getElementTestArray2()[0]
260: .equals(new Character('J')));
261: assertTrue("F62", inner.getElementTestArray2()[1]
262: .equals(new Character('K')));
263: assertTrue("F63", inner.getElementTestArray2()[2]
264: .equals(new Character('L')));
265: assertTrue("F64", inner.getElementTestArray2()[3]
266: .equals(new Character('O')));
267:
268: Config1.Inner1.Inner11[] outer = cfg.getElementInner()
269: .getElementOuter();
270: assertTrue("F71", outer[0].getAttributeJklo().equals("r1"));
271: assertTrue("F72", outer[1].getAttributeJklo().equals("r2"));
272: assertTrue("F73", outer[2].getAttributeJklo().equals("r3"));
273: }
274:
275: public void test2() throws Exception {
276: Config2 cfg = (Config2) XML2Java.bind(
277: getRootElement("/resources/correct/test2.xml"),
278: Config2.class);
279:
280: assertTrue("F1", cfg.getAttributeXyz().equals("asdf"));
281: assertTrue("F2", cfg.getElementTest().equals("TEST"));
282: assertTrue("F41", cfg.getElementTestArray()[0]
283: .equals(new Character('A')));
284: assertTrue("F42", cfg.getElementTestArray()[1]
285: .equals(new Character('S')));
286: assertTrue("F43", cfg.getElementTestArray()[2]
287: .equals(new Character('D')));
288: assertTrue("F44", cfg.getElementTestArray()[3]
289: .equals(new Character('F')));
290:
291: Config2.Inner1 inner = cfg.getElementInner();
292: assertTrue("F4", inner.getAttributeAsdf().equals("xyz"));
293: assertTrue("F5", inner.getElementTest2().equals("TEST_INNER"));
294: assertTrue("F61", inner.getElementTestArray2()[0]
295: .equals(new Character('J')));
296: assertTrue("F62", inner.getElementTestArray2()[1]
297: .equals(new Character('K')));
298: assertTrue("F63", inner.getElementTestArray2()[2]
299: .equals(new Character('L')));
300: assertTrue("F64", inner.getElementTestArray2()[3]
301: .equals(new Character('O')));
302:
303: Config2.Inner1.Inner11[] outer = cfg.getElementInner()
304: .getElementOuter();
305: assertTrue("F71", outer[0].getAttributeJklo().equals("r1"));
306: assertTrue("F72", outer[1].getAttributeJklo().equals("r2"));
307: assertTrue("F73", outer[2].getAttributeJklo().equals("r3"));
308: }
309:
310: public void test3() throws Exception {
311: Config2 cfg = (Config2) XML2Java.bind(
312: getRootElement("/resources/correct/test3.xml"),
313: Config2.class);
314:
315: assertTrue("F1", cfg.getAttributeXyz().equals("asdf"));
316: assertTrue("F2", cfg.getElementTest().equals("TEST"));
317: assertTrue("F41", cfg.getElementTestArray()[0]
318: .equals(new Character('A')));
319: assertTrue("F42", cfg.getElementTestArray()[1]
320: .equals(new Character('S')));
321: assertTrue("F43", cfg.getElementTestArray()[2]
322: .equals(new Character('D')));
323: assertTrue("F44", cfg.getElementTestArray()[3]
324: .equals(new Character('F')));
325:
326: Config2.Inner1 inner = cfg.getElementInner();
327: assertTrue("F4", inner.getAttributeAsdf().equals("xyz"));
328: assertTrue("F5", inner.getElementTest2().equals("TEST_INNER"));
329: assertTrue("F61", inner.getElementTestArray2()[0]
330: .equals(new Character('J')));
331: assertTrue("F62", inner.getElementTestArray2()[1]
332: .equals(new Character('K')));
333: assertTrue("F63", inner.getElementTestArray2()[2]
334: .equals(new Character('L')));
335: assertTrue("F64", inner.getElementTestArray2()[3]
336: .equals(new Character('O')));
337:
338: Config2.Inner1.Inner11[] outer = cfg.getElementInner()
339: .getElementOuter();
340: assertTrue("F71", outer[0].getAttributeJklo().equals("r1"));
341: assertTrue("F72", outer[1].getAttributeJklo().equals("r2"));
342: assertTrue("F73", outer[2].getAttributeJklo().equals("r3"));
343: }
344:
345: public void test4() throws Exception {
346: Config3 cfg = (Config3) XML2Java.bind(
347: getRootElement("/resources/correct/test4.xml"),
348: Config3.class);
349:
350: assertTrue("F1", cfg.getAttributeXyz().equals("asdf"));
351: assertTrue("F2", cfg.getElementTest().equals("TEST"));
352: assertTrue("F41", cfg.getElementTestArray()[0]
353: .equals(new Character('A')));
354: assertTrue("F42", cfg.getElementTestArray()[1]
355: .equals(new Character('S')));
356: assertTrue("F43", cfg.getElementTestArray()[2]
357: .equals(new Character('D')));
358: assertTrue("F44", cfg.getElementTestArray()[3]
359: .equals(new Character('F')));
360:
361: Config3.Inner1 inner = cfg.getElementInner();
362: assertTrue("F4", inner.getAttributeAsdf().equals("xyz"));
363: assertTrue("F5", inner.getElementTest2().equals("TEST_INNER"));
364: assertTrue("F61", inner.getElementTestArray2()[0]
365: .equals(new Character('J')));
366: assertTrue("F62", inner.getElementTestArray2()[1]
367: .equals(new Character('K')));
368: assertTrue("F63", inner.getElementTestArray2()[2]
369: .equals(new Character('L')));
370: assertTrue("F64", inner.getElementTestArray2()[3]
371: .equals(new Character('O')));
372:
373: Config3.Inner1.Inner11[] outer = cfg.getElementInner()
374: .getElementOuter();
375: assertTrue("F71", outer[0].getAttributeJklo().equals("r1"));
376: assertTrue("F72", outer[1].getAttributeJklo().equals("r2"));
377: assertTrue("F73", outer[2].getAttributeJklo().equals("r3"));
378: }
379:
380: public void testCustomHandler() throws Exception {
381: Element rootElement = getRootElement("/resources/correct/test1.xml");
382:
383: CustomHandler cfg = (CustomHandler) XML2Java.bind(rootElement,
384: CustomHandler.class);
385: String[] retVal = cfg.getInner().getTestArray1();
386: assertTrue("CH1", "j".equals(retVal[0]));
387: assertTrue("CH2", "k".equals(retVal[1]));
388: assertTrue("CH3", "l".equals(retVal[2]));
389: assertTrue("CH4", "o".equals(retVal[3]));
390:
391: CustomHandler2 cfg2 = (CustomHandler2) XML2Java.bind(
392: rootElement, CustomHandler2.class);
393: String[] retVal2 = cfg2.getInner().getTestArray1();
394: assertTrue("CH1", "j".equals(retVal2[0]));
395: assertTrue("CH2", "k".equals(retVal2[1]));
396: assertTrue("CH3", "l".equals(retVal2[2]));
397: assertTrue("CH4", "o".equals(retVal2[3]));
398:
399: CustomHandler4 cfg4 = (CustomHandler4) XML2Java.bind(
400: rootElement, CustomHandler4.class);
401: String[] retVal4 = cfg4.getInner().values;
402: assertTrue("CH1", "j".equals(retVal4[0]));
403: assertTrue("CH2", "k".equals(retVal4[1]));
404: assertTrue("CH3", "l".equals(retVal4[2]));
405: assertTrue("CH4", "o".equals(retVal4[3]));
406: }
407:
408: public void testMultidimensionalArray() throws Exception {
409: MultiDimensionalArrays cfg = (MultiDimensionalArrays) XML2Java
410: .bind(getRootElement("/resources/correct/test6.xml"),
411: MultiDimensionalArrays.class);
412:
413: int[][] array1 = cfg.getArray1();
414: assertTrue("a11", array1[0][0] == 1);
415: assertTrue("a12", array1[0][1] == 2);
416: assertTrue("a13", array1[0][2] == 3);
417: assertTrue("a14", array1[1][0] == 4);
418: assertTrue("a15", array1[1][1] == 5);
419: assertTrue("a16", array1[1][2] == 6);
420: assertTrue("a17", array1[2][0] == 7);
421: assertTrue("a18", array1[2][1] == 8);
422: assertTrue("a19", array1[2][2] == 9);
423:
424: int[] array2 = cfg.getArray2();
425: assertTrue("a21", array2[0] == 1);
426: assertTrue("a22", array2[1] == 2);
427: assertTrue("a23", array2[2] == 3);
428: assertTrue("a24", array2[3] == 4);
429: assertTrue("a25", array2[4] == 5);
430: assertTrue("a26", array2[5] == 6);
431: assertTrue("a27", array2[6] == 7);
432: assertTrue("a28", array2[7] == 8);
433: assertTrue("a29", array2[8] == 9);
434: }
435:
436: public void testServerConfig() throws Exception {
437: ServerConfig cfg = (ServerConfig) XML2Java.bind(
438: getRootElement("/resources/correct/server.xml"),
439: ServerConfig.class);
440:
441: ServerConfig.Module[] modules = cfg.getModules();
442: assertTrue(modules.length == 3);
443: assertEquals(modules[0].getInstanceName(),
444: "TransportRepository");
445: assertEquals(modules[0].getImplementationClass(),
446: "com.test.module.TransportRepositoryImpl");
447:
448: assertEquals(modules[1].getInstanceName(),
449: "InterceptorRepository");
450: assertEquals(modules[1].getImplementationClass(),
451: "com.test.module.InterceptorRepositoryImpl");
452:
453: assertEquals(modules[2].getInstanceName(), "PackageRepository");
454: assertEquals(modules[2].getImplementationClass(),
455: "com.test.module.PackageRepositoryImpl");
456:
457: Element packageRepositoryModuleElement = modules[2]
458: .getCurrentElement();
459: assertNotNull(packageRepositoryModuleElement);
460:
461: ServerConfig.PackageRepositoryConfig pckgConfig = (ServerConfig.PackageRepositoryConfig) XML2Java
462: .bind(packageRepositoryModuleElement,
463: ServerConfig.PackageRepositoryConfig.class);
464:
465: assertEquals(pckgConfig.getAppDir(), "app");
466: assertEquals(pckgConfig.getWorkDir(), "work");
467: assertEquals(pckgConfig.getSystemDir(), "system");
468:
469: ServerConfig.PackageRepositoryConfig2 pckgConfig2 = (ServerConfig.PackageRepositoryConfig2) XML2Java
470: .bind(packageRepositoryModuleElement,
471: ServerConfig.PackageRepositoryConfig2.class);
472:
473: Element[] subelements = pckgConfig2.getSubelements();
474:
475: assertTrue(subelements.length == 3);
476:
477: assertEquals(subelements[0].getNodeName(), "appDir");
478: assertEquals(Helper.getTextContent(subelements[0]), "app");
479:
480: assertEquals(subelements[1].getNodeName(), "workDir");
481: assertEquals(Helper.getTextContent(subelements[1]), "work");
482:
483: assertEquals(subelements[2].getNodeName(), "sysCtx");
484: assertEquals(Helper.getTextContent(subelements[2]), "system");
485:
486: ServerConfig.PackageRepositoryConfig3 pckgConfig3 = (ServerConfig.PackageRepositoryConfig3) XML2Java
487: .bind(packageRepositoryModuleElement,
488: ServerConfig.PackageRepositoryConfig3.class);
489:
490: Element[] appDirSubelements = pckgConfig3
491: .getAllAppDirSubelements();
492:
493: assertTrue(appDirSubelements.length == 1);
494:
495: assertEquals(subelements[0].getNodeName(), "appDir");
496: assertEquals(Helper.getTextContent(subelements[0]), "app");
497:
498: ServerConfig.PackageRepositoryConfig4 pckgConfig4 = (ServerConfig.PackageRepositoryConfig4) XML2Java
499: .bind(packageRepositoryModuleElement,
500: ServerConfig.PackageRepositoryConfig4.class);
501:
502: Element appDirSubelement = pckgConfig4.getAppDirSubelement();
503:
504: assertEquals(appDirSubelement.getNodeName(), "appDir");
505: assertEquals(Helper.getTextContent(appDirSubelement), "app");
506: }
507:
508: public void testWrongMappingOfElementHandlerToAttribute()
509: throws Exception {
510: runNegativeTest("/resources/correct/test1.xml",
511: ElementHandlerMappedToAttribute.class, new String[] {
512: "Attribute handler", "for class", "not found" });
513: }
514:
515: public void testWrongMappingOfAttributeHandlerToElement()
516: throws Exception {
517: runNegativeTest("/resources/correct/test1.xml",
518: AttributeHandlerMappedToElement.class, new String[] {
519: "Method", "Element handler", "for class",
520: "not found" });
521: }
522:
523: public void testNonExistingElementHandler() throws Exception {
524: runNegativeTest(
525: "/resources/correct/test1.xml",
526: NotExistingElementHandler.class,
527: new String[] { "test.sample.valid.VirtualElementHandler" });
528: }
529:
530: public void testNonExistingAttributeHandler() throws Exception {
531: runNegativeTest(
532: "/resources/correct/test1.xml",
533: NotExistingAttributeHandler.class,
534: new String[] { "test.sample.valid.VirtualElementHandler" });
535: }
536:
537: public void testWrongMultiDimensionalArray1() throws Exception {
538: runNegativeTest("/resources/incorrect/test8.xml",
539: WrongMultiDimensionalArray1.class, new String[] {
540: "Method", "Default element handler for class",
541: "not found" });
542: }
543:
544: public void testWrongMultiDimensionalArray2() throws Exception {
545: runNegativeTest("/resources/incorrect/test8.xml",
546: WrongMultiDimensionalArray2.class, new String[] {
547: "Method", "Default element handler for class",
548: "not found" });
549: }
550:
551: public void testElementRequiredButNotPresent() throws Exception {
552: runNegativeTest("/resources/incorrect/test4.xml",
553: ElementRequiredButNotPresent.class, new String[] {
554: "Element", "must contain subelement",
555: "as specified in method" });
556: }
557:
558: public void testElementRequiredButNotPresent2() throws Exception {
559: runNegativeTest("/resources/incorrect/test4.xml",
560: ElementRequiredButNotPresent2.class, new String[] {
561: "Element", "must contain subelement",
562: "as specified in method" });
563: }
564:
565: public void testElementRequiredButNotPresent3() throws Exception {
566: runNegativeTest("/resources/incorrect/test4.xml",
567: ElementRequiredButNotPresent3.class, new String[] {
568: "Element", "must contain subelement",
569: "as specified in method" });
570: }
571:
572: public void testElementRequiredButNotPresent4() throws Exception {
573: runNegativeTest("/resources/incorrect/test4.xml",
574: ElementRequiredButNotPresent4.class, new String[] {
575: "Element", "must contain subelement",
576: "as specified in method" });
577: }
578:
579: public void testAttributeRequiredButNotPresent() throws Exception {
580: runNegativeTest("/resources/incorrect/test6.xml",
581: AttributeRequiredButNotPresent.class, new String[] {
582: "Method", "specifies mandatory attribute",
583: "but this attribute is not present" });
584: }
585:
586: public void testAttributeRequiredButNotPresent2() throws Exception {
587: runNegativeTest("/resources/incorrect/test7.xml",
588: AttributeRequiredButNotPresent2.class, new String[] {
589: "Method", "specifies mandatory attribute",
590: "but this attribute is not present" });
591: }
592:
593: public void testIncorrectElementName() throws Exception {
594: runNegativeTest("/resources/incorrect/test2.xml",
595: InvalidElementName.class, new String[] { "Method",
596: "contains incorrect node name value" });
597: }
598:
599: public void testMultipleElementsButSimpleReturnType()
600: throws Exception {
601: runNegativeTest("/resources/incorrect/test2.xml",
602: MultipleElementsButSimpleReturnType.class,
603: new String[] { "Method",
604: "does not return array but multiple elements",
605: "are present" });
606: }
607:
608: public void testMultipleElementsButSimpleReturnType2()
609: throws Exception {
610: runNegativeTest("/resources/incorrect/test2.xml",
611: MultipleElementsButSimpleReturnType2.class,
612: new String[] { "Method",
613: "does not return array but multiple elements",
614: "are present" });
615: }
616:
617: public void testUnsupportedPrimitiveVoidReturnType()
618: throws Exception {
619: runNegativeTest("/resources/incorrect/test2.xml",
620: PrimitiveVoidNotAllowedAsReturnType.class,
621: new String[] { "Method",
622: "cannot return neither void nor Void" });
623: }
624:
625: public void testUnsupportedVoidWrapperReturnType() throws Exception {
626: runNegativeTest("/resources/incorrect/test2.xml",
627: VoidWrapperNotAllowedAsReturnType.class,
628: new String[] { "Method",
629: "cannot return neither void nor Void" });
630: }
631:
632: public void testMethodWithoutMetadata() throws Exception {
633: runNegativeTest("/resources/incorrect/test2.xml",
634: MethodWithoutAnnotation.class, new String[] {
635: "Metadata for method", "must be declared" });
636: }
637:
638: public void testMethodWithParam() throws Exception {
639: runNegativeTest("/resources/incorrect/test2.xml",
640: MethodWithParams.class, new String[] { "Method",
641: "cannot contain parameters" });
642: }
643:
644: public void testInnerIfaceNamespaceMismatch() throws Exception {
645: runNegativeTest("/resources/incorrect/test2.xml",
646: InnerIfaceNamespaceMismatch.class, new String[] {
647: "Element", "must contain subelement",
648: "as specified in method" });
649: }
650:
651: public void testInnerIfaceMappedToAttribute() throws Exception {
652: runNegativeTest("/resources/incorrect/test2.xml",
653: InnerIfaceMappedToAttribute.class, new String[] {
654: "Interface", "must be mapped to element" });
655: }
656:
657: private void runNegativeTest(String resourceName, Class clazz,
658: String[] expectedSubstrings) throws Exception {
659: try {
660: XML2Java.bind(getRootElement(resourceName), clazz);
661: fail("This test method had to fail!");
662: } catch (Exception e) {
663: Helper.assertContains(e, expectedSubstrings);
664: }
665:
666: }
667:
668: private Element getRootElement(String resourceName)
669: throws Exception {
670: return this.builder.parse(
671: getClass().getResourceAsStream(resourceName))
672: .getDocumentElement();
673: }
674:
675: }
|