KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > SourceRootsTest


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;
21
22 import java.net.URL JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Collections JavaDoc;
29 import org.openide.filesystems.FileObject;
30 import org.openide.util.Lookup;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.modules.web.project.test.TestBase;
35 import org.netbeans.modules.web.project.test.TestUtil;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.openide.filesystems.FileUtil;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41 import org.w3c.dom.Document JavaDoc;
42
43 public class SourceRootsTest extends NbTestCase {
44
45     public SourceRootsTest (String JavaDoc testName) {
46         super(testName);
47     }
48
49     private FileObject projdir;
50     private FileObject sources;
51     private FileObject tests;
52     private WebProject pp;
53     private AntProjectHelper helper;
54
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         
58         TestBase.setLookup(new Object JavaDoc[] {
59             new org.netbeans.modules.web.project.WebProjectType(),
60             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation()
61         });
62         
63         File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/WebApplication1");
64         projdir = FileUtil.toFileObject(f);
65         sources = projdir.getFileObject("src/java");
66         tests = projdir.getFileObject("test");
67         Project p = ProjectManager.getDefault().findProject(projdir);
68         assertTrue("Invalid project type",p instanceof WebProject);
69         pp = (WebProject) p;
70         helper = pp.getAntProjectHelper();
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         projdir = null;
75         sources = null;
76         tests = null;
77         pp = null;
78         helper = null;
79         TestUtil.setLookup(Lookup.EMPTY);
80         super.tearDown();
81     }
82
83     public void testSourceRoots () throws Exception JavaDoc {
84         SourceRoots sources = pp.getSourceRoots();
85         String JavaDoc[] srcProps = sources.getRootProperties();
86         assertNotNull ("Source properties can not be null",srcProps);
87         assertEquals ("Source properties length must be 1",1,srcProps.length);
88         assertEquals("Source property should be src.dir","src.dir",srcProps[0]);
89         FileObject[] srcFos = sources.getRoots();
90         assertNotNull ("Roots can not be null",srcFos);
91         assertEquals ("Roots length must be 1",1,srcFos.length);
92         assertEquals("Root should be "+this.sources.getPath(),this.sources,srcFos[0]);
93         URL JavaDoc[] srcURLs = sources.getRootURLs();
94         assertNotNull ("Root URLs can not be null",srcURLs);
95         assertEquals ("Root URLs length must be 1",1,srcURLs.length);
96         assertEquals("Root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]);
97         SourceRoots tests = pp.getTestSourceRoots();
98         srcProps = tests.getRootProperties();
99         assertNotNull ("Source properties can not be null",srcProps);
100         assertEquals ("Source properties length must be 1",1,srcProps.length);
101         assertEquals("Source property should be test.src.dir","test.src.dir",srcProps[0]);
102         srcFos = tests.getRoots();
103         assertNotNull ("Roots can not be null",srcFos);
104         assertEquals ("Roots length must be 1",1,srcFos.length);
105         assertEquals("Root should be "+this.tests.getPath(),this.tests,srcFos[0]);
106         srcURLs = tests.getRootURLs();
107         assertNotNull ("Root URLs can not be null",srcURLs);
108         assertEquals ("Root URLs length must be 1",1,srcURLs.length);
109         assertEquals("Root URLs should be "+this.tests.getURL(),this.tests.getURL(),srcURLs[0]);
110         //Now add new source root
111
TestListener tl = new TestListener();
112         sources.addPropertyChangeListener (tl);
113         FileObject newRoot = addSourceRoot (helper, projdir, "src.other.dir","other");
114         srcProps = sources.getRootProperties();
115         assertNotNull ("Source properties can not be null",srcProps);
116         assertEquals ("Source properties length must be 2",2,srcProps.length);
117         assertEquals("The first source property should be src.dir","src.dir",srcProps[0]);
118         assertEquals("The second source property should be src.other.dir","src.other.dir",srcProps[1]);
119         srcFos = sources.getRoots();
120         assertNotNull ("Roots can not be null",srcFos);
121         assertEquals ("Roots length must be 2",2,srcFos.length);
122         assertEquals("The first root should be "+this.sources.getPath(),this.sources,srcFos[0]);
123         assertEquals("The second root should be "+newRoot.getPath(),newRoot,srcFos[1]);
124         srcURLs = sources.getRootURLs();
125         assertNotNull ("Root URLs can not be null",srcURLs);
126         assertEquals ("Root URLs length must be 2",2,srcURLs.length);
127         assertEquals("The first root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]);
128         assertEquals("The second root URLs should be "+newRoot.getURL(),newRoot.getURL(),srcURLs[1]);
129         Set JavaDoc events = tl.getEvents();
130         assertTrue ("PROP_ROOT_PROPERTIES has to be fired",events.contains(SourceRoots.PROP_ROOT_PROPERTIES));
131         assertTrue ("PROP_ROOTS has to be fired",events.contains(SourceRoots.PROP_ROOTS));
132         tl.reset();
133         newRoot = changeSourceRoot (helper, projdir, "src.other.dir","other2");
134         srcProps = sources.getRootProperties();
135         assertNotNull ("Source properties can not be null",srcProps);
136         assertEquals ("Source properties length must be 2",2,srcProps.length);
137         assertEquals("The first source property should be src.dir","src.dir",srcProps[0]);
138         assertEquals("The second source property should be src.other.dir","src.other.dir",srcProps[1]);
139         srcFos = sources.getRoots();
140         assertNotNull ("Roots can not be null",srcFos);
141         assertEquals ("Roots length must be 2",2,srcFos.length);
142         assertEquals("The first root should be "+this.sources.getPath(),this.sources,srcFos[0]);
143         assertEquals("The second root should be "+newRoot.getPath(),newRoot,srcFos[1]);
144         srcURLs = sources.getRootURLs();
145         assertNotNull ("Root URLs can not be null",srcURLs);
146         assertEquals ("Root URLs length must be 2",2,srcURLs.length);
147         assertEquals("The first root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]);
148         assertEquals("The second root URLs should be "+newRoot.getURL(),newRoot.getURL(),srcURLs[1]);
149         events = tl.getEvents();
150         assertTrue ("Only PROP_ROOTS has to be fired", events.size() == 1 && events.contains(SourceRoots.PROP_ROOTS));
151         sources.removePropertyChangeListener(tl);
152     }
153
154     public static FileObject addSourceRoot (AntProjectHelper helper, FileObject projdir,
155                                             String JavaDoc propName, String JavaDoc folderName) throws Exception JavaDoc {
156         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
157         NodeList JavaDoc nl = data.getElementsByTagNameNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
158         assert nl.getLength() == 1;
159         Element JavaDoc roots = (Element JavaDoc) nl.item(0);
160         Document JavaDoc doc = roots.getOwnerDocument();
161         Element JavaDoc root = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root");
162         root.setAttributeNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"id",propName);
163         roots.appendChild (root);
164         helper.putPrimaryConfigurationData (data,true);
165         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
166         props.put (propName,folderName);
167         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
168         FileObject fo = projdir.getFileObject(folderName);
169         if (fo==null) {
170             fo = projdir.createFolder(folderName);
171         }
172         return fo;
173     }
174
175     public static FileObject changeSourceRoot (AntProjectHelper helper, FileObject projdir,
176                                                String JavaDoc propName, String JavaDoc folderName) throws Exception JavaDoc {
177         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
178         assert props.containsKey(propName);
179         props.put (propName,folderName);
180         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
181         FileObject fo = projdir.getFileObject(folderName);
182         if (fo==null) {
183             fo = projdir.createFolder(folderName);
184         }
185         return fo;
186     }
187
188     private static final class TestListener implements PropertyChangeListener JavaDoc {
189         Set JavaDoc events = new HashSet JavaDoc ();
190
191         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
192             String JavaDoc propName = evt.getPropertyName();
193             if (propName != null) {
194                 this.events.add (propName);
195             }
196         }
197
198         public void reset () {
199             this.events.clear();
200         }
201
202         public Set JavaDoc getEvents () {
203             return Collections.unmodifiableSet(this.events);
204         }
205     }
206
207 }
208
Popular Tags