KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > libraries > JavadocForBinaryQueryLibraryImplTest


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.java.j2seplatform.libraries;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.jar.JarOutputStream JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
29 import org.netbeans.api.project.TestUtil;
30 import org.netbeans.core.startup.layers.ArchiveURLMapper;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.modules.java.j2seplatform.platformdefinition.JavaPlatformProviderImpl;
33 import org.netbeans.modules.project.libraries.DefaultLibraryImplementation;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.Lookup;
36 import org.openide.util.Utilities;
37 import org.openide.util.lookup.Lookups;
38 import org.netbeans.modules.masterfs.MasterURLMapper;
39
40 // XXX needs to test listening as well
41

42 /**
43  * JavadocForBinaryQueryLibraryImpl test
44  *
45  * @author David Konecny
46  */

47 public class JavadocForBinaryQueryLibraryImplTest extends NbTestCase implements Lookup.Provider {
48     
49     private Lookup lookup;
50     
51     
52     public JavadocForBinaryQueryLibraryImplTest(java.lang.String JavaDoc testName) {
53         super(testName);
54         TestUtil.setLookup(Lookups.proxy(this));
55     }
56     
57     private String JavaDoc getBase() throws Exception JavaDoc {
58         File JavaDoc dir = getWorkDir();
59         if (Utilities.isWindows()) {
60             dir = new File JavaDoc(dir.getCanonicalPath());
61         }
62         return dir.toString();
63     }
64     
65     protected void setUp() throws Exception JavaDoc {
66         System.setProperty("netbeans.user", getWorkDirPath());
67         super.setUp();
68         clearWorkDir();
69     }
70     
71     private void setupLibraries() throws Exception JavaDoc {
72         File JavaDoc dir = new File JavaDoc(getBase());
73         
74         // create library1:
75
String JavaDoc libPath = dir.toString() + "/library1";
76         File JavaDoc library = createJar(new File JavaDoc(libPath), "library1.jar", new String JavaDoc[]{"Main.class"});
77         File JavaDoc javadoc = new File JavaDoc(libPath+"/javadoc1");
78         javadoc.mkdir();
79         registerLibrary("library1", library, javadoc);
80         
81         // create library2:
82
libPath = dir.toString() + "/library2";
83         library = createJar(new File JavaDoc(libPath), "library2.jar", new String JavaDoc[]{"Main.class"});
84         javadoc = createJar(new File JavaDoc(libPath), "library2javadoc.jar", new String JavaDoc[]{"index.html"});
85         registerLibrary("library2", library, javadoc);
86         
87         // create library3:
88
libPath = dir.toString() + "/library3";
89         library = new File JavaDoc(libPath+"/library3");
90         library.mkdirs();
91         javadoc = new File JavaDoc(libPath+"/javadoc3");
92         javadoc.mkdirs();
93         registerLibrary("library3", library, javadoc);
94         
95         // refresh FS
96
FileUtil.toFileObject(dir).getFileSystem().refresh(false);
97     }
98     
99     private File JavaDoc createJar(File JavaDoc folder, String JavaDoc name, String JavaDoc resources[]) throws Exception JavaDoc {
100         folder.mkdirs();
101         File JavaDoc f = new File JavaDoc(folder,name);
102         if (!f.exists()) {
103             f.createNewFile();
104         }
105         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
106         for (int i = 0; i < resources.length; i++) {
107             jos.putNextEntry(new ZipEntry JavaDoc(resources[i]));
108         }
109         jos.close();
110         return f;
111     }
112     
113     private void registerLibrary(final String JavaDoc libName, final File JavaDoc cp, final File JavaDoc javadoc) throws Exception JavaDoc {
114         DefaultLibraryImplementation lib;
115         lib = new DefaultLibraryImplementation("j2se", new String JavaDoc[]{"classpath", "javadoc"});
116         ArrayList JavaDoc l = new ArrayList JavaDoc();
117         URL JavaDoc u = cp.toURI().toURL();
118         if (cp.getPath().endsWith(".jar")) {
119             u = FileUtil.getArchiveRoot(u);
120         }
121         l.add(u);
122         lib.setContent("classpath", l);
123         l = new ArrayList JavaDoc();
124         u = javadoc.toURI().toURL();
125         if (javadoc.getPath().endsWith(".jar")) {
126             u = FileUtil.getArchiveRoot(u);
127         }
128         l.add(u);
129         lib.setContent("javadoc", l);
130         LibraryProviderImpl prov = LibraryProviderImpl.getDefault();
131         prov.addLibrary(lib);
132     }
133     
134     public void testQuery() throws Exception JavaDoc {
135         setupLibraries();
136         
137         // library1: test that folder with javadoc is found for the jar
138
File JavaDoc f = new File JavaDoc(getBase()+"/library1/library1.jar");
139         URL JavaDoc u = f.toURI().normalize().toURL();
140         u = FileUtil.getArchiveRoot(u);
141         URL JavaDoc urls[] = JavadocForBinaryQuery.findJavadoc(u).getRoots();
142         assertEquals(1, urls.length);
143         String JavaDoc base = new File JavaDoc(getBase()).toURI().toString();
144         assertEquals(base+"library1/javadoc1/", urls[0].toExternalForm());
145         
146         // library2: test that jar with javadoc is found for the class from library jar
147
f = new File JavaDoc(getBase()+"/library2/library2.jar");
148         String JavaDoc us = f.toURI().normalize().toString();
149         us = "jar:" + us + "!/";
150         u = new URL JavaDoc(us);
151         urls = JavadocForBinaryQuery.findJavadoc(u).getRoots();
152         assertEquals(1, urls.length);
153         assertEquals("jar:"+base+"library2/library2javadoc.jar!/", urls[0].toExternalForm());
154         
155         // library2: test that folder with javadoc is found for the classpath root from the library
156
f = new File JavaDoc(getBase()+"/library3/library3");
157         u = f.toURI().normalize().toURL();
158         urls = JavadocForBinaryQuery.findJavadoc(u).getRoots();
159         assertEquals(1, urls.length);
160         assertEquals(base+"library3/javadoc3/", urls[0].toExternalForm());
161     }
162     
163     public synchronized Lookup getLookup() {
164         if (this.lookup == null) {
165             this.lookup = Lookups.fixed (
166                 new Object JavaDoc[] {
167                     LibraryProviderImpl.getDefault(),
168                     new JavaPlatformProviderImpl (),
169                     new ArchiveURLMapper (),
170                     new JavadocForBinaryQueryLibraryImpl(),
171                     new MasterURLMapper(),
172                 });
173         }
174         return this.lookup;
175     }
176     
177 }
178
Popular Tags