1 19 package org.netbeans.modules.xml.tools.generator; 20 21 import java.util.Arrays ; 22 import java.util.Comparator ; 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 39 40 public class SAXGeneratorJTest extends JXTest { 41 42 43 public SAXGeneratorJTest(String testName) { 44 super(testName); 45 } 46 47 49 50 public void test() throws Exception { 51 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 SAXDocumentHandlerWizardAction saxAction = new SAXDocumentHandlerWizardAction(); 62 try { 63 Node node = findDataNode("sax" + DELIM + "books"); 64 saxAction.perform(node); 65 } catch (Exception ex) { 66 fail("Cannot perform SAX Wizard Action.", ex); 67 } 68 69 try { 71 SAXDocumentHandlerWizardPage1 page1 = new SAXDocumentHandlerWizardPage1(); 72 page1.verify(); 73 sleepTest(2000); 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 ex) { 80 fail("Test fails on Page-1.", ex); 81 } 82 83 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 ex) { 98 fail("Test fails on Page-2.", ex); 99 } 100 101 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 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 ex) { 136 fail("Test fails on Page-3.", ex); 137 } 138 139 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 ex) { 154 fail("Test fails on Page-4.", ex); 155 } 156 157 try { 159 new ConfirmChangesDialog().processAll(); 160 } catch (Exception ex) { 161 fail("Test fails on Confirm Changes dialog.", ex); 162 } 163 164 try { 166 Node implNode = findDataNode("sax" + DELIM + "BHImpl"); 167 new OpenAction().perform(implNode); 168 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 ex) { 188 fail("Test fails during dumping generated classes.", ex); 189 } 190 compareReferenceFiles(); 191 } 192 193 195 196 public String 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 str = ""; 207 for (int a = 0; a < newcea.length; a ++) 208 str += newcea[a].toString(); 209 return str; 210 } 211 212 213 public ClassElement[] sortClasses(ClassElement[] cea) { 214 Arrays.sort(cea, new Comparator () { 215 public int compare(Object o1, Object 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 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 237 public static Identifier[] sortInterfaces(Identifier[] ar) { 238 Arrays.sort(ar, new Comparator () { 239 public int compare(Object o1, Object o2) { 240 return ((Identifier) o1).getName().compareTo(((Identifier) o2).getName()); 241 } 242 }); 243 return ar; 244 } 245 246 247 public static FieldElement[] sortFields(FieldElement[] ar) { 248 Arrays.sort(ar, new Comparator () { 249 public int compare(Object o1, Object o2) { 250 return ((FieldElement) o1).getName().getName().compareTo(((FieldElement) o2).getName().getName()); 251 } 252 }); 253 return ar; 254 } 255 256 257 public static InitializerElement[] sortInitializers(InitializerElement[] ar) { 258 Arrays.sort(ar, new Comparator () { 259 public int compare(Object o1, Object 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 271 public static ConstructorElement[] sortConstructors(ConstructorElement[] ar) { 272 Arrays.sort(ar, new Comparator () { 273 public int compare(Object o1, Object o2) { 274 return ((ConstructorElement) o1).getName().getName().compareTo(((ConstructorElement) o2).getName().getName()); 275 } 276 }); 277 return ar; 278 } 279 280 281 public static MethodElement[] sortMethods(MethodElement[] ar) { 282 Arrays.sort(ar, new Comparator () { 283 public int compare(Object o1, Object o2) { 284 return ((MethodElement) o1).getName().getName().compareTo(((MethodElement) o2).getName().getName()); 285 } 286 }); 287 return ar; 288 } 289 290 292 public static void main(String [] args) throws Exception { 293 DEBUG = false; 294 System.setProperty("xmltest.dbgTimeouts", "true"); 295 TestRunner.run(SAXGeneratorJTest.class); 296 } 298 } 299 | Popular Tags |