KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > multiview > DDTestUtils


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.test.j2ee.multiview;
21
22 import java.awt.Component JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import javax.swing.JCheckBox JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JTextArea JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.table.AbstractTableModel JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
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 /**
59  *
60  * @author jp159440
61  */

62 public class DDTestUtils /*extends JellyTestCase*/{
63     
64     
65     private DDDataObject ddObj;
66     private WebApp webapp;
67     private JellyTestCase testCase;
68     
69     /** Creates a new instance of DDTestUtils */
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 JavaDoc[][] 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 JavaDoc[] 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 JavaDoc[] values) throws Exception JavaDoc {
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 JavaDoc prop, Object JavaDoc 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 JavaDoc 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 JavaDoc bean) {
113         JPanel JavaDoc panel = getInnerSectionPanel(bean);
114         Component JavaDoc[] comp = panel.getComponents();
115         DDBeanTableModel model = (DDBeanTableModel) ((DefaultTablePanel) comp[1]).getModel();
116         return model;
117     }
118     
119     public void checkProperyGrpup(JspPropertyGroup prop,String JavaDoc name, String JavaDoc desc, String JavaDoc enc, String JavaDoc[] headers, String JavaDoc[] footers, String JavaDoc[] 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 JavaDoc panel = getInnerSectionPanel(prop);
130         Component JavaDoc[] comp = panel.getComponents();
131         testCase.assertEquals("Display name doesn't match.",name,((JTextField JavaDoc)comp[1]).getText());
132         testCase.assertEquals("Description doesn't match.",desc,((JTextArea JavaDoc)comp[3]).getText());
133         String JavaDoc 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 JavaDoc)comp[5]).getText());
139         testCase.assertEquals("Encoding doesn't match.",enc,((JTextField JavaDoc)comp[9]).getText());
140         testCase.assertEquals("EL ignore doesn't match.",switches[0],((JCheckBox JavaDoc)comp[10]).isSelected());
141         testCase.assertEquals("XML syntax doesn't match.",switches[1],((JCheckBox JavaDoc)comp[11]).isSelected());
142         testCase.assertEquals("Script invalid doesn't match.",switches[2],((JCheckBox JavaDoc)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 JavaDoc)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 JavaDoc)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 JavaDoc panel = getInnerSectionPanel(servlets[0]);
161         Component JavaDoc[] 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 JavaDoc panel = getInnerSectionPanel(filters[0]);
170         Component JavaDoc[] comp = panel.getComponents();
171         FilterParamsPanel tablePanel = ((FilterParamsPanel)comp[9]);
172         return (DDBeanTableModel) tablePanel.getTable().getModel();
173     }
174     
175     public void testProperties(CommonDDBean bean, String JavaDoc[] properties, Object JavaDoc[] values) throws Exception JavaDoc {
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 JavaDoc {
182         new StepIterator() {
183             SaveCookie saveCookie;
184             public boolean step() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc() {
208             public void run() {
209                 finished[0] = true;
210             }
211         });
212         new StepIterator() {
213             public boolean step() throws Exception JavaDoc {
214                 return finished[0];
215             }
216         };
217     }
218     
219     public JPanel JavaDoc getInnerSectionPanel(Object JavaDoc 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 JavaDoc p = sp.getInnerPanel();
226         testCase.assertNotNull("Section panel has no inner panel",p);
227         return p;
228     }
229     
230     public void checkInDDXML(String JavaDoc findText) {
231         checkInDDXML(findText,true);
232     }
233     
234     public void checkInDDXML(String JavaDoc 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 JavaDoc document() {
244         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class);
245         javax.swing.text.Document JavaDoc document = editor.getDocument();
246         try {
247             String JavaDoc text = document.getText(0,document.getLength());
248             return text;
249         } catch (javax.swing.text.BadLocationException JavaDoc ex) {
250             throw new AssertionFailedErrorException("Failed to read the document: ",ex);
251         }
252     }
253     
254     public boolean contains(String JavaDoc findText) {
255         waitForDispatchThread();
256         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class);
257         javax.swing.text.Document JavaDoc document = editor.getDocument();
258         try {
259             String JavaDoc text = document.getText(0,document.getLength());
260             Pattern JavaDoc p = Pattern.compile(findText,Pattern.DOTALL);
261             Matcher JavaDoc m = p.matcher(text.subSequence(0,text.length()-1));
262             return m.matches();
263         } catch (javax.swing.text.BadLocationException JavaDoc ex) {
264             throw new AssertionFailedErrorException("Failed to read the document: ",ex);
265         }
266     }
267     
268     public void checkNotInDDXML(String JavaDoc findText) {
269         checkInDDXML(findText,false);
270     }
271     
272     public void testStringArrayEquals(String JavaDoc[] expected,String JavaDoc[] 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 JavaDoc component,String JavaDoc text) {
280         component.requestFocus();
281         waitForDispatchThread();
282         Document JavaDoc doc = component.getDocument();
283         try {
284             doc.remove(0,doc.getLength());
285             doc.insertString(0,text,null);
286         } catch (BadLocationException JavaDoc ex) {
287             testCase.fail(ex);
288         }
289         ddObj.modelUpdatedFromUI();
290         waitForDispatchThread();
291     }
292 }
293
Popular Tags