KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > tests > GenerateTests


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 package org.netbeans.test.editor.app.tests;
20
21 import java.io.File JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.netbeans.test.editor.app.core.Test;
28 import java.io.InputStream JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import org.netbeans.test.editor.app.core.TestCallAction;
31 import org.netbeans.test.editor.app.core.TestSubTest;
32 import org.netbeans.test.editor.app.util.ParsingUtils;
33 import org.openide.filesystems.Repository;
34
35 /**
36  *
37  * @author ehucka
38  * @version
39  */

40 public class GenerateTests extends Object JavaDoc {
41     
42     /** Generates Performer class from <CODE>test</CODE> with package <CODE>packag</CODE>
43      * to the <CODE>path</CODE>. Test is stored into <CODE>file</CODE>.
44      * @param test source test
45      * @param file source test xml file
46      * @param path destination java file path
47      * @param packag source file package
48      * @throws Exception all errors is thrown out
49      */

50     public static void generateTest(Test test,String JavaDoc file,String JavaDoc path,String JavaDoc packag) throws Exception JavaDoc {
51         System.err.println("Starting generate XTest source code for test "+test.getName());
52         String JavaDoc name=test.getName()+"Performer";
53         if (Character.isLowerCase(name.charAt(0))) {
54             name=name.substring(0,1).toUpperCase()+name.substring(2);
55         }
56         File JavaDoc dir=new File JavaDoc(path);
57         File JavaDoc javaFile=new File JavaDoc(dir,name+".java");
58         TestCallAction[] tcas=test.getCallActions();
59         generateSource(test,javaFile,file,name,packag);
60         generateGoldenFiles(dir,name,tcas);
61         System.err.println("Source code: "+javaFile.getName()+" generated.");
62     }
63     
64     /** Generates Golden files to <CODE>dir</CODE> with test <CODE>name</CODE>
65      * from TestCallAction list <CODE>acts</CODE>.
66      * @param dir destination directory
67      * @param name name of the test
68      * @param acts list of test's call actions
69      * @throws Exception all errors is thrown out
70      */

71     private static void generateGoldenFiles(File JavaDoc dir,String JavaDoc name,TestCallAction[] acts) throws Exception JavaDoc {
72         System.err.println("Generate golden files.");
73         File JavaDoc goldenDir=new File JavaDoc(dir,"data");
74         if (!goldenDir.exists()) {
75             goldenDir.mkdir();
76         }
77         goldenDir=new File JavaDoc(goldenDir,"goldenfiles");
78         if (!goldenDir.exists()) {
79             goldenDir.mkdir();
80         }
81         goldenDir=new File JavaDoc(goldenDir,name);
82         if (!goldenDir.exists()) {
83             goldenDir.mkdir();
84         }
85         File JavaDoc golden;
86         PrintWriter JavaDoc pw;
87         for (int i=0;i < acts.length;i++) {
88             System.err.println(" Golden file: "+"test"+acts[i].getName()+".pass");
89             golden=new File JavaDoc(goldenDir,"test"+acts[i].getName()+".pass");
90             pw=new PrintWriter JavaDoc(new FileWriter JavaDoc(golden));
91             pw.print(acts[i].getOutput());
92             pw.close();
93         }
94     }
95     
96     /** Generates source file (.java) from test into source.
97      * @param test Source test.
98      * @param source destination java file
99      * @param file test xml file
100      * @param name name of test performer (class)
101      * @param packag package of destination java file
102      * @throws Exception all errors
103      */

104     private static void generateSource(Test test,File JavaDoc source,String JavaDoc file,String JavaDoc name,String JavaDoc packag) throws Exception JavaDoc {
105         PrintWriter JavaDoc result = null;
106         result = new PrintWriter JavaDoc(new FileWriter JavaDoc(source));
107         
108         result.println("//This file was automaticaly generated. Do not modify.");
109         result.println("/*");
110         result.println(" * Some License");
111         result.println(" */");
112         result.println("");
113         result.println("package " + packag + ";");
114         result.println("import java.io.PrintWriter;");
115         result.println("import org.openide.filesystems.*;");
116         result.println("import org.netbeans.junit.NbTestCase;");
117         result.println("import org.netbeans.junit.NbTestSuite;");
118         result.println("import java.io.File;");
119         result.println("import org.netbeans.test.editor.LineDiff;\n");
120         result.println("\n/**\n *\n * @author "+test.getAuthor());
121         result.println(" * @version "+test.getVersion());
122         result.println(" * "+test.getComment()+" */\n");
123         result.println("public class "+name+" extends NbTestCase {");
124         result.println("public "+name+"(String name) {");
125         result.println("super(name);");
126         result.println("}");
127         result.println("public void tearDown() throws Exception {");
128         result.println(" assertFile(\"Output does not match golden file.\", getGoldenFile(), new File(getWorkDir(), this.getName() + \".ref\"), new File(getWorkDir(), this.getName() + \".diff\"), new LineDiff(false));");
129         result.println("}");
130         TestCallAction[] tcas=test.getCallActions();
131         
132         for (int cntr = 0; cntr < tcas.length; cntr++) {
133             generateMethod(tcas[cntr],result,file,packag);
134         }
135         result.println("}");
136         result.close();
137     }
138     
139     /** Generates one test... method for <CODE>TestCallAction</CODE> into result.
140      * @param tca TestCallAction
141      * @param result destination stream
142      * @param file test xml file
143      * @param packag source file package
144      * @throws Exception all errors
145      */

146     private static void generateMethod(TestCallAction tca, PrintWriter JavaDoc result, String JavaDoc file, String JavaDoc packag) throws Exception JavaDoc {
147         System.err.println("Generate method for action "+tca.getName());
148         result.println("\n/**\n * Call Action: "+tca.getName());
149         if (tca.getOwner() instanceof TestSubTest) {
150             result.println(" * Sub Test: "+((TestSubTest)(tca.getOwner())).getName());
151             result.println(" * Author: "+((TestSubTest)(tca.getOwner())).getAuthor());
152             result.println(" * Version: "+((TestSubTest)(tca.getOwner())).getVersion());
153             result.println(" * Comment: "+((TestSubTest)(tca.getOwner())).getComment());
154         }
155         result.println(" */\n");
156         String JavaDoc name=tca.getName();
157         if (Character.isLowerCase(name.charAt(0))) {
158             name=name.substring(0,1).toUpperCase()+name.substring(2);
159         }
160         result.println("public void test" + tca.getName() + "() throws Exception {");
161         result.println("PrintWriter ref = null;");
162         result.println("PrintWriter log = null;");
163         result.println("String[] arguments = new String[] {");
164         result.println("\""+generateFileName(file,packag)+"\",");
165         result.println("\""+makeCallActionName(tca)+"\"");
166         result.println("};");
167         result.println("try {");
168         result.println("ref = new PrintWriter(getRef());");
169         result.println("log = new PrintWriter(getLog());");
170         result.println("new CallTestGeneric().runTest(arguments, log, ref);");
171         result.println("} finally {");
172         result.println("if (ref != null) ref.flush();");
173         result.println("if (log != null) log.flush();");
174         result.println("}");
175         result.println("}");
176     }
177     
178     /** Generates file name to source (parameter). */
179     private static String JavaDoc generateFileName(String JavaDoc name, String JavaDoc packag) {
180         String JavaDoc n=name.substring(name.lastIndexOf('/')+1);
181         if (n == null || n.length() == 0) {
182             n=name.substring(name.lastIndexOf('\\')+1);
183         }
184         return "/"+packag.replace('.','/')+"/"+n;
185     }
186     
187     private static String JavaDoc makeCallActionName(TestCallAction act) {
188         if (act.getOwner() instanceof TestSubTest) {
189             return act.getOwner()+"."+act.getName();
190         } else {
191             return act.getName();
192         }
193     }
194 }
195
Popular Tags