KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > test > DDEditorTest


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
20 package org.netbeans.modules.j2ee.ddloaders.web.test;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
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 JavaDoc;
47 import javax.swing.text.BadLocationException JavaDoc;
48
49 /**
50  *
51  * @author Milan Kuchtiak
52  */

53 public class DDEditorTest extends NbTestCase {
54
55     private static final String JavaDoc CAR_VOLVO = "Volvo";
56     private static final String JavaDoc CAR_AUDI = "Audi";
57
58     private DDDataObject dObj;
59     private static final String JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc) getDDBeanModel().getValueAt(0, 1));
79     }
80
81     public void testAddParamValueInDesignView() throws IOException JavaDoc {
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 JavaDoc[]{"color","Blue",""});
88         dObj.modelUpdatedFromUI();
89         new StepIterator() {
90             int sizeContextParam;
91
92             public boolean step() throws Exception JavaDoc {
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         // test the model
103

104         openInXmlView(dObj);
105
106         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) dObj.getCookie(EditorCookie.class);
107         final Document JavaDoc document = editor.getDocument();
108         new StepIterator() {
109             public boolean step() throws Exception JavaDoc {
110                 return document.getText(0, document.getLength()).indexOf("<param-value>Blue</param-value>") >= 0;
111             }
112
113             public void finalCheck() {
114                 final Exception JavaDoc 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 JavaDoc {
127                 steps++;
128                 saveCookie = (SaveCookie) dObj.getCookie(SaveCookie.class);
129                 return saveCookie != null;
130             }
131
132             public void finalCheck() {
133                 // for debugging random failures
134
if (saveCookie == null){
135                     log("Data object was not modified. Steps taken: " + steps + ", DataCache contents: " + dObj.getDataCache().getStringData());
136                 }
137                 // check if save cookie was created
138
assertNotNull("Data Object Not Modified", saveCookie);
139             }
140         }.saveCookie.save();
141     }
142
143     public void testAddParamValueInXmlView() throws IOException JavaDoc {
144         initDataObject();
145         openInXmlView(dObj);
146         Helper.waitForDispatchThread();
147         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)dObj.getCookie(EditorCookie.class);
148         final Document JavaDoc document = editor.getDocument();
149         Helper.waitForDispatchThread();
150
151         // wait to see the changes in XML view
152
new StepIterator() {
153             private int index;
154
155             public boolean step() throws Exception JavaDoc {
156                 //test the editor document
157
String JavaDoc 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 JavaDoc 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 JavaDoc paramValue;
177
178             public boolean step() throws Exception JavaDoc {
179                 // get context params table model
180
DDBeanTableModel model = getDDBeanModel();
181                 if (model.getRowCount() > 2) {
182                     paramValue = (String JavaDoc) 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         // check if save cookie was created
195
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 JavaDoc {
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 JavaDoc document = editor.getDocument();
210
211         new StepIterator() {
212             public boolean step() throws Exception JavaDoc {
213                 return document.getText(0, document.getLength()).indexOf("<param-value>Audi</param-value>") >= 0;
214             }
215             
216             public void finalCheck() {
217                 final Exception JavaDoc 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 JavaDoc {
227         initDataObject();
228         openInDesignView(dObj);
229         Helper.waitForDispatchThread();
230         new StepIterator() {
231             private String JavaDoc paramValue;
232
233             public boolean step() throws Exception JavaDoc {
234                 paramValue = (String JavaDoc) 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 JavaDoc {
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 JavaDoc 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 JavaDoc getDDFile() {
264         return Helper.getDDFile(getDataDir());
265     }
266
267     private void compareGoldenFile(String JavaDoc goldenFileName) throws IOException JavaDoc {
268         assertFile(getDDFile(), getGoldenFile(goldenFileName), getWorkDir());
269     }
270
271     private void initDataObject() throws DataObjectNotFoundException {
272         if (dObj == null) {
273             File JavaDoc 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 JavaDoc ex) {
288             throw new AssertionFailedErrorException("Failed to switch to Design View",ex);
289         }
290     }
291
292     /**
293      * Used for running test from inside the IDE by internal execution.
294      *
295      * @param args the command line arguments
296      */

297     public static void main(String JavaDoc[] args) {
298         TestRunner.run(suite());
299     }
300
301 }
302
Popular Tags