1 19 20 package org.netbeans.modules.form.layoutdesign; 21 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.ExpressionTree; 24 import com.sun.source.tree.MethodTree; 25 import com.sun.source.tree.TypeParameterTree; 26 import com.sun.source.tree.VariableTree; 27 import java.awt.Rectangle ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.LineNumberReader ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.EnumSet ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 import javax.lang.model.element.Modifier; 40 import javax.lang.model.element.TypeElement; 41 import javax.lang.model.type.TypeKind; 42 import org.netbeans.api.java.classpath.ClassPath; 43 import org.netbeans.api.java.source.CancellableTask; 44 import org.netbeans.api.java.source.CompilationController; 45 import org.netbeans.api.java.source.JavaSource; 46 import org.netbeans.api.java.source.TreeMaker; 47 import org.netbeans.api.java.source.WorkingCopy; 48 import org.netbeans.modules.form.FormDataObject; 49 import org.netbeans.modules.form.FormDesigner; 50 import org.openide.awt.StatusDisplayer; 51 import org.openide.filesystems.FileObject; 52 import org.openide.filesystems.FileUtil; 53 import org.openide.loaders.DataObject; 54 import org.openide.util.Utilities; 55 56 62 63 public class LayoutTestUtils implements LayoutConstants { 64 65 static void writeString(List codeList, String name, String value) { 66 if (value != null) { 67 codeList.add("String " + name + "= \"" + value + "\";"); } else { 69 codeList.add("String " + name + " = null;"); } 71 } 72 73 static void writeLayoutComponentArray(List codeList, String arrayName, String lcName) { 74 codeList.add("LayoutComponent[] " + arrayName + " = new LayoutComponent[] { " + lcName + " };"); } 76 77 static void writeCollection(List codeList, String name, Collection c) { 78 codeList.add("Collection " + name + " = new ArrayList();"); Iterator i = c.iterator(); 80 while (i.hasNext()) { 81 codeList.add(name + ".add(\"" + (String )i.next() + "\");"); } 83 } 84 85 static void writeStringArray(List codeList, String name, String [] compIds) { 86 codeList.add("String[] " + name + " = new String[] {"); for (int i=0; i < compIds.length; i++) { 88 codeList.add("\"" + compIds[i] + "\"" + (i+1 < compIds.length ? "," : "")); } 90 codeList.add("};"); } 92 93 static void writeIntArray(List codeList, String name, int[] values) { 94 codeList.add("int[] " + name + " = new int[] {"); for (int i=0; i < values.length; i++) { 96 codeList.add(Integer.toString(values[i]) + (i+1 < values.length ? "," : "")); } 98 codeList.add("};"); } 100 101 static void writeRectangleArray(List codeList, String name, Rectangle [] bounds) { 102 codeList.add("Rectangle[] " + name + " = new Rectangle[] {"); for (int i=0; i < bounds.length; i++) { 104 codeList.add("new Rectangle(" + bounds[i].x + ", " + bounds[i].y + ", " + bounds[i].width + ", " + bounds[i].height + (i+1 < bounds.length ? "), " : ")")); } 109 codeList.add("};"); } 111 112 static void dumpTestcode(List codeList, DataObject form, final int modelCounter) { 113 FileWriter fw = null; 114 StringBuffer template = new StringBuffer (); 115 116 if (form == null) return; 117 try { 118 119 FileObject primaryFile = form.getPrimaryFile(); 120 121 InputStream in = LayoutTestUtils.class.getResourceAsStream("/org/netbeans/modules/form/resources/LayoutModelAutoTest_template"); LineNumberReader lReader = new LineNumberReader (new InputStreamReader (in)); 124 while (lReader.ready()) { 125 template.append(lReader.readLine()).append('\n'); 126 } 127 lReader.close(); 128 129 final StringBuffer code = new StringBuffer (); 131 Iterator i = codeList.iterator(); 132 while (i.hasNext()) { 133 String line = (String )i.next(); 134 code.append(line).append('\n'); 135 } 136 137 ClassPath cpath = ClassPath.getClassPath(primaryFile, ClassPath.SOURCE); 139 final String primaryFileClassFQN = cpath.getResourceName(primaryFile, '.', false); 140 final boolean[] resolved = new boolean[1]; 141 JavaSource js = JavaSource.forFileObject(primaryFile); 142 js.runUserActionTask(new CancellableTask<CompilationController>() { 143 public void cancel() { 144 } 145 public void run(CompilationController controller) throws Exception { 146 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 147 TypeElement clazz = controller.getElements().getTypeElement(primaryFileClassFQN); 148 resolved[0] = clazz != null; 149 } 150 }, true); 151 152 if (!resolved[0]) return; 153 154 String testClassName = primaryFile.getName() + "Test"; 156 FileObject testFO = primaryFile.getParent().getFileObject(testClassName, "java"); if (testFO == null) { 158 testFO = primaryFile.getParent().createData(testClassName, "java"); 160 String output = Utilities.replaceString(template.toString(), "${CLASS_NAME}", testFO.getName()); 163 fw = new FileWriter (FileUtil.toFile(testFO)); 165 fw.write(output); 166 fw.close(); 167 } 168 169 final String testClassFQN = "org.netbeans.modules.form.layoutdesign." + testFO.getName(); js = JavaSource.forFileObject(testFO); 172 js.runModificationTask(new CancellableTask<WorkingCopy>() { 173 public void cancel() { 174 } 175 public void run(WorkingCopy wcopy) throws Exception { 176 wcopy.toPhase(JavaSource.Phase.RESOLVED); 177 178 TypeElement classElm = wcopy.getElements().getTypeElement(testClassFQN); 179 if (classElm != null) { 180 ClassTree classTree = wcopy.getTrees().getTree(classElm); 181 TreeMaker make = wcopy.getTreeMaker(); 182 MethodTree method = make.Method( 183 make.Modifiers(EnumSet.of(Modifier.PUBLIC)), 184 "doChanges" + modelCounter, make.PrimitiveType(TypeKind.VOID), 186 Collections.<TypeParameterTree>emptyList(), 187 Collections.<VariableTree>emptyList(), 188 Collections.<ExpressionTree>emptyList(), 189 code.toString(), 190 null 191 ); 192 ClassTree classCopy = make.addClassMember(classTree, method); 193 wcopy.rewrite(classTree, classCopy); 194 } 195 196 } 197 }).commit(); 198 199 } catch (IOException ex) { 200 ex.printStackTrace(); 201 return; 202 } 203 204 } 205 206 public static FileObject getTargetFolder(FileObject file) { 207 FileObject targetFolder = file.getParent(); 208 try { 209 FileObject folder = file.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getFileObject("data/goldenfiles"); if (folder != null) { 211 targetFolder = folder; 212 } 213 } catch (NullPointerException npe) { 214 } 216 return targetFolder; 217 } 218 219 public static void writeTest(FormDesigner fd, FormDataObject formDO, Map idToNameMap, LayoutModel lm) { 220 FileObject formFO = formDO.getFormFile(); 221 222 fd.getLayoutDesigner().dumpTestcode(formDO); 223 224 FileWriter fw = null; 225 try { 226 FileObject targetFolder = getTargetFolder(formFO); 227 FileObject fo = targetFolder.createData(formFO.getName() + "Test-ExpectedEndModel" + Integer.toString(fd.getLayoutDesigner().getModelCounter()), "txt"); fw = new FileWriter (FileUtil.toFile(fo)); 229 fw.write(lm.dump(idToNameMap)); 230 StatusDisplayer.getDefault().setStatusText("The test was successfully written: " + fo.getPath()); } catch (IOException ex) { 232 ex.printStackTrace(); 233 return; 234 } finally { 235 try { 236 if (fw != null) fw.close(); 237 } catch (IOException io) { 238 } 240 } 241 } 242 243 } 244 | Popular Tags |