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.apisupport.project.queries;
043:
044: import java.io.File;
045: import java.net.URL;
046: import java.util.Arrays;
047: import java.util.Collections;
048: import java.util.jar.Manifest;
049: import org.netbeans.api.java.classpath.ClassPath;
050: import org.netbeans.api.java.queries.SourceForBinaryQuery;
051: import org.netbeans.api.project.ProjectManager;
052: import org.netbeans.modules.apisupport.project.NbModuleProject;
053: import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator;
054: import org.netbeans.modules.apisupport.project.TestBase;
055: import org.netbeans.modules.apisupport.project.Util;
056: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
057: import org.netbeans.spi.project.support.ant.PropertyUtils;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060:
061: /**
062: * Test functionality of SourceForBinaryImpl.
063: * @author Jesse Glick
064: */
065: public class SourceForBinaryImplTest extends TestBase {
066:
067: public SourceForBinaryImplTest(String name) {
068: super (name);
069: }
070:
071: public void testFindSourceRootForCompiledClasses() throws Exception {
072: doTestFindSourceRootForCompiledClasses("java.project/src",
073: "java.project/build/classes");
074: doTestFindSourceRootForCompiledClasses(
075: "java.project/test/unit/src",
076: "java.project/build/test/unit/classes");
077: doTestFindSourceRootForCompiledClasses("ant.freeform/src",
078: "ant.freeform/build/classes");
079: doTestFindSourceRootForCompiledClasses(
080: "ant.freeform/test/unit/src",
081: "ant.freeform/build/test/unit/classes");
082: }
083:
084: public void testExtraCompilationUnits() throws Exception {
085: doTestFindSourceRootForCompiledClasses(
086: "o.apache.tools.ant.module/src-bridge",
087: "o.apache.tools.ant.module/build/bridge-classes");
088: // Have to load at least one module to get the scan going.
089: ClassPath.getClassPath(
090: FileUtil.toFileObject(file("beans/src")),
091: ClassPath.COMPILE);
092: check("o.apache.tools.ant.module/src-bridge",
093: TestBase.CLUSTER_JAVA + "/ant/nblib/bridge.jar");
094: }
095:
096: public void testFindSourceRootForModuleJar() throws Exception {
097: ClassPath.getClassPath(FileUtil
098: .toFileObject(file("o.apache.tools.ant.module/src")),
099: ClassPath.COMPILE);
100: check("java.project/src", TestBase.CLUSTER_JAVA
101: + "/modules/org-netbeans-modules-java-project.jar");
102: check("openide.loaders/src", TestBase.CLUSTER_PLATFORM
103: + "/modules/org-openide-loaders.jar");
104: check("o.n.bootstrap/src", TestBase.CLUSTER_PLATFORM
105: + "/lib/boot.jar");
106: check("diff/src", TestBase.CLUSTER_IDE
107: + "/modules/org-netbeans-modules-diff.jar");
108: check("editor.lib/src", TestBase.CLUSTER_IDE
109: + "/modules/org-netbeans-modules-editor-lib.jar");
110: check("nbjunit/src",
111: "testtools/modules/org-netbeans-modules-nbjunit.jar");
112: check(
113: "apisupport.project/test/unit/src",
114: file("nbbuild/build/testdist/unit/"
115: + TestBase.CLUSTER_APISUPPORT
116: + "/org-netbeans-modules-apisupport-project/tests.jar"));
117: }
118:
119: public void testExternalModules() throws Exception {
120: ClassPath.getClassPath(
121: resolveEEP("/suite1/action-project/src"),
122: ClassPath.COMPILE);
123: check(
124: resolveEEPPath("/suite1/action-project/src"),
125: resolveEEPFile("/suite1/build/cluster/modules/org-netbeans-examples-modules-action.jar"));
126: ClassPath.getClassPath(resolveEEP("/suite3/dummy-project/src"),
127: ClassPath.COMPILE);
128: check(
129: resolveEEPPath("/suite3/dummy-project/src"),
130: resolveEEPFile("/suite3/dummy-project/build/cluster/modules/org-netbeans-examples-modules-dummy.jar"));
131: // test dependencies
132: ClassPath.getClassPath(
133: resolveEEP("/suite4/module1/test/unit/src"),
134: ClassPath.COMPILE);
135: check(
136: resolveEEPPath("/suite4/module1/test/unit/src"),
137: resolveEEPFile("/suite4/build/testdist/unit/cluster/module1/tests.jar"));
138: }
139:
140: public void testCompletionWorks_69735() throws Exception {
141: SuiteProject suite = generateSuite("suite");
142: NbModuleProject project = TestBase.generateSuiteComponent(
143: suite, "module");
144: File library = new File(getWorkDir(), "test-library-0.1_01.jar");
145: createJar(library, Collections.EMPTY_MAP, new Manifest());
146: FileObject libraryFO = FileUtil.toFileObject(library);
147: FileObject yyJar = FileUtil.copyFile(libraryFO, FileUtil
148: .toFileObject(getWorkDir()), "yy");
149:
150: // library wrapper
151: File suiteDir = suite.getProjectDirectoryFile();
152: File wrapperDirF = new File(new File(getWorkDir(), "suite"),
153: "wrapper");
154: NbModuleProjectGenerator.createSuiteLibraryModule(
155: wrapperDirF,
156: "yy", // 69735 - the same name as jar
157: "Testing Wrapper (yy)", // display name
158: "org/example/wrapper/resources/Bundle.properties",
159: suiteDir, // suite directory
160: null, new File[] { FileUtil.toFile(yyJar) });
161:
162: Util.addDependency(project, "yy");
163: ProjectManager.getDefault().saveProject(project);
164:
165: URL wrappedJar = Util.urlForJar(new File(wrapperDirF,
166: "release/modules/ext/yy.jar"));
167: assertEquals("no sources for wrapper", 0, SourceForBinaryQuery
168: .findSourceRoots(wrappedJar).getRoots().length);
169: }
170:
171: private void check(String srcS, File jarF) throws Exception {
172: File srcF = PropertyUtils.resolveFile(nbRootFile(), srcS);
173: FileObject src = FileUtil.toFileObject(srcF);
174: assertNotNull("have " + srcF, src);
175: URL u = FileUtil.getArchiveRoot(jarF.toURI().toURL());
176: assertEquals("right results for " + u, Collections
177: .singletonList(src), Arrays.asList(SourceForBinaryQuery
178: .findSourceRoots(u).getRoots()));
179: }
180:
181: private void check(String srcS, String jarS) throws Exception {
182: check(srcS, file("nbbuild/netbeans/" + jarS));
183: }
184:
185: private void doTestFindSourceRootForCompiledClasses(String srcPath,
186: String classesPath) throws Exception {
187: File classesF = file(classesPath);
188: File srcF = file(srcPath);
189: FileObject src = FileUtil.toFileObject(srcF);
190: assertNotNull("have " + srcF, src);
191: URL u = Util.urlForDir(classesF);
192: assertEquals("right source root for " + u, Collections
193: .singletonList(src), Arrays.asList(SourceForBinaryQuery
194: .findSourceRoots(u).getRoots()));
195: }
196:
197: }
|