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.api.project.libraries;
043:
044: import static org.netbeans.modules.project.libraries.TestUtil.*;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Collections;
050: import java.util.List;
051: import java.util.StringTokenizer;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.project.libraries.ui.LibrariesModel;
054: import org.netbeans.spi.project.libraries.LibraryFactory;
055: import org.netbeans.spi.project.libraries.LibraryImplementation;
056: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
057: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.Repository;
060: import org.openide.loaders.DataFolder;
061: import org.openide.loaders.InstanceDataObject;
062: import org.openide.util.lookup.Lookups;
063: import org.openide.util.test.MockLookup;
064: import org.openide.util.test.MockPropertyChangeListener;
065:
066: public class LibraryManagerTest extends NbTestCase {
067:
068: private WLP lp;
069:
070: private static final String LIBRARY_TYPE = "j2se"; //NOI18N
071: private static final String[] VOLUME_TYPES = new String[] { "bin",
072: "src", "doc" };
073:
074: public LibraryManagerTest(String testName) {
075: super (testName);
076: }
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080: lp = new WLP();
081: MockLookup.setLookup(Lookups.singleton(lp));
082: registerLibraryTypeProvider();
083: }
084:
085: private static void registerLibraryTypeProvider() throws Exception {
086: StringTokenizer tk = new StringTokenizer(
087: "org-netbeans-api-project-libraries/LibraryTypeProviders",
088: "/");
089: FileObject root = Repository.getDefault()
090: .getDefaultFileSystem().getRoot();
091: while (tk.hasMoreElements()) {
092: String pathElement = tk.nextToken();
093: FileObject tmp = root.getFileObject(pathElement);
094: if (tmp == null) {
095: tmp = root.createFolder(pathElement);
096: }
097: root = tmp;
098: }
099: if (root.getChildren().length == 0) {
100: InstanceDataObject.create(DataFolder.findFolder(root),
101: "TestLibraryTypeProvider",
102: TestLibraryTypeProvider.class);
103: }
104: }
105:
106: public void testGetLibraries() throws Exception {
107: LibraryManager lm = LibraryManager.getDefault();
108: MockPropertyChangeListener pcl = new MockPropertyChangeListener();
109: lm.addPropertyChangeListener(pcl);
110: Library[] libs = lm.getLibraries();
111: assertEquals("Libraries count", 0, libs.length);
112: LibraryImplementation[] impls = createTestLibs();
113: lp.set(impls);
114: libs = lm.getLibraries();
115: pcl.assertEvents(LibraryManager.PROP_LIBRARIES);
116: assertEquals("Libraries count", 2, libs.length);
117: assertLibsEquals(libs, impls);
118: lp.set();
119: pcl.assertEvents(LibraryManager.PROP_LIBRARIES);
120: libs = lm.getLibraries();
121: assertEquals("Libraries count", 0, libs.length);
122: }
123:
124: public void testGetLibrary() throws Exception {
125: LibraryImplementation[] impls = createTestLibs();
126: lp.set(impls);
127: LibraryManager lm = LibraryManager.getDefault();
128: Library[] libs = lm.getLibraries();
129: assertEquals("Libraries count", 2, libs.length);
130: assertLibsEquals(libs, impls);
131: Library lib = lm.getLibrary("Library1");
132: assertNotNull("Existing library", lib);
133: assertLibsEquals(new Library[] { lib },
134: new LibraryImplementation[] { impls[0] });
135: lib = lm.getLibrary("Library2");
136: assertNotNull("Existing library", lib);
137: assertLibsEquals(new Library[] { lib },
138: new LibraryImplementation[] { impls[1] });
139: lib = lm.getLibrary("Library3");
140: assertNull("Nonexisting library", lib);
141: }
142:
143: @SuppressWarnings("deprecation")
144: public void testAddRemoveLibrary() throws Exception {
145: final LibraryImplementation[] impls = createTestLibs();
146: lp.set(impls);
147: final LibraryManager lm = LibraryManager.getDefault();
148: Library[] libs = lm.getLibraries();
149: assertEquals("Libraries count", 2, libs.length);
150: assertLibsEquals(libs, impls);
151: final LibraryTypeProvider provider = LibrariesSupport
152: .getLibraryTypeProvider(LIBRARY_TYPE);
153: assertNotNull(provider);
154: final LibraryImplementation newLibImplementation = provider
155: .createLibrary();
156: newLibImplementation.setName("NewLib");
157: final Library newLibrary = LibraryFactory
158: .createLibrary(newLibImplementation);
159: lm.addLibrary(newLibrary);
160: libs = lm.getLibraries();
161: assertEquals("Libraries count", 3, libs.length);
162: List<LibraryImplementation> newLibs = new ArrayList<LibraryImplementation>(
163: Arrays.asList(impls));
164: newLibs.add(newLibImplementation);
165: assertLibsEquals(libs, newLibs
166: .toArray(new LibraryImplementation[newLibs.size()]));
167: lm.removeLibrary(newLibrary);
168: libs = lm.getLibraries();
169: assertEquals("Libraries count", 2, libs.length);
170: assertLibsEquals(libs, impls);
171: }
172:
173: public void testCreateRemoveLibrary() throws Exception {
174: LibraryManager mgr = LibraryManager.getDefault();
175: URL fooJar = mkJar("foo.jar");
176: Library lib = mgr.createLibrary(LIBRARY_TYPE, "NewLib",
177: Collections.singletonMap("bin", Arrays.asList(fooJar)));
178: assertEquals(mgr, lib.getManager());
179: assertEquals(Collections.singletonList(lib), Arrays.asList(mgr
180: .getLibraries()));
181: assertEquals(1, lp.libs.size());
182: assertLibsEquals(new Library[] { lib }, lp.libs
183: .toArray(new LibraryImplementation[1]));
184: mgr.removeLibrary(lib);
185: assertEquals(Collections.emptyList(), Arrays.asList(mgr
186: .getLibraries()));
187: assertEquals(0, lp.libs.size());
188: }
189:
190: public void testArealLibraryManagers() throws Exception {
191: ALP alp = new ALP();
192: MockLookup.setLookup(Lookups.fixed(lp, alp));
193: new LibrariesModel().createArea();
194: Area home = new Area("home");
195: Area away = new Area("away");
196: alp.setOpen(home, away);
197: List<String> locations = new ArrayList<String>(); // use list, not set, to confirm size also
198: for (LibraryManager mgr : LibraryManager.getOpenManagers()) {
199: URL loc = mgr.getLocation();
200: locations.add(loc != null ? loc.toString() : "<none>");
201: }
202: Collections.sort(locations);
203: assertEquals(
204: "[<none>, http://nowhere.net/away, http://nowhere.net/home, http://nowhere.net/new]",
205: locations.toString());
206: alp.setOpen();
207: try {
208: LibraryManager.forLocation(new URL(
209: "http://even-less-anywhere.net/"));
210: fail();
211: } catch (IllegalArgumentException x) {
212: }
213: LibraryManager mgr = LibraryManager.forLocation(home
214: .getLocation());
215: assertEquals(home.getLocation(), mgr.getLocation());
216: assertEquals("home", mgr.getDisplayName());
217: assertEquals(0, mgr.getLibraries().length);
218: MockPropertyChangeListener pcl = new MockPropertyChangeListener();
219: mgr.addPropertyChangeListener(pcl);
220: URL fooJar = mkJar("foo.jar");
221: Library lib = mgr.createLibrary(LIBRARY_TYPE, "NewLib",
222: Collections.singletonMap("bin", Arrays.asList(fooJar)));
223: assertEquals(mgr, lib.getManager());
224: pcl.assertEvents(LibraryManager.PROP_LIBRARIES);
225: assertEquals(Collections.singletonList(lib), Arrays.asList(mgr
226: .getLibraries()));
227: assertEquals(1, alp.libs.get(home).size());
228: LibraryImplementation impl = alp.libs.get(home).iterator()
229: .next();
230: assertEquals("NewLib", impl.getName());
231: assertEquals(LIBRARY_TYPE, impl.getType());
232: assertEquals(Arrays.asList(fooJar), impl.getContent("bin"));
233: mgr.removeLibrary(lib);
234: pcl.assertEvents(LibraryManager.PROP_LIBRARIES);
235: assertEquals(Collections.emptyList(), Arrays.asList(mgr
236: .getLibraries()));
237: assertEquals(0, alp.libs.get(home).size());
238: }
239:
240: static LibraryImplementation[] createTestLibs()
241: throws MalformedURLException {
242: LibraryImplementation[] impls = {
243: LibrariesSupport.createLibraryImplementation(
244: LIBRARY_TYPE, VOLUME_TYPES),
245: LibrariesSupport.createLibraryImplementation(
246: LIBRARY_TYPE, VOLUME_TYPES), };
247: impls[0].setName("Library1");
248: impls[1].setName("Library2");
249: impls[0].setDescription("Library1 description");
250: impls[1].setDescription("Library2 description");
251: impls[0].setContent("bin", Collections.singletonList(new URL(
252: "file:/lib/libest1.so")));
253: impls[1].setContent("bin", Collections.singletonList(new URL(
254: "file:/lib/libest2.so")));
255: return impls;
256: }
257:
258: static void assertLibsEquals(Library[] libs,
259: LibraryImplementation[] impls) {
260: assertEquals("libs equals (size)", impls.length, libs.length);
261: for (int i = 0; i < libs.length; i++) {
262: assertEquals("libs equals (name)", impls[i].getName(),
263: libs[i].getName());
264: assertEquals("libs equals (description)", impls[i]
265: .getDescription(), libs[i].getDescription());
266: List lc = libs[i].getContent("bin");
267: List ic = impls[i].getContent("bin");
268: assertEquals("libs equals (content bin)", ic, lc);
269: lc = libs[i].getContent("src");
270: ic = impls[i].getContent("src");
271: assertEquals("libs equals (content src)", ic, lc);
272: lc = libs[i].getContent("doc");
273: ic = impls[i].getContent("doc");
274: assertEquals("libs equals (content doc)", ic, lc);
275: }
276: }
277:
278: public static class TestLibraryTypeProvider implements
279: LibraryTypeProvider {
280:
281: public String getDisplayName() {
282: return LIBRARY_TYPE;
283: }
284:
285: public String getLibraryType() {
286: return LIBRARY_TYPE;
287: }
288:
289: public String[] getSupportedVolumeTypes() {
290: return VOLUME_TYPES;
291: }
292:
293: public LibraryImplementation createLibrary() {
294: return LibrariesSupport.createLibraryImplementation(
295: LIBRARY_TYPE, VOLUME_TYPES);
296: }
297:
298: public void libraryDeleted(LibraryImplementation library) {
299: }
300:
301: public void libraryCreated(LibraryImplementation library) {
302: }
303:
304: public java.beans.Customizer getCustomizer(String volumeType) {
305: return null;
306: }
307:
308: public org.openide.util.Lookup getLookup() {
309: return null;
310: }
311: }
312:
313: }
|