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.io.File;
045: import org.netbeans.api.project.ProjectUtils;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.NodeList;
048: import org.w3c.dom.Document;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.util.Lookup;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.Sources;
056: import org.netbeans.api.queries.SharabilityQuery;
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:
062: public class WebSharabilityQueryTest extends NbTestCase {
063:
064: public WebSharabilityQueryTest(String testName) {
065: super (testName);
066: }
067:
068: private FileObject scratch;
069: private FileObject projdir;
070: private FileObject sources;
071: private FileObject tests;
072: private FileObject docRoot;
073: private FileObject dist;
074: private FileObject build;
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 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:
090: scratch = TestUtil.makeScratchDir(this );
091: sources = projdir.getFileObject("src/java");
092: tests = projdir.getFileObject("test");
093: docRoot = projdir.getFileObject("web");
094: dist = FileUtil.createFolder(projdir, "dist");
095: build = FileUtil.createFolder(projdir, "build");
096: Project p = ProjectManager.getDefault().findProject(projdir);
097: assertTrue("Invalid project type", p instanceof WebProject);
098: pp = (WebProject) p;
099: helper = pp.getAntProjectHelper();
100: }
101:
102: protected void tearDown() throws Exception {
103: scratch = null;
104: projdir = null;
105: sources = null;
106: tests = null;
107: docRoot = null;
108: pp = null;
109: helper = null;
110: TestUtil.setLookup(Lookup.EMPTY);
111: super .tearDown();
112: }
113:
114: public void testSharability() throws Exception {
115: File f = FileUtil.toFile(this .sources);
116: int res = SharabilityQuery.getSharability(f);
117: assertEquals("Sources must be sharable",
118: SharabilityQuery.SHARABLE, res);
119: f = FileUtil.toFile(this .tests);
120: res = SharabilityQuery.getSharability(f);
121: assertEquals("Tests must be sharable",
122: SharabilityQuery.SHARABLE, res);
123: f = FileUtil.toFile(this .docRoot);
124: res = SharabilityQuery.getSharability(f);
125: assertEquals("Web Pages must be sharable",
126: SharabilityQuery.SHARABLE, res);
127: f = FileUtil.toFile(this .build);
128: res = SharabilityQuery.getSharability(f);
129: assertEquals("Build can't be sharable",
130: SharabilityQuery.NOT_SHARABLE, res);
131: f = FileUtil.toFile(this .dist);
132: res = SharabilityQuery.getSharability(f);
133: assertEquals("Dist can't be sharable",
134: SharabilityQuery.NOT_SHARABLE, res);
135: FileObject newSourceRoot = addSourceRoot(helper, projdir,
136: "src2.dir", new File(FileUtil.toFile(scratch),
137: "sources2"));
138: ProjectUtils.getSources(pp).getSourceGroups(
139: Sources.TYPE_GENERIC);
140: f = FileUtil.toFile(newSourceRoot);
141: res = SharabilityQuery.getSharability(f);
142: assertEquals("Sources2 must be sharable",
143: SharabilityQuery.SHARABLE, res);
144: FileObject newSourceRoot2 = changeSourceRoot(helper, projdir,
145: "src2.dir", new File(FileUtil.toFile(scratch),
146: "sources3"));
147: f = FileUtil.toFile(newSourceRoot2);
148: res = SharabilityQuery.getSharability(f);
149: assertEquals("Sources3 must be sharable",
150: SharabilityQuery.SHARABLE, res);
151: f = FileUtil.toFile(newSourceRoot);
152: res = SharabilityQuery.getSharability(f);
153: assertEquals("Sources2 must be unknown",
154: SharabilityQuery.UNKNOWN, res);
155: }
156:
157: public static FileObject addSourceRoot(AntProjectHelper helper,
158: FileObject projdir, String propName, File folder)
159: throws Exception {
160: if (!folder.exists()) {
161: FileUtil.createFolder(folder);
162: }
163: Element data = helper.getPrimaryConfigurationData(true);
164: NodeList nl = data.getElementsByTagNameNS(
165: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,
166: "source-roots");
167: assert nl.getLength() == 1;
168: Element roots = (Element) nl.item(0);
169: Document doc = roots.getOwnerDocument();
170: Element root = doc.createElementNS(
171: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root");
172: root.setAttributeNS(
173: WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "id",
174: propName);
175: roots.appendChild(root);
176: helper.putPrimaryConfigurationData(data, true);
177: EditableProperties props = helper
178: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
179: props.put(propName, folder.getAbsolutePath());
180: helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
181: props);
182: return FileUtil.toFileObject(folder);
183: }
184:
185: public static FileObject changeSourceRoot(AntProjectHelper helper,
186: FileObject projdir, String propName, File folder)
187: throws Exception {
188: if (!folder.exists()) {
189: FileUtil.createFolder(folder);
190: }
191: EditableProperties props = helper
192: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
193: assert props.containsKey(propName);
194: props.put(propName, folder.getAbsolutePath());
195: helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
196: props);
197: return FileUtil.toFileObject(folder);
198: }
199:
200: }
|