1 6 7 package org.netbeans.test.editor.indentation.programmatic; 8 9 import java.util.Vector ; 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import javax.xml.parsers.DocumentBuilderFactory ; 13 import org.w3c.dom.Document ; 14 import java.util.Iterator ; 15 import javax.xml.parsers.DocumentBuilder ; 16 import org.xml.sax.EntityResolver ; 17 import java.io.InputStream ; 18 import org.xml.sax.InputSource ; 19 import java.io.File ; 20 import java.io.Writer ; 21 import java.io.FileWriter ; 22 23 27 public class IndentCoreGenerator { 28 29 30 public IndentCoreGenerator() { 31 } 32 33 36 public static void main(String [] args) throws Exception { 37 String generatorLocation = "/home/lahvac/31/tests/editor/test/qa-functional/src/org/netbeans/test/editor/indentation/programmatic"; 38 cases = new Vector (); 39 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 40 41 factory.setValidating(true); 42 43 DocumentBuilder builder = factory.newDocumentBuilder(); 44 45 builder.setEntityResolver(new EntityResolver () { 46 public InputSource resolveEntity(String a, String b) { 47 return new InputSource (IndentCoreGenerator.class.getResourceAsStream("definition.dtd")); 48 } 49 }); 50 51 Document doc = builder.parse(IndentCoreGenerator.class.getResourceAsStream("definition.xml"), generatorLocation); 52 DefinitionScanner scanner = new DefinitionScanner(doc); 53 54 scanner.visitDocument(); 55 56 Writer output = new FileWriter (new File (generatorLocation, "IndentCorePerformer.java")); 57 58 output.write("package " + IndentCoreGenerator.class.getPackage().getName() + ";\n"); 59 output.write("import java.io.*;\n"); 60 output.write("import java.util.*;\n"); 61 output.write("import org.netbeans.junit.NbTestCase;\n"); 62 output.write("import org.netbeans.junit.NbTestSuite;\n"); 63 output.write("import java.io.File;\n"); 64 output.write("public class IndentCorePerformer extends NbTestCase {\n"); 65 output.write(" public IndentCorePerformer(String testCase) {\n"); 66 output.write(" super(testCase);\n"); 67 output.write(" }\n"); 68 output.write(" public void tearDown() throws Exception {\n"); 69 output.write(" assertFile(\"Output does not match golden file.\", getGoldenFile(), new File(getWorkDir(), this.getName() + \".ref\"), null, new org.netbeans.test.editor.LineDiff(false));\n"); 70 output.write(" }\n"); 71 72 Iterator iterator = cases.iterator(); 73 74 while (iterator.hasNext()) { 75 TestCase testCase = (TestCase) iterator.next(); 76 77 output.write(testCase.generate()); 78 } 79 output.write("}\n"); 80 output.close(); 81 } 82 83 private static Vector cases; 84 85 public static void addTestCase(TestCase testCase) { 86 cases.add(testCase); 87 } 88 89 public static class TestCase { 90 private String name; 91 private String textLocation; 92 private String textMIMEType; 93 private Map indentProperties; 94 private boolean willFail; 95 private String failReason; 96 97 public TestCase() { 98 failReason = ""; 99 name = ""; 100 textLocation = ""; 101 textMIMEType = ""; 102 indentProperties = new HashMap (); 103 willFail = false; 104 } 105 106 public Map getIndentProperties() { 107 return indentProperties; 108 } 109 110 public void addIndentProperty(String name, String value) { 111 indentProperties.put(name, value); 112 } 113 114 public String generate() { 115 StringBuffer result = new StringBuffer (); 116 117 result.append(" public void test"); 118 result.append(getName()); 119 result.append("() throws Exception {\n"); 120 result.append(" PrintWriter log = null;\n"); 121 result.append(" PrintWriter ref = null;\n"); 122 result.append(" try {\n"); 123 result.append(" log = new PrintWriter(getLog());\n"); 124 result.append(" ref = new PrintWriter(getRef());\n"); 125 result.append(" Map indentationProperties = new HashMap();\n"); 126 for (Iterator iterator = getIndentProperties().keySet().iterator(); 127 iterator.hasNext(); 128 ) { 129 String key = (String ) iterator.next(); 130 131 result.append(" indentationProperties.put(\"" + key + "\", \"" + getIndentProperties().get(key).toString() + "\");\n"); 132 } 133 result.append(" new IndentCore().run(log, ref, \"" + getTextLocation() + "\", \"" + getTextMIMEType() + "\", indentationProperties);\n"); 134 result.append(" } finally {\n"); 135 result.append(" log.flush();\n"); 136 result.append(" ref.flush();\n"); 137 result.append(" }\n"); 138 result.append(" }\n"); 139 140 return result.toString(); 141 } 142 143 146 public java.lang.String getName() { 147 return name; 148 } 149 150 153 public void setName(java.lang.String name) { 154 this.name = name; 155 } 156 157 160 public java.lang.String getFailReason() { 161 return failReason; 162 } 163 164 167 public void setFailReason(java.lang.String failReason) { 168 this.failReason = failReason; 169 } 170 171 174 public java.lang.String getTextLocation() { 175 return textLocation; 176 } 177 178 181 public void setTextLocation(java.lang.String textLocation) { 182 this.textLocation = textLocation; 183 } 184 185 188 public java.lang.String getTextMIMEType() { 189 return textMIMEType; 190 } 191 192 195 public void setTextMIMEType(java.lang.String textMIMEType) { 196 this.textMIMEType = textMIMEType; 197 } 198 199 202 public boolean isWillFail() { 203 return willFail; 204 } 205 206 209 public void setWillFail(boolean willFail) { 210 this.willFail = willFail; 211 } 212 213 } 214 215 } 216 | Popular Tags |