1 19 package org.netbeans.test.editor.app.tests; 20 21 import java.io.File ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.util.ArrayList ; 26 27 import org.netbeans.test.editor.app.core.Test; 28 import java.io.InputStream ; 29 import java.util.Enumeration ; 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 40 public class GenerateTests extends Object { 41 42 50 public static void generateTest(Test test,String file,String path,String packag) throws Exception { 51 System.err.println("Starting generate XTest source code for test "+test.getName()); 52 String name=test.getName()+"Performer"; 53 if (Character.isLowerCase(name.charAt(0))) { 54 name=name.substring(0,1).toUpperCase()+name.substring(2); 55 } 56 File dir=new File (path); 57 File javaFile=new File (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 71 private static void generateGoldenFiles(File dir,String name,TestCallAction[] acts) throws Exception { 72 System.err.println("Generate golden files."); 73 File goldenDir=new File (dir,"data"); 74 if (!goldenDir.exists()) { 75 goldenDir.mkdir(); 76 } 77 goldenDir=new File (goldenDir,"goldenfiles"); 78 if (!goldenDir.exists()) { 79 goldenDir.mkdir(); 80 } 81 goldenDir=new File (goldenDir,name); 82 if (!goldenDir.exists()) { 83 goldenDir.mkdir(); 84 } 85 File golden; 86 PrintWriter 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 (goldenDir,"test"+acts[i].getName()+".pass"); 90 pw=new PrintWriter (new FileWriter (golden)); 91 pw.print(acts[i].getOutput()); 92 pw.close(); 93 } 94 } 95 96 104 private static void generateSource(Test test,File source,String file,String name,String packag) throws Exception { 105 PrintWriter result = null; 106 result = new PrintWriter (new FileWriter (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 146 private static void generateMethod(TestCallAction tca, PrintWriter result, String file, String packag) throws Exception { 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 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 179 private static String generateFileName(String name, String packag) { 180 String 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 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 |