001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant;
020:
021: import junit.framework.TestCase;
022: import junit.framework.AssertionFailedError;
023: import java.io.File;
024: import java.lang.reflect.Method;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.Hashtable;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Locale;
032: import java.util.Map;
033: import org.apache.tools.ant.taskdefs.condition.Os;
034:
035: /**
036: * JUnit 3 testcases for org.apache.tools.ant.IntrospectionHelper.
037: *
038: */
039:
040: public class IntrospectionHelperTest extends TestCase {
041:
042: private Project p;
043: private IntrospectionHelper ih;
044: private static final String projectBasedir = File.separator;
045:
046: public IntrospectionHelperTest(String name) {
047: super (name);
048: }
049:
050: public void setUp() {
051: p = new Project();
052: p.setBasedir(projectBasedir);
053: ih = IntrospectionHelper.getHelper(getClass());
054: }
055:
056: public void testIsDynamic() {
057: assertTrue("Not dynamic", false == ih.isDynamic());
058: }
059:
060: public void testIsContainer() {
061: assertTrue("Not a container", false == ih.isContainer());
062: }
063:
064: public void testAddText() throws BuildException {
065: ih.addText(p, this , "test");
066: try {
067: ih.addText(p, this , "test2");
068: fail("test2 shouldn\'t be equal to test");
069: } catch (BuildException be) {
070: assertTrue(be.getException() instanceof AssertionFailedError);
071: }
072:
073: ih = IntrospectionHelper.getHelper(String.class);
074: try {
075: ih.addText(p, "", "test");
076: fail("String doesn\'t support addText");
077: } catch (BuildException be) {
078: }
079: }
080:
081: public void testGetAddTextMethod() {
082: Method m = ih.getAddTextMethod();
083: assertMethod(m, "addText", String.class, "test", "bing!");
084:
085: ih = IntrospectionHelper.getHelper(String.class);
086: try {
087: m = ih.getAddTextMethod();
088: } catch (BuildException e) {
089: }
090: }
091:
092: public void testSupportsCharacters() {
093: assertTrue("IntrospectionHelperTest supports addText", ih
094: .supportsCharacters());
095:
096: ih = IntrospectionHelper.getHelper(String.class);
097: assertTrue("String doesn\'t support addText", !ih
098: .supportsCharacters());
099: }
100:
101: public void addText(String text) {
102: assertEquals("test", text);
103: }
104:
105: public void testElementCreators() throws BuildException {
106: try {
107: ih.getElementType("one");
108: fail("don't have element type one");
109: } catch (BuildException be) {
110: }
111: try {
112: ih.getElementType("two");
113: fail("createTwo takes arguments");
114: } catch (BuildException be) {
115: }
116: try {
117: ih.getElementType("three");
118: fail("createThree returns void");
119: } catch (BuildException be) {
120: }
121: try {
122: ih.getElementType("four");
123: fail("createFour returns array");
124: } catch (BuildException be) {
125: }
126: try {
127: ih.getElementType("five");
128: fail("createFive returns primitive type");
129: } catch (BuildException be) {
130: }
131: assertEquals(String.class, ih.getElementType("six"));
132: assertEquals("test", ih.createElement(p, this , "six"));
133:
134: try {
135: ih.getElementType("seven");
136: fail("addSeven takes two arguments");
137: } catch (BuildException be) {
138: }
139: try {
140: ih.getElementType("eight");
141: fail("addEight takes no arguments");
142: } catch (BuildException be) {
143: }
144: try {
145: ih.getElementType("nine");
146: fail("nine return non void");
147: } catch (BuildException be) {
148: }
149: try {
150: ih.getElementType("ten");
151: fail("addTen takes array argument");
152: } catch (BuildException be) {
153: }
154: try {
155: ih.getElementType("eleven");
156: fail("addEleven takes primitive argument");
157: } catch (BuildException be) {
158: }
159: try {
160: ih.getElementType("twelve");
161: fail("no primitive constructor for java.lang.Class");
162: } catch (BuildException be) {
163: }
164: assertEquals(StringBuffer.class, ih.getElementType("thirteen"));
165: assertEquals("test", ih.createElement(p, this , "thirteen")
166: .toString());
167:
168: try {
169: ih.createElement(p, this , "fourteen");
170: fail("fourteen throws NullPointerException");
171: } catch (BuildException be) {
172: assertTrue(be.getException() instanceof NullPointerException);
173: }
174:
175: try {
176: ih.createElement(p, this , "fourteen");
177: fail("fifteen throws NullPointerException");
178: } catch (BuildException be) {
179: assertTrue(be.getException() instanceof NullPointerException);
180: }
181: }
182:
183: private Map getExpectedNestedElements() {
184: Map elemMap = new Hashtable();
185: elemMap.put("six", String.class);
186: elemMap.put("thirteen", StringBuffer.class);
187: elemMap.put("fourteen", StringBuffer.class);
188: elemMap.put("fifteen", StringBuffer.class);
189: return elemMap;
190: }
191:
192: public void testGetNestedElements() {
193: Map elemMap = getExpectedNestedElements();
194: Enumeration e = ih.getNestedElements();
195: while (e.hasMoreElements()) {
196: String name = (String) e.nextElement();
197: Class expect = (Class) elemMap.get(name);
198: assertNotNull("Support for " + name
199: + " in IntrospectioNHelperTest?", expect);
200: assertEquals("Return type of " + name, expect, ih
201: .getElementType(name));
202: elemMap.remove(name);
203: }
204: assertTrue("Found all", elemMap.isEmpty());
205: }
206:
207: public void testGetNestedElementMap() {
208: Map elemMap = getExpectedNestedElements();
209: Map actualMap = ih.getNestedElementMap();
210: for (Iterator i = actualMap.entrySet().iterator(); i.hasNext();) {
211: Map.Entry entry = (Map.Entry) i.next();
212: String elemName = (String) entry.getKey();
213: Class elemClass = (Class) elemMap.get(elemName);
214: assertNotNull("Support for " + elemName
215: + " in IntrospectionHelperTest?", elemClass);
216: assertEquals("Type of " + elemName, elemClass, entry
217: .getValue());
218: elemMap.remove(elemName);
219: }
220: assertTrue("Found all", elemMap.isEmpty());
221:
222: // Check it's a read-only map.
223: try {
224: actualMap.clear();
225: } catch (UnsupportedOperationException e) {
226: }
227: }
228:
229: public void testGetElementMethod() {
230: assertElemMethod("six", "createSix", String.class, null);
231: assertElemMethod("thirteen", "addThirteen", null,
232: StringBuffer.class);
233: assertElemMethod("fourteen", "addFourteen", null,
234: StringBuffer.class);
235: assertElemMethod("fifteen", "createFifteen",
236: StringBuffer.class, null);
237: }
238:
239: private void assertElemMethod(String elemName, String methodName,
240: Class returnType, Class methodArg) {
241: Method m = ih.getElementMethod(elemName);
242: assertEquals("Method name", methodName, m.getName());
243: Class expectedReturnType = (returnType == null) ? Void.TYPE
244: : returnType;
245: assertEquals("Return type", expectedReturnType, m
246: .getReturnType());
247: Class[] args = m.getParameterTypes();
248: if (methodArg != null) {
249: assertEquals("Arg Count", 1, args.length);
250: assertEquals("Arg Type", methodArg, args[0]);
251: } else {
252: assertEquals("Arg Count", 0, args.length);
253: }
254: }
255:
256: public Object createTwo(String s) {
257: return null;
258: }
259:
260: public void createThree() {
261: }
262:
263: public Object[] createFour() {
264: return null;
265: }
266:
267: public int createFive() {
268: return 0;
269: }
270:
271: public String createSix() {
272: return "test";
273: }
274:
275: public StringBuffer createFifteen() {
276: throw new NullPointerException();
277: }
278:
279: public void addSeven(String s, String s2) {
280: }
281:
282: public void addEight() {
283: }
284:
285: public String addNine(String s) {
286: return null;
287: }
288:
289: public void addTen(String[] s) {
290: }
291:
292: public void addEleven(int i) {
293: }
294:
295: public void addTwelve(Class c) {
296: }
297:
298: public void addThirteen(StringBuffer sb) {
299: sb.append("test");
300: }
301:
302: public void addFourteen(StringBuffer s) {
303: throw new NullPointerException();
304: }
305:
306: public void testAttributeSetters() throws BuildException {
307: try {
308: ih.setAttribute(p, this , "one", "test");
309: fail("setOne doesn't exist");
310: } catch (BuildException be) {
311: }
312: try {
313: ih.setAttribute(p, this , "two", "test");
314: fail("setTwo returns non void");
315: } catch (BuildException be) {
316: }
317: try {
318: ih.setAttribute(p, this , "three", "test");
319: fail("setThree takes no args");
320: } catch (BuildException be) {
321: }
322: try {
323: ih.setAttribute(p, this , "four", "test");
324: fail("setFour takes two args");
325: } catch (BuildException be) {
326: }
327: try {
328: ih.setAttribute(p, this , "five", "test");
329: fail("setFive takes array arg");
330: } catch (BuildException be) {
331: }
332: try {
333: ih.setAttribute(p, this , "six", "test");
334: fail("Project doesn't have a String constructor");
335: } catch (BuildException be) {
336: }
337: ih.setAttribute(p, this , "seven", "2");
338: try {
339: ih.setAttribute(p, this , "seven", "3");
340: fail("2 shouldn't be equals to three");
341: } catch (BuildException be) {
342: assertTrue(be.getException() instanceof AssertionFailedError);
343: }
344: ih.setAttribute(p, this , "eight", "2");
345: try {
346: ih.setAttribute(p, this , "eight", "3");
347: fail("2 shouldn't be equals to three - as int");
348: } catch (BuildException be) {
349: assertTrue(be.getException() instanceof AssertionFailedError);
350: }
351: ih.setAttribute(p, this , "nine", "2");
352: try {
353: ih.setAttribute(p, this , "nine", "3");
354: fail("2 shouldn't be equals to three - as Integer");
355: } catch (BuildException be) {
356: assertTrue(be.getException() instanceof AssertionFailedError);
357: }
358: ih.setAttribute(p, this , "ten", "2");
359: try {
360: ih.setAttribute(p, this , "ten", "3");
361: fail(projectBasedir + "2 shouldn't be equals to "
362: + projectBasedir + "3");
363: } catch (BuildException be) {
364: assertTrue(be.getException() instanceof AssertionFailedError);
365: }
366: ih.setAttribute(p, this , "eleven", "2");
367: try {
368: ih.setAttribute(p, this , "eleven", "on");
369: fail("on shouldn't be false");
370: } catch (BuildException be) {
371: assertTrue(be.getException() instanceof AssertionFailedError);
372: }
373: ih.setAttribute(p, this , "twelve", "2");
374: try {
375: ih.setAttribute(p, this , "twelve", "on");
376: fail("on shouldn't be false");
377: } catch (BuildException be) {
378: assertTrue(be.getException() instanceof AssertionFailedError);
379: }
380: ih.setAttribute(p, this , "thirteen",
381: "org.apache.tools.ant.Project");
382: try {
383: ih.setAttribute(p, this , "thirteen",
384: "org.apache.tools.ant.ProjectHelper");
385: fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
386: } catch (BuildException be) {
387: assertTrue(be.getException() instanceof AssertionFailedError);
388: }
389: try {
390: ih.setAttribute(p, this , "thirteen",
391: "org.apache.tools.ant.Project2");
392: fail("org.apache.tools.ant.Project2 doesn't exist");
393: } catch (BuildException be) {
394: assertTrue(be.getException() instanceof ClassNotFoundException);
395: }
396: ih.setAttribute(p, this , "fourteen", "2");
397: try {
398: ih.setAttribute(p, this , "fourteen", "on");
399: fail("2 shouldn't be equals to three - as StringBuffer");
400: } catch (BuildException be) {
401: assertTrue(be.getException() instanceof AssertionFailedError);
402: }
403: ih.setAttribute(p, this , "fifteen", "abcd");
404: try {
405: ih.setAttribute(p, this , "fifteen", "on");
406: fail("o shouldn't be equal to a");
407: } catch (BuildException be) {
408: assertTrue(be.getException() instanceof AssertionFailedError);
409: }
410: ih.setAttribute(p, this , "sixteen", "abcd");
411: try {
412: ih.setAttribute(p, this , "sixteen", "on");
413: fail("o shouldn't be equal to a");
414: } catch (BuildException be) {
415: assertTrue(be.getException() instanceof AssertionFailedError);
416: }
417: ih.setAttribute(p, this , "seventeen", "17");
418: try {
419: ih.setAttribute(p, this , "seventeen", "3");
420: fail("17 shouldn't be equals to three");
421: } catch (BuildException be) {
422: assertTrue(be.getException() instanceof AssertionFailedError);
423: }
424: ih.setAttribute(p, this , "eightteen", "18");
425: try {
426: ih.setAttribute(p, this , "eightteen", "3");
427: fail("18 shouldn't be equals to three");
428: } catch (BuildException be) {
429: assertTrue(be.getException() instanceof AssertionFailedError);
430: }
431: ih.setAttribute(p, this , "nineteen", "19");
432: try {
433: ih.setAttribute(p, this , "nineteen", "3");
434: fail("19 shouldn't be equals to three");
435: } catch (BuildException be) {
436: assertTrue(be.getException() instanceof AssertionFailedError);
437: }
438: }
439:
440: private Map getExpectedAttributes() {
441: Map attrMap = new Hashtable();
442: attrMap.put("seven", String.class);
443: attrMap.put("eight", Integer.TYPE);
444: attrMap.put("nine", Integer.class);
445: attrMap.put("ten", File.class);
446: attrMap.put("eleven", Boolean.TYPE);
447: attrMap.put("twelve", Boolean.class);
448: attrMap.put("thirteen", Class.class);
449: attrMap.put("fourteen", StringBuffer.class);
450: attrMap.put("fifteen", Character.TYPE);
451: attrMap.put("sixteen", Character.class);
452: attrMap.put("seventeen", Byte.TYPE);
453: attrMap.put("eightteen", Short.TYPE);
454: attrMap.put("nineteen", Double.TYPE);
455:
456: /*
457: * JUnit 3.7 adds a getName method to TestCase - so we now
458: * have a name attribute in IntrospectionHelperTest if we run
459: * under JUnit 3.7 but not in earlier versions.
460: *
461: * Simply add it here and remove it after the tests.
462: */
463: attrMap.put("name", String.class);
464:
465: return attrMap;
466: }
467:
468: public void testGetAttributes() {
469: Map attrMap = getExpectedAttributes();
470: Enumeration e = ih.getAttributes();
471: while (e.hasMoreElements()) {
472: String name = (String) e.nextElement();
473: Class expect = (Class) attrMap.get(name);
474: assertNotNull("Support for " + name
475: + " in IntrospectionHelperTest?", expect);
476: assertEquals("Type of " + name, expect, ih
477: .getAttributeType(name));
478: attrMap.remove(name);
479: }
480: attrMap.remove("name");
481: assertTrue("Found all", attrMap.isEmpty());
482: }
483:
484: public void testGetAttributeMap() {
485: Map attrMap = getExpectedAttributes();
486: Map actualMap = ih.getAttributeMap();
487: for (Iterator i = actualMap.entrySet().iterator(); i.hasNext();) {
488: Map.Entry entry = (Map.Entry) i.next();
489: String attrName = (String) entry.getKey();
490: Class attrClass = (Class) attrMap.get(attrName);
491: assertNotNull("Support for " + attrName
492: + " in IntrospectionHelperTest?", attrClass);
493: assertEquals("Type of " + attrName, attrClass, entry
494: .getValue());
495: attrMap.remove(attrName);
496: }
497: attrMap.remove("name");
498: assertTrue("Found all", attrMap.isEmpty());
499:
500: // Check it's a read-only map.
501: try {
502: actualMap.clear();
503: } catch (UnsupportedOperationException e) {
504: }
505: }
506:
507: public void testGetAttributeMethod() {
508: assertAttrMethod("seven", "setSeven", String.class, "2", "3");
509: assertAttrMethod("eight", "setEight", Integer.TYPE,
510: new Integer(2), new Integer(3));
511: assertAttrMethod("nine", "setNine", Integer.class, new Integer(
512: 2), new Integer(3));
513: assertAttrMethod("ten", "setTen", File.class, new File(
514: projectBasedir + 2), new File("toto"));
515: assertAttrMethod("eleven", "setEleven", Boolean.TYPE,
516: Boolean.FALSE, Boolean.TRUE);
517: assertAttrMethod("twelve", "setTwelve", Boolean.class,
518: Boolean.FALSE, Boolean.TRUE);
519: assertAttrMethod("thirteen", "setThirteen", Class.class,
520: Project.class, Map.class);
521: assertAttrMethod("fourteen", "setFourteen", StringBuffer.class,
522: new StringBuffer("2"), new StringBuffer("3"));
523: assertAttrMethod("fifteen", "setFifteen", Character.TYPE,
524: new Character('a'), new Character('b'));
525: assertAttrMethod("sixteen", "setSixteen", Character.class,
526: new Character('a'), new Character('b'));
527: assertAttrMethod("seventeen", "setSeventeen", Byte.TYPE,
528: new Byte((byte) 17), new Byte((byte) 10));
529: assertAttrMethod("eightteen", "setEightteen", Short.TYPE,
530: new Short((short) 18), new Short((short) 10));
531: assertAttrMethod("nineteen", "setNineteen", Double.TYPE,
532: new Double(19), new Double((short) 10));
533:
534: try {
535: assertAttrMethod("onehundred", null, null, null, null);
536: fail("Should have raised a BuildException!");
537: } catch (BuildException e) {
538: }
539: }
540:
541: private void assertAttrMethod(String attrName, String methodName,
542: Class methodArg, Object arg, Object badArg) {
543: Method m = ih.getAttributeMethod(attrName);
544: assertMethod(m, methodName, methodArg, arg, badArg);
545: }
546:
547: public int setTwo(String s) {
548: return 0;
549: }
550:
551: public void setThree() {
552: }
553:
554: public void setFour(String s1, String s2) {
555: }
556:
557: public void setFive(String[] s) {
558: }
559:
560: public void setSix(Project p) {
561: }
562:
563: public void setSeven(String s) {
564: assertEquals("2", s);
565: }
566:
567: public void setEight(int i) {
568: assertEquals(2, i);
569: }
570:
571: public void setNine(Integer i) {
572: assertEquals(2, i.intValue());
573: }
574:
575: public void setTen(File f) {
576: String path = f.getAbsolutePath();
577: if (Os.isFamily("unix") || Os.isFamily("openvms")) {
578: assertEquals(projectBasedir + "2", path);
579: } else if (Os.isFamily("netware")) {
580: assertEquals(projectBasedir + "2", path
581: .toLowerCase(Locale.US));
582: } else {
583: assertEquals(":" + projectBasedir + "2", path.toLowerCase(
584: Locale.US).substring(1));
585: }
586: }
587:
588: public void setEleven(boolean b) {
589: assertTrue(!b);
590: }
591:
592: public void setTwelve(Boolean b) {
593: assertTrue(!b.booleanValue());
594: }
595:
596: public void setThirteen(Class c) {
597: assertEquals(Project.class, c);
598: }
599:
600: public void setFourteen(StringBuffer sb) {
601: assertEquals("2", sb.toString());
602: }
603:
604: public void setFifteen(char c) {
605: assertEquals(c, 'a');
606: }
607:
608: public void setSixteen(Character c) {
609: assertEquals(c.charValue(), 'a');
610: }
611:
612: public void setSeventeen(byte b) {
613: assertEquals(17, b);
614: }
615:
616: public void setEightteen(short s) {
617: assertEquals(18, s);
618: }
619:
620: public void setNineteen(double d) {
621: double diff = d - 19;
622: assertTrue("Expected 19, received " + d, diff > -1e-6
623: && diff < 1e-6);
624: }
625:
626: public void testGetExtensionPoints() {
627: List extensions = ih.getExtensionPoints();
628: final int adders = 2;
629: assertEquals("extension count", adders, extensions.size());
630:
631: // this original test assumed something about the order of
632: // add(Number) and addConfigured(Map) returned by reflection.
633: // Unfortunately the assumption doesn't hold for all VMs
634: // (failed on MacOS X using JDK 1.4.2_05) and the possible
635: // combinatorics are too hard to check. We really only want
636: // to ensure that the more derived Hashtable can be found
637: // before Map.
638: // assertExtMethod(extensions.get(0), "add", Number.class,
639: // new Integer(2), new Integer(3));
640:
641: // addConfigured(Hashtable) should come before addConfigured(Map)
642: assertExtMethod(extensions.get(adders - 2), "addConfigured",
643: Hashtable.class, makeTable("key", "value"), makeTable(
644: "1", "2"));
645:
646: assertExtMethod(extensions.get(adders - 1), "addConfigured",
647: Map.class, new HashMap(), makeTable("1", "2"));
648: }
649:
650: private void assertExtMethod(Object mo, String methodName,
651: Class methodArg, Object arg, Object badArg) {
652: assertMethod((Method) mo, methodName, methodArg, arg, badArg);
653: }
654:
655: private void assertMethod(Method m, String methodName,
656: Class methodArg, Object arg, Object badArg) {
657: assertEquals("Method name", methodName, m.getName());
658: assertEquals("Return type", Void.TYPE, m.getReturnType());
659: Class[] args = m.getParameterTypes();
660: assertEquals("Arg Count", 1, args.length);
661: assertEquals("Arg Type", methodArg, args[0]);
662:
663: try {
664: m.invoke(this , new Object[] { arg });
665: } catch (IllegalAccessException e) {
666: throw new BuildException(e);
667: } catch (InvocationTargetException e) {
668: throw new BuildException(e);
669: }
670:
671: try {
672: m.invoke(this , new Object[] { badArg });
673: fail("Should have raised an assertion exception");
674: } catch (IllegalAccessException e) {
675: throw new BuildException(e);
676: } catch (InvocationTargetException e) {
677: Throwable t = e.getTargetException();
678: assertTrue(t instanceof junit.framework.AssertionFailedError);
679: }
680: }
681:
682: public List add(List l) {
683: // INVALID extension point
684: return null;
685: }
686:
687: // see comments in testGetExtensionPoints
688: // public void add(Number n) {
689: // // Valid extension point
690: // assertEquals(2, n.intValue());
691: // }
692:
693: public void add(List l, int i) {
694: // INVALID extension point
695: }
696:
697: public void addConfigured(Map m) {
698: // Valid extension point
699: assertTrue(m.size() == 0);
700: }
701:
702: public void addConfigured(Hashtable h) {
703: // Valid extension point, more derived than Map above, but *after* it!
704: assertEquals(makeTable("key", "value"), h);
705: }
706:
707: private Hashtable makeTable(Object key, Object value) {
708: Hashtable table = new Hashtable();
709: table.put(key, value);
710: return table;
711: }
712:
713: } // IntrospectionHelperTest
|