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.io.IOException;
046: import java.net.URL;
047: import java.util.Arrays;
048: import java.util.Collections;
049: import javax.swing.event.ChangeEvent;
050: import javax.swing.event.ChangeListener;
051: import org.netbeans.api.java.queries.SourceForBinaryQuery;
052: import org.netbeans.modules.apisupport.project.TestBase;
053: import org.netbeans.modules.apisupport.project.Util;
054: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.URLMapper;
058: import org.openide.filesystems.test.TestFileUtils;
059:
060: /**
061: * Test functionality of GlobalSourceForBinaryImpl.
062: *
063: * @author Martin Krauskopf
064: */
065: public class GlobalSourceForBinaryImplTest extends TestBase {
066:
067: // Doesn't need to be precise and/or valid. Should show what actual
068: // GlobalSourceForBinaryImpl works with.
069: private static final String LOADERS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
070: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n"
071: + "<type>org.netbeans.modules.apisupport.project</type>\n"
072: + "<configuration>\n"
073: + "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n"
074: + "<code-name-base>org.openide.loaders</code-name-base>\n"
075: + "</data>\n" + "</configuration>\n" + "</project>\n";
076:
077: public GlobalSourceForBinaryImplTest(String name) {
078: super (name);
079: }
080:
081: public void testFindSourceRootForZipWithFirstLevelDepthNbBuild()
082: throws Exception {
083: File nbSrcZip = generateNbSrcZip("");
084: NbPlatform.getDefaultPlatform().addSourceRoot(
085: Util.urlForJar(nbSrcZip));
086:
087: URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/"
088: + TestBase.CLUSTER_PLATFORM
089: + "/modules/org-openide-loaders.jar"));
090: URL loadersSrcURL = new URL(Util.urlForJar(nbSrcZip),
091: "openide/loaders/src/");
092: assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
093: }
094:
095: public void testFindSourceRootForZipWithSecondLevelDepthNbBuild()
096: throws Exception {
097: File nbSrcZip = generateNbSrcZip("netbeans-src/");
098: NbPlatform.getDefaultPlatform().addSourceRoot(
099: Util.urlForJar(nbSrcZip));
100:
101: URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/"
102: + TestBase.CLUSTER_PLATFORM
103: + "/modules/org-openide-loaders.jar"));
104: URL loadersSrcURL = new URL(Util.urlForJar(nbSrcZip),
105: "netbeans-src/openide/loaders/src/");
106: assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
107: }
108:
109: // just sanity check that exception is not thrown
110: public void testBehaviourWithNonZipFile() throws Exception {
111: GlobalSourceForBinaryImpl.quiet = true;
112: File nbSrcZip = new File(getWorkDir(), "wrong-nbsrc.zip");
113: nbSrcZip.createNewFile();
114: NbPlatform.getDefaultPlatform().addSourceRoot(
115: Util.urlForJar(nbSrcZip));
116: URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/"
117: + TestBase.CLUSTER_PLATFORM
118: + "/modules/org-openide-loaders.jar"));
119: SourceForBinaryQuery.findSourceRoots(loadersURL).getRoots();
120: }
121:
122: public void testResolveSpecialNBSrcPaths() throws Exception {
123: String[] srcDirs = { "xtest/nbjunit/src",
124: "xtest/nbjunit/ide/src", "o.n.insane/src" };
125: String[] xtestJars = { "xtest/lib/nbjunit.jar",
126: "xtest/lib/nbjunit-ide.jar", "xtest/lib/insanelib.jar" };
127: String[] ideJars = {
128: "testtools/modules/org-netbeans-modules-nbjunit.jar",
129: "testtools/modules/org-netbeans-modules-nbjunit-ide.jar",
130: "testtools/modules/ext/insanelib.jar" };
131: for (int i = 0; i < srcDirs.length; i++) {
132: if (!file(srcDirs[i]).isDirectory()) {
133: System.err
134: .println("Skipping testResolveSpecialNBSrcPaths since "
135: + srcDirs[i] + " is not checked out");
136: continue;
137: }
138: assertRoot(Util.urlForJar(file(xtestJars[i])), FileUtil
139: .toFileObject(file(srcDirs[i])));
140: File jarFile = new File(file("nbbuild/netbeans"),
141: ideJars[i]);
142: if (jarFile.exists()) {
143: assertResolved(ideJars[i], srcDirs[i]);
144: } else {
145: assertEquals(
146: "no resolved root",
147: 0,
148: SourceForBinaryQuery.findSourceRoots(
149: Util.urlForJar(jarFile)).getRoots().length);
150: }
151: }
152: }
153:
154: private void assertResolved(String jarInNBBuild, String dirInNBSrc) {
155: File jarFile = new File(file("nbbuild/netbeans"), jarInNBBuild);
156: assertRoot(Util.urlForJar(jarFile), FileUtil
157: .toFileObject((file(dirInNBSrc))));
158: }
159:
160: public void testListeningToNbPlatform() throws Exception {
161: File nbSrcZip = generateNbSrcZip("");
162: URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/"
163: + TestBase.CLUSTER_PLATFORM
164: + "/modules/org-openide-loaders.jar"));
165: SourceForBinaryQuery.Result res = SourceForBinaryQuery
166: .findSourceRoots(loadersURL);
167: assertNotNull("got result", res);
168: ResultChangeListener resultCL = new ResultChangeListener();
169: res.addChangeListener(resultCL);
170: assertFalse("not changed yet", resultCL.changed);
171: assertEquals("non source root", 0, res.getRoots().length);
172: NbPlatform.getDefaultPlatform().addSourceRoot(
173: Util.urlForJar(nbSrcZip));
174: assertTrue("changed yet", resultCL.changed);
175: assertEquals("one source root", 1, res.getRoots().length);
176: URL loadersSrcURL = new URL(Util.urlForJar(nbSrcZip),
177: "openide/loaders/src/");
178: assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
179: }
180:
181: private File generateNbSrcZip(String topLevelEntry)
182: throws IOException {
183: return FileUtil.toFile(TestFileUtils.writeZipFile(FileUtil
184: .toFileObject(getWorkDir()), "nbsrc.zip", topLevelEntry
185: + "nbbuild/nbproject/project.xml:", topLevelEntry
186: + "openide/loaders/src/dummy:", topLevelEntry
187: + "openide/loaders/nbproject/project.xml:"
188: + LOADERS_XML));
189: }
190:
191: private static void assertRoot(final URL loadersURL,
192: final FileObject loadersSrcFO) {
193: assertEquals("right results for " + loadersURL, Collections
194: .singletonList(loadersSrcFO), Arrays
195: .asList(SourceForBinaryQuery
196: .findSourceRoots(loadersURL).getRoots()));
197: }
198:
199: private static final class ResultChangeListener implements
200: ChangeListener {
201:
202: private boolean changed;
203:
204: public void stateChanged(ChangeEvent e) {
205: changed = true;
206: }
207:
208: }
209:
210: }
|