001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax;
042:
043: import java.io.FileWriter;
044: import java.io.PrintWriter;
045: import java.lang.reflect.Constructor;
046: import java.lang.reflect.Modifier;
047: import java.net.URL;
048: import java.util.HashSet;
049:
050: public class FactoryTestGenerator {
051: public static String[] CLASS_NAMES = new String[] {
052: "TreeAttlistDecl", "TreeAttribute", "TreeCDATASection",
053: "TreeCharacterReference", "TreeComment",
054: "TreeConditionalSection", "TreeDTD", "TreeDocument",
055: "TreeDocumentFragment", "TreeDocumentType", "TreeElement",
056: "TreeElementDecl", "TreeEntityDecl",
057: "TreeGeneralEntityReference", "TreeNotationDecl",
058: "TreeParameterEntityReference",
059: "TreeProcessingInstruction", "TreeText",
060: // Abstract classes
061: /* "AbstractTreeDTD",
062: "AbstractTreeDocument",
063: "TreeChild",
064: "TreeCharacterData",
065: "TreeData",
066: "TreeNode",
067: "TreeObject",
068: "TreeParentNode",
069: "TreeNodeDecl",
070: "TreeEntityReference",*/
071: };
072:
073: /** Creates a new instance of ElementTestGenerator */
074: public FactoryTestGenerator() {
075: }
076:
077: /**
078: * @param args the command line arguments
079: */
080: public static void main(String args[]) throws Exception {
081: final Class this Cls = FactoryTestGenerator.class;
082: //URL url = thisCls.getProtectionDomain().getCodeSource().getLocation();
083: URL url = this Cls.getResource(".");
084: PrintWriter out = new PrintWriter(new FileWriter(url.getPath()
085: + "/AbstractFactoryTest.java"));
086: //System.out.println(STATIC_CODE);
087:
088: //if (true != false) { System.exit(0); }
089:
090: out.println(CLASS_HEADER);
091:
092: for (int i = 0; i < CLASS_NAMES.length; i++) {
093: String clsName = CLASS_NAMES[i];
094: Class cls = Class.forName("org.netbeans.tax." + clsName);
095: Constructor[] constructs = cls.getDeclaredConstructors();
096:
097: for (int j = 0; j < constructs.length; j++) {
098: Constructor con = constructs[j];
099: if (Modifier.isPublic(con.getModifiers())
100: && con.getParameterTypes().length > 0) {
101: String methods = createMethods(clsName, con);
102: out
103: .println("\n //--------------------------------------------------------------------------\n");
104: out.println(methods);
105: }
106: }
107: }
108: out.println(STATIC_CODE);
109: out.println("}");
110: out.close();
111:
112: for (int i = 0; i < CLASS_NAMES.length; i++) {
113: String clsName = CLASS_NAMES[i];
114: String sname = clsName.substring(4); // Cut "Tree"
115:
116: System.out.println("\n" + " public void test" + sname
117: + "() throws Exception {\n" + " create"
118: + sname + "(\"\", \"\");\n" + " create"
119: + sname + "(\"\", \"\");\n" + " create"
120: + sname + "(\"\", \"\");\n" + " create"
121: + sname + "Invalid(null);\n" + " create"
122: + sname + "Invalid(null);\n" + " }\n");
123: }
124: }
125:
126: private static String createMethods(String clsName,
127: Constructor constructor) {
128: String sname = clsName.substring(4); // Cut "Tree"
129: Class[] params = constructor.getParameterTypes();
130: HashSet set = new HashSet();
131: String header = "";
132: String atrs = "";
133: String res = "";
134:
135: // create header declaration an attr. list
136: for (int i = 0; i < params.length; i++) {
137: String pType = params[i].getName();
138: String pName;
139:
140: if (params[i].isPrimitive()) {
141: pName = pType + "_val";
142: } else {
143: pName = pType.substring(pType.lastIndexOf('.') + 1)
144: .toLowerCase();
145: }
146:
147: if (set.contains(pName)) {
148: pName += i;
149: }
150: set.add(pName);
151:
152: header += pType + " " + pName;
153: atrs += pName;
154: if (i < params.length - 1) {
155: header += ", ";
156: atrs += ", ";
157: }
158: }
159:
160: // create() method
161: res += "\n" + " static Tree" + sname + " create" + sname
162: + "(" + header + ", String view) throws Exception {\n"
163: + " Tree" + sname + " node = new Tree" + sname
164: + "(" + atrs + ");\n" + " \n"
165: + " assertEquals(node, view);\n"
166: + " cloneNodeTest(node, view);\n"
167: + " return node;\n" + " }\n";
168:
169: Class[] exs = constructor.getExceptionTypes();
170:
171: // if constructor throw InvalidArgumentException create method createInvalid()
172: for (int i = 0; i < exs.length; i++) {
173: if (InvalidArgumentException.class.isAssignableFrom(exs[i])) {
174: res += "\n"
175: + " static void create"
176: + sname
177: + "Invalid("
178: + header
179: + ") throws Exception {\n"
180: + " try {\n"
181: + " new Tree"
182: + sname
183: + "("
184: + atrs
185: + ");\n"
186: + " // Fail if previous line doesn't trhow exception.\n"
187: + " fail(NOT_EXCEPTION + \"from: new Tree"
188: + sname
189: + "("
190: + atrs
191: + ")\");\n"
192: + " } catch (InvalidArgumentException e) {\n"
193: + " // OK\n" + " }\n"
194: + " }\n";
195: break;
196: }
197: }
198: return res;
199: }
200:
201: private static String CLASS_HEADER = ""
202: + "package org.netbeans.modules.xml.tax.test.api.element;\n\n"
203:
204: + "import org.netbeans.tax.*;\n"
205: + "import org.netbeans.junit.NbTestCase;\n"
206: + "import org.openide.util.Utilities;\n"
207:
208: + "import org.netbeans.modules.xml.core.test.api.TestUtil;\n\n"
209:
210: + "abstract class AbstractFactoryTest extends NbTestCase {\n"
211: + " final private static String NOT_EXCEPTION = \"The InvalidArgumetException wasn't throwed \";\n\n"
212:
213: + " public AbstractFactoryTest(String testName) {\n"
214: + " super(testName);\n" + " }\n";
215:
216: private static String STATIC_CODE = "\n"
217: + " private static void cloneNodeTest(TreeParentNode node, String view) throws Exception {\n"
218: + " TreeParentNode clone = (TreeParentNode) node.clone(true);\n"
219: + " assertNotEquals(clone, node);\n"
220: + " assertEquals(clone, view);\n"
221: + " \n"
222: + " clone = (TreeParentNode) node.clone(false);\n"
223: + " assertNotEquals(clone, node);\n"
224: + " assertEquals(clone, view);\n"
225: + " }\n"
226: + " \n"
227: + " private static void cloneNodeTest(TreeNode node, String view) throws Exception {\n"
228: + " TreeNode clone = (TreeNode) node.clone();\n"
229: + " assertNotEquals(clone, node);\n"
230: + " assertEquals(clone, view);\n"
231: + " }\n"
232: + " \n"
233: + " private static void assertNotEquals(Object orig, Object clone) {\n"
234: + " if (orig == clone) {\n"
235: + " fail(\"Invalid clone.\");\n"
236: + " }\n"
237: + " }\n"
238: + " \n"
239: + " private static void assertEquals(TreeNode node, String view) throws TreeException{\n"
240: + " String str = Utilities.replaceString(TestUtil.nodeToString(node), \"\\n\", \"\");\n"
241: + " if (!!! str.equals(view)) {\n"
242: + " fail(\"Invalid node view \\n is : \\\"\" + str + \"\\\"\\n should be: \\\"\" + view + \"\\\"\");\n"
243: + " }\n" + " }\n";
244:
245: }
|