1 19 20 package org.netbeans.modules.j2ee.ddloaders.web.test; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import junit.textui.TestRunner; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.junit.NbTestSuite; 28 import org.netbeans.junit.AssertionFailedErrorException; 29 30 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject; 31 import org.openide.cookies.EditorCookie; 32 import org.openide.cookies.EditCookie; 33 import org.openide.cookies.SaveCookie; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 39 import org.netbeans.modules.j2ee.ddloaders.web.test.util.Helper; 40 import org.netbeans.modules.j2ee.ddloaders.web.test.util.StepIterator; 41 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport; 42 import org.netbeans.modules.j2ee.ddloaders.web.multiview.DDBeanTableModel; 43 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 44 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 45 46 import javax.swing.text.Document ; 47 import javax.swing.text.BadLocationException ; 48 49 53 public class DDEditorTest extends NbTestCase { 54 55 private static final String CAR_VOLVO = "Volvo"; 56 private static final String CAR_AUDI = "Audi"; 57 58 private DDDataObject dObj; 59 private static final String CONTEXT_PARAM_CYLINDERS = "\n <context-param>\n <param-name>cylinders</param-name>\n <param-value>6</param-value>\n </context-param>"; 60 61 public DDEditorTest(String testName) { 62 super(testName); 63 } 64 65 public static NbTestSuite suite() { 66 return new NbTestSuite(DDEditorTest.class); 67 } 68 69 public void testReplaceParamValueFromDDAPI() throws IOException { 70 initDataObject(); 71 FileObject fo = FileUtil.toFileObject(getDDFile()); 72 WebApp webApp = DDProvider.getDefault().getDDRoot(fo); 73 webApp.getContextParam()[0].setParamValue(CAR_VOLVO); 74 webApp.write(fo); 75 compareGoldenFile("ReplaceParamValue.pass"); 76 openInDesignView(dObj); 77 Helper.waitForDispatchThread(); 78 assertEquals("Context Params Table wasn't changed: ", CAR_VOLVO, (String ) getDDBeanModel().getValueAt(0, 1)); 79 } 80 81 public void testAddParamValueInDesignView() throws IOException { 82 initDataObject(); 83 openInDesignView(dObj); 84 Helper.waitForDispatchThread(); 85 final DDBeanTableModel model = getDDBeanModel(); 86 final int n = model.getRowCount() + 1; 87 model.addRow(new Object []{"color","Blue",""}); 88 dObj.modelUpdatedFromUI(); 89 new StepIterator() { 90 int sizeContextParam; 91 92 public boolean step() throws Exception { 93 sizeContextParam = dObj.getWebApp().sizeContextParam(); 94 return sizeContextParam == n; 95 } 96 97 public void finalCheck() { 98 assertEquals("Context Param wasn't added to the model", n, sizeContextParam); 99 } 100 }; 101 102 104 openInXmlView(dObj); 105 106 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) dObj.getCookie(EditorCookie.class); 107 final Document document = editor.getDocument(); 108 new StepIterator() { 109 public boolean step() throws Exception { 110 return document.getText(0, document.getLength()).indexOf("<param-value>Blue</param-value>") >= 0; 111 } 112 113 public void finalCheck() { 114 final Exception error = getError(); 115 if (error != null) { 116 throw new AssertionFailedErrorException("Failed to read the document: ", error); 117 } 118 assertEquals("Cannot find new context param element in XML view (editor document)", true, isSuccess()); 119 } 120 }; 121 122 new StepIterator() { 123 private SaveCookie saveCookie; 124 private int steps; 125 126 public boolean step() throws Exception { 127 steps++; 128 saveCookie = (SaveCookie) dObj.getCookie(SaveCookie.class); 129 return saveCookie != null; 130 } 131 132 public void finalCheck() { 133 if (saveCookie == null){ 135 log("Data object was not modified. Steps taken: " + steps + ", DataCache contents: " + dObj.getDataCache().getStringData()); 136 } 137 assertNotNull("Data Object Not Modified", saveCookie); 139 } 140 }.saveCookie.save(); 141 } 142 143 public void testAddParamValueInXmlView() throws IOException { 144 initDataObject(); 145 openInXmlView(dObj); 146 Helper.waitForDispatchThread(); 147 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)dObj.getCookie(EditorCookie.class); 148 final Document document = editor.getDocument(); 149 Helper.waitForDispatchThread(); 150 151 new StepIterator() { 153 private int index; 154 155 public boolean step() throws Exception { 156 String text = document.getText(0,document.getLength()); 158 index = text.lastIndexOf("</context-param>"); 159 return index >= 0; 160 } 161 162 public void finalCheck() { 163 assertEquals("Cannot find new context param element in XML view (editor document)", true, index > 0); 164 try { 165 document.insertString(index + 16, CONTEXT_PARAM_CYLINDERS, null); 166 } catch (BadLocationException ex) { 167 throw new AssertionFailedErrorException("Failed to read the document: ", ex); 168 } 169 } 170 }; 171 172 openInDesignView(dObj); 173 Helper.waitForDispatchThread(); 174 175 new StepIterator() { 176 private String paramValue; 177 178 public boolean step() throws Exception { 179 DDBeanTableModel model = getDDBeanModel(); 181 if (model.getRowCount() > 2) { 182 paramValue = (String ) model.getValueAt(2, 0); 183 return "cylinders".equals(paramValue); 184 } else { 185 return false; 186 } 187 } 188 189 public void finalCheck() { 190 assertEquals("Context Params Table wasn't changed: ", "cylinders", paramValue); 191 } 192 }; 193 194 SaveCookie cookie = (SaveCookie) dObj.getCookie(SaveCookie.class); 196 assertNotNull("Data Object Not Modified",cookie); 197 cookie.save(); 198 } 199 200 public void testReplaceParamValueFromDDAPI2() throws IOException { 201 initDataObject(); 202 openInXmlView(dObj); 203 final FileObject fo = FileUtil.toFileObject(getDDFile()); 204 WebApp webApp = DDProvider.getDefault().getDDRoot(fo); 205 webApp.getContextParam()[0].setParamValue(CAR_AUDI); 206 webApp.write(fo); 207 208 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) dObj.getCookie(EditorCookie.class); 209 final Document document = editor.getDocument(); 210 211 new StepIterator() { 212 public boolean step() throws Exception { 213 return document.getText(0, document.getLength()).indexOf("<param-value>Audi</param-value>") >= 0; 214 } 215 216 public void finalCheck() { 217 final Exception error = getError(); 218 if (error != null) { 219 throw new AssertionFailedErrorException("Failed to read the document: ", error); 220 } 221 assertEquals("Cannot find new context param element in XML view (editor document)", true, isSuccess()); 222 } 223 }; 224 } 225 226 public void testCheckParamValueInDesignView2() throws IOException { 227 initDataObject(); 228 openInDesignView(dObj); 229 Helper.waitForDispatchThread(); 230 new StepIterator() { 231 private String paramValue; 232 233 public boolean step() throws Exception { 234 paramValue = (String ) getDDBeanModel().getValueAt(0, 1); 235 return CAR_AUDI.equals(paramValue); 236 } 237 238 public void finalCheck() { 239 assertEquals("Context Params Table wasn't changed: ", CAR_AUDI, paramValue); 240 } 241 }; 242 } 243 244 public void testFinalSave() throws IOException { 245 initDataObject(); 246 SaveCookie cookie = (SaveCookie) dObj.getCookie(SaveCookie.class); 247 if (cookie != null) { 248 cookie.save(); 249 } 250 } 251 252 public DDBeanTableModel getDDBeanModel() { 253 DDBeanTableModel ddBeanModel; 254 try { 255 ddBeanModel = Helper.getContextParamsTableModel(dObj); 256 } catch (Exception ex) { 257 throw new AssertionFailedErrorException("Failed to open Context Params section", ex); 258 } 259 assertNotNull("Table Model Not Found", ddBeanModel); 260 return ddBeanModel; 261 } 262 263 private File getDDFile() { 264 return Helper.getDDFile(getDataDir()); 265 } 266 267 private void compareGoldenFile(String goldenFileName) throws IOException { 268 assertFile(getDDFile(), getGoldenFile(goldenFileName), getWorkDir()); 269 } 270 271 private void initDataObject() throws DataObjectNotFoundException { 272 if (dObj == null) { 273 File f = getDDFile(); 274 FileObject fo = FileUtil.toFileObject(f); 275 dObj = ((DDDataObject) DataObject.find(fo)); 276 assertNotNull("DD DataObject not found", dObj); 277 } 278 } 279 280 private static void openInXmlView(DDDataObject dObj) { 281 ((EditCookie) dObj.getCookie(EditCookie.class)).edit(); 282 } 283 284 private static void openInDesignView(DDDataObject dObj) { 285 try { 286 dObj.showElement(dObj.getWebApp().getContextParam()[0]); 287 } catch (Exception ex) { 288 throw new AssertionFailedErrorException("Failed to switch to Design View",ex); 289 } 290 } 291 292 297 public static void main(String [] args) { 298 TestRunner.run(suite()); 299 } 300 301 } 302 | Popular Tags |