1 19 20 import java.io.*; 21 import java.net.URL ; 22 import java.util.*; 23 import junit.framework.*; 24 import org.netbeans.junit.*; 25 26 import org.netbeans.modules.schema2beans.*; 27 import org.netbeans.modules.schema2beansdev.*; 28 import org.netbeans.modules.schema2beansdev.beangraph.*; 29 30 public class TestMain extends NbTestCase { 31 32 public TestMain(java.lang.String testName) { 33 super(testName); 34 } 35 36 public static void main(java.lang.String [] args) { 37 junit.textui.TestRunner.run(suite()); 38 } 39 40 public static Test suite() { 41 TestSuite suite = new NbTestSuite(TestMain.class); 42 43 return suite; 44 } 45 46 public void testPurchaseOrder() throws IOException, Schema2BeansException, InterruptedException { 47 generalTest("TestPurchaseOrder", true, true, true); 48 } 49 50 public void testInvoice() throws IOException, Schema2BeansException, InterruptedException { 51 GenBeans.Config config = new GenBeans.Config(); 52 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 53 config.setAttributesAsProperties(true); 54 config.setGenerateValidate(true); 55 config.setProcessComments(true); 56 config.setProcessDocType(true); 57 BeanGraph bg = new BeanGraph(); 58 SchemaTypeMappingType stm = new SchemaTypeMappingType(); 59 stm.setSchemaTypeNamespace("http://www.w3.org/2001/XMLSchema"); 60 stm.setSchemaTypeName("integer"); 61 stm.setJavaType("int"); 62 bg.addSchemaTypeMapping(stm); 63 config.addReadBeanGraphs(bg); 64 65 generalTest("TestInvoice", true, config); 66 } 67 68 public void testBookXMLSchema() throws IOException, Schema2BeansException, InterruptedException { 69 generalTest("TestBookXMLSchema", true, false, false); 70 } 71 72 public void testBook() throws IOException, Schema2BeansException, InterruptedException { 73 generalTest("TestBook"); 74 } 75 76 public void testDupInternalNames() throws IOException, Schema2BeansException, InterruptedException { 77 generalTest("TestDupInternalNames", true, true, true); 78 } 79 80 public void testEvents() throws IOException, Schema2BeansException, InterruptedException { 81 generalTest("TestEvents"); 82 } 83 84 public void testMerge() throws IOException, Schema2BeansException, InterruptedException { 85 generalTest("TestMerge"); 86 } 87 88 public void testAttr() throws IOException, Schema2BeansException, InterruptedException { 89 generalTest("TestAttr", false, false, true); 90 } 91 92 public void testMdd() throws IOException, Schema2BeansException, InterruptedException { 93 generalTest("TestMdd"); 94 } 95 96 public void testValid() throws IOException, Schema2BeansException, InterruptedException { 97 generalTest("TestValid"); 98 } 99 100 public void testFind() throws IOException, Schema2BeansException, InterruptedException { 101 generalTest("TestFind"); 102 } 103 104 public void testVeto() throws IOException, Schema2BeansException, InterruptedException { 105 generalTest("TestVeto"); 106 } 107 108 public void testContrivedApp() throws IOException, Schema2BeansException, InterruptedException { 109 generalTest("TestContrivedApp"); 110 } 111 112 public void testEncoding() throws IOException, Schema2BeansException, InterruptedException { 113 generalTest("TestEncoding"); 114 } 115 116 public void testExceptions() throws IOException, Schema2BeansException, InterruptedException { 117 generalTest("TestExceptions"); 118 } 119 120 public void testEmpty() throws IOException, Schema2BeansException, InterruptedException { 121 generalTest("TestEmpty"); 122 } 123 124 public void testNamespace() throws IOException, Schema2BeansException, InterruptedException { 125 GenBeans.Config config = new GenBeans.Config(); 126 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 127 config.setAttributesAsProperties(true); 128 config.setRemoveUnreferencedNodes(true); 129 config.setGenerateValidate(true); 130 generalTest("TestNamespace", true, config); 131 } 132 133 public void testExtensionSample() throws IOException, Schema2BeansException, InterruptedException { 134 generalTest("TestExtensionSample", true, true, true); 135 } 136 137 public void testExtension() throws IOException, Schema2BeansException, InterruptedException { 138 GenBeans.Config config = new GenBeans.Config(); 139 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 140 config.setAttributesAsProperties(true); 141 config.setGenerateValidate(true); 142 config.setProcessComments(true); 143 config.setProcessDocType(true); 144 config.setGenerateCommonInterface("CommonBean"); 145 generalTest("TestExtension", true, config); 146 } 147 148 public void testExtension2() throws IOException, Schema2BeansException, InterruptedException { 149 GenBeans.Config config = new GenBeans.Config(); 150 config.buyPremium(); 151 config.setRespectExtension(true); 152 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 153 config.setAttributesAsProperties(true); 154 config.setGenerateCommonInterface("CommonBean"); 155 generalTest("TestExtension2", true, config); 156 } 157 158 public void testWebApp() throws IOException, Schema2BeansException, InterruptedException { 159 GenBeans.Config config = new GenBeans.Config(); 160 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 161 config.setAttributesAsProperties(true); 162 config.setGenerateCommonInterface("CommonBean"); 163 config.setRemoveUnreferencedNodes(true); 164 generalTest("TestWebApp", true, config); 165 } 166 167 public void testWebAppDelegator() throws IOException, Schema2BeansException, InterruptedException { 168 GenBeans.Config config = new GenBeans.Config(); 169 config.buyPremium(); 170 config.setGenerateDelegator(true); 171 config.setGenerateHasChanged(true); 172 generalTest("TestWebAppDelegator", true, config); 173 } 174 175 public void testWebAppDelegatorBaseBean() throws IOException, Schema2BeansException, InterruptedException { 176 GenBeans.Config config = new GenBeans.Config(); 177 config.buyPremium(); 178 config.setGenerateDelegator(true); 179 config.setOutputType(GenBeans.Config.OUTPUT_TRADITIONAL_BASEBEAN); 180 generalTest("TestWebAppDelegatorBaseBean", true, config); 181 } 182 183 public void testFinalWebApp() throws IOException, Schema2BeansException, InterruptedException { 184 GenBeans.Config config = new GenBeans.Config(); 185 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 186 config.setAttributesAsProperties(true); 187 config.setGenerateCommonInterface("CommonBean"); 188 config.setGenerateValidate(true); 189 config.setProcessComments(true); 190 config.setProcessDocType(true); 191 config.addReadBeanGraphFiles(new File(dataDir, "TestFinalWebAppBeanGraph.xml")); 192 generalTest("TestFinalWebApp", true, config); 193 } 194 195 public void testBadNames() throws IOException, Schema2BeansException, InterruptedException { 196 GenBeans.Config config = new GenBeans.Config(); 197 config.buyPremium(); 198 config.setGenerateCommonInterface(null); 199 config.setOutputType(GenBeans.Config.OUTPUT_TRADITIONAL_BASEBEAN); 200 generalTest("TestBadNames", true, config); 201 } 202 203 public void testPositions() throws IOException, Schema2BeansException, InterruptedException { 204 GenBeans.Config config = new GenBeans.Config(); 205 config.buyPremium(); 206 config.setKeepElementPositions(true); 207 generalTest("TestPositions", false, config); 208 } 209 210 public void testOr() throws IOException, Schema2BeansException, InterruptedException { 211 GenBeans.Config config = new GenBeans.Config(); 212 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 213 config.setGenerateValidate(true); 214 generalTest("TestOr", false, config); 215 } 216 217 public void testGroupUnbounded() throws IOException, Schema2BeansException, InterruptedException { 218 GenBeans.Config config = new GenBeans.Config(); 219 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 220 config.setAttributesAsProperties(true); 221 config.setGenerateValidate(true); 222 generalTest("TestGroupUnbounded", true, config); 223 } 224 225 public void testApplication1_4() throws IOException, Schema2BeansException, InterruptedException { 226 generalTest("TestApplication1_4", true, true, true); 227 } 228 229 public void testMergeExtendBaseBean() throws IOException, Schema2BeansException, InterruptedException { 230 GenBeans.Config config = new GenBeans.Config(); 231 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 232 config.setAttributesAsProperties(true); 233 config.setGenerateValidate(true); 234 config.setGenerateCommonInterface("CommonBean"); 235 config.setExtendBaseBean(true); 236 config.setUseInterfaces(true); 237 config.setDumpToString(true); 238 generalTest("TestMergeExtendBaseBean", false, config); 239 } 240 241 public void testBeanWrapper() throws IOException, Schema2BeansException, InterruptedException { 242 String testName = "TestBeanWrapper"; 243 try { 244 System.out.println(": Starting "+testName); 245 File workDir = getWorkDir(); 246 System.out.println("workDir="+workDir.toString()); 247 File schemaFile; 248 File beanTreeFile = File.createTempFile("beanTree", "txt"); 249 InputStream dtdIn; 250 InputStream mddIn; 251 252 GenBeans.Config config = new GenBeans.Config(); 253 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 254 config.setAttributesAsProperties(true); 255 config.setGenerateCommonInterface("CommonBean"); 256 config.setGenerateInterfaces(true); 257 config.setGeneratePropertyEvents(true); 258 config.setAuto(true); 259 config.setStandalone(false); 260 schemaFile = new File(dataDir, "simple.xsd"); 261 dtdIn = new FileInputStream(schemaFile); 262 config.setFileIn(dtdIn); 263 config.setInputURI(schemaFile.toString()); 264 config.setMddIn(null); 265 config.setSchemaType(GenBeans.Config.XML_SCHEMA); 266 config.setRootDir(new File(workDir.toString())); 267 String simpleBeanGraph = dataDir + File.pathSeparator + "simpleBeanGraph.xml"; 268 config.setWriteBeanGraphFile(new File(simpleBeanGraph)); 269 ref("Calling GenBeans.doIt"); 270 GenBeans.doIt(config); 271 beanTreeFile.delete(); 272 273 config = new GenBeans.Config(); 274 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 275 config.setAttributesAsProperties(true); 276 config.setGenerateCommonInterface("CommonBean"); 277 config.setGenerateInterfaces(true); 278 config.setGeneratePropertyEvents(true); 279 config.setAuto(true); 280 config.setStandalone(false); 281 schemaFile = new File(dataDir, "TestNamespace.xsd"); 282 dtdIn = new FileInputStream(schemaFile); 283 config.setFileIn(dtdIn); 284 config.setInputURI(schemaFile.toString()); 285 config.setSchemaType(GenBeans.Config.XML_SCHEMA); 288 config.setRootDir(workDir); 289 config.setDumpBeanTree(beanTreeFile); 290 config.addReadBeanGraphFiles(new File(simpleBeanGraph)); 291 ref("Calling GenBeans.doIt"); 292 GenBeans.doIt(config); 293 294 ref("Bean Tree:"); 295 ref(beanTreeFile); 296 297 ref("Compiling"); 298 String cmd = getJdkHome() + "javac -nowarn -classpath "+workDir.toString()+File.pathSeparator+dataDir.toString()+File.pathSeparator+theClassPath+" "+dataDir.toString()+"/"+testName+".java"; 299 int result = runCommandToSystemOut(cmd); 300 ref("Finished compiling: "+result); 301 302 cmd = getJdkHome() + "java -classpath "+workDir.toString()+File.pathSeparator+dataDir.toString()+File.pathSeparator+theClassPath+" "+testName+" "+dataDir.toString()+"/"; 303 result = runCommand(cmd); 304 ref("Finished running "+testName+": "+result); 305 306 System.out.println("Finished.\n"); 307 } catch (Exception e) { 308 ref(e.getMessage()); 309 e.printStackTrace(); 310 } 311 } 312 313 public void generalTest(String testName) throws IOException, Schema2BeansException, InterruptedException { 314 generalTest(testName, false, false, false); 315 } 316 317 public void generalTest(String testName, boolean xmlSchema, 318 boolean pureJavaBeans, boolean attrProp) throws IOException, Schema2BeansException, InterruptedException { 319 GenBeans.Config config = new GenBeans.Config(); 320 if (pureJavaBeans) { 321 config.setOutputType(GenBeans.Config.OUTPUT_JAVABEANS); 322 } 323 if (attrProp) { 324 config.setAttributesAsProperties(true); 325 } 326 generalTest(testName, xmlSchema, config); 327 } 328 329 public void generalTest(String testName, boolean xmlSchema, 330 GenBeans.Config config) throws IOException, Schema2BeansException, InterruptedException { 331 String testOnly = System.getProperty("TestMain.testOnly"); 332 if (testOnly != null && !testOnly.equals(testName)) 333 return; 334 try { 335 System.out.println(": Starting "+testName); 336 File workDir = getWorkDir(); 337 338 343 config.setAuto(true); 344 config.setStandalone(false); 345 System.out.println("workDir="+workDir.toString()); 349 File schemaFile; 350 if (xmlSchema) { 351 schemaFile = new File(dataDir, testName+".xsd"); 352 InputStream dtdIn = new FileInputStream(schemaFile); 353 config.setFileIn(dtdIn); 354 config.setSchemaType(GenBeans.Config.XML_SCHEMA); 355 } else { 356 schemaFile = new File(dataDir, testName+".dtd"); 357 InputStream dtdIn = new FileInputStream(schemaFile); 358 config.setFileIn(dtdIn); 359 config.setSchemaType(GenBeans.Config.DTD); 360 } 361 try { 362 InputStream mddIn = new FileInputStream(new File(dataDir, testName+".mdd")); 363 System.out.println("Found mdd file"); 364 config.setMddIn(mddIn); 365 } catch (FileNotFoundException e) { 366 } 368 config.setInputURI(schemaFile.toString()); 369 config.setRootDir(workDir); 370 File beanTreeFile = File.createTempFile("beanTree", "txt"); 377 config.setDumpBeanTree(beanTreeFile); 378 ref("Calling GenBeans.doIt"); 379 GenBeans.doIt(config); 380 381 ref("Bean Tree:"); 382 ref(beanTreeFile); 383 beanTreeFile.delete(); 384 385 ref("Compiling"); 386 String cmd = getJdkHome() + "javac -nowarn -classpath "+workDir.toString()+File.pathSeparator+dataDir.toString()+File.pathSeparator+theClassPath+" "+dataDir.toString()+"/"+testName+".java"; 387 int result = runCommandToSystemOut(cmd); 388 ref("Finished compiling: "+result); 389 390 cmd = getJdkHome() + "java -classpath "+workDir.toString()+File.pathSeparator+dataDir.toString()+File.pathSeparator+theClassPath+" "+testName+" "+dataDir.toString()+"/"; 392 result = runCommand(cmd); 393 ref("Finished running "+testName+": "+result); 394 395 System.out.println("Finished.\n"); 396 } catch (Exception e) { 397 ref(e.getMessage()); 398 e.printStackTrace(); 399 } 400 } 401 402 private int runCommand(String cmd) throws java.io.IOException , java.lang.InterruptedException { 403 System.out.println(cmd); 404 Process proc = Runtime.getRuntime().exec(cmd); 405 Writer out = new BufferedWriter(new OutputStreamWriter(getRef(), "UTF-8")); 406 Thread outThread = new Thread (new InputMonitor("out: ", proc.getInputStream(), out)); 407 outThread.start(); 408 Thread errThread = new Thread (new InputMonitor("err: ", proc.getErrorStream(), out)); 409 errThread.start(); 410 411 int result = proc.waitFor(); 412 out.flush(); 413 414 outThread.join(32000); 416 errThread.join(32000); 417 return result; 418 } 419 420 private int runCommandToSystemOut(String cmd) throws java.io.IOException , java.lang.InterruptedException { 421 System.out.println(cmd); 422 Process proc = Runtime.getRuntime().exec(cmd); 423 int result = proc.waitFor(); 424 return result; 425 } 426 427 static class InputMonitor implements Runnable { 428 private String prefix; 429 private InputStream is; 430 private Writer out; 431 432 public InputMonitor(String prefix, InputStream is, Writer out) { 433 this.prefix = prefix; 434 this.is = is; 435 this.out = out; 436 } 437 438 public void run() { 439 try { 440 int c; 441 boolean freshLine = true; 442 while ((c = is.read()) != -1) { 443 if (freshLine) { 444 out.write(prefix); 445 freshLine = false; 446 } 447 char ch = (char)c; 448 if (ch == '\n') 449 freshLine = true; 450 out.write(ch); 451 } 452 out.flush(); 453 } catch (java.io.IOException e) { 454 try { 455 out.write(e.getMessage()); 456 } catch (java.io.IOException e2) { 457 } 459 } 460 } 461 } 462 463 protected File dataDir; 464 protected String theClassPath = ""; 465 466 protected void setUp() { 467 String packageName = ""; 468 if (null != getClass().getPackage()) 469 packageName = getClass().getPackage().getName(); 470 471 String dataDirName = NbTestCase.convertNBFSURL(TestMain.class.getResource("data")); 475 dataDir = new File(dataDirName); 476 System.out.println("dataDirName="+dataDirName); 477 478 theClassPath += classPathEntryFromURL(org.netbeans.modules.schema2beans.BaseBean.class); 479 theClassPath += File.pathSeparator + classPathEntryFromURL(org.openide.filesystems.FileObject.class); 480 System.out.println("classpath="+theClassPath); 485 } 486 487 private String classPathEntryFromURL(Class cls) { 488 String shortName = cls.getName().substring(1+cls.getName().lastIndexOf('.')); 489 URL url = cls.getResource(shortName + ".class"); 490 String file = url.getFile(); 491 if (url.getProtocol().equals("jar")) { 492 String jarFile = file.substring(file.indexOf(':')+1); 494 jarFile = jarFile.substring(0, jarFile.indexOf('!')); 495 return jarFile; 496 } else if (url.getProtocol().equals("file")) { 497 String result = file.substring(0, file.length() - cls.getName().length() - 6); 499 return result; 500 } else { 501 return file; 502 } 503 } 504 505 protected void tearDown() { 506 compareReferenceFiles(); 507 } 508 509 public void ref(File f) throws IOException { 510 Reader r = new FileReader(f); 511 char buf[] = new char[1024]; 512 StringBuffer s = new StringBuffer (); 513 int len; 514 while ((len = r.read(buf, 0, 1024)) > 0) { 515 s.append(buf, 0, len); 516 } 517 r.close(); 518 ref(s.toString()); 519 } 520 521 private String getJdkHome(){ 522 return System.getProperty("java.home") + File.separator + ".." + File.separator + "bin" + File.separator; 523 524 } 525 } 526 | Popular Tags |