1 19 20 package org.netbeans.modules.form.layoutdesign; 21 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.regex.*; 33 import junit.framework.TestCase; 34 import org.netbeans.modules.form.FormModel; 35 import org.netbeans.modules.form.GandalfPersistenceManager; 36 import org.netbeans.modules.form.PersistenceException; 37 import org.openide.filesystems.FileObject; 38 39 public abstract class LayoutTestCase extends TestCase { 40 41 private String testSwitch; 42 43 protected LayoutModel lm = null; 44 protected LayoutDesigner ld = null; 45 46 protected URL url = getClass().getClassLoader().getResource(""); 47 48 protected FileObject startingFormFile; 49 protected File expectedLayoutFile; 50 51 protected HashMap contInterior = new HashMap (); 52 protected HashMap baselinePosition = new HashMap (); 53 54 protected HashMap prefPaddingInParent = new HashMap (); 55 protected HashMap prefPadding = new HashMap (); 56 protected HashMap compBounds = new HashMap (); 57 protected HashMap compMinSize = new HashMap (); 58 protected HashMap compPrefSize = new HashMap (); 59 protected HashMap hasExplicitPrefSize = new HashMap (); 60 61 protected LayoutComponent lc = null; 62 63 protected String goldenFilesPath = "../../../../test/unit/data/goldenfiles/"; 64 65 protected String className; 66 67 public LayoutTestCase(String name) { 68 super(name); 69 } 70 71 72 78 public void testLayout() throws IOException { 79 loadForm(startingFormFile); 80 81 Method [] methods = this.getClass().getMethods(); 82 for (int i=0; i < methods.length; i++) { 83 Method m = methods[i]; 84 if (m.getName().startsWith("doChanges")) { 85 try { 86 String name = getClass().getName(); 87 String simpleName = name.substring(name.lastIndexOf('.')+1); 88 System.out.println("Invoking " + simpleName + "." + m.getName()); 89 m.invoke(this, null); 90 91 String methodCount = m.getName().substring(9); 93 String currentLayout = getCurrentLayoutDump(); 94 currentLayout = Pattern.compile("\n").matcher(currentLayout) .replaceAll(System.getProperty("line.separator")); String expectedLayout = getExpectedLayoutDump(methodCount); 98 99 System.out.print("Comparing ... "); 100 101 boolean same = expectedLayout.equals(currentLayout); 102 if (!same) { 103 System.out.println("failed"); 104 System.out.println("EXPECTED: "); 105 System.out.println(expectedLayout); 106 System.out.println(""); 107 System.out.println("CURRENT: "); 108 System.out.println(currentLayout); 109 writeCurrentWrongLayout(methodCount, currentLayout); 110 } 111 else System.out.println("OK"); 112 System.out.println(""); 113 114 assertTrue("Model dump in step " + methodCount + " gives different result than expected", same); 115 116 } catch (IllegalArgumentException ex) { 117 ex.printStackTrace(); 118 fail("Error while invoking method: " + m); 119 } catch (IllegalAccessException ex) { 120 ex.printStackTrace(); 121 fail("Error while invoking method: " + m); 122 } catch (InvocationTargetException ex) { 123 ex.printStackTrace(); 124 fail("Error while invoking method: " + m); 125 } 126 } 127 } 128 } 129 130 protected void setUp() throws Exception { 131 super.setUp(); 132 testSwitch = System.getProperty(LayoutDesigner.TEST_SWITCH); 133 System.setProperty(LayoutDesigner.TEST_SWITCH, "true"); } 135 136 protected void tearDown() throws Exception { 137 if (testSwitch != null) 138 System.setProperty(LayoutDesigner.TEST_SWITCH, testSwitch); 139 else 140 System.getProperties().remove(LayoutDesigner.TEST_SWITCH); 141 super.tearDown(); 142 } 143 144 private void loadForm(FileObject file) { 145 FormModel fm = null; 146 GandalfPersistenceManager gpm = new GandalfPersistenceManager(); 147 List errors = new ArrayList (); 148 try { 149 fm = gpm.loadForm(file, file, null, errors); 150 } catch (PersistenceException pe) { 151 fail(pe.toString()); 152 } 153 154 if (errors.size() > 0) { 155 System.out.println("There were errors while loading the form: " + errors); 156 } 157 158 lm = fm.getLayoutModel(); 159 160 ld = new LayoutDesigner(lm, new FakeLayoutMapper(fm, 161 contInterior, 162 baselinePosition, 163 prefPaddingInParent, 164 compBounds, 165 compMinSize, 166 compPrefSize, 167 hasExplicitPrefSize, 168 prefPadding)); 169 } 170 171 private String getCurrentLayoutDump() { 172 return lm.dump(null); 173 } 174 175 private String getExpectedLayoutDump(String methodCount) throws IOException { 176 expectedLayoutFile = new File (url.getFile() + goldenFilesPath + getExpectedResultFileName(methodCount) + ".txt").getCanonicalFile(); 177 int length = (int) expectedLayoutFile.length(); 178 FileReader fr = null; 179 try { 180 fr = new FileReader (expectedLayoutFile); 181 char[] buf = new char[length]; 182 fr.read(buf); 183 return new String (buf); 184 } catch (IOException ioe) { 185 fail(ioe.toString()); 186 } finally { 187 if (fr != null) { 188 try { 189 fr.close(); 190 } catch (IOException io) { 191 fail(io.toString()); 192 } 193 } 194 } 195 return null; 196 } 197 198 private String getExpectedResultFileName(String methodCount) { 199 return className + "-ExpectedEndModel" + methodCount; 200 } 201 202 private void writeCurrentWrongLayout(String methodCount, String dump) throws IOException { 203 File file = new File (url.getFile() + "../results").getCanonicalFile(); 205 if (!file.exists()) { 206 file.mkdirs(); 207 } 208 file = new File (file, getExpectedResultFileName(methodCount)+".txt"); 209 if (file.exists()) { 210 file.delete(); 211 } 212 file.createNewFile(); 213 214 FileWriter fw = null; 215 try { 216 fw = new FileWriter (file); 217 fw.write(dump); 218 } 219 finally { 220 if (fw != null) { 221 fw.close(); 222 } 223 } 224 } 225 226 } 227 | Popular Tags |