KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > api > IntrospectedInfoTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.api;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.junit.MockServices;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.modules.InstalledFileLocator;
29
30 // XXX testSubElements
31
// XXX testSpecials
32

33 /**
34  * Test functionality of IntrospectedInfo.
35  * @author Jesse Glick
36  */

37 public class IntrospectedInfoTest extends NbTestCase {
38
39     public IntrospectedInfoTest(String JavaDoc name) {
40         super(name);
41     }
42
43     private IntrospectedInfo ii;
44
45     @Override JavaDoc
46     protected void setUp() throws Exception JavaDoc {
47         MockServices.setServices(IFL.class);
48         ii = IntrospectedInfo.getDefaults();
49     }
50     
51     public void testBasicDefinitions() throws Exception JavaDoc {
52         Map JavaDoc<String JavaDoc,String JavaDoc> tasks = ii.getDefs("task");
53         assertEquals("binding for javac", "org.apache.tools.ant.taskdefs.Javac", tasks.get("javac"));
54         assertEquals("binding for sql", "org.apache.tools.ant.taskdefs.SQLExec", tasks.get("sql"));
55         Map JavaDoc<String JavaDoc,String JavaDoc> types = ii.getDefs("type");
56         assertEquals("binding for path", "org.apache.tools.ant.types.FileSet", types.get("fileset"));
57         assertEquals("binding for path", "org.apache.tools.ant.types.Path", types.get("path"));
58     }
59     
60     public void testBasicAttributes() throws Exception JavaDoc {
61         Map JavaDoc<String JavaDoc,String JavaDoc> attrs = ii.getAttributes("org.apache.tools.ant.taskdefs.Javac");
62         assertEquals("right type for destdir", "java.io.File", attrs.get("destdir"));
63         // XXX sometimes this line fails - when run from inside the IDE, but not on the command line!
64
// (related to #50160?)
65
// Debugger shows that IntrospectionHelper.createAttributeSetter is calling
66
// Path.class.getConstructor(new Class[] {Project.class, String.class})
67
// and this is (for some reason) throwing a NoSuchMethodException.
68
// Seems to be that Path.class has a matching constructor but with a different
69
// version of Project.class - some sort of class loader snafu perhaps.
70
// The code sources appear correct.
71
/*
72         assertEquals("right type for srcdir", "org.apache.tools.ant.types.Path", attrs.get("srcdir"));
73          */

74         /* This however works:
75         ClassLoader l = org.apache.tools.ant.module.bridge.AntBridge.getMainClassLoader();
76         Class prj = l.loadClass("org.apache.tools.ant.Project");
77         Class path = l.loadClass("org.apache.tools.ant.types.Path");
78         System.out.println("constructor: " + path.getConstructor(new Class[] {prj, String.class}));
79          */

80     }
81     
82     public void testEnumeratedAttributes() throws Exception JavaDoc {
83         ii.register("enumtask", EnumTask.class, "task");
84         String JavaDoc k1 = EnumTask.class.getName();
85         assertEquals(k1, ii.getDefs("task").get("enumtask"));
86         String JavaDoc k2 = EnumTask.E.class.getName();
87         assertEquals(Collections.singletonMap("attr", k2), ii.getAttributes(k1));
88         assertEquals("[chocolate, vanilla, strawberry]", Arrays.toString(ii.getTags(k2)));
89     }
90     
91     public static class EnumTask {
92         public enum E {chocolate, vanilla, strawberry}
93         public void setAttr(E e) {}
94     }
95     
96     public static final class IFL extends InstalledFileLocator {
97         public IFL() {
98             //System.err.println("ant.home=" + System.getProperty("test.ant.home"));
99
}
100         @Override JavaDoc
101         public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc codeNameBase, boolean localized) {
102             if (relativePath.equals("ant/nblib/bridge.jar")) {
103                 String JavaDoc path = System.getProperty("test.bridge.jar");
104                 assertNotNull("must set test.bridge.jar", path);
105                 return new File JavaDoc(path);
106             } else if (relativePath.equals("ant")) {
107                 String JavaDoc path = System.getProperty("test.ant.home");
108                 assertNotNull("must set test.ant.home", path);
109                 return new File JavaDoc(path);
110             } else if (relativePath.startsWith("ant/")) {
111                 String JavaDoc path = System.getProperty("test.ant.home");
112                 assertNotNull("must set test.ant.home", path);
113                 return new File JavaDoc(path, relativePath.substring(4).replace('/', File.separatorChar));
114             } else {
115                 return null;
116             }
117         }
118     }
119
120 }
121
Popular Tags