KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > SAXGeneratorJTest


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 package org.netbeans.modules.xml.tools.generator;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Comparator JavaDoc;
23 import junit.textui.TestRunner;
24 import org.netbeans.jellytools.EditorOperator;
25 import org.netbeans.jellytools.EditorWindowOperator;
26 import org.netbeans.jellytools.actions.OpenAction;
27 import org.netbeans.jellytools.modules.xml.actions.SAXDocumentHandlerWizardAction;
28 import org.netbeans.jellytools.modules.xml.saxwizard.*;
29 import org.netbeans.jellytools.nodes.Node;
30 import org.netbeans.jellytools.util.FolderContext;
31 import org.netbeans.jemmy.operators.JTableOperator;
32 import org.netbeans.junit.AssertionFailedErrorException;
33 import org.netbeans.tests.xml.JXTest;
34 import org.openide.cookies.SourceCookie;
35 import org.openide.loaders.DataObject;
36 import org.openide.src.*;
37
38 /** Checks XSL Transformation action. */
39
40 public class SAXGeneratorJTest extends JXTest {
41     
42     /** Creates new XMLNodeTest */
43     public SAXGeneratorJTest(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     // TESTS ///////////////////////////////////////////////////////////////////
48

49     /** Performs 'XSL Transformation...' action and checks output. */
50     public void test() throws Exception JavaDoc {
51         // clear output and display Transformation Dialog
52
FolderContext df = FolderContext.getDataFolder(this);
53         df.deleteDataObject("sax/BooksSAXBindings.xml");
54         df.deleteDataObject("sax/BHImpl.java");
55         df.deleteDataObject("sax/BHandler.java");
56         df.deleteDataObject("sax/BPImpl.java");
57         df.deleteDataObject("sax/BParser.java");
58         df.deleteDataObject("sax/BParslet.java");
59         
60         // perform SAX Wizard Action
61
SAXDocumentHandlerWizardAction saxAction = new SAXDocumentHandlerWizardAction();
62         try {
63             Node node = findDataNode("sax" + DELIM + "books");
64             saxAction.perform(node);
65         } catch (Exception JavaDoc ex) {
66             fail("Cannot perform SAX Wizard Action.", ex);
67         }
68         
69         // Page-1
70
try {
71             SAXDocumentHandlerWizardPage1 page1 = new SAXDocumentHandlerWizardPage1();
72             page1.verify();
73             sleepTest(2000); //!!!
74
page1.cboJAXPVersion().selectItem(page1.ITEM_JAXP10);
75             page1.cboSAXParserVersion().selectItem(page1.ITEM_SAX20);
76             page1.checkPropagateSAXEventsToGeneratedHandler(true);
77             if (DEBUG) sleepTest(2000);
78             page1.next();
79         } catch (Exception JavaDoc ex) {
80             fail("Test fails on Page-1.", ex);
81         }
82         
83         // Page-2
84
try {
85             SAXDocumentHandlerWizardPage2 page2 = new SAXDocumentHandlerWizardPage2();
86             page2.verify();
87             JTableOperator table = page2.tabElementMappings();
88             int row = table.findCellRow("inc-level-1");
89             page2.selectHandlerType(row, page2.IGNORE);
90             row = table.findCellRow("Product");
91             page2.selectHandlerType(row, page2.CONTAINER);
92             page2.setHandlerMethod(row, "productHandlerMethod");
93             page2.setHandlerMethod(row, "product Handler Method");
94             assertEquals("Invalid method name.", "productHandlerMethod", page2.getHandlerMethod(row));
95             if (DEBUG) sleepTest(2000);
96             page2.next();
97         } catch (Exception JavaDoc ex) {
98             fail("Test fails on Page-2.", ex);
99         }
100         
101         // Page-3
102
try {
103             SAXDocumentHandlerWizardPage3 page3 = new SAXDocumentHandlerWizardPage3();
104             page3.verify();
105             JTableOperator table = page3.tabDataConvertors();
106             int row = table.findCellRow("inc-level-1");
107             assertEquals("Invalid initial method.", page3.NO_METHOD, page3.getConvertorMethod(row));
108             page3.setConvertorMethod(row, "getText");
109             assertEquals("Invalid return type.", "java.lang.String", page3.getReturnType(row));
110             
111             row = table.findCellRow("Price");
112             page3.setConvertorMethod(row, "priceToInt");
113             page3.selectReturnType(row, "int");
114             
115             row = table.findCellRow("Image");
116             page3.setConvertorMethod(row, "getImage");
117             page3.setReturnType(row, "java.awt.Image");
118             
119             row = table.findCellRow("Product");
120             page3.selectConvertorMethod(row, "priceToInt");
121             assertEquals("Invalid return type.", "int", page3.getReturnType(row));
122             page3.selectConvertorMethod(row, "getImage");
123             assertEquals("Invalid return type.", "java.awt.Image", page3.getReturnType(row));
124             page3.selectConvertorMethod(row, "getText");
125             assertEquals("Invalid return type.", "java.lang.String", page3.getReturnType(row));
126             page3.setReturnType(row, "java*lang*String");
127             assertEquals("Invalid return type.", "java.lang.String", page3.getReturnType(row));
128             page3.setReturnType(row, "java.lang.StringBuffer");
129             
130             // getText() should have consistent return type for "Product" and "inc-level-1" elements
131
int row1 = table.findCellRow("inc-level-1");
132             assertEquals("Invalid return type.", page3.getReturnType(row1), page3.getReturnType(row));
133             if (DEBUG) sleepTest(2000);
134             page3.next();
135         } catch (Exception JavaDoc ex) {
136             fail("Test fails on Page-3.", ex);
137         }
138         
139         // Page-4
140
try {
141             SAXDocumentHandlerWizardPage4 page4 = new SAXDocumentHandlerWizardPage4();
142             page4.verify();
143             page4.setHandlerInterface("BHandler");
144             page4.setHandlerImplementation("BHImpl");
145             page4.setGeneratedParser("BParser");
146             page4.setDataConvertorInterface("BParslet");
147             page4.setDataConvertorImplementation("BPImpl");
148             page4.checkSaveCustomizedBindings(false);
149             page4.checkSaveCustomizedBindings(true);
150             assertEquals("Invalid bindings location.", "BooksSAXBindings", page4.getBindingsLocation());
151             if (DEBUG) sleepTest(2000);
152             page4.finish();
153         } catch (Exception JavaDoc ex) {
154             fail("Test fails on Page-4.", ex);
155         }
156         
157         // confirm changes - Propagate SAXE vents was checked
158
try {
159             new ConfirmChangesDialog().processAll();
160         } catch (Exception JavaDoc ex) {
161             fail("Test fails on Confirm Changes dialog.", ex);
162         }
163         
164         // save handler implementation dump generated classes
165
try {
166             Node implNode = findDataNode("sax" + DELIM + "BHImpl");
167             new OpenAction().perform(implNode);
168             // force editor to reload document
169
EditorWindowOperator ewo = new EditorWindowOperator();
170             EditorOperator eo = ewo.getEditor("BHImpl");
171             eo.setCaretPositionToLine(1);
172             eo.insert("\n");
173             eo.waitModified(true);
174             eo.deleteLine(1);
175             eo.save();
176             
177             ref("\n=== Handler Interface:\n");
178             ref(dumpFile(TestUtil.THIS.findData("sax/BHandler.java")));
179             ref("=== Handler Implementation:\n");
180             ref(dumpFile(TestUtil.THIS.findData("sax/BHImpl.java")));
181             ref("\n=== Generated Parser:\n");
182             ref(dumpFile(TestUtil.THIS.findData("sax/BParser.java")));
183             ref("\n=== Data Convertor Interfaces:\n");
184             ref(dumpFile(TestUtil.THIS.findData("sax/BParslet.java")));
185             ref("\n=== Data Convertor Implementation:\n");
186             ref(dumpFile(TestUtil.THIS.findData("sax/BPImpl.java")));
187         } catch (Exception JavaDoc ex) {
188             fail("Test fails during dumping generated classes.", ex);
189         }
190         compareReferenceFiles();
191     }
192     
193     // LIB ////////////////////////////////////////////////////////////////////
194

195     /** @author David Kaspar */
196     public String JavaDoc dumpFile(DataObject dao) {
197         SourceCookie sc = (SourceCookie) dao.getCookie(SourceCookie.class);
198         SourceElement se = sc.getSource();
199         ClassElement[] cea = se.getClasses();
200         if (cea == null)
201             return "";
202         ClassElement[] newcea = new ClassElement[cea.length];
203         for (int a = 0; a < cea.length; a ++)
204             newcea[a] = (ClassElement) cea[a].clone();
205         newcea = sortClasses(newcea);
206         String JavaDoc str = "";
207         for (int a = 0; a < newcea.length; a ++)
208             str += newcea[a].toString();
209         return str;
210     }
211     
212     /** @author David Kaspar */
213     public ClassElement[] sortClasses(ClassElement[] cea) {
214         Arrays.sort(cea, new Comparator JavaDoc() {
215             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
216                 return ((ClassElement) o1).getName().getName().compareTo(((ClassElement) o2).getName().getName());
217             }
218         });
219         for (int a = 0; a < cea.length; a ++) {
220             ClassElement ce = cea[a];
221             try {
222                 ce.setInterfaces(sortInterfaces(ce.getInterfaces()));
223                 ce.setFields(sortFields(ce.getFields()));
224                 ce.setInitializers(sortInitializers(ce.getInitializers()));
225                 ce.setConstructors(sortConstructors(ce.getConstructors()));
226                 ce.setMethods(sortMethods(ce.getMethods()));
227                 ce.setClasses(sortClasses(ce.getClasses()));
228             } catch (Exception JavaDoc e) {
229                 log("ERROR: Exception while normalizing class: ClassElement: " + ce.getName() + " | " + ce.getSignature(), e);
230                 throw new AssertionFailedErrorException(e);
231             }
232         }
233         return cea;
234     }
235     
236     /** @author David Kaspar */
237     public static Identifier[] sortInterfaces(Identifier[] ar) {
238         Arrays.sort(ar, new Comparator JavaDoc() {
239             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
240                 return ((Identifier) o1).getName().compareTo(((Identifier) o2).getName());
241             }
242         });
243         return ar;
244     }
245     
246     /** @author David Kaspar */
247     public static FieldElement[] sortFields(FieldElement[] ar) {
248         Arrays.sort(ar, new Comparator JavaDoc() {
249             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
250                 return ((FieldElement) o1).getName().getName().compareTo(((FieldElement) o2).getName().getName());
251             }
252         });
253         return ar;
254     }
255     
256     /** @author David Kaspar */
257     public static InitializerElement[] sortInitializers(InitializerElement[] ar) {
258         Arrays.sort(ar, new Comparator JavaDoc() {
259             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
260                 InitializerElement s1 = (InitializerElement) o1;
261                 InitializerElement s2 = (InitializerElement) o2;
262                 if (s1.isStatic() == s2.isStatic())
263                     return 0;
264                 return (s1.isStatic()) ? -1 : 1;
265             }
266         });
267         return ar;
268     }
269     
270     /** @author David Kaspar */
271     public static ConstructorElement[] sortConstructors(ConstructorElement[] ar) {
272         Arrays.sort(ar, new Comparator JavaDoc() {
273             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
274                 return ((ConstructorElement) o1).getName().getName().compareTo(((ConstructorElement) o2).getName().getName());
275             }
276         });
277         return ar;
278     }
279     
280     /** @author David Kaspar */
281     public static MethodElement[] sortMethods(MethodElement[] ar) {
282         Arrays.sort(ar, new Comparator JavaDoc() {
283             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
284                 return ((MethodElement) o1).getName().getName().compareTo(((MethodElement) o2).getName().getName());
285             }
286         });
287         return ar;
288     }
289     
290     // MAIN ////////////////////////////////////////////////////////////////////
291

292     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
293         DEBUG = false;
294         System.setProperty("xmltest.dbgTimeouts", "true");
295         TestRunner.run(SAXGeneratorJTest.class);
296         //TestRunner.run(suite());
297
}
298 }
299
Popular Tags