PageRenderTime 316ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/project.ant/test/unit/src/org/netbeans/modules/project/ant/ProjectLibraryProviderTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 647 lines | 527 code | 56 blank | 64 comment | 16 complexity | 6f4c6b50df16a0824cf17500d8403e68 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.project.ant;
  45. import java.beans.PropertyChangeEvent;
  46. import java.beans.PropertyChangeListener;
  47. import java.beans.PropertyChangeSupport;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileOutputStream;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.lang.reflect.Method;
  55. import java.net.URI;
  56. import java.net.URL;
  57. import java.util.ArrayList;
  58. import java.util.Arrays;
  59. import java.util.Collections;
  60. import java.util.HashMap;
  61. import java.util.List;
  62. import java.util.Map;
  63. import java.util.Properties;
  64. import java.util.StringTokenizer;
  65. import java.util.TreeMap;
  66. import java.util.regex.Matcher;
  67. import java.util.zip.CRC32;
  68. import java.util.zip.ZipEntry;
  69. import java.util.zip.ZipOutputStream;
  70. import org.netbeans.api.project.Project;
  71. import org.netbeans.api.project.ProjectManager;
  72. import org.netbeans.api.project.TestUtil;
  73. import org.netbeans.api.project.libraries.Library;
  74. import org.netbeans.api.project.libraries.LibraryManager;
  75. import org.netbeans.api.project.ui.OpenProjects;
  76. import org.netbeans.api.queries.SharabilityQuery;
  77. import org.netbeans.junit.NbTestCase;
  78. import org.netbeans.spi.project.AuxiliaryConfiguration;
  79. import org.netbeans.spi.project.libraries.LibraryImplementation;
  80. import org.netbeans.spi.project.libraries.LibraryImplementation2;
  81. import org.netbeans.spi.project.libraries.LibraryProvider;
  82. import org.netbeans.spi.project.libraries.LibraryTypeProvider;
  83. import org.netbeans.spi.project.libraries.support.LibrariesSupport;
  84. import org.netbeans.spi.project.support.ant.AntBasedTestUtil;
  85. import org.netbeans.spi.project.support.ant.AntProjectHelper;
  86. import org.netbeans.spi.project.support.ant.EditableProperties;
  87. import org.netbeans.spi.project.support.ant.ProjectGenerator;
  88. import org.netbeans.spi.project.support.ant.PropertyProvider;
  89. import org.netbeans.spi.queries.CollocationQueryImplementation;
  90. import org.openide.filesystems.FileObject;
  91. import org.openide.filesystems.FileSystem;
  92. import org.openide.filesystems.FileUtil;
  93. import org.openide.loaders.DataFolder;
  94. import org.openide.loaders.InstanceDataObject;
  95. import org.openide.util.NbCollections;
  96. import org.openide.util.Utilities;
  97. import org.openide.util.lookup.Lookups;
  98. import org.openide.util.test.MockChangeListener;
  99. import org.openide.util.test.MockLookup;
  100. import org.openide.util.test.MockPropertyChangeListener;
  101. import org.openide.xml.XMLUtil;
  102. import org.w3c.dom.Document;
  103. import org.w3c.dom.Element;
  104. public class ProjectLibraryProviderTest extends NbTestCase {
  105. public ProjectLibraryProviderTest(String name) {
  106. super(name);
  107. }
  108. private FileObject projdir;
  109. private AntProjectHelper helper;
  110. private Project project;
  111. private URL base;
  112. private TestLibraryProvider libraryProvider;
  113. @Override
  114. protected void setUp() throws Exception {
  115. super.setUp();
  116. libraryProvider = new TestLibraryProvider();
  117. MockLookup.setLookup(Lookups.fixed(AntBasedTestUtil.testAntBasedProjectType(), AntBasedTestUtil.testCollocationQueryImplementation(Utilities.toURI(getWorkDir())), libraryProvider),
  118. // Filter out standard CQIs since they are bogus.
  119. Lookups.exclude(Lookups.metaInfServices(ProjectLibraryProviderTest.class.getClassLoader()), CollocationQueryImplementation.class));
  120. projdir = TestUtil.makeScratchDir(this).createFolder("prj");
  121. helper = ProjectGenerator.createProject(projdir, "test");
  122. project = ProjectManager.getDefault().findProject(projdir);
  123. close(OpenProjects.getDefault().getOpenProjects());
  124. base = Utilities.toURI(getWorkDir()).toURL();
  125. ProjectLibraryProvider.FIRE_CHANGES_SYNCH = true;
  126. registerTestLibraryTypeProvider();
  127. }
  128. public void testPatternMatching() throws Exception {
  129. Matcher matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht.classpath");
  130. assertTrue(matcher.matches());
  131. assertEquals(matcher.group(2), "classpath");
  132. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht_1_0.classpath");
  133. assertTrue(matcher.matches());
  134. assertEquals(matcher.group(2), "classpath");
  135. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht_1_0.classpath2");
  136. assertTrue(matcher.matches());
  137. assertEquals(matcher.group(2), "classpath2");
  138. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht_1_0.classpath_1");
  139. assertTrue(matcher.matches());
  140. assertEquals(matcher.group(2), "classpath_1");
  141. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht-1.0.classpath");
  142. assertTrue(matcher.matches());
  143. assertEquals(matcher.group(2), "classpath");
  144. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht-1.0.classpath_1");
  145. assertTrue(matcher.matches());
  146. assertEquals(matcher.group(2), "classpath_1");
  147. matcher = ProjectLibraryProvider.LIBS_LINE.matcher("libs.grapht-1.0.1-classpath");
  148. assertTrue(matcher.matches());
  149. assertEquals(matcher.group(2), "1-classpath");
  150. }
  151. // XXX test name/type/description
  152. // XXX test : vs. ; and / vs. \ (in <definitions> and in *.properties)
  153. // XXX test set name, description
  154. public void testLibraryLoadingBasic() throws Exception {
  155. writeProperties("libs/my libraries.properties",
  156. "libs.jgraph.classpath=${base}/jgraph.jar:${base}/../extra libs/jgraph-extras.jar",
  157. "libs.jgraph.javadoc=${base}/api/jgraph-docs:${base}/api/jgraph-docs.zip!/docs/api/",
  158. "irrelevant=stuff");
  159. storeDefs(project, "../libs/my libraries.properties");
  160. Library lib = LibraryManager.forLocation(new URL(base, "libs/my%20libraries.properties")).getLibrary("jgraph");
  161. assertNotNull(lib);
  162. assertEquals("jgraph", lib.getName());
  163. assertEquals("jgraph", lib.getDisplayName());
  164. assertNull(lib.getDescription());
  165. assertEquals("j2se", lib.getType());
  166. assertEquals(Arrays.asList(new URI("jgraph.jar!/"), new URI("../extra%20libs/jgraph-extras.jar!/")), lib.getURIContent("classpath"));
  167. assertEquals(Arrays.asList(new URL("jar:"+base.toExternalForm()+"libs/jgraph.jar!/"), new URL("jar:"+base.toExternalForm()+"extra%20libs/jgraph-extras.jar!/")), lib.getContent("classpath"));
  168. assertEquals(Arrays.asList(new URI("api/jgraph-docs/"), new URI("api/jgraph-docs.zip!/docs/api/")), lib.getURIContent("javadoc"));
  169. assertEquals(Collections.emptyList(), lib.getContent("src"));
  170. //if this field is null, it means the reflection won't work on Library instances
  171. // and localized names fro libraries won't be found
  172. assertNotNull(ProjectLibraryProvider.ProjectLibraryImplementation.libraryImplField);
  173. }
  174. public void testLibraryLoadingPrivateAbsolute() throws Exception {
  175. writeProperties("libs/libraries.properties",
  176. "libs.jgraph.classpath=${base}/jgraph.jar");
  177. writeProperties("libs/libraries-private.properties",
  178. "libs.jgraph.src=" + new File(getWorkDir(), "jgraph-src.zip"),
  179. "libs.jgraph.javadoc=" + new File(getWorkDir(), "jgraph-api"));
  180. storeDefs(project, "../libs/libraries.properties");
  181. Library lib = LibraryManager.forLocation(new URL(base, "libs/libraries.properties")).getLibrary("jgraph");
  182. assertEquals(Collections.singletonList(new URI("jgraph.jar!/")), lib.getURIContent("classpath"));
  183. assertEquals(Collections.singletonList(new URI("jar:" + base.toExternalForm() + "jgraph-src.zip!/")), lib.getURIContent("src"));
  184. assertEquals(Collections.singletonList(new URL("jar:" + base.toExternalForm() + "jgraph-src.zip!/")), lib.getContent("src"));
  185. assertEquals(Collections.singletonList(new URL(base, "jgraph-api/").toURI()), lib.getURIContent("javadoc"));
  186. }
  187. public void testPrivateOverridesSharedProperties() throws Exception {
  188. writeProperties("libs/libraries.properties",
  189. "libs.jgraph.classpath=${base}/jgraph.jar");
  190. writeProperties("libs/libraries-private.properties",
  191. "libs.jgraph.classpath=" + new File(getWorkDir(), "jgraph-api"));
  192. storeDefs(project, "../libs/libraries.properties");
  193. Library lib = LibraryManager.forLocation(new URL(base, "libs/libraries.properties")).getLibrary("jgraph");
  194. assertEquals(Collections.singletonList(new URL(base, "jgraph-api/").toURI()), lib.getURIContent("classpath"));
  195. }
  196. public void testSetContent() throws Exception {
  197. writeProperties("libs/libraries.properties",
  198. "libs.jgraph.classpath=");
  199. storeDefs(project, "../libs/libraries.properties");
  200. Library lib = LibraryManager.forLocation(new URL(base, "libs/libraries.properties")).getLibrary("jgraph");
  201. setLibraryContent(lib, "classpath", new URI("jgraph.jar!/"), new URI("../extra%20libs/jgraph-extras.jar!/"));
  202. setLibraryContent(lib, "src", new URL(base, "separate/jgraph-src/").toURI(), new URL(base, "jgraph-other-src/").toURI());
  203. setLibraryContent(lib, "javadoc", new URI("jar:" + base + "separate/jgraph-api.zip!/"), new URI("../separate/jgraph-api.zip!/docs/api/"));
  204. Map<String,String> m = new HashMap<String,String>();
  205. File separate = new File(getWorkDir(), "separate");
  206. m.put("libs.jgraph.classpath", "${base}/jgraph.jar"+File.pathSeparatorChar+"${base}/../extra libs/jgraph-extras.jar");
  207. m.put("libs.jgraph.src", new File(separate, "jgraph-src").getAbsolutePath().replace('\\', '/') + File.pathSeparator +
  208. new File(getWorkDir(), "jgraph-other-src").getAbsolutePath().replace('\\', '/'));
  209. m.put("libs.jgraph.javadoc", new File(separate, "jgraph-api.zip").getAbsolutePath().replace('\\', '/') + File.pathSeparator +
  210. "${base}/../separate/jgraph-api.zip!/docs/api/");
  211. assertEquals(m, loadProperties("libs/libraries.properties"));
  212. }
  213. public void testAreaChangesFromProjectsOpenedClosed() throws Exception {
  214. storeDefs(project, "../libraries.properties");
  215. assertEquals("[<none>]", openedLibraryManagers());
  216. open(project);
  217. assertEquals("[<none>, " + base + "libraries.properties]", openedLibraryManagers());
  218. close(project);
  219. assertEquals("[<none>]", openedLibraryManagers());
  220. Project project2 = ProjectManager.getDefault().findProject(
  221. ProjectGenerator.createProject(projdir.getParent().createFolder("prj2"), "test").getProjectDirectory());
  222. storeDefs(project2, "../lib2.properties");
  223. open(project, project2);
  224. assertEquals("[<none>, " + base + "lib2.properties, "+ base + "libraries.properties]", openedLibraryManagers());
  225. close(project);
  226. assertEquals("[<none>, " + base + "lib2.properties]", openedLibraryManagers());
  227. }
  228. private static String openedLibraryManagers() {
  229. List<String> urls = new ArrayList<String>();
  230. for (LibraryManager mgr : LibraryManager.getOpenManagers()) {
  231. URL u = mgr.getLocation();
  232. urls.add(u != null ? u.toExternalForm() : "<none>");
  233. }
  234. Collections.sort(urls);
  235. return urls.toString();
  236. }
  237. public void testChangesLibraries() throws Exception {
  238. PropertyProvider pp = helper.getProjectLibrariesPropertyProvider();
  239. writeProperties("libraries.properties",
  240. "libs.jgraph.classpath=");
  241. storeDefs(project, "../libraries.properties");
  242. LibraryManager mgr = LibraryManager.forLocation(new URL(base, "libraries.properties"));
  243. Library lib1 = mgr.getLibrary("jgraph");
  244. assertEquals(Collections.emptyList(), lib1.getContent("classpath"));
  245. assertEquals("{libs.jgraph.classpath=}", new TreeMap<String,String>(pp.getProperties()).toString());
  246. MockPropertyChangeListener liblist = new MockPropertyChangeListener(LibraryManager.PROP_LIBRARIES);
  247. MockPropertyChangeListener contentlist = new MockPropertyChangeListener(Library.PROP_CONTENT);
  248. mgr.addPropertyChangeListener(liblist);
  249. lib1.addPropertyChangeListener(contentlist);
  250. MockChangeListener pplist = new MockChangeListener();
  251. pp.addChangeListener(pplist);
  252. writeProperties("libraries.properties",
  253. "libs.jgraph.classpath=${base}/jgraph",
  254. "libs.collections.classpath=${base}/collections");
  255. contentlist.assertEventCount(1);
  256. assertEquals(Collections.singletonList(new URI("jgraph/")), lib1.getURIContent("classpath"));
  257. liblist.assertEventCount(1);
  258. assertEquals(lib1, mgr.getLibrary("jgraph"));
  259. Library lib2 = mgr.getLibrary("collections");
  260. assertEquals(Collections.singletonList(new URI("collections/")), lib2.getURIContent("classpath"));
  261. pplist.assertEventCount(1);
  262. assertEquals(("{libs.collections.classpath=" + getWorkDir() + "/collections, libs.jgraph.classpath=" +
  263. getWorkDir() + "/jgraph}").replace('/', File.separatorChar),
  264. new TreeMap<String,String>(pp.getProperties()).toString());
  265. writeProperties("others.properties",
  266. "libs.jrcs.classpath=");
  267. storeDefs(project, "../others.properties");
  268. contentlist.assertEventCount(0);
  269. liblist.assertEventCount(0);
  270. // [ storeDefs() fires configurationXmlChanged twice - after put() and after save() ]
  271. // after fixing #146072 the previous statement doesn't hold - leaving it here for reference
  272. // setting number of events to 1
  273. pplist.assertEventCount(1);
  274. assertEquals(("{libs.jrcs.classpath=}").replace('/', File.separatorChar),
  275. new TreeMap<String,String>(pp.getProperties()).toString());
  276. }
  277. public void testCreateRemoveLibrary() throws Exception {
  278. LibraryManager mgr = LibraryManager.forLocation(new URL(base, "libraries.properties"));
  279. Map<String,List<URI>> content = new HashMap<String,List<URI>>();
  280. content.put("classpath", Arrays.asList(new URI("jh.jar!/"), new URI("jh-search.jar!/")));
  281. content.put("javadoc", Arrays.asList(new URI("jh-api/")));
  282. Library lib = mgr.createURILibrary("j2se", "javahelp", content);
  283. assertEquals("j2se", lib.getType());
  284. assertEquals("javahelp", lib.getName());
  285. assertEquals(content.get("classpath"), lib.getURIContent("classpath"));
  286. assertEquals(content.get("javadoc"), lib.getURIContent("javadoc"));
  287. lib = mgr.createURILibrary("j2me", "gps", Collections.<String,List<URI>>emptyMap());
  288. assertEquals("j2me", lib.getType());
  289. assertEquals("gps", lib.getName());
  290. Map<String,String> expected = new HashMap<String,String>();
  291. expected.put("libs.javahelp.classpath", "${base}/jh.jar"+File.pathSeparatorChar+"${base}/jh-search.jar");
  292. expected.put("libs.javahelp.javadoc", "${base}/jh-api/");
  293. expected.put("libs.gps.type", "j2me");
  294. assertEquals(expected, loadProperties("libraries.properties"));
  295. mgr.removeLibrary(lib);
  296. expected.remove("libs.gps.type");
  297. assertEquals(expected, loadProperties("libraries.properties"));
  298. }
  299. public void testCreateLibraryUnderFSAtomicAction() throws Exception {
  300. final LibraryManager mgr = LibraryManager.forLocation(new URL(base, "libraries.properties"));
  301. final Map<String,List<URI>> content = new HashMap<String,List<URI>>();
  302. content.put("classpath", Arrays.asList(new URI("jh.jar!/"), new URI("jh-search.jar!/")));
  303. content.put("javadoc", Arrays.asList(new URI("jh-api/")));
  304. FileSystem fs = projdir.getFileSystem();
  305. fs.runAtomicAction(new FileSystem.AtomicAction() {
  306. public void run() throws IOException {
  307. Library lib = mgr.createURILibrary("j2se", "javahelp", content);
  308. assertEquals("j2se", lib.getType());
  309. assertEquals("javahelp", lib.getName());
  310. assertEquals(content.get("classpath"), lib.getURIContent("classpath"));
  311. assertEquals(content.get("javadoc"), lib.getURIContent("javadoc"));
  312. try {
  313. setLibraryContent(lib, "src", new URL(base, "separate/jgraph-src/").toURI(), new URL(base, "jgraph-other-src/").toURI());
  314. } catch (Exception e) {
  315. throw new IOException(e.toString());
  316. }
  317. }});
  318. }
  319. public void testCreateLibraryAndLibrariesEventFiring() throws Exception {
  320. final LibraryManager mgr = LibraryManager.forLocation(new URL(base, "libraries.properties"));
  321. final Map<String,List<URI>> content = new HashMap<String,List<URI>>();
  322. content.put("classpath", Arrays.asList(new URI("jh.jar!/"), new URI("jh-search.jar!/")));
  323. content.put("javadoc", Arrays.asList(new URI("jh-api/")));
  324. final List<PropertyChangeEvent> list = new ArrayList<PropertyChangeEvent>();
  325. final PropertyChangeListener l = new PropertyChangeListener() {
  326. public void propertyChange(PropertyChangeEvent evt) {
  327. list.add(evt);
  328. if (evt.getPropertyName().equals(LibraryManager.PROP_LIBRARIES)) {
  329. // by the time we got this event library must be fully set up:
  330. assertTrue("must have one library", mgr.getLibraries().length == 1);
  331. assertEquals("library content must be set", content.get("classpath"), mgr.getLibraries()[0].getURIContent("classpath"));
  332. assertEquals("library content must be set", content.get("javadoc"), mgr.getLibraries()[0].getURIContent("javadoc"));
  333. }
  334. }
  335. };
  336. mgr.addPropertyChangeListener(l);
  337. Library lib = mgr.createURILibrary("j2se", "javahelp", content);
  338. mgr.removePropertyChangeListener(l);
  339. assertEquals(1, list.size());
  340. final PropertyChangeListener l2 = new PropertyChangeListener() {
  341. public void propertyChange(PropertyChangeEvent evt) {
  342. list.add(evt);
  343. if (evt.getPropertyName().equals(LibraryManager.PROP_LIBRARIES)) {
  344. // by the time we got this event library must be removed
  345. assertTrue("must have zero libraries", mgr.getLibraries().length == 0);
  346. }
  347. }
  348. };
  349. mgr.addPropertyChangeListener(l2);
  350. mgr.removeLibrary(lib);
  351. mgr.removePropertyChangeListener(l2);
  352. assertEquals(2, list.size());
  353. FileSystem fs = projdir.getFileSystem();
  354. fs.runAtomicAction(new FileSystem.AtomicAction() {
  355. public void run() throws IOException {
  356. mgr.addPropertyChangeListener(l);
  357. Library lib = mgr.createURILibrary("j2se", "javahelp", content);
  358. mgr.removePropertyChangeListener(l);
  359. assertEquals(3, list.size());
  360. mgr.addPropertyChangeListener(l2);
  361. mgr.removeLibrary(lib);
  362. mgr.removePropertyChangeListener(l2);
  363. assertEquals(4, list.size());
  364. }});
  365. }
  366. public void testPropertyProviderBasic() throws Exception {
  367. writeProperties("libs/libraries.properties",
  368. "libs.jgraph.classpath=${base}/jgraph.jar:${base}/../extralibs/jgraph-extras.jar");
  369. storeDefs(project, "../libs/libraries.properties");
  370. PropertyProvider pp = helper.getProjectLibrariesPropertyProvider();
  371. assertEquals(Collections.singletonMap("libs.jgraph.classpath", (getWorkDir() + "/libs/jgraph.jar:" +
  372. getWorkDir() + "/libs/../extralibs/jgraph-extras.jar").replace('/', File.separatorChar)), pp.getProperties());
  373. }
  374. public void testPropertyProviderPrivateAbsolute() throws Exception {
  375. writeProperties("libs/libraries.properties",
  376. "libs.jgraph.classpath=${base}/jgraph-1.0.jar");
  377. writeProperties("libs/libraries-private.properties",
  378. "libs.jgraph.classpath=" + new File(getWorkDir(), "jgraph-2.0-beta.jar"));
  379. storeDefs(project, "../libs/libraries.properties");
  380. PropertyProvider pp = helper.getProjectLibrariesPropertyProvider();
  381. assertEquals(Collections.singletonMap("libs.jgraph.classpath", new File(getWorkDir(), "jgraph-2.0-beta.jar").getAbsolutePath()), pp.getProperties());
  382. }
  383. public void testSharability() throws Exception {
  384. assertSharability(SharabilityQuery.Sharability.UNKNOWN, "libs/index.properties");
  385. assertSharability(SharabilityQuery.Sharability.NOT_SHARABLE, "libs/index-private.properties");
  386. assertSharability(SharabilityQuery.Sharability.SHARABLE, "prj/libs/index.properties");
  387. assertSharability(SharabilityQuery.Sharability.NOT_SHARABLE, "prj/libs/index-private.properties");
  388. assertSharability(SharabilityQuery.Sharability.SHARABLE, "prj/libs/");
  389. storeDefs(project, "libs/index.properties");
  390. assertSharability(SharabilityQuery.Sharability.SHARABLE, "prj/libs/index.properties");
  391. assertSharability(SharabilityQuery.Sharability.NOT_SHARABLE, "prj/libs/index-private.properties");
  392. assertSharability(SharabilityQuery.Sharability.MIXED, "prj/libs/");
  393. }
  394. private void assertSharability(SharabilityQuery.Sharability mode, String path) throws Exception {
  395. File f = new File(getWorkDir(), path.replace('/', File.separatorChar));
  396. if (path.endsWith("/")) {
  397. FileUtil.createFolder(f);
  398. } else {
  399. FileUtil.createData(f);
  400. }
  401. assertEquals(mode, SharabilityQuery.getSharability(Utilities.toURI(f)));
  402. }
  403. private void writeProperties(String path, String... properties) throws IOException {
  404. FileObject f = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), path);
  405. EditableProperties ep = new EditableProperties(false);
  406. for (String def : properties) {
  407. String[] nameValue = def.split("=", 2);
  408. ep.put(nameValue[0], nameValue[1]);
  409. }
  410. OutputStream os = f.getOutputStream();
  411. ep.store(os);
  412. os.close();
  413. }
  414. private static void storeDefs(Project project, String... definitions) throws IOException {
  415. Document doc = XMLUtil.createDocument("x", null, null, null);
  416. Element libraries = doc.createElementNS("http://www.netbeans.org/ns/ant-project-libraries/1", "libraries");
  417. for (String def : definitions) {
  418. libraries.appendChild(doc.createElementNS("http://www.netbeans.org/ns/ant-project-libraries/1", "definitions")).appendChild(doc.createTextNode(def));
  419. }
  420. project.getLookup().lookup(AuxiliaryConfiguration.class).putConfigurationFragment(libraries, true);
  421. ProjectManager.getDefault().saveProject(project); // to assist in debugging
  422. }
  423. private static void open(Project... projects) {
  424. OpenProjects.getDefault().open(projects, false);
  425. }
  426. private static void close(Project... projects) {
  427. OpenProjects.getDefault().close(projects);
  428. }
  429. private static void setLibraryContent(Library lib, String volumeType, URI... paths) throws Exception {
  430. MockPropertyChangeListener l = new MockPropertyChangeListener(Library.PROP_CONTENT);
  431. lib.addPropertyChangeListener(l);
  432. LibraryImplementation2 impl = getLibraryImplementation(lib);
  433. List<URI> path = Arrays.asList(paths);
  434. impl.setURIContent(volumeType, path);
  435. l.assertEventCount(1);
  436. assertEquals(path, lib.getURIContent(volumeType));
  437. }
  438. private static LibraryImplementation2 getLibraryImplementation(Library lib) throws Exception {
  439. Method getLibraryImplementation = Library.class.getDeclaredMethod("getLibraryImplementation");
  440. getLibraryImplementation.setAccessible(true);
  441. return (LibraryImplementation2) getLibraryImplementation.invoke(lib);
  442. }
  443. private Map<String,String> loadProperties(String path) throws IOException {
  444. File f = new File(getWorkDir(), path.replace('/', File.separatorChar));
  445. if (!f.isFile()) {
  446. return Collections.emptyMap();
  447. }
  448. Properties p = new Properties();
  449. InputStream is = new FileInputStream(f);
  450. p.load(is);
  451. is.close();
  452. return NbCollections.checkedMapByFilter(p, String.class, String.class, true);
  453. }
  454. /**
  455. * Test of copyLibrary method, of class LibrariesSupport.
  456. */
  457. public void testCopyLibrary() throws Exception {
  458. // disable all collocation queries:
  459. MockLookup.setLookup(Lookups.fixed(AntBasedTestUtil.testAntBasedProjectType(), libraryProvider),
  460. // Filter out standard CQIs since they are bogus.
  461. Lookups.exclude(Lookups.metaInfServices(ProjectLibraryProviderTest.class.getClassLoader()), CollocationQueryImplementation.class));
  462. File f = new File(this.getWorkDir(), "bertie.jar");
  463. createFakeJAR(f, "smth");
  464. File f1 = new File(this.getWorkDir(), "dog.jar");
  465. createFakeJAR(f1, "smth");
  466. new File(this.getWorkDir(), "sources").mkdir();
  467. File f2 = new File(this.getWorkDir(), "sources/bertie.jar");
  468. createFakeJAR(f2, "docs/api/test.smth");
  469. new File(this.getWorkDir(), "libraries").mkdir();
  470. File f3 = new File(this.getWorkDir(), "libraries/libs.properties");
  471. f3.createNewFile();
  472. new File(this.getWorkDir(), "libraries2").mkdir();
  473. File f4 = new File(this.getWorkDir(), "libraries2/libs.properties");
  474. f4.createNewFile();
  475. FileUtil.toFileObject(getWorkDir()).getFileSystem().refresh(false);
  476. LibraryImplementation l1 = LibrariesSupport.createLibraryImplementation("j2test", new String[]{"jars", "sources"});
  477. l1.setName("vino");
  478. l1.setContent("jars", Arrays.asList(new URL[]{Utilities.toURI(f).toURL(), Utilities.toURI(f1).toURL()}));
  479. l1.setContent("sources", Arrays.asList(new URL[]{new URL("jar:" + Utilities.toURI(f2).toURL() + "!/docs/api/")}));
  480. libraryProvider.set(l1);
  481. Library l = LibraryManager.getDefault().getLibrary("vino");
  482. assertNotNull(l);
  483. assertEquals(LibraryManager.getDefault(), l.getManager());
  484. URL u = Utilities.toURI(f3).toURL();
  485. Library result = ProjectLibraryProvider.copyLibrary(l, u, false);
  486. assertNotNull(result);
  487. assertEquals(u, result.getManager().getLocation());
  488. assertEquals(Arrays.asList(new URI("vino/bertie.jar!/"),
  489. new URI("vino/dog.jar!/")), result.getURIContent("jars"));
  490. assertEquals(Arrays.asList(new URI("vino/bertie-2.jar!/docs/api/")), result.getURIContent("sources"));
  491. assertEquals("vino", result.getName());
  492. assertEquals("j2test", result.getType());
  493. //assertNotNull(LibrariesSupport.resolveLibraryEntryFileObject(u, result.getURIContent("jars").get(0)));
  494. assertEquals("jar:"+(Utilities.toURI(new File(this.getWorkDir(), "libraries/vino/bertie.jar")).toString())+"!/",
  495. LibrariesSupport.resolveLibraryEntryURI(u, result.getURIContent("jars").get(0)).toString());
  496. assertEquals("jar:"+(Utilities.toURI(new File(this.getWorkDir(), "libraries/vino/bertie.jar")).toString())+"!/",
  497. result.getContent("jars").get(0).toExternalForm());
  498. //assertNotNull(LibrariesSupport.resolveLibraryEntryFileObject(u, result.getContent("sources").get(0)));
  499. assertEquals("jar:"+(Utilities.toURI(new File(this.getWorkDir(), "libraries/vino/bertie-2.jar")))+"!/docs/api/",
  500. LibrariesSupport.resolveLibraryEntryURI(u, result.getURIContent("sources").get(0)).toString());
  501. assertEquals("jar:"+(Utilities.toURI(new File(this.getWorkDir(), "libraries/vino/bertie-2.jar")))+"!/docs/api/",
  502. result.getContent("sources").get(0).toExternalForm());
  503. // enable test collocation query:
  504. MockLookup.setLookup(Lookups.fixed(AntBasedTestUtil.testAntBasedProjectType(), AntBasedTestUtil.testCollocationQueryImplementation(Utilities.toURI(getWorkDir())), libraryProvider),
  505. // Filter out standard CQIs since they are bogus.
  506. Lookups.exclude(Lookups.metaInfServices(ProjectLibraryProviderTest.class.getClassLoader()), CollocationQueryImplementation.class));
  507. u = Utilities.toURI(f4).toURL();
  508. result = ProjectLibraryProvider.copyLibrary(l, u, false);
  509. assertNotNull(result);
  510. assertEquals(u, result.getManager().getLocation());
  511. assertEquals(Arrays.asList(new URI("../bertie.jar!/"),
  512. new URI("../dog.jar!/")), result.getURIContent("jars"));
  513. assertEquals(Arrays.asList(new URI("../sources/bertie.jar!/docs/api/")), result.getURIContent("sources"));
  514. }
  515. private void createFakeJAR(File f, String content) throws IOException {
  516. // create just enough to make URLMapper recognize file as JAR:
  517. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f));
  518. writeZipFileEntry(zos, content, "some content".getBytes());
  519. zos.finish();
  520. zos.close();
  521. }
  522. private static void writeZipFileEntry(ZipOutputStream zos, String zipEntryName, byte[] byteArray) throws IOException {
  523. int byteArraySize = byteArray.length;
  524. CRC32 crc = new CRC32();
  525. crc.update(byteArray, 0, byteArraySize);
  526. ZipEntry entry = new ZipEntry(zipEntryName);
  527. entry.setMethod(ZipEntry.STORED);
  528. entry.setSize(byteArraySize);
  529. entry.setCrc(crc.getValue());
  530. zos.putNextEntry(entry);
  531. zos.write(byteArray, 0, byteArraySize);
  532. zos.closeEntry();
  533. }
  534. public static class TestLibraryProvider implements LibraryProvider<LibraryImplementation> {
  535. public final List<LibraryImplementation> libs = new ArrayList<LibraryImplementation>();
  536. final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  537. public void set(LibraryImplementation... nue) {
  538. libs.clear();
  539. libs.addAll(Arrays.asList(nue));
  540. pcs.firePropertyChange(PROP_LIBRARIES, null, null);
  541. }
  542. public LibraryImplementation[] getLibraries() {
  543. return libs.toArray(new LibraryImplementation[0]);
  544. }
  545. public void addPropertyChangeListener(PropertyChangeListener listener) {
  546. pcs.addPropertyChangeListener(listener);
  547. }
  548. public void removePropertyChangeListener(PropertyChangeListener listener) {
  549. pcs.removePropertyChangeListener(listener);
  550. }
  551. }
  552. public static class TestLibraryTypeProvider implements LibraryTypeProvider {
  553. public String getDisplayName() {
  554. return "j2test";
  555. }
  556. public String getLibraryType() {
  557. return "j2test";
  558. }
  559. public String[] getSupportedVolumeTypes() {
  560. return new String[] {"jars","sources"};
  561. }
  562. public LibraryImplementation createLibrary() {
  563. return LibrariesSupport.createLibraryImplementation("j2test", new String[] {"jars","sources"});
  564. }
  565. public void libraryDeleted(LibraryImplementation library) {
  566. }
  567. public void libraryCreated(LibraryImplementation library) {
  568. }
  569. public java.beans.Customizer getCustomizer(String volumeType) {
  570. return null;
  571. }
  572. public org.openide.util.Lookup getLookup() {
  573. return null;
  574. }
  575. }
  576. private static void registerTestLibraryTypeProvider () throws Exception {
  577. StringTokenizer tk = new StringTokenizer("org-netbeans-api-project-libraries/LibraryTypeProviders","/");
  578. FileObject root = FileUtil.getConfigRoot();
  579. while (tk.hasMoreElements()) {
  580. String pathElement = tk.nextToken();
  581. FileObject tmp = root.getFileObject(pathElement);
  582. if (tmp == null) {
  583. tmp = root.createFolder(pathElement);
  584. }
  585. root = tmp;
  586. }
  587. if (root.getChildren().length == 0) {
  588. InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class);
  589. }
  590. }
  591. }