KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestMain


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
20 import java.io.*;
21 import java.net.URL JavaDoc;
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 JavaDoc testName) {
33         super(testName);
34     }
35
36     public static void main(java.lang.String JavaDoc[] 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 JavaDoc {
47         generalTest("TestPurchaseOrder", true, true, true);
48     }
49     
50     public void testInvoice() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
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 JavaDoc {
69         generalTest("TestBookXMLSchema", true, false, false);
70     }
71     
72     public void testBook() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
73         generalTest("TestBook");
74     }
75     
76     public void testDupInternalNames() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
77         generalTest("TestDupInternalNames", true, true, true);
78     }
79     
80     public void testEvents() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
81         generalTest("TestEvents");
82     }
83     
84     public void testMerge() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
85         generalTest("TestMerge");
86     }
87     
88     public void testAttr() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
89         generalTest("TestAttr", false, false, true);
90     }
91     
92     public void testMdd() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
93         generalTest("TestMdd");
94     }
95     
96     public void testValid() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
97         generalTest("TestValid");
98     }
99     
100     public void testFind() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
101         generalTest("TestFind");
102     }
103     
104     public void testVeto() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
105         generalTest("TestVeto");
106     }
107     
108     public void testContrivedApp() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
109         generalTest("TestContrivedApp");
110     }
111     
112     public void testEncoding() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
113         generalTest("TestEncoding");
114     }
115     
116     public void testExceptions() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
117         generalTest("TestExceptions");
118     }
119     
120     public void testEmpty() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
121         generalTest("TestEmpty");
122     }
123
124     public void testNamespace() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
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 JavaDoc {
134         generalTest("TestExtensionSample", true, true, true);
135     }
136
137     public void testExtension() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
226         generalTest("TestApplication1_4", true, true, true);
227     }
228
229     public void testMergeExtendBaseBean() throws IOException, Schema2BeansException, InterruptedException JavaDoc {
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 JavaDoc {
242         String JavaDoc 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 JavaDoc 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             //mddIn = new FileInputStream(new File(dataDir, "TestBeanWrapper.mdd"));
286
//config.setMddIn(mddIn);
287
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 JavaDoc 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 JavaDoc e) {
308             ref(e.getMessage());
309             e.printStackTrace();
310         }
311     }
312     
313     public void generalTest(String JavaDoc testName) throws IOException, Schema2BeansException, InterruptedException JavaDoc {
314         generalTest(testName, false, false, false);
315     }
316
317     public void generalTest(String JavaDoc testName, boolean xmlSchema,
318                             boolean pureJavaBeans, boolean attrProp) throws IOException, Schema2BeansException, InterruptedException JavaDoc {
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 JavaDoc testName, boolean xmlSchema,
330                             GenBeans.Config config) throws IOException, Schema2BeansException, InterruptedException JavaDoc {
331         String JavaDoc 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             //File diffCommandFile = new File(workDir, "diffCommand");
339
//Writer cmdOut = new FileWriter(diffCommandFile);
340
//cmdOut.write("ediff "+workDir.toString());
341
//cmdOut.close();
342

343             config.setAuto(true);
344             config.setStandalone(false);
345             //config.setTraceParse(true);
346
//config.setTraceGen(true);
347
//config.setThrowErrors(true);
348
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                 // It's okay if there is no mdd file present
367
}
368             config.setInputURI(schemaFile.toString());
369             config.setRootDir(workDir);
370             //config.setPackagePath();
371
//config.setMessageOut(getRef());
372
//if (testName.equals("TestBookXMLSchema")) {
373
// config.setTraceParse(true);
374
// config.setTraceGen(true);
375
//}
376
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 JavaDoc 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             //runCommand("ls -l "+dataDir);
391
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 JavaDoc e) {
397             ref(e.getMessage());
398             e.printStackTrace();
399         }
400     }
401
402     private int runCommand(String JavaDoc cmd) throws java.io.IOException JavaDoc, java.lang.InterruptedException JavaDoc {
403         System.out.println(cmd);
404         Process JavaDoc proc = Runtime.getRuntime().exec(cmd);
405         Writer out = new BufferedWriter(new OutputStreamWriter(getRef(), "UTF-8"));
406         Thread JavaDoc outThread = new Thread JavaDoc(new InputMonitor("out: ", proc.getInputStream(), out));
407         outThread.start();
408         Thread JavaDoc errThread = new Thread JavaDoc(new InputMonitor("err: ", proc.getErrorStream(), out));
409         errThread.start();
410
411         int result = proc.waitFor();
412         out.flush();
413
414         // Wait upto 32s for that thread to finish before going on.
415
outThread.join(32000);
416         errThread.join(32000);
417         return result;
418     }
419     
420     private int runCommandToSystemOut(String JavaDoc cmd) throws java.io.IOException JavaDoc, java.lang.InterruptedException JavaDoc {
421         System.out.println(cmd);
422         Process JavaDoc proc = Runtime.getRuntime().exec(cmd);
423         int result = proc.waitFor();
424         return result;
425     }
426
427     static class InputMonitor implements Runnable JavaDoc {
428         private String JavaDoc prefix;
429         private InputStream is;
430         private Writer out;
431         
432         public InputMonitor(String JavaDoc 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 JavaDoc e) {
454                 try {
455                     out.write(e.getMessage());
456                 } catch (java.io.IOException JavaDoc e2) {
457                     // try only once.
458
}
459             }
460         }
461     }
462
463     protected File dataDir;
464     protected String JavaDoc theClassPath = "";
465     
466     protected void setUp() {
467         String JavaDoc packageName = "";
468         if (null != getClass().getPackage())
469             packageName = getClass().getPackage().getName();
470
471         // when running this code inside IDE, getResource method returns URL in NBFS
472
// format, so we need to convert it to filename
473
// when running this code inside code mode, nothing happens
474
String JavaDoc 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         //theClassPath += File.pathSeparator + classPathEntryFromURL(javax.xml.namespace.QName.class);
481
//theClassPath += File.pathSeparator + classPathEntryFromURL(org.w3c.dom.Node.class);
482
//theClassPath += File.pathSeparator + classPathEntryFromURL(javax.xml.parsers.DocumentBuilderFactory.newInstance().getClass());
483
//theClassPath += File.pathSeparator + classPathEntryFromURL(org.w3c.dom.ranges.DocumentRange.class);
484
System.out.println("classpath="+theClassPath);
485     }
486
487     private String JavaDoc classPathEntryFromURL(Class JavaDoc cls) {
488         String JavaDoc shortName = cls.getName().substring(1+cls.getName().lastIndexOf('.'));
489         URL JavaDoc url = cls.getResource(shortName + ".class");
490         String JavaDoc file = url.getFile();
491         if (url.getProtocol().equals("jar")) {
492             // example: file = 'jar:/usr/local/j2sdkee1.3.1/lib/j2ee.jar!/org/w3c/dom/Node.class'
493
String JavaDoc jarFile = file.substring(file.indexOf(':')+1);
494             jarFile = jarFile.substring(0, jarFile.indexOf('!'));
495             return jarFile;
496         } else if (url.getProtocol().equals("file")) {
497             // example: file='/home/cliffwd/cvs/dublin/nb_all/schema2beans/rt/src/org/netbeans/modules/schema2beans/GenBeans.class'
498
String JavaDoc 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 JavaDoc s = new StringBuffer JavaDoc();
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 JavaDoc getJdkHome(){
522         return System.getProperty("java.home") + File.separator + ".." + File.separator + "bin" + File.separator;
523
524     }
525 }
526
Popular Tags