001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.project;
043:
044: import java.net.URL;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeEvent;
047: import java.io.File;
048: import java.util.Set;
049: import java.util.HashSet;
050: import java.util.Collections;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.Lookup;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.api.project.ProjectManager;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.modules.java.api.common.SourceRoots;
057: import org.netbeans.modules.web.project.test.TestBase;
058: import org.netbeans.modules.web.project.test.TestUtil;
059: import org.netbeans.spi.project.support.ant.AntProjectHelper;
060: import org.netbeans.spi.project.support.ant.EditableProperties;
061: import org.openide.filesystems.FileUtil;
062: import org.w3c.dom.Element;
063: import org.w3c.dom.NodeList;
064: import org.w3c.dom.Document;
065:
066: public class SourceRootsTest extends NbTestCase {
067:
068: public SourceRootsTest(String testName) {
069: super (testName);
070: }
071:
072: private FileObject projdir;
073: private FileObject sources;
074: private FileObject tests;
075: private WebProject pp;
076: private AntProjectHelper helper;
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080:
081: TestBase
082: .setLookup(new Object[] {
083: new org.netbeans.modules.web.project.WebProjectType(),
084: new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation() });
085:
086: File f = new File(getDataDir().getAbsolutePath(),
087: "projects/WebApplication1");
088: projdir = FileUtil.toFileObject(f);
089: sources = projdir.getFileObject("src/java");
090: tests = projdir.getFileObject("test");
091: Project p = ProjectManager.getDefault().findProject(projdir);
092: assertTrue("Invalid project type", p instanceof WebProject);
093: pp = (WebProject) p;
094: helper = pp.getAntProjectHelper();
095: }
096:
097: protected void tearDown() throws Exception {
098: projdir = null;
099: sources = null;
100: tests = null;
101: pp = null;
102: helper = null;
103: TestUtil.setLookup(Lookup.EMPTY);
104: super .tearDown();
105: }
106:
107: public void testSourceRoots() throws Exception {
108: SourceRoots sources = pp.getSourceRoots();
109: String[] srcProps = sources.getRootProperties();
110: assertNotNull("Source properties can not be null", srcProps);
111: assertEquals("Source properties length must be 1", 1,
112: srcProps.length);
113: assertEquals("Source property should be src.dir", "src.dir",
114: srcProps[0]);
115: FileObject[] srcFos = sources.getRoots();
116: assertNotNull("Roots can not be null", srcFos);
117: assertEquals("Roots length must be 1", 1, srcFos.length);
118: assertEquals("Root should be " + this .sources.getPath(),
119: this .sources, srcFos[0]);
120: URL[] srcURLs = sources.getRootURLs();
121: assertNotNull("Root URLs can not be null", srcURLs);
122: assertEquals("Root URLs length must be 1", 1, srcURLs.length);
123: assertEquals("Root URLs should be " + this .sources.getURL(),
124: this .sources.getURL(), srcURLs[0]);
125: SourceRoots tests = pp.getTestSourceRoots();
126: srcProps = tests.getRootProperties();
127: assertNotNull("Source properties can not be null", srcProps);
128: assertEquals("Source properties length must be 1", 1,
129: srcProps.length);
130: assertEquals("Source property should be test.src.dir",
131: "test.src.dir", srcProps[0]);
132: srcFos = tests.getRoots();
133: assertNotNull("Roots can not be null", srcFos);
134: assertEquals("Roots length must be 1", 1, srcFos.length);
135: assertEquals("Root should be " + this .tests.getPath(),
136: this .tests, srcFos[0]);
137: srcURLs = tests.getRootURLs();
138: assertNotNull("Root URLs can not be null", srcURLs);
139: assertEquals("Root URLs length must be 1", 1, srcURLs.length);
140: assertEquals("Root URLs should be " + this .tests.getURL(),
141: this .tests.getURL(), srcURLs[0]);
142: //Now add new source root
143: TestListener tl = new TestListener();
144: sources.addPropertyChangeListener(tl);
145: FileObject newRoot = addSourceRoot(helper, projdir,
146: "src.other.dir", "other");
147: srcProps = sources.getRootProperties();
148: assertNotNull("Source properties can not be null", srcProps);
149: assertEquals("Source properties length must be 2", 2,
150: srcProps.length);
151: assertEquals("The first source property should be src.dir",
152: "src.dir", srcProps[0]);
153: assertEquals(
154: "The second source property should be src.other.dir",
155: "src.other.dir", srcProps[1]);
156: srcFos = sources.getRoots();
157: assertNotNull("Roots can not be null", srcFos);
158: assertEquals("Roots length must be 2", 2, srcFos.length);
159: assertEquals("The first root should be "
160: + this .sources.getPath(), this .sources, srcFos[0]);
161: assertEquals("The second root should be " + newRoot.getPath(),
162: newRoot, srcFos[1]);
163: srcURLs = sources.getRootURLs();
164: assertNotNull("Root URLs can not be null", srcURLs);
165: assertEquals("Root URLs length must be 2", 2, srcURLs.length);
166: assertEquals("The first root URLs should be "
167: + this .sources.getURL(), this .sources.getURL(),
168: srcURLs[0]);
169: assertEquals("The second root URLs should be "
170: + newRoot.getURL(), newRoot.getURL(), srcURLs[1]);
171: Set events = tl.getEvents();
172: assertTrue("PROP_ROOT_PROPERTIES has to be fired", events
173: .contains(SourceRoots.PROP_ROOT_PROPERTIES));
174: assertTrue("PROP_ROOTS has to be fired", events
175: .contains(SourceRoots.PROP_ROOTS));
176: tl.reset();
177: newRoot = changeSourceRoot(helper, projdir, "src.other.dir",
178: "other2");
179: srcProps = sources.getRootProperties();
180: assertNotNull("Source properties can not be null", srcProps);
181: assertEquals("Source properties length must be 2", 2,
182: srcProps.length);
183: assertEquals("The first source property should be src.dir",
184: "src.dir", srcProps[0]);
185: assertEquals(
186: "The second source property should be src.other.dir",
187: "src.other.dir", srcProps[1]);
188: srcFos = sources.getRoots();
189: assertNotNull("Roots can not be null", srcFos);
190: assertEquals("Roots length must be 2", 2, srcFos.length);
191: assertEquals("The first root should be "
192: + this .sources.getPath(), this .sources, srcFos[0]);
193: assertEquals("The second root should be " + newRoot.getPath(),
194: newRoot, srcFos[1]);
195: srcURLs = sources.getRootURLs();
196: assertNotNull("Root URLs can not be null", srcURLs);
197: assertEquals("Root URLs length must be 2", 2, srcURLs.length);
198: assertEquals("The first root URLs should be "
199: + this .sources.getURL(), this .sources.getURL(),
200: srcURLs[0]);
201: assertEquals("The second root URLs should be "
202: + newRoot.getURL(), newRoot.getURL(), srcURLs[1]);
203: events = tl.getEvents();
204: assertTrue("Only PROP_ROOTS has to be fired",
205: events.size() == 1
206: && events.contains(SourceRoots.PROP_ROOTS));
207: sources.removePropertyChangeListener(tl);
208: }
209:
210: public static FileObject addSourceRoot(AntProjectHelper helper,
211: FileObject projdir, String propName, String folderName)
212: throws Exception {
213: Element data = helper.getPrimaryConfigurationData(true);
214: NodeList nl = data.getElementsByTagNameNS(
215: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,
216: "source-roots");
217: assert nl.getLength() == 1;
218: Element roots = (Element) nl.item(0);
219: Document doc = roots.getOwnerDocument();
220: Element root = doc.createElementNS(
221: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root");
222: root.setAttributeNS(
223: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "id",
224: propName);
225: roots.appendChild(root);
226: helper.putPrimaryConfigurationData(data, true);
227: EditableProperties props = helper
228: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
229: props.put(propName, folderName);
230: helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
231: props);
232: FileObject fo = projdir.getFileObject(folderName);
233: if (fo == null) {
234: fo = projdir.createFolder(folderName);
235: }
236: return fo;
237: }
238:
239: public static FileObject changeSourceRoot(AntProjectHelper helper,
240: FileObject projdir, String propName, String folderName)
241: throws Exception {
242: EditableProperties props = helper
243: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
244: assert props.containsKey(propName);
245: props.put(propName, folderName);
246: helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
247: props);
248: FileObject fo = projdir.getFileObject(folderName);
249: if (fo == null) {
250: fo = projdir.createFolder(folderName);
251: }
252: return fo;
253: }
254:
255: private static final class TestListener implements
256: PropertyChangeListener {
257: Set events = new HashSet();
258:
259: public void propertyChange(PropertyChangeEvent evt) {
260: String propName = evt.getPropertyName();
261: if (propName != null) {
262: this .events.add(propName);
263: }
264: }
265:
266: public void reset() {
267: this .events.clear();
268: }
269:
270: public Set getEvents() {
271: return Collections.unmodifiableSet(this.events);
272: }
273: }
274:
275: }
|