PageRenderTime 69ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/java.project/test/qa-functional/src/projects/LibrariesTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 399 lines | 273 code | 22 blank | 104 comment | 3 complexity | a3bb00dd451a5fda96ca94a2131cdf53 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. /*
  45. * TestDefaultLibraries.java
  46. * NetBeans JUnit based test
  47. *
  48. * Created on 06 September 2004, 15:37
  49. */
  50. package projects;
  51. import java.io.BufferedReader;
  52. import java.io.File;
  53. import java.io.FileNotFoundException;
  54. import java.io.FileOutputStream;
  55. import java.io.FileReader;
  56. import java.io.FileWriter;
  57. import java.io.IOException;
  58. import java.io.InputStream;
  59. import java.io.PrintStream;
  60. import java.io.PrintWriter;
  61. import java.net.InetAddress;
  62. import java.net.URL;
  63. import java.net.UnknownHostException;
  64. import java.util.ArrayList;
  65. import java.util.Arrays;
  66. import java.util.HashMap;
  67. import java.util.Iterator;
  68. import java.util.List;
  69. import java.util.Map;
  70. import java.util.Properties;
  71. import java.util.Set;
  72. import java.util.StringTokenizer;
  73. import java.util.TreeSet;
  74. import org.netbeans.jellytools.JellyTestCase;
  75. import junit.framework.Test;
  76. import org.netbeans.junit.NbModuleSuite;
  77. //import org.netbeans.junit.ide.ProjectSupport;
  78. import org.netbeans.api.project.libraries.LibraryManager;
  79. import org.netbeans.api.project.libraries.Library;
  80. /**
  81. *
  82. */
  83. public class LibrariesTest extends JellyTestCase {
  84. Map<String, Set<String>> librariesUrls;
  85. public LibrariesTest(java.lang.String testName) {
  86. super(testName);
  87. }
  88. public static Test suite() {
  89. return NbModuleSuite.create(
  90. NbModuleSuite.createConfiguration(LibrariesTest.class).
  91. addTest("testDefaultLibrariesIgnoreOrder").
  92. enableModules(".*").clusters(".*"));
  93. }
  94. @Override
  95. protected void setUp() throws Exception {
  96. super.setUp();
  97. librariesUrls = new HashMap<String, Set<String>>();
  98. }
  99. // -------------------------------------------------------------------------
  100. /**
  101. * This test fails only for golden files that differ from current state
  102. * in content (ignored are differnces related to lines order).
  103. *
  104. */
  105. public void testDefaultLibrariesIgnoreOrder() throws IOException {
  106. init1();
  107. LibraryManager libMan = LibraryManager.getDefault();
  108. Library[] libs = libMan.getLibraries();
  109. String errorString = "";
  110. PrintWriter initMethodBuff = new PrintWriter(new FileWriter(new File(getWorkDir(), "testDefaultLibrariesIgnoreOrder.txt")));
  111. try {
  112. for (int i = 0; i < libs.length; i++) {
  113. // names of files are based on library name
  114. String baseName = libs[i].getName();
  115. Set<String> urls = new TreeSet<String>();
  116. addURLs(urls, libs[i].getContent("classpath"));
  117. addURLs(urls, libs[i].getContent("javadoc"));
  118. addURLs(urls, libs[i].getContent("src"));
  119. generateInitDefaultLibrariesIngoreOrder(initMethodBuff, urls, baseName);
  120. assertEquals("Following files differ other way than lines order:", librariesUrls.get(baseName), urls);
  121. }
  122. } finally {
  123. initMethodBuff.close();
  124. }
  125. }
  126. public void init1() {
  127. Set<String> set = new TreeSet<String>();
  128. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/FastInfoset.jar!/");
  129. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/activation.jar!/");
  130. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jax-qname.jar!/");
  131. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxp-api.jar!/");
  132. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-api.jar!/");
  133. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-impl.jar!/");
  134. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-spi.jar!/");
  135. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jsr173_api.jar!/");
  136. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/mail.jar!/");
  137. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/relaxngDatatype.jar!/");
  138. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/saaj-api.jar!/");
  139. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/saaj-impl.jar!/");
  140. set.add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/xsdlib.jar!/");
  141. librariesUrls.put("jaxrpc16", set);
  142. set = new TreeSet<String>();
  143. set.add("jar:nbinst://org.netbeans.modules.form/modules/ext/AbsoluteLayout.jar!/");
  144. librariesUrls.put("absolutelayout", set);
  145. set = new TreeSet<String>();
  146. set.add("jar:nbinst:///modules/ext/jaxws21/FastInfoset.jar!/");
  147. set.add("jar:nbinst:///modules/ext/jaxws21/activation.jar!/");
  148. set.add("jar:nbinst:///modules/ext/jaxws21/api/jaxb-api.jar!/");
  149. set.add("jar:nbinst:///modules/ext/jaxws21/api/jaxws-api.jar!/");
  150. set.add("jar:nbinst:///modules/ext/jaxws21/api/jsr173_api.jar!/");
  151. set.add("jar:nbinst:///modules/ext/jaxws21/api/jsr181-api.jar!/");
  152. set.add("jar:nbinst:///modules/ext/jaxws21/api/saaj-api.jar!/");
  153. set.add("jar:nbinst:///modules/ext/jaxws21/http.jar!/");
  154. set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-impl.jar!/");
  155. set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-xjc.jar!/");
  156. set.add("jar:nbinst:///modules/ext/jaxws21/jaxws-rt.jar!/");
  157. set.add("jar:nbinst:///modules/ext/jaxws21/jaxws-tools.jar!/");
  158. set.add("jar:nbinst:///modules/ext/jaxws21/jsr250-api.jar!/");
  159. set.add("jar:nbinst:///modules/ext/jaxws21/saaj-impl.jar!/");
  160. set.add("jar:nbinst:///modules/ext/jaxws21/sjsxp.jar!/");
  161. set.add("jar:nbinst:///modules/ext/jaxws21/stax-ex.jar!/");
  162. set.add("jar:nbinst:///modules/ext/jaxws21/streambuffer.jar!/");
  163. librariesUrls.put("jaxws20", set);
  164. set = new TreeSet<String>();
  165. set.add("jar:nbinst:///docs/org-netbeans-libs-javacapi.zip!/");
  166. set.add("jar:nbinst:///modules/ext/javac-api-nb-7.0-b07.jar!/");
  167. librariesUrls.put("javac-api", set);
  168. set = new TreeSet<String>();
  169. set.add("jar:nbinst:///docs/javaee6-doc-api.zip!/");
  170. set.add("jar:nbinst://org.netbeans.modules.web.jstl11/modules/ext/jstl.jar!/");
  171. set.add("jar:nbinst://org.netbeans.modules.web.jstl11/modules/ext/standard.jar!/");
  172. librariesUrls.put("jstl11", set);
  173. set = new TreeSet<String>();
  174. set.add("jar:nbinst:///modules/ext/jaxws21/activation.jar!/");
  175. set.add("jar:nbinst:///modules/ext/jaxws21/api/jaxb-api.jar!/");
  176. set.add("jar:nbinst:///modules/ext/jaxws21/api/jsr173_api.jar!/");
  177. set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-impl.jar!/");
  178. set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-xjc.jar!/");
  179. librariesUrls.put("jaxb20", set);
  180. set = new TreeSet<String>();
  181. set.add("jar:nbinst://org.netbeans.modules.swingapp/docs/appframework-0.30-doc.zip!/");
  182. set.add("jar:nbinst://org.netbeans.modules.swingapp/modules/ext/appframework-0.30.jar!/");
  183. set.add("jar:nbinst://org.netbeans.modules.swingapp/modules/ext/swing-worker.jar!/");
  184. librariesUrls.put("swing-app-framework", set);
  185. set = new TreeSet<String>();
  186. set.add("jar:nbinst:///docs/struts-1.2.9-javadoc.zip!/");
  187. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/antlr.jar!/");
  188. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-beanutils.jar!/");
  189. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-digester.jar!/");
  190. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-fileupload.jar!/");
  191. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-logging.jar!/");
  192. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-validator.jar!/");
  193. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/jakarta-oro.jar!/");
  194. set.add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/struts.jar!/");
  195. librariesUrls.put("struts", set);
  196. set = new TreeSet<String>();
  197. set.add("jar:nbinst://org.netbeans.modules.junit/docs/junit-3.8.2-api.zip!/");
  198. set.add("jar:nbinst://org.netbeans.modules.junit/modules/ext/junit-3.8.2.jar!/");
  199. set.add("jar:nbinst://org.netbeans.modules.junit/modules/ext/junit-4.1.jar!/");
  200. librariesUrls.put("junit", set);
  201. set = new TreeSet<String>();
  202. set.add("jar:nbinst://org.jdesktop.layout/docs/swing-layout-1.0.2-doc.zip!/");
  203. set.add("jar:nbinst://org.jdesktop.layout/docs/swing-layout-1.0.2-src.zip!/");
  204. set.add("jar:nbinst://org.jdesktop.layout/modules/ext/swing-layout-1.0.2.jar!/");
  205. librariesUrls.put("swing-layout", set);
  206. set = new TreeSet<String>();
  207. set.add("jar:nbinst://org.netbeans.modules.java.j2seproject/ant/extra/org-netbeans-modules-java-j2seproject-copylibstask.jar!/");
  208. librariesUrls.put("CopyLibs", set);
  209. set = new TreeSet<String>();
  210. set.add("jar:nbinst:///modules/ext/beansbinding-0.5.jar!/");
  211. librariesUrls.put("beans-binding", set);
  212. set = new TreeSet<String>();
  213. set.add("jar:nbinst:///docs/javaee6-doc-api.zip!/");
  214. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-beanutils.jar!/");
  215. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-collections.jar!/");
  216. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-digester.jar!/");
  217. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-logging.jar!/");
  218. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/jsf-api.jar!/");
  219. set.add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/jsf-impl.jar!/");
  220. librariesUrls.put("jsf", set);
  221. }
  222. private void generateInitDefaultLibrariesIngoreOrder(PrintWriter buff, Set<String> urls, String baseName) {
  223. buff.println("set = new TreeSet<String>();");
  224. for (String url : urls) {
  225. buff.println("set.add(\"" + url + "\");");
  226. }
  227. buff.println("librariesUrls.put(\"" + baseName + "\", set);");
  228. }
  229. private void addURLs(Set<String> urls, Iterable<URL> sourceUrls) {
  230. for (URL url : sourceUrls) {
  231. urls.add(url.toExternalForm());
  232. }
  233. }
  234. /**
  235. * This test fails if golden files differ from current state even if there are
  236. * only differnces in lines order.
  237. */
  238. // public void testDefaultLibraries() {
  239. //
  240. // LibraryManager libMan = LibraryManager.getDefault();
  241. // Library [] libs = libMan.getLibraries();
  242. //
  243. // for (int i = 0; i < libs.length; i++) {
  244. // System.out.println("**********" + libs[i].getName() + "**********");
  245. // File refFile = null;
  246. // PrintStream ps = null;
  247. // // names of files are based on library name
  248. // String baseName = libs[i].getName();
  249. // String refFileName = baseName + "_ref.txt";
  250. // String goldenFileName = baseName + "_pass.txt";
  251. // String diffFileName = baseName + "_diff.txt";
  252. //
  253. // // write library data to ref file
  254. // try {
  255. // ps = new PrintStream(new java.io.FileOutputStream(new File(getWorkDir(), refFileName)));
  256. // } catch (Exception exc) {
  257. // fail(exc.getMessage());
  258. // }
  259. //
  260. // List listOfClasspaths = libs[i].getContent("classpath");
  261. // dumpList("", listOfClasspaths, ps);
  262. // List listOfJavadocs = libs[i].getContent("javadoc");
  263. // dumpList("", listOfJavadocs, ps);
  264. // List listOfSrcs = libs[i].getContent("src");
  265. // dumpList("", listOfSrcs, ps);
  266. // ps.close();
  267. //
  268. // try {
  269. // compareReferenceFiles(refFileName, goldenFileName, diffFileName);
  270. // } catch (Exception exc) {
  271. // fail(exc.getMessage());
  272. // }
  273. // }
  274. //
  275. // }
  276. public void __testCreateLibrary() {
  277. // learn hostname
  278. String hostName = null;
  279. try {
  280. hostName = InetAddress.getLocalHost().getHostName();
  281. } catch (UnknownHostException uhe) {
  282. fail("Cannot get hostname: " + uhe.getMessage()); // NOI18N
  283. }
  284. hostName = hostName.replace('-', '_');
  285. // load platforms.properties file
  286. InputStream is = this.getClass().getResourceAsStream("libraries.properties");
  287. Properties props = new Properties();
  288. try {
  289. props.load(is);
  290. } catch (java.io.IOException ioe) {
  291. fail("Cannot load platforms properties: " + ioe.getMessage()); // NOI18N
  292. }
  293. String[] libCp = getTokensAsArray(props.getProperty(hostName + "library1_cp"));
  294. String[] libSrc = getTokensAsArray(props.getProperty(hostName + "library1_src"));
  295. String[] libJdoc = getTokensAsArray(props.getProperty(hostName + "library1_jdoc"));
  296. TestProjectUtils.addLibrary(props.getProperty(hostName + "library1_name"),
  297. libCp, libSrc, libJdoc);
  298. }
  299. public void __testListDefaultLibraries() {
  300. listDefaultLibs("e:\\work\\libs\\");
  301. }
  302. // -------------------------------------------------------------------------
  303. /* This method is intended only for generation of golden files from Beta2 release build
  304. */
  305. private void listDefaultLibs(String folder) {
  306. PrintStream pw = null;
  307. LibraryManager libMan = LibraryManager.getDefault();
  308. Library[] libs = libMan.getLibraries();
  309. for (int i = 0; i < libs.length; i++) {
  310. try {
  311. pw = new PrintStream(new FileOutputStream(folder + libs[i].getName() + ".txt"));
  312. } catch (FileNotFoundException fnfe) {
  313. fnfe.printStackTrace();
  314. }
  315. System.out.println("Display name: " + libs[i].getDisplayName());
  316. System.out.println("Name: " + libs[i].getName());
  317. List listOfClasspaths = libs[i].getContent("classpath");
  318. dumpList("Classpath: ", listOfClasspaths, System.out);
  319. dumpList("", listOfClasspaths, pw);
  320. List listOfJavadocs = libs[i].getContent("javadoc");
  321. dumpList("Javadoc: ", listOfJavadocs, System.out);
  322. dumpList("", listOfJavadocs, pw);
  323. List listOfSrcs = libs[i].getContent("src");
  324. dumpList("Sources: ", listOfSrcs, System.out);
  325. dumpList("", listOfSrcs, pw);
  326. pw.close();
  327. }
  328. }
  329. public String[] getGoldenFileLines(String goldenFileName) {
  330. File goldenFile = null;
  331. BufferedReader reader = null;
  332. try {
  333. goldenFile = getGoldenFile(goldenFileName);
  334. reader = new BufferedReader(new FileReader(goldenFile));
  335. } catch (Exception e) {
  336. fail("Unable to open following golden file '" + goldenFile.toString() + "'"); // NOI18N
  337. }
  338. List<String> linesList = new ArrayList<String>();
  339. String line;
  340. try {
  341. while ((line = reader.readLine()) != null) {
  342. System.out.println(line);
  343. if (line.trim() != "") {
  344. linesList.add(line);
  345. }
  346. }
  347. reader.close();
  348. } catch (Exception e) {
  349. }
  350. String[] lines = linesList.toArray(new String[0]);
  351. Arrays.sort(lines);
  352. return lines;
  353. }
  354. private void dumpList(String prefix, List list, PrintStream ps) {
  355. Iterator iter = list.iterator();
  356. while (iter.hasNext()) {
  357. ps.println(prefix + iter.next());
  358. }
  359. }
  360. private String[] getTokensAsArray(String str) {
  361. StringTokenizer st = new StringTokenizer(str, ",");
  362. String[] array = new String[st.countTokens()];
  363. int index = 0;
  364. while (st.hasMoreTokens()) {
  365. array[index++] = st.nextToken().trim();
  366. }
  367. return array;
  368. }
  369. }