KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > ModuleNodeTest


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.modules.j2ee.earproject.ui;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.modules.j2ee.earproject.test.TestUtil;
27 import org.netbeans.modules.j2ee.earproject.ui.wizards.NewEarProjectWizardIteratorTest;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.nodes.Node;
31
32 /**
33  * Test functionality of {@link ModuleNode} and maybe more of EAR's
34  * {@link LogicalViewProvider logical view provider}.
35  *
36  * @author Martin Krauskopf
37  */

38 public class ModuleNodeTest extends NbTestCase {
39     
40     private static final int CHILDREN_UPDATE_TIME_OUT = 20000;
41     
42     private String JavaDoc serverInstanceID;
43     
44     public ModuleNodeTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         clearWorkDir();
50         serverInstanceID = TestUtil.registerSunAppServer(this);
51     }
52     
53     public void testRemoveFromJarContent() throws Exception JavaDoc {
54         File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "testEA");
55         String JavaDoc name = "Test EnterpriseApplication";
56         String JavaDoc j2eeLevel = "1.4";
57         String JavaDoc jarName = "testEA-ejb";
58         
59         // creates a project we will use for the import
60
NewEarProjectWizardIteratorTest.generateEARProject(prjDirF, name, j2eeLevel,
61                 serverInstanceID, null, null, jarName, null, null, null);
62         
63         Project earProject = ProjectManager.getDefault().findProject(FileUtil.toFileObject(prjDirF));
64         
65         LogicalViewProvider lvp = (LogicalViewProvider) earProject.getLookup().lookup(LogicalViewProvider.class);
66         assertNotNull("have a LogicalViewProvider", lvp);
67         Node root = lvp.createLogicalView();
68         LogicalViewNode j2eeModules = (LogicalViewNode) root.getChildren().findChild(LogicalViewNode.J2EE_MODULES_NAME);
69         assertSame("have ejb module's node", 1, j2eeModules.getChildren().getNodes(true).length);
70         
71         ModuleNode moduleNode = (ModuleNode) j2eeModules.getChildren().findChild(ModuleNode.MODULE_NODE_NAME);
72         assertNotNull("have modules node", moduleNode);
73         moduleNode.removeFromJarContent();
74         assertNumberOfNodes("ejb module removed", j2eeModules, 0);
75     }
76     
77     // See also issue #70943
78
public void testRemoveFromJarContentWithDeletedProject() throws Exception JavaDoc {
79         File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "testEA");
80         String JavaDoc name = "Test EnterpriseApplication";
81         String JavaDoc j2eeLevel = "1.4";
82         String JavaDoc jarName = "testEA-ejb";
83         
84         // creates a project we will use for the import
85
NewEarProjectWizardIteratorTest.generateEARProject(prjDirF, name, j2eeLevel,
86                 serverInstanceID, null, null, jarName, null, null, null);
87         
88         FileObject prjDirFO = FileUtil.toFileObject(prjDirF);
89         Project earProject = ProjectManager.getDefault().findProject(prjDirFO);
90         
91         LogicalViewProvider lvp = (LogicalViewProvider) earProject.getLookup().lookup(LogicalViewProvider.class);
92         assertNotNull("have a LogicalViewProvider", lvp);
93         Node root = lvp.createLogicalView();
94         LogicalViewNode j2eeModules = (LogicalViewNode) root.getChildren().findChild(LogicalViewNode.J2EE_MODULES_NAME);
95         assertSame("have ejb module's node", 1, j2eeModules.getChildren().getNodes(true).length);
96         
97         ModuleNode moduleNode = (ModuleNode) j2eeModules.getChildren().findChild(ModuleNode.MODULE_NODE_NAME);
98         assertNotNull("have modules node", moduleNode);
99         
100         // Simulata one of scenarios in #70943
101
FileObject ejbJarFO = prjDirFO.getFileObject("testEA-ejb");
102         ejbJarFO.delete();
103         moduleNode.removeFromJarContent();
104         j2eeModules.getChildren().getNodes(true);
105         
106         assertNumberOfNodes("ejb module removed", j2eeModules, 0);
107     }
108     
109     private void assertNumberOfNodes(final String JavaDoc message, final LogicalViewNode j2eeModules,
110             int expectedNumber) throws InterruptedException JavaDoc {
111         int waitTime = 0;
112         boolean failed = false;
113         while (!failed && j2eeModules.getChildren().getNodes(true).length != 0) {
114             failed = ++waitTime > CHILDREN_UPDATE_TIME_OUT/50;
115             Thread.sleep(50);
116         }
117         assertFalse(message, failed);
118     }
119     
120 }
121
Popular Tags