KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > JspSourcePathImplementationTest


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.web.project.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.ProjectManager;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.web.project.WebProject;
30 import org.netbeans.modules.web.project.test.TestBase;
31 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
32 import org.netbeans.spi.java.classpath.PathResourceImplementation;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.netbeans.spi.project.support.ant.EditableProperties;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  *
40  * @author Andrei Badea
41  */

42 public class JspSourcePathImplementationTest extends NbTestCase {
43     
44     public JspSourcePathImplementationTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     public void setUp() throws Exception JavaDoc {
49         // just in order to add our repository implementation
50
TestBase.setLookup(new Object JavaDoc[0]);
51     }
52     
53     public void testJspSourcePathImplementation() throws Exception JavaDoc {
54         File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/WebApplication1");
55         FileObject projectDir = FileUtil.toFileObject(f);
56         FileUtil.createFolder(projectDir, "web2");
57         
58         Project p = ProjectManager.getDefault().findProject(projectDir);
59         
60         // XXX should not cast a Project
61
final AntProjectHelper helper = ((WebProject)p).getAntProjectHelper();
62         
63         JspSourcePathImplementation jspi = new JspSourcePathImplementation(helper, helper.getStandardPropertyEvaluator());
64         
65         final boolean[] changed = new boolean[1];
66         
67         jspi.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
68             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
69                 if (evt.getPropertyName().equals(JspSourcePathImplementation.PROP_RESOURCES)) {
70                     changed[0] = true;
71                 }
72             }
73         });
74         
75         // simple test
76

77         PathResourceImplementation res = (PathResourceImplementation)jspi.getResources().get(0);
78         assertTrue(res.getRoots()[0].equals(helper.resolveFileObject("web").getURL()));
79         
80         // test the change of the web pages folder
81

82         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
83             public void run() {
84                 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
85                 props.setProperty(WebProjectProperties.WEB_DOCBASE_DIR, "web2");
86                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
87             }
88         });
89         
90         assertTrue(changed[0]);
91         res = (PathResourceImplementation)jspi.getResources().get(0);
92         assertTrue(res.getRoots()[0].equals(helper.resolveFileObject("web2").getURL()));
93         
94         // test the deletion of the web pages folder
95

96         changed[0] = false;
97         projectDir.getFileObject("web2").delete();
98         
99         assertTrue(changed[0]);
100         assertTrue(jspi.getResources().size() == 0);
101         
102         // test the recreation of the web pages folder
103

104         changed[0] = false;
105         FileUtil.createFolder(projectDir, "web2");
106         
107         assertTrue(changed[0]);
108         res = (PathResourceImplementation)jspi.getResources().get(0);
109         assertTrue(res.getRoots()[0].equals(helper.resolveFileObject("web2").getURL()));
110         
111         // test the deletion of web pages folder and changing it to an existing folder
112

113         changed[0] = false;
114         projectDir.getFileObject("web2").delete();
115         
116         assertTrue(changed[0]);
117         assertTrue(jspi.getResources().size() == 0);
118         
119         changed[0] = false;
120         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
121             public void run() {
122                 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
123                 props.setProperty(WebProjectProperties.WEB_DOCBASE_DIR, "web");
124                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
125             }
126         });
127         
128         assertTrue(changed[0]);
129         res = (PathResourceImplementation)jspi.getResources().get(0);
130         assertTrue(res.getRoots()[0].equals(helper.resolveFileObject("web").getURL()));
131         
132         // test the JSPI instance can be GC'd
133

134         WeakReference JavaDoc ref = new WeakReference JavaDoc(jspi);
135         jspi = null;
136         
137         assertGC("The JSPI instance could not be GC'd.", ref);
138     }
139 }
140
Popular Tags