KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > GlobalSourceForBinaryImplTest


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.apisupport.project.queries;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipOutputStream JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.java.queries.SourceForBinaryQuery;
33 import org.netbeans.modules.apisupport.project.TestBase;
34 import org.netbeans.modules.apisupport.project.Util;
35 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.filesystems.URLMapper;
39
40 /**
41  * Test functionality of GlobalSourceForBinaryImpl.
42  *
43  * @author Martin Krauskopf
44  */

45 public class GlobalSourceForBinaryImplTest extends TestBase {
46     
47     // Doesn't need to be precise and/or valid. Should show what actual
48
// GlobalSourceForBinaryImpl works with.
49
private static final String JavaDoc LOADERS_XML =
50             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
51             "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n" +
52             "<type>org.netbeans.modules.apisupport.project</type>\n" +
53             "<configuration>\n" +
54             "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n" +
55             "<code-name-base>org.openide.loaders</code-name-base>\n" +
56             "</data>\n" +
57             "</configuration>\n" +
58             "</project>\n";
59     
60     public GlobalSourceForBinaryImplTest(String JavaDoc name) {
61         super(name);
62     }
63     
64     public void testFindSourceRootForZipWithFirstLevelDepthNbBuild() throws Exception JavaDoc {
65         File JavaDoc nbSrcZip = generateNbSrcZip("");
66         NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip));
67         
68         URL JavaDoc loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar"));
69         URL JavaDoc loadersSrcURL = new URL JavaDoc(Util.urlForJar(nbSrcZip), "openide/loaders/src/");
70         assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
71     }
72     
73     public void testFindSourceRootForZipWithSecondLevelDepthNbBuild() throws Exception JavaDoc {
74         File JavaDoc nbSrcZip = generateNbSrcZip("netbeans-src/");
75         NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip));
76         
77         URL JavaDoc loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar"));
78         URL JavaDoc loadersSrcURL = new URL JavaDoc(Util.urlForJar(nbSrcZip), "netbeans-src/openide/loaders/src/");
79         assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
80     }
81     
82     // just sanity check that exception is not thrown
83
public void testBehaviourWithNonZipFile() throws Exception JavaDoc {
84         GlobalSourceForBinaryImpl.quiet = true;
85         File JavaDoc nbSrcZip = new File JavaDoc(getWorkDir(), "wrong-nbsrc.zip");
86         nbSrcZip.createNewFile();
87         NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip));
88         URL JavaDoc loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar"));
89         SourceForBinaryQuery.findSourceRoots(loadersURL).getRoots();
90     }
91     
92     public void testResolveSpecialNBSrcPaths() throws Exception JavaDoc {
93         String JavaDoc[] srcDirs = {"xtest/nbjunit/src",
94                             "xtest/nbjunit/ide/src",
95                             "performance/insanelib/src"};
96         String JavaDoc[] xtestJars = {"xtest/lib/nbjunit.jar",
97                             "xtest/lib/nbjunit-ide.jar",
98                             "xtest/lib/insanelib.jar"};
99         String JavaDoc[] ideJars = {"testtools/modules/org-netbeans-modules-nbjunit.jar",
100                             "testtools/modules/org-netbeans-modules-nbjunit-ide.jar",
101                             "testtools/modules/ext/insanelib.jar"};
102         for (int i = 0; i < srcDirs.length; i++) {
103             if(!file(srcDirs[i]).isDirectory()) {
104                 System.err.println("Skipping testResolveSpecialNBSrcPaths since "+srcDirs[i]+" is not checked out");
105                 continue;
106             }
107             assertRoot(Util.urlForJar(file(xtestJars[i])),
108                     FileUtil.toFileObject(file(srcDirs[i])));
109             File JavaDoc jarFile = new File JavaDoc(file("nbbuild/netbeans"), ideJars[i]);
110             if (jarFile.exists()) {
111                 assertResolved(ideJars[i], srcDirs[i]);
112             } else {
113                 assertEquals("no resolved root", 0,
114                         SourceForBinaryQuery.findSourceRoots(Util.urlForJar(jarFile)).getRoots().length);
115             }
116         }
117     }
118     
119     private void assertResolved(String JavaDoc jarInNBBuild, String JavaDoc dirInNBSrc) {
120         File JavaDoc jarFile = new File JavaDoc(file("nbbuild/netbeans"), jarInNBBuild);
121         assertRoot(Util.urlForJar(jarFile), FileUtil.toFileObject((file(dirInNBSrc))));
122     }
123     
124     public void testListeningToNbPlatform() throws Exception JavaDoc {
125         File JavaDoc nbSrcZip = generateNbSrcZip("");
126         URL JavaDoc loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar"));
127         SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots(loadersURL);
128         assertNotNull("got result", res);
129         ResultChangeListener resultCL = new ResultChangeListener();
130         res.addChangeListener(resultCL);
131         assertFalse("not changed yet", resultCL.changed);
132         assertEquals("non source root", 0, res.getRoots().length);
133         NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip));
134         assertTrue("changed yet", resultCL.changed);
135         assertEquals("one source root", 1, res.getRoots().length);
136         URL JavaDoc loadersSrcURL = new URL JavaDoc(Util.urlForJar(nbSrcZip), "openide/loaders/src/");
137         assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL));
138     }
139     
140     private File JavaDoc generateNbSrcZip(String JavaDoc topLevelEntry) throws IOException JavaDoc {
141         File JavaDoc zip = new File JavaDoc(getWorkDir(), "nbsrc.zip");
142         ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(zip));
143         if (topLevelEntry.length() != 0) {
144             zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry));
145         }
146         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "nbbuild/"));
147         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "nbbuild/nbproject/"));
148         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "nbbuild/nbproject/project.xml"));
149         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "openide/"));
150         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "openide/loaders/"));
151         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "openide/loaders/src/"));
152         zos.putNextEntry(new ZipEntry JavaDoc(topLevelEntry + "openide/loaders/nbproject/"));
153         ZipEntry JavaDoc loadersXML = new ZipEntry JavaDoc(topLevelEntry + "openide/loaders/nbproject/project.xml");
154         zos.putNextEntry(loadersXML);
155         try {
156             zos.write(LOADERS_XML.getBytes());
157         } finally {
158             zos.close();
159         }
160         return zip;
161     }
162     
163     private static void assertRoot(final URL JavaDoc loadersURL, final FileObject loadersSrcFO) {
164         assertEquals("right results for " + loadersURL,
165                 Collections.singletonList(loadersSrcFO),
166                 Arrays.asList(SourceForBinaryQuery.findSourceRoots(loadersURL).getRoots()));
167     }
168     
169     private static final class ResultChangeListener implements ChangeListener JavaDoc {
170         
171         private boolean changed;
172         
173         public void stateChanged(ChangeEvent JavaDoc e) {
174             changed = true;
175         }
176         
177     }
178     
179 }
180
Popular Tags