1 19 package org.netbeans.tax; 20 21 import java.io.FileWriter ; 22 import java.io.PrintWriter ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.Modifier ; 25 import java.net.URL ; 26 import java.util.HashSet ; 27 28 public class FactoryTestGenerator { 29 public static String [] CLASS_NAMES = new String [] { 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 59 }; 60 61 62 public FactoryTestGenerator() { 63 } 64 65 68 public static void main(String args[]) throws Exception { 69 final Class thisCls = FactoryTestGenerator.class; 70 URL url = thisCls.getResource("."); 72 PrintWriter out = new PrintWriter (new FileWriter (url.getPath() + "/AbstractFactoryTest.java")); 73 75 77 78 out.println(CLASS_HEADER); 79 80 for (int i = 0; i < CLASS_NAMES.length; i++) { 81 String clsName = CLASS_NAMES[i]; 82 Class cls = Class.forName("org.netbeans.tax." + clsName); 83 Constructor [] constructs = cls.getDeclaredConstructors(); 84 85 for (int j = 0 ; j < constructs.length; j++) { 86 Constructor con = constructs[j]; 87 if (Modifier.isPublic(con.getModifiers()) && con.getParameterTypes().length > 0) { 88 String 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 clsName = CLASS_NAMES[i]; 100 String sname = clsName.substring(4); 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 createMethods(String clsName, Constructor constructor) { 114 String sname = clsName.substring(4); Class [] params = constructor.getParameterTypes(); 116 HashSet set = new HashSet (); 117 String header = ""; 118 String atrs = ""; 119 String res = ""; 120 121 for (int i = 0; i < params.length; i++) { 123 String pType = params[i].getName(); 124 String 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 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 [] exs = constructor.getExceptionTypes(); 156 157 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 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 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 |