KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > FactoryTestGenerator


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 package org.netbeans.tax;
20
21 import java.io.FileWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.HashSet JavaDoc;
27
28 public class FactoryTestGenerator {
29     public static String JavaDoc[] CLASS_NAMES = new String JavaDoc [] {
30         "TreeAttlistDecl",
31         "TreeAttribute",
32         "TreeCDATASection",
33         "TreeCharacterReference",
34         "TreeComment",
35         "TreeConditionalSection",
36         "TreeDTD",
37         "TreeDocument",
38         "TreeDocumentFragment",
39         "TreeDocumentType",
40         "TreeElement",
41         "TreeElementDecl",
42         "TreeEntityDecl",
43         "TreeGeneralEntityReference",
44         "TreeNotationDecl",
45         "TreeParameterEntityReference",
46         "TreeProcessingInstruction",
47         "TreeText",
48         // Abstract classes
49
/* "AbstractTreeDTD",
50             "AbstractTreeDocument",
51             "TreeChild",
52             "TreeCharacterData",
53             "TreeData",
54             "TreeNode",
55             "TreeObject",
56             "TreeParentNode",
57             "TreeNodeDecl",
58             "TreeEntityReference",*/

59     };
60     
61     /** Creates a new instance of ElementTestGenerator */
62     public FactoryTestGenerator() {
63     }
64     
65     /**
66      * @param args the command line arguments
67      */

68     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
69         final Class JavaDoc thisCls = FactoryTestGenerator.class;
70         //URL url = thisCls.getProtectionDomain().getCodeSource().getLocation();
71
URL JavaDoc url = thisCls.getResource(".");
72         PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(url.getPath() + "/AbstractFactoryTest.java"));
73         //System.out.println(STATIC_CODE);
74

75         //if (true != false) { System.exit(0); }
76

77         
78         out.println(CLASS_HEADER);
79         
80         for (int i = 0; i < CLASS_NAMES.length; i++) {
81             String JavaDoc clsName = CLASS_NAMES[i];
82             Class JavaDoc cls = Class.forName("org.netbeans.tax." + clsName);
83             Constructor JavaDoc[] constructs = cls.getDeclaredConstructors();
84             
85             for (int j = 0 ; j < constructs.length; j++) {
86                 Constructor JavaDoc con = constructs[j];
87                 if (Modifier.isPublic(con.getModifiers()) && con.getParameterTypes().length > 0) {
88                     String JavaDoc methods = createMethods(clsName, con);
89                     out.println("\n //--------------------------------------------------------------------------\n");
90                     out.println(methods);
91                 }
92             }
93         }
94         out.println(STATIC_CODE);
95         out.println("}");
96         out.close();
97         
98         for (int i = 0; i < CLASS_NAMES.length; i++) {
99             String JavaDoc clsName = CLASS_NAMES[i];
100             String JavaDoc sname = clsName.substring(4); // Cut "Tree"
101

102             System.out.println("\n"
103             + " public void test" + sname + "() throws Exception {\n"
104             + " create" + sname + "(\"\", \"\");\n"
105             + " create" + sname + "(\"\", \"\");\n"
106             + " create" + sname + "(\"\", \"\");\n"
107             + " create" + sname + "Invalid(null);\n"
108             + " create" + sname + "Invalid(null);\n"
109             + " }\n");
110         }
111     }
112     
113     private static String JavaDoc createMethods(String JavaDoc clsName, Constructor JavaDoc constructor) {
114         String JavaDoc sname = clsName.substring(4); // Cut "Tree"
115
Class JavaDoc[] params = constructor.getParameterTypes();
116         HashSet JavaDoc set = new HashSet JavaDoc();
117         String JavaDoc header = "";
118         String JavaDoc atrs = "";
119         String JavaDoc res = "";
120         
121         // create header declaration an attr. list
122
for (int i = 0; i < params.length; i++) {
123             String JavaDoc pType = params[i].getName();
124             String JavaDoc pName;
125             
126             if (params[i].isPrimitive()) {
127                 pName = pType + "_val";
128             } else {
129                 pName = pType.substring(pType.lastIndexOf('.') + 1).toLowerCase();
130             }
131             
132             if (set.contains(pName)) {
133                 pName += i;
134             }
135             set.add(pName);
136             
137             header += pType + " " + pName;
138             atrs += pName;
139             if (i < params.length - 1) {
140                 header += ", ";
141                 atrs += ", ";
142             }
143         }
144         
145         // create() method
146
res += "\n"
147         + " static Tree" + sname + " create" + sname + "(" + header + ", String view) throws Exception {\n"
148         + " Tree" + sname + " node = new Tree" + sname + "(" + atrs + ");\n"
149         + " \n"
150         + " assertEquals(node, view);\n"
151         + " cloneNodeTest(node, view);\n"
152         + " return node;\n"
153         + " }\n";
154         
155         Class JavaDoc[] exs = constructor.getExceptionTypes();
156         
157         // if constructor throw InvalidArgumentException create method createInvalid()
158
for (int i = 0; i < exs.length; i++) {
159             if (InvalidArgumentException.class.isAssignableFrom(exs[i])) {
160                 res += "\n"
161                 + " static void create" + sname + "Invalid(" + header + ") throws Exception {\n"
162                 + " try {\n"
163                 + " new Tree" + sname + "(" + atrs + ");\n"
164                 + " // Fail if previous line doesn't trhow exception.\n"
165                 + " fail(NOT_EXCEPTION + \"from: new Tree" + sname +"(" + atrs + ")\");\n"
166                 + " } catch (InvalidArgumentException e) {\n"
167                 + " // OK\n"
168                 + " }\n"
169                 + " }\n";
170                 break;
171             }
172         }
173         return res;
174     }
175     
176     private static String JavaDoc CLASS_HEADER = ""
177     + "package org.netbeans.modules.xml.tax.test.api.element;\n\n"
178     
179     + "import org.netbeans.tax.*;\n"
180     + "import org.netbeans.junit.NbTestCase;\n"
181     + "import org.openide.util.Utilities;\n"
182     
183     + "import org.netbeans.modules.xml.core.test.api.TestUtil;\n\n"
184     
185     + "abstract class AbstractFactoryTest extends NbTestCase {\n"
186     + " final private static String NOT_EXCEPTION = \"The InvalidArgumetException wasn't throwed \";\n\n"
187     
188     + " public AbstractFactoryTest(String testName) {\n"
189     + " super(testName);\n"
190     + " }\n";
191     
192     
193     
194     private static String JavaDoc STATIC_CODE = "\n"
195     + " private static void cloneNodeTest(TreeParentNode node, String view) throws Exception {\n"
196     + " TreeParentNode clone = (TreeParentNode) node.clone(true);\n"
197     + " assertNotEquals(clone, node);\n"
198     + " assertEquals(clone, view);\n"
199     + " \n"
200     + " clone = (TreeParentNode) node.clone(false);\n"
201     + " assertNotEquals(clone, node);\n"
202     + " assertEquals(clone, view);\n"
203     + " }\n"
204     + " \n"
205     + " private static void cloneNodeTest(TreeNode node, String view) throws Exception {\n"
206     + " TreeNode clone = (TreeNode) node.clone();\n"
207     + " assertNotEquals(clone, node);\n"
208     + " assertEquals(clone, view);\n"
209     + " }\n"
210     + " \n"
211     + " private static void assertNotEquals(Object orig, Object clone) {\n"
212     + " if (orig == clone) {\n"
213     + " fail(\"Invalid clone.\");\n"
214     + " }\n"
215     + " }\n"
216     + " \n"
217     + " private static void assertEquals(TreeNode node, String view) throws TreeException{\n"
218     + " String str = Utilities.replaceString(TestUtil.nodeToString(node), \"\\n\", \"\");\n"
219     + " if (!!! str.equals(view)) {\n"
220     + " fail(\"Invalid node view \\n is : \\\"\" + str + \"\\\"\\n should be: \\\"\" + view + \"\\\"\");\n"
221     + " }\n"
222     + " }\n";
223     
224 }
225
Popular Tags