001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.project.ant;
021:
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyChangeSupport;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.lang.reflect.Method;
032: import java.net.URL;
033: import java.util.ArrayList;
034: import java.util.Arrays;
035: import java.util.Collections;
036: import java.util.HashMap;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.Properties;
040: import java.util.StringTokenizer;
041: import java.util.TreeMap;
042: import java.util.regex.Matcher;
043: import java.util.zip.CRC32;
044: import java.util.zip.ZipEntry;
045: import java.util.zip.ZipOutputStream;
046: import org.netbeans.api.project.Project;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.api.project.TestUtil;
049: import org.netbeans.api.project.libraries.Library;
050: import org.netbeans.api.project.libraries.LibraryManager;
051: import org.netbeans.api.project.ui.OpenProjects;
052: import org.netbeans.api.queries.SharabilityQuery;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.spi.project.AuxiliaryConfiguration;
055: import org.netbeans.spi.project.libraries.LibraryImplementation;
056: import org.netbeans.spi.project.libraries.LibraryProvider;
057: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
058: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
059: import org.netbeans.spi.project.support.ant.AntBasedTestUtil;
060: import org.netbeans.spi.project.support.ant.AntProjectHelper;
061: import org.netbeans.spi.project.support.ant.EditableProperties;
062: import org.netbeans.spi.project.support.ant.ProjectGenerator;
063: import org.netbeans.spi.project.support.ant.PropertyProvider;
064: import org.netbeans.spi.queries.CollocationQueryImplementation;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.FileSystem;
067: import org.openide.filesystems.FileUtil;
068: import org.openide.filesystems.Repository;
069: import org.openide.loaders.DataFolder;
070: import org.openide.loaders.InstanceDataObject;
071: import org.openide.util.NbCollections;
072: import org.openide.util.lookup.Lookups;
073: import org.openide.util.test.MockChangeListener;
074: import org.openide.util.test.MockLookup;
075: import org.openide.util.test.MockPropertyChangeListener;
076: import org.openide.xml.XMLUtil;
077: import org.w3c.dom.Document;
078: import org.w3c.dom.Element;
079:
080: public class ProjectLibraryProviderTest extends NbTestCase {
081:
082: public ProjectLibraryProviderTest(String name) {
083: super (name);
084: }
085:
086: private FileObject projdir;
087: private AntProjectHelper helper;
088: private Project project;
089: private URL base;
090: private TestLibraryProvider libraryProvider;
091:
092: @Override
093: protected void setUp() throws Exception {
094: super .setUp();
095: libraryProvider = new TestLibraryProvider();
096: MockLookup
097: .setLookup(
098: Lookups
099: .fixed(
100: AntBasedTestUtil
101: .testAntBasedProjectType(),
102: AntBasedTestUtil
103: .testCollocationQueryImplementation(getWorkDir()),
104: libraryProvider),
105: // Filter out standard CQIs since they are bogus.
106: Lookups
107: .exclude(
108: Lookups
109: .metaInfServices(ProjectLibraryProviderTest.class
110: .getClassLoader()),
111: CollocationQueryImplementation.class));
112: projdir = TestUtil.makeScratchDir(this ).createFolder("prj");
113: helper = ProjectGenerator.createProject(projdir, "test");
114: project = ProjectManager.getDefault().findProject(projdir);
115: close(OpenProjects.getDefault().getOpenProjects());
116: base = getWorkDir().toURI().toURL();
117: ProjectLibraryProvider.FIRE_CHANGES_SYNCH = true;
118: registerTestLibraryTypeProvider();
119: }
120:
121: public void testPatternMatching() throws Exception {
122: Matcher matcher = ProjectLibraryProvider.LIBS_LINE
123: .matcher("libs.grapht.classpath");
124: assertTrue(matcher.matches());
125: assertEquals(matcher.group(2), "classpath");
126: matcher = ProjectLibraryProvider.LIBS_LINE
127: .matcher("libs.grapht_1_0.classpath");
128: assertTrue(matcher.matches());
129: assertEquals(matcher.group(2), "classpath");
130: matcher = ProjectLibraryProvider.LIBS_LINE
131: .matcher("libs.grapht_1_0.classpath2");
132: assertTrue(matcher.matches());
133: assertEquals(matcher.group(2), "classpath2");
134: matcher = ProjectLibraryProvider.LIBS_LINE
135: .matcher("libs.grapht_1_0.classpath_1");
136: assertTrue(matcher.matches());
137: assertEquals(matcher.group(2), "classpath_1");
138: matcher = ProjectLibraryProvider.LIBS_LINE
139: .matcher("libs.grapht-1.0.classpath");
140: assertTrue(matcher.matches());
141: assertEquals(matcher.group(2), "classpath");
142: matcher = ProjectLibraryProvider.LIBS_LINE
143: .matcher("libs.grapht-1.0.classpath_1");
144: assertTrue(matcher.matches());
145: assertEquals(matcher.group(2), "classpath_1");
146: matcher = ProjectLibraryProvider.LIBS_LINE
147: .matcher("libs.grapht-1.0.1-classpath");
148: assertTrue(matcher.matches());
149: assertEquals(matcher.group(2), "1-classpath");
150: }
151:
152: // XXX test name/type/description
153: // XXX test : vs. ; and / vs. \ (in <definitions> and in *.properties)
154: // XXX test set name, description
155:
156: public void testLibraryLoadingBasic() throws Exception {
157: writeProperties(
158: "libs/my libraries.properties",
159: "libs.jgraph.classpath=${base}/jgraph.jar:${base}/../extra libs/jgraph-extras.jar",
160: "libs.jgraph.javadoc=${base}/api/jgraph-docs:${base}/api/jgraph-docs.zip!/docs/api/",
161: "irrelevant=stuff");
162: storeDefs(project, "../libs/my libraries.properties");
163: Library lib = LibraryManager.forLocation(
164: new URL(base, "libs/my%20libraries.properties"))
165: .getLibrary("jgraph");
166: assertNotNull(lib);
167: assertEquals("jgraph", lib.getName());
168: assertEquals("jgraph", lib.getDisplayName());
169: assertNull(lib.getDescription());
170: assertEquals("j2se", lib.getType());
171: assertEquals(
172: Arrays
173: .asList(
174: new URL("jar:file:jgraph.jar!/"),
175: new URL(
176: "jar:file:../extra%20libs/jgraph-extras.jar!/")),
177: lib.getRawContent("classpath"));
178: assertEquals(Arrays.asList(new URL("file:api/jgraph-docs/"),
179: new URL("jar:file:api/jgraph-docs.zip!/docs/api/")),
180: lib.getRawContent("javadoc"));
181: assertEquals(Collections.emptyList(), lib.getContent("src"));
182: }
183:
184: public void testLibraryLoadingPrivateAbsolute() throws Exception {
185: writeProperties("libs/libraries.properties",
186: "libs.jgraph.classpath=${base}/jgraph.jar");
187: writeProperties("libs/libraries-private.properties",
188: "libs.jgraph.src="
189: + new File(getWorkDir(), "jgraph-src.zip"),
190: "libs.jgraph.javadoc="
191: + new File(getWorkDir(), "jgraph-api"));
192: storeDefs(project, "../libs/libraries.properties");
193: Library lib = LibraryManager.forLocation(
194: new URL(base, "libs/libraries.properties")).getLibrary(
195: "jgraph");
196: assertEquals(Collections.singletonList(new URL(
197: "jar:file:jgraph.jar!/")), lib
198: .getRawContent("classpath"));
199: assertEquals(Collections.singletonList(new URL("jar:" + base
200: + "jgraph-src.zip!/")), lib.getRawContent("src"));
201: assertEquals(Collections.singletonList(new URL(base,
202: "jgraph-api/")), lib.getRawContent("javadoc"));
203: }
204:
205: public void testPrivateOverridesSharedProperties() throws Exception {
206: writeProperties("libs/libraries.properties",
207: "libs.jgraph.classpath=${base}/jgraph.jar");
208: writeProperties("libs/libraries-private.properties",
209: "libs.jgraph.classpath="
210: + new File(getWorkDir(), "jgraph-api"));
211: storeDefs(project, "../libs/libraries.properties");
212: Library lib = LibraryManager.forLocation(
213: new URL(base, "libs/libraries.properties")).getLibrary(
214: "jgraph");
215: assertEquals(Collections.singletonList(new URL(base,
216: "jgraph-api/")), lib.getContent("classpath"));
217: }
218:
219: public void testSetContent() throws Exception {
220: writeProperties("libs/libraries.properties",
221: "libs.jgraph.classpath=");
222: storeDefs(project, "../libs/libraries.properties");
223: Library lib = LibraryManager.forLocation(
224: new URL(base, "libs/libraries.properties")).getLibrary(
225: "jgraph");
226: setLibraryContent(lib, "classpath", new URL(
227: "jar:file:jgraph.jar!/"), new URL(
228: "jar:file:../extra%20libs/jgraph-extras.jar!/"));
229: setLibraryContent(lib, "src", new URL(base,
230: "separate/jgraph-src/"), new URL(base,
231: "jgraph-other-src/"));
232: setLibraryContent(lib, "javadoc", new URL("jar:" + base
233: + "separate/jgraph-api.zip!/"), new URL(
234: "jar:file:../separate/jgraph-api.zip!/docs/api/"));
235: Map<String, String> m = new HashMap<String, String>();
236: File separate = new File(getWorkDir(), "separate");
237: m.put("libs.jgraph.classpath", "${base}/jgraph.jar"
238: + File.pathSeparatorChar
239: + "${base}/../extra libs/jgraph-extras.jar");
240: m.put("libs.jgraph.src", new File(separate, "jgraph-src")
241: .getAbsolutePath().replace('\\', '/')
242: + File.pathSeparator
243: + new File(getWorkDir(), "jgraph-other-src")
244: .getAbsolutePath().replace('\\', '/'));
245: m.put("libs.jgraph.javadoc", new File(separate,
246: "jgraph-api.zip").getAbsolutePath().replace('\\', '/')
247: + File.pathSeparator
248: + "${base}/../separate/jgraph-api.zip!/docs/api/");
249: assertEquals(m, loadProperties("libs/libraries.properties"));
250: }
251:
252: public void testAreaChangesFromProjectsOpenedClosed()
253: throws Exception {
254: storeDefs(project, "../libraries.properties");
255: assertEquals("[<none>]", openedLibraryManagers());
256: open(project);
257: assertEquals("[<none>, " + base + "libraries.properties]",
258: openedLibraryManagers());
259: close(project);
260: assertEquals("[<none>]", openedLibraryManagers());
261: Project project2 = ProjectManager.getDefault().findProject(
262: ProjectGenerator.createProject(
263: projdir.getParent().createFolder("prj2"),
264: "test").getProjectDirectory());
265: storeDefs(project2, "../lib2.properties");
266: open(project, project2);
267: assertEquals("[<none>, " + base + "lib2.properties, " + base
268: + "libraries.properties]", openedLibraryManagers());
269: close(project);
270: assertEquals("[<none>, " + base + "lib2.properties]",
271: openedLibraryManagers());
272: }
273:
274: private static String openedLibraryManagers() {
275: List<String> urls = new ArrayList<String>();
276: for (LibraryManager mgr : LibraryManager.getOpenManagers()) {
277: URL u = mgr.getLocation();
278: urls.add(u != null ? u.toExternalForm() : "<none>");
279: }
280: Collections.sort(urls);
281: return urls.toString();
282: }
283:
284: public void testChangesLibraries() throws Exception {
285: PropertyProvider pp = helper
286: .getProjectLibrariesPropertyProvider();
287: writeProperties("libraries.properties",
288: "libs.jgraph.classpath=");
289: storeDefs(project, "../libraries.properties");
290: LibraryManager mgr = LibraryManager.forLocation(new URL(base,
291: "libraries.properties"));
292: Library lib1 = mgr.getLibrary("jgraph");
293: assertEquals(Collections.emptyList(), lib1
294: .getContent("classpath"));
295: assertEquals("{libs.jgraph.classpath=}",
296: new TreeMap<String, String>(pp.getProperties())
297: .toString());
298: MockPropertyChangeListener liblist = new MockPropertyChangeListener(
299: LibraryManager.PROP_LIBRARIES);
300: MockPropertyChangeListener contentlist = new MockPropertyChangeListener(
301: Library.PROP_CONTENT);
302: mgr.addPropertyChangeListener(liblist);
303: lib1.addPropertyChangeListener(contentlist);
304: MockChangeListener pplist = new MockChangeListener();
305: pp.addChangeListener(pplist);
306: writeProperties("libraries.properties",
307: "libs.jgraph.classpath=${base}/jgraph",
308: "libs.collections.classpath=${base}/collections");
309: contentlist.assertEventCount(1);
310: assertEquals(
311: Collections.singletonList(new URL("file:jgraph/")),
312: lib1.getRawContent("classpath"));
313: liblist.assertEventCount(1);
314: assertEquals(lib1, mgr.getLibrary("jgraph"));
315: Library lib2 = mgr.getLibrary("collections");
316: assertEquals(Collections.singletonList(new URL(
317: "file:collections/")), lib2.getRawContent("classpath"));
318: pplist.assertEventCount(1);
319: assertEquals(
320: ("{libs.collections.classpath=" + getWorkDir()
321: + "/collections, libs.jgraph.classpath="
322: + getWorkDir() + "/jgraph}").replace('/',
323: File.separatorChar),
324: new TreeMap<String, String>(pp.getProperties())
325: .toString());
326: writeProperties("others.properties", "libs.jrcs.classpath=");
327: storeDefs(project, "../others.properties");
328: contentlist.assertEventCount(0);
329: liblist.assertEventCount(0);
330: // storeDefs() fires configurationXmlChanged twice - after put() and after save()
331: pplist.assertEventCount(2);
332: assertEquals(("{libs.jrcs.classpath=}").replace('/',
333: File.separatorChar), new TreeMap<String, String>(pp
334: .getProperties()).toString());
335: }
336:
337: public void testCreateRemoveLibrary() throws Exception {
338: LibraryManager mgr = LibraryManager.forLocation(new URL(base,
339: "libraries.properties"));
340: Map<String, List<URL>> content = new HashMap<String, List<URL>>();
341: content.put("classpath", Arrays.asList(new URL(
342: "jar:file:jh.jar!/"), new URL(
343: "jar:file:jh-search.jar!/")));
344: content.put("javadoc", Arrays.asList(new URL("file:jh-api/")));
345: Library lib = mgr.createLibrary("j2se", "javahelp", content);
346: assertEquals("j2se", lib.getType());
347: assertEquals("javahelp", lib.getName());
348: assertEquals(content.get("classpath"), lib
349: .getRawContent("classpath"));
350: assertEquals(content.get("javadoc"), lib
351: .getRawContent("javadoc"));
352: lib = mgr.createLibrary("j2me", "gps", Collections
353: .<String, List<URL>> emptyMap());
354: assertEquals("j2me", lib.getType());
355: assertEquals("gps", lib.getName());
356: Map<String, String> expected = new HashMap<String, String>();
357: expected.put("libs.javahelp.classpath", "${base}/jh.jar"
358: + File.pathSeparatorChar + "${base}/jh-search.jar");
359: expected.put("libs.javahelp.javadoc", "${base}/jh-api");
360: expected.put("libs.gps.type", "j2me");
361: assertEquals(expected, loadProperties("libraries.properties"));
362: mgr.removeLibrary(lib);
363: expected.remove("libs.gps.type");
364: assertEquals(expected, loadProperties("libraries.properties"));
365: }
366:
367: public void testCreateLibraryUnderFSAtomicAction() throws Exception {
368: final LibraryManager mgr = LibraryManager.forLocation(new URL(
369: base, "libraries.properties"));
370: final Map<String, List<URL>> content = new HashMap<String, List<URL>>();
371: content.put("classpath", Arrays.asList(new URL(
372: "jar:file:jh.jar!/"), new URL(
373: "jar:file:jh-search.jar!/")));
374: content.put("javadoc", Arrays.asList(new URL("file:jh-api/")));
375:
376: FileSystem fs = projdir.getFileSystem();
377: fs.runAtomicAction(new FileSystem.AtomicAction() {
378: public void run() throws IOException {
379: Library lib = mgr.createLibrary("j2se", "javahelp",
380: content);
381: assertEquals("j2se", lib.getType());
382: assertEquals("javahelp", lib.getName());
383: assertEquals(content.get("classpath"), lib
384: .getRawContent("classpath"));
385: assertEquals(content.get("javadoc"), lib
386: .getRawContent("javadoc"));
387: try {
388: setLibraryContent(lib, "src", new URL(base,
389: "separate/jgraph-src/"), new URL(base,
390: "jgraph-other-src/"));
391: } catch (Exception e) {
392: throw new IOException(e.toString());
393: }
394: }
395: });
396: }
397:
398: public void testCreateLibraryAndLibrariesEventFiring()
399: throws Exception {
400: final LibraryManager mgr = LibraryManager.forLocation(new URL(
401: base, "libraries.properties"));
402: final Map<String, List<URL>> content = new HashMap<String, List<URL>>();
403: content.put("classpath", Arrays.asList(new URL(
404: "jar:file:jh.jar!/"), new URL(
405: "jar:file:jh-search.jar!/")));
406: content.put("javadoc", Arrays.asList(new URL("file:jh-api/")));
407: final List<PropertyChangeEvent> list = new ArrayList<PropertyChangeEvent>();
408: final PropertyChangeListener l = new PropertyChangeListener() {
409: public void propertyChange(PropertyChangeEvent evt) {
410: list.add(evt);
411: if (evt.getPropertyName().equals(
412: LibraryManager.PROP_LIBRARIES)) {
413: // by the time we got this event library must be fully set up:
414: assertTrue("must have one library", mgr
415: .getLibraries().length == 1);
416: assertEquals("library content must be set", content
417: .get("classpath"), mgr.getLibraries()[0]
418: .getRawContent("classpath"));
419: assertEquals("library content must be set", content
420: .get("javadoc"), mgr.getLibraries()[0]
421: .getRawContent("javadoc"));
422: }
423: }
424: };
425: mgr.addPropertyChangeListener(l);
426: Library lib = mgr.createLibrary("j2se", "javahelp", content);
427: mgr.removePropertyChangeListener(l);
428: assertTrue(list.size() == 1);
429: mgr.removeLibrary(lib);
430: FileSystem fs = projdir.getFileSystem();
431: fs.runAtomicAction(new FileSystem.AtomicAction() {
432: public void run() throws IOException {
433: mgr.addPropertyChangeListener(l);
434: mgr.createLibrary("j2se", "javahelp", content);
435: mgr.removePropertyChangeListener(l);
436: assertTrue(list.size() == 2);
437: }
438: });
439: }
440:
441: public void testPropertyProviderBasic() throws Exception {
442: writeProperties(
443: "libs/libraries.properties",
444: "libs.jgraph.classpath=${base}/jgraph.jar:${base}/../extralibs/jgraph-extras.jar");
445: storeDefs(project, "../libs/libraries.properties");
446: PropertyProvider pp = helper
447: .getProjectLibrariesPropertyProvider();
448: assertEquals(
449: Collections
450: .singletonMap(
451: "libs.jgraph.classpath",
452: (getWorkDir() + "/libs/jgraph.jar:"
453: + getWorkDir() + "/libs/../extralibs/jgraph-extras.jar")
454: .replace('/',
455: File.separatorChar)),
456: pp.getProperties());
457: }
458:
459: public void testPropertyProviderPrivateAbsolute() throws Exception {
460: writeProperties("libs/libraries.properties",
461: "libs.jgraph.classpath=${base}/jgraph-1.0.jar");
462: writeProperties("libs/libraries-private.properties",
463: "libs.jgraph.classpath="
464: + new File(getWorkDir(), "jgraph-2.0-beta.jar"));
465: storeDefs(project, "../libs/libraries.properties");
466: PropertyProvider pp = helper
467: .getProjectLibrariesPropertyProvider();
468: assertEquals(Collections.singletonMap("libs.jgraph.classpath",
469: new File(getWorkDir(), "jgraph-2.0-beta.jar")
470: .getAbsolutePath()), pp.getProperties());
471: }
472:
473: public void testSharability() throws Exception {
474: assertSharability(SharabilityQuery.UNKNOWN,
475: "libs/index.properties");
476: assertSharability(SharabilityQuery.NOT_SHARABLE,
477: "libs/index-private.properties");
478: assertSharability(SharabilityQuery.SHARABLE,
479: "prj/libs/index.properties");
480: assertSharability(SharabilityQuery.NOT_SHARABLE,
481: "prj/libs/index-private.properties");
482: assertSharability(SharabilityQuery.SHARABLE, "prj/libs/");
483: storeDefs(project, "libs/index.properties");
484: assertSharability(SharabilityQuery.SHARABLE,
485: "prj/libs/index.properties");
486: assertSharability(SharabilityQuery.NOT_SHARABLE,
487: "prj/libs/index-private.properties");
488: assertSharability(SharabilityQuery.MIXED, "prj/libs/");
489: }
490:
491: private void assertSharability(int mode, String path)
492: throws Exception {
493: File f = new File(getWorkDir(), path.replace('/',
494: File.separatorChar));
495: if (path.endsWith("/")) {
496: FileUtil.createFolder(f);
497: } else {
498: FileUtil.createData(f);
499: }
500: assertEquals(mode, SharabilityQuery.getSharability(f));
501: }
502:
503: private void writeProperties(String path, String... properties)
504: throws IOException {
505: FileObject f = FileUtil.createData(FileUtil
506: .toFileObject(getWorkDir()), path);
507: EditableProperties ep = new EditableProperties();
508: for (String def : properties) {
509: String[] nameValue = def.split("=", 2);
510: ep.put(nameValue[0], nameValue[1]);
511: }
512: OutputStream os = f.getOutputStream();
513: ep.store(os);
514: os.close();
515: }
516:
517: private static void storeDefs(Project project,
518: String... definitions) throws IOException {
519: Document doc = XMLUtil.createDocument("x", null, null, null);
520: Element libraries = doc.createElementNS(
521: "http://www.netbeans.org/ns/ant-project-libraries/1",
522: "libraries");
523: for (String def : definitions) {
524: libraries
525: .appendChild(
526: doc
527: .createElementNS(
528: "http://www.netbeans.org/ns/ant-project-libraries/1",
529: "definitions"))
530: .appendChild(doc.createTextNode(def));
531: }
532: project.getLookup().lookup(AuxiliaryConfiguration.class)
533: .putConfigurationFragment(libraries, true);
534: ProjectManager.getDefault().saveProject(project); // to assist in debugging
535: }
536:
537: private static void open(Project... projects) {
538: OpenProjects.getDefault().open(projects, false);
539: }
540:
541: private static void close(Project... projects) {
542: OpenProjects.getDefault().close(projects);
543: }
544:
545: private static void setLibraryContent(Library lib,
546: String volumeType, URL... paths) throws Exception {
547: MockPropertyChangeListener l = new MockPropertyChangeListener(
548: Library.PROP_CONTENT);
549: lib.addPropertyChangeListener(l);
550: LibraryImplementation impl = getLibraryImplementation(lib);
551: List<URL> path = Arrays.asList(paths);
552: impl.setContent(volumeType, path);
553: l.assertEventCount(1);
554: assertEquals(path, lib.getRawContent(volumeType));
555: }
556:
557: private static LibraryImplementation getLibraryImplementation(
558: Library lib) throws Exception {
559: Method getLibraryImplementation = Library.class
560: .getDeclaredMethod("getLibraryImplementation");
561: getLibraryImplementation.setAccessible(true);
562: return (LibraryImplementation) getLibraryImplementation
563: .invoke(lib);
564: }
565:
566: private Map<String, String> loadProperties(String path)
567: throws IOException {
568: File f = new File(getWorkDir(), path.replace('/',
569: File.separatorChar));
570: if (!f.isFile()) {
571: return Collections.emptyMap();
572: }
573: Properties p = new Properties();
574: InputStream is = new FileInputStream(f);
575: p.load(is);
576: is.close();
577: return NbCollections.checkedMapByFilter(p, String.class,
578: String.class, true);
579: }
580:
581: /**
582: * Test of copyLibrary method, of class LibrariesSupport.
583: */
584: public void testCopyLibrary() throws Exception {
585: // disable all collocation queries:
586: MockLookup
587: .setLookup(
588: Lookups.fixed(AntBasedTestUtil
589: .testAntBasedProjectType(),
590: libraryProvider),
591: // Filter out standard CQIs since they are bogus.
592: Lookups
593: .exclude(
594: Lookups
595: .metaInfServices(ProjectLibraryProviderTest.class
596: .getClassLoader()),
597: CollocationQueryImplementation.class));
598: File f = new File(this .getWorkDir(), "bertie.jar");
599: createFakeJAR(f, "smth");
600: File f1 = new File(this .getWorkDir(), "dog.jar");
601: createFakeJAR(f1, "smth");
602: new File(this .getWorkDir(), "sources").mkdir();
603: File f2 = new File(this .getWorkDir(), "sources/bertie.jar");
604: createFakeJAR(f2, "docs/api/test.smth");
605: new File(this .getWorkDir(), "libraries").mkdir();
606: File f3 = new File(this .getWorkDir(),
607: "libraries/libs.properties");
608: f3.createNewFile();
609: new File(this .getWorkDir(), "libraries2").mkdir();
610: File f4 = new File(this .getWorkDir(),
611: "libraries2/libs.properties");
612: f4.createNewFile();
613: FileUtil.toFileObject(getWorkDir()).refresh();
614: LibraryImplementation l1 = LibrariesSupport
615: .createLibraryImplementation("j2test", new String[] {
616: "jars", "sources" });
617: l1.setName("vino");
618: l1.setContent("jars", Arrays.asList(new URL[] {
619: f.toURI().toURL(), f1.toURI().toURL() }));
620: l1.setContent("sources", Arrays.asList(new URL[] { new URL(
621: "jar:" + f2.toURI().toURL() + "!/docs/api/") }));
622: libraryProvider.set(l1);
623: Library l = LibraryManager.getDefault().getLibrary("vino");
624: assertNotNull(l);
625: assertEquals(LibraryManager.getDefault(), l.getManager());
626: URL u = f3.toURI().toURL();
627: Library result = ProjectLibraryProvider
628: .copyLibrary(l, u, false);
629: assertNotNull(result);
630: assertEquals(u, result.getManager().getLocation());
631: assertEquals(Arrays.asList(
632: new URL("jar:file:vino/bertie.jar!/"), new URL(
633: "jar:file:vino/dog.jar!/")), result
634: .getRawContent("jars"));
635: assertEquals(Arrays.asList(new URL(
636: "jar:file:vino/bertie-2.jar!/docs/api/")), result
637: .getRawContent("sources"));
638: assertEquals("vino", result.getName());
639: assertEquals("j2test", result.getType());
640: //assertNotNull(LibrariesSupport.resolveLibraryEntryFileObject(u, result.getContent("jars").get(0)));
641: assertEquals(
642: new File(this .getWorkDir(), "libraries/vino/bertie.jar")
643: .getPath(),
644: FileUtil
645: .toFile(
646: LibrariesSupport
647: .resolveLibraryEntryFileObject(
648: u,
649: FileUtil
650: .getArchiveFile(result
651: .getRawContent(
652: "jars")
653: .get(0))))
654: .getPath());
655: //assertNotNull(LibrariesSupport.resolveLibraryEntryFileObject(u, result.getContent("sources").get(0)));
656: assertEquals(new File(this .getWorkDir(),
657: "libraries/vino/bertie-2.jar").getPath(), FileUtil
658: .toFile(
659: LibrariesSupport.resolveLibraryEntryFileObject(
660: u, FileUtil.getArchiveFile(result
661: .getRawContent("sources")
662: .get(0)))).getPath());
663: // enable test collocation query:
664: MockLookup
665: .setLookup(
666: Lookups
667: .fixed(
668: AntBasedTestUtil
669: .testAntBasedProjectType(),
670: AntBasedTestUtil
671: .testCollocationQueryImplementation(getWorkDir()),
672: libraryProvider),
673: // Filter out standard CQIs since they are bogus.
674: Lookups
675: .exclude(
676: Lookups
677: .metaInfServices(ProjectLibraryProviderTest.class
678: .getClassLoader()),
679: CollocationQueryImplementation.class));
680: u = f4.toURI().toURL();
681: result = ProjectLibraryProvider.copyLibrary(l, u, false);
682: assertNotNull(result);
683: assertEquals(u, result.getManager().getLocation());
684: assertEquals(Arrays.asList(new URL("jar:file:../bertie.jar!/"),
685: new URL("jar:file:../dog.jar!/")), result
686: .getRawContent("jars"));
687: assertEquals(Arrays.asList(new URL(
688: "jar:file:../sources/bertie.jar!/docs/api/")), result
689: .getRawContent("sources"));
690:
691: }
692:
693: private void createFakeJAR(File f, String content)
694: throws IOException {
695: // create just enough to make URLMapper recognize file as JAR:
696: ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
697: f));
698: writeZipFileEntry(zos, content, "some content".getBytes());
699: zos.finish();
700: zos.close();
701: }
702:
703: private static void writeZipFileEntry(ZipOutputStream zos,
704: String zipEntryName, byte[] byteArray) throws IOException {
705: int byteArraySize = byteArray.length;
706:
707: CRC32 crc = new CRC32();
708: crc.update(byteArray, 0, byteArraySize);
709:
710: ZipEntry entry = new ZipEntry(zipEntryName);
711: entry.setMethod(ZipEntry.STORED);
712: entry.setSize(byteArraySize);
713: entry.setCrc(crc.getValue());
714:
715: zos.putNextEntry(entry);
716: zos.write(byteArray, 0, byteArraySize);
717: zos.closeEntry();
718: }
719:
720: public static class TestLibraryProvider implements
721: LibraryProvider<LibraryImplementation> {
722:
723: public final List<LibraryImplementation> libs = new ArrayList<LibraryImplementation>();
724: final PropertyChangeSupport pcs = new PropertyChangeSupport(
725: this );
726:
727: public void set(LibraryImplementation... nue) {
728: libs.clear();
729: libs.addAll(Arrays.asList(nue));
730: pcs.firePropertyChange(PROP_LIBRARIES, null, null);
731: }
732:
733: public LibraryImplementation[] getLibraries() {
734: return libs.toArray(new LibraryImplementation[0]);
735: }
736:
737: public void addPropertyChangeListener(
738: PropertyChangeListener listener) {
739: pcs.addPropertyChangeListener(listener);
740: }
741:
742: public void removePropertyChangeListener(
743: PropertyChangeListener listener) {
744: pcs.removePropertyChangeListener(listener);
745: }
746:
747: }
748:
749: public static class TestLibraryTypeProvider implements
750: LibraryTypeProvider {
751:
752: public String getDisplayName() {
753: return "j2test";
754: }
755:
756: public String getLibraryType() {
757: return "j2test";
758: }
759:
760: public String[] getSupportedVolumeTypes() {
761: return new String[] { "jars", "sources" };
762: }
763:
764: public LibraryImplementation createLibrary() {
765: return LibrariesSupport.createLibraryImplementation(
766: "j2test", new String[] { "jars", "sources" });
767: }
768:
769: public void libraryDeleted(LibraryImplementation library) {
770: }
771:
772: public void libraryCreated(LibraryImplementation library) {
773: }
774:
775: public java.beans.Customizer getCustomizer(String volumeType) {
776: return null;
777: }
778:
779: public org.openide.util.Lookup getLookup() {
780: return null;
781: }
782: }
783:
784: private static void registerTestLibraryTypeProvider()
785: throws Exception {
786: StringTokenizer tk = new StringTokenizer(
787: "org-netbeans-api-project-libraries/LibraryTypeProviders",
788: "/");
789: FileObject root = Repository.getDefault()
790: .getDefaultFileSystem().getRoot();
791: while (tk.hasMoreElements()) {
792: String pathElement = tk.nextToken();
793: FileObject tmp = root.getFileObject(pathElement);
794: if (tmp == null) {
795: tmp = root.createFolder(pathElement);
796: }
797: root = tmp;
798: }
799: if (root.getChildren().length == 0) {
800: InstanceDataObject.create(DataFolder.findFolder(root),
801: "TestLibraryTypeProvider",
802: TestLibraryTypeProvider.class);
803: }
804: }
805: }
|