1 19 20 21 package org.netbeans.test.j2ee.multiview; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.jar.JarEntry ; 26 import java.util.jar.JarFile ; 27 import javax.swing.SwingUtilities ; 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 48 public class Utils { 49 private NbTestCase nbTestCase; 50 public static final String EJB_PROJECT_NAME = "TestCMP"; 51 public static final String EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME; 52 53 54 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 () { 62 public void run() { 63 finished[0] = true; 64 } 65 }); 66 while (!finished[0]) { 67 try { 68 Thread.sleep(200); 69 } catch (Exception e) { 70 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 71 } 72 } 73 } 74 75 void save(DataObject dObj) throws IOException { 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 findText)throws Exception { 85 Thread.sleep(3000); 86 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.getCookie(EditorCookie.class); 88 javax.swing.text.Document document = editor.getDocument(); 89 try { 90 String 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 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 methodName, String [] ddNames, String [] classNames) throws IOException { 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 (EJB_PROJECT_PATH + File.separator + "src" + File.separator + "conf"), ddNames, methodName+"_"); 125 } 126 if(classNames != null){ 127 utils.assertFiles(new File (EJB_PROJECT_PATH + File.separator + "src" + File.separator + "java" + File.separator + "cmp"), classNames, methodName+"_"); 128 } 129 } 130 131 } 132 | Popular Tags |