KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.test.j2ee.multiview;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.junit.AssertionFailedErrorException;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
31 import org.netbeans.modules.j2ee.ddloaders.multiview.EnterpriseBeansNode;
32 import org.netbeans.modules.j2ee.ddloaders.multiview.EntityNode;
33 import org.netbeans.modules.xml.multiview.SectionNode;
34 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement;
35 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport;
36 import org.netbeans.modules.xml.multiview.ui.SectionNodeView;
37 import org.openide.ErrorManager;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.cookies.SaveCookie;
40 import org.openide.loaders.DataObject;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43
44 /**
45  *
46  * @author blaha
47  */

48 public class Utils {
49     private NbTestCase nbTestCase;
50     public static final String JavaDoc EJB_PROJECT_NAME = "TestCMP";
51     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
52     
53     
54     /** Creates a new instance of Utils */
55     public Utils(NbTestCase nbTestCase) {
56         this.nbTestCase = nbTestCase;
57     }
58     
59     public static void waitForAWTDispatchThread() {
60         final boolean[] finished = new boolean[]{false};
61         SwingUtilities.invokeLater(new Runnable JavaDoc() {
62             public void run() {
63                 finished[0] = true;
64             }
65         });
66         while (!finished[0]) {
67             try {
68                 Thread.sleep(200);
69             } catch (Exception JavaDoc e) {
70                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
71             }
72         }
73     }
74     
75     void save(DataObject dObj) throws IOException JavaDoc{
76         SaveCookie saveCookie = (SaveCookie)dObj.getCookie(SaveCookie.class);
77         nbTestCase.assertNotNull("Save cookie is null, Data object isn't changed!", saveCookie);
78         if(saveCookie != null)
79             saveCookie.save();
80     }
81     
82     
83     
84     void checkInXML(EjbJarMultiViewDataObject ddObj, String JavaDoc findText)throws Exception JavaDoc{
85         Thread.sleep(3000);
86         //check editor in text node
87
XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class);
88         javax.swing.text.Document JavaDoc document = editor.getDocument();
89         try {
90             String JavaDoc text = document.getText(0,document.getLength());
91             int index = text.indexOf(findText);
92             nbTestCase.assertEquals("Cannot find correct element in XML view (editor document)",true,index>0);
93         } catch (javax.swing.text.BadLocationException JavaDoc ex) {
94             throw new AssertionFailedErrorException("Failed to read the document: ",ex);
95         }
96     }
97     
98     
99     public static EntityNode getEntityNode(EjbJarMultiViewDataObject ddObj){
100         ToolBarMultiViewElement toolBar = ddObj.getActiveMVElement();
101         SectionNodeView sectionView = (SectionNodeView)toolBar.getSectionView();
102         
103         Node[] n = getChildrenNodes(sectionView.getRootNode());
104         for(int i =0; i < n.length; i++){
105             if(n[i] instanceof EnterpriseBeansNode){
106                 Node[] nChild = getChildrenNodes((EnterpriseBeansNode)n[i]);
107                 for(int j = 0; j < nChild.length; j++){
108                     if(nChild[j] instanceof EntityNode)
109                         return (EntityNode)nChild[j];
110                 }
111             }
112         }
113         return null;
114     }
115     
116     public static Node[] getChildrenNodes(SectionNode parent){
117         Children nodes = parent.getChildren();
118         return nodes.getNodes();
119     }
120     
121     public void checkFiles(String JavaDoc methodName, String JavaDoc[] ddNames, String JavaDoc[] classNames) throws IOException JavaDoc{
122         org.netbeans.test.j2ee.lib.Utils utils = new org.netbeans.test.j2ee.lib.Utils(nbTestCase);
123         if(ddNames != null){
124             utils.assertFiles(new File JavaDoc(EJB_PROJECT_PATH + File.separator + "src" + File.separator + "conf"), ddNames, methodName+"_");
125         }
126         if(classNames != null){
127             utils.assertFiles(new File JavaDoc(EJB_PROJECT_PATH + File.separator + "src" + File.separator + "java" + File.separator + "cmp"), classNames, methodName+"_");
128         }
129     }
130     
131 }
132
Popular Tags