1 19 20 package org.netbeans.test.j2ee.multiview; 21 22 import java.awt.Component ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 import javax.swing.JCheckBox ; 26 import javax.swing.JPanel ; 27 import javax.swing.JTextArea ; 28 import javax.swing.JTextField ; 29 import javax.swing.SwingUtilities ; 30 import javax.swing.table.AbstractTableModel ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.JTextComponent ; 34 import junit.framework.AssertionFailedError; 35 import org.netbeans.jellytools.JellyTestCase; 36 import org.netbeans.junit.AssertionFailedErrorException; 37 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 38 import org.netbeans.modules.j2ee.dd.api.web.Filter; 39 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping; 40 import org.netbeans.modules.j2ee.dd.api.web.JspPropertyGroup; 41 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 42 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 43 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject; 44 import org.netbeans.modules.j2ee.ddloaders.web.multiview.DDBeanTableModel; 45 import org.netbeans.modules.j2ee.ddloaders.web.multiview.FilterParamsPanel; 46 import org.netbeans.modules.j2ee.ddloaders.web.multiview.InitParamsPanel; 47 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement; 48 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport; 49 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel; 50 import org.netbeans.modules.xml.multiview.ui.SectionPanel; 51 import org.netbeans.modules.xml.multiview.ui.SectionView; 52 import org.openide.cookies.EditorCookie; 53 import org.openide.cookies.SaveCookie; 54 import org.openide.nodes.Children; 55 import org.openide.nodes.Node; 56 57 58 62 public class DDTestUtils { 63 64 65 private DDDataObject ddObj; 66 private WebApp webapp; 67 private JellyTestCase testCase; 68 69 70 public DDTestUtils(DDDataObject ddObj,JellyTestCase testCase) { 71 this.ddObj = ddObj; 72 webapp = ddObj.getWebApp(); 73 this.testCase = testCase; 74 } 75 76 public void testTable(DDBeanTableModel model,String [][] values) { 77 testCase.assertEquals("Wrong count of table rows",values.length,model.getRowCount()); 78 for(int i = 0; i<model.getRowCount();i++) { 79 this.testTableRow(model,i,values[i]); 80 } 81 } 82 83 public void testTableRow(DDBeanTableModel model, int row, String [] values) { 84 testCase.assertTrue("No such row in table model",model.getRowCount()>=row); 85 int i; 86 87 for(i=0;i<values.length;i++ ) { 88 testCase.assertEquals("Value at "+row+","+i+" does not match.",values[i],model.getValueAt(row,i)); 89 } 90 } 91 92 public void setTableRow(DDBeanTableModel model, int row, Object [] values) throws Exception { 93 for (int i = 0; i < values.length; i++) { 94 model.setValueAt(values[i],row,i); 95 } 96 } 97 98 public CommonDDBean getBeanByProp(CommonDDBean[] beans,String prop, Object value) { 99 for (int i = 0; i < beans.length; i++) { 100 if(beans[i].getValue(prop).equals(value)) return beans[i]; 101 } 102 return null; 103 } 104 105 public int getRowIndexByProp(DDBeanTableModel model, int col, Object value) { 106 for(int i=0; i < model.getRowCount() ; i++ ) { 107 if(model.getValueAt(i,col).equals(value)) return i; 108 } 109 return -1; 110 } 111 112 public DDBeanTableModel getModelByBean(Object bean) { 113 JPanel panel = getInnerSectionPanel(bean); 114 Component [] comp = panel.getComponents(); 115 DDBeanTableModel model = (DDBeanTableModel) ((DefaultTablePanel) comp[1]).getModel(); 116 return model; 117 } 118 119 public void checkProperyGrpup(JspPropertyGroup prop,String name, String desc, String enc, String [] headers, String [] footers, String [] urls, boolean[] switches) { 120 testCase.assertEquals("Display name doesn't match",name,prop.getDefaultDisplayName()); 121 testCase.assertEquals("Description doesn't match",desc,prop.getDefaultDescription()); 122 testCase.assertEquals("Encoding doesn't match",enc,prop.getPageEncoding()); 123 testStringArrayEquals(prop.getIncludeCoda(),footers); 124 testStringArrayEquals(prop.getIncludePrelude(),headers); 125 testStringArrayEquals(prop.getUrlPattern(),urls); 126 testCase.assertEquals("ELIgnored: ",switches[0],prop.isElIgnored()); 127 testCase.assertEquals("XML syntax: ",switches[1],prop.isIsXml()); 128 testCase.assertEquals("Scripting invalid: ",switches[2],prop.isScriptingInvalid()); 129 JPanel panel = getInnerSectionPanel(prop); 130 Component [] comp = panel.getComponents(); 131 testCase.assertEquals("Display name doesn't match.",name,((JTextField )comp[1]).getText()); 132 testCase.assertEquals("Description doesn't match.",desc,((JTextArea )comp[3]).getText()); 133 String tmp=""; 134 for (int i = 0; i < urls.length; i++) { 135 if(i>0) tmp = tmp+", "; 136 tmp = tmp+urls[i]; 137 } 138 testCase.assertEquals("Url patterns doesn't match.",tmp,((JTextField )comp[5]).getText()); 139 testCase.assertEquals("Encoding doesn't match.",enc,((JTextField )comp[9]).getText()); 140 testCase.assertEquals("EL ignore doesn't match.",switches[0],((JCheckBox )comp[10]).isSelected()); 141 testCase.assertEquals("XML syntax doesn't match.",switches[1],((JCheckBox )comp[11]).isSelected()); 142 testCase.assertEquals("Script invalid doesn't match.",switches[2],((JCheckBox )comp[12]).isSelected()); 143 tmp=""; 144 for (int i = 0; i < headers.length; i++) { 145 if(i>0) tmp = tmp+", "; 146 tmp = tmp+headers[i]; 147 } 148 testCase.assertEquals("Preludes doesn't match.",tmp,((JTextField )comp[14]).getText()); 149 tmp=""; 150 for (int i = 0; i < footers.length; i++) { 151 if(i>0) tmp = tmp+", "; 152 tmp = tmp+footers[i]; 153 } 154 testCase.assertEquals("Codas doesn't match.",tmp,((JTextField )comp[17]).getText()); 155 } 156 157 public DDBeanTableModel getServletInitParamsTableModel() { 158 Servlet[] servlets = webapp.getServlet(); 159 testCase.assertEquals("Wrong count of servlets",1,servlets.length); 160 JPanel panel = getInnerSectionPanel(servlets[0]); 161 Component [] comp = panel.getComponents(); 162 InitParamsPanel tablePanel = ((InitParamsPanel)comp[17]); 163 return (DDBeanTableModel) tablePanel.getTable().getModel(); 164 } 165 166 public DDBeanTableModel getFilterInitParamsTableModel() { 167 Filter[] filters = webapp.getFilter(); 168 testCase.assertEquals("Wrong count of filters",1,filters.length); 169 JPanel panel = getInnerSectionPanel(filters[0]); 170 Component [] comp = panel.getComponents(); 171 FilterParamsPanel tablePanel = ((FilterParamsPanel)comp[9]); 172 return (DDBeanTableModel) tablePanel.getTable().getModel(); 173 } 174 175 public void testProperties(CommonDDBean bean, String [] properties, Object [] values) throws Exception { 176 for(int i=0;i<properties.length;i++) { 177 testCase.assertEquals("Property "+properties[i]+" has wrong value.",values[i],bean.getValue(properties[i])); 178 } 179 } 180 181 public void save() throws Exception { 182 new StepIterator() { 183 SaveCookie saveCookie; 184 public boolean step() throws Exception { 185 saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class); 186 return saveCookie!=null; 187 } 188 }; 189 SaveCookie saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class); 190 testCase.assertNotNull("Document was not modified",saveCookie); 191 saveCookie.save(); 192 } 193 194 public void traverse(Node n,String pref) { 195 System.out.println(pref+n.getName()+" "+n.getClass().getName()); 196 Children ch = n.getChildren(); 197 Node[] nodes = ch.getNodes(); 198 for(int i = 0;i<nodes.length;i++ ) { 199 traverse(nodes[i],pref+" "); 200 } 201 } 202 public static void waitForDispatchThread() { 203 if (SwingUtilities.isEventDispatchThread()) { 204 return; 205 } 206 final boolean[] finished = new boolean[]{false}; 207 SwingUtilities.invokeLater(new Runnable () { 208 public void run() { 209 finished[0] = true; 210 } 211 }); 212 new StepIterator() { 213 public boolean step() throws Exception { 214 return finished[0]; 215 } 216 }; 217 } 218 219 public JPanel getInnerSectionPanel(Object sectionPanel) { 220 ToolBarMultiViewElement multi = ddObj.getActiveMVElement(); 221 SectionView section = multi.getSectionView(); 222 section.openPanel(sectionPanel); 223 SectionPanel sp = section.findSectionPanel(sectionPanel); 224 testCase.assertNotNull("Section panel "+sectionPanel+" not found",sp); 225 JPanel p = sp.getInnerPanel(); 226 testCase.assertNotNull("Section panel has no inner panel",p); 227 return p; 228 } 229 230 public void checkInDDXML(String findText) { 231 checkInDDXML(findText,true); 232 } 233 234 public void checkInDDXML(String findText,boolean present) { 235 boolean matches = contains(findText); 236 if(present) { 237 testCase.assertTrue("Cannot find correct element in XML view (editor document)",matches); 238 } else { 239 testCase.assertFalse("Unexpected element found in XML view (editor document)",matches); 240 } 241 } 242 243 public String document() { 244 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class); 245 javax.swing.text.Document document = editor.getDocument(); 246 try { 247 String text = document.getText(0,document.getLength()); 248 return text; 249 } catch (javax.swing.text.BadLocationException ex) { 250 throw new AssertionFailedErrorException("Failed to read the document: ",ex); 251 } 252 } 253 254 public boolean contains(String findText) { 255 waitForDispatchThread(); 256 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class); 257 javax.swing.text.Document document = editor.getDocument(); 258 try { 259 String text = document.getText(0,document.getLength()); 260 Pattern p = Pattern.compile(findText,Pattern.DOTALL); 261 Matcher m = p.matcher(text.subSequence(0,text.length()-1)); 262 return m.matches(); 263 } catch (javax.swing.text.BadLocationException ex) { 264 throw new AssertionFailedErrorException("Failed to read the document: ",ex); 265 } 266 } 267 268 public void checkNotInDDXML(String findText) { 269 checkInDDXML(findText,false); 270 } 271 272 public void testStringArrayEquals(String [] expected,String [] actual) { 273 testCase.assertEquals("Wrong array size. ",expected.length,actual.length); 274 for (int i = 0; i < expected.length; i++) { 275 testCase.assertEquals("The "+i+" element has wrong value ",expected[i],actual[i]); 276 } 277 } 278 279 public void setText(JTextComponent component,String text) { 280 component.requestFocus(); 281 waitForDispatchThread(); 282 Document doc = component.getDocument(); 283 try { 284 doc.remove(0,doc.getLength()); 285 doc.insertString(0,text,null); 286 } catch (BadLocationException ex) { 287 testCase.fail(ex); 288 } 289 ddObj.modelUpdatedFromUI(); 290 waitForDispatchThread(); 291 } 292 } 293 | Popular Tags |