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: /*
043: * TestDefaultLibraries.java
044: * NetBeans JUnit based test
045: *
046: * Created on 06 September 2004, 15:37
047: */
048:
049: package projects;
050:
051: import java.io.BufferedReader;
052: import java.io.File;
053: import java.io.FileNotFoundException;
054: import java.io.FileOutputStream;
055: import java.io.FileReader;
056: import java.io.FileWriter;
057: import java.io.IOException;
058: import java.io.InputStream;
059: import java.io.PrintStream;
060: import java.io.PrintWriter;
061: import java.net.InetAddress;
062: import java.net.URL;
063: import java.net.UnknownHostException;
064: import java.util.ArrayList;
065: import java.util.Arrays;
066: import java.util.HashMap;
067: import java.util.Iterator;
068: import java.util.List;
069: import java.util.Map;
070: import java.util.Properties;
071: import java.util.Set;
072: import java.util.StringTokenizer;
073: import java.util.TreeSet;
074: import junit.framework.*;
075: import org.netbeans.jellytools.JellyTestCase;
076: import org.netbeans.junit.*;
077: import org.netbeans.api.project.libraries.LibraryManager;
078: import org.netbeans.api.project.libraries.Library;
079:
080: /**
081: *
082: */
083: public class LibrariesTest extends JellyTestCase {
084: Map<String, Set<String>> librariesUrls;
085:
086: public LibrariesTest(java.lang.String testName) {
087: super (testName);
088: }
089:
090: public static void main(java.lang.String[] args) {
091: junit.textui.TestRunner.run(suite());
092: }
093:
094: public static Test suite() {
095: TestSuite suite = new NbTestSuite(LibrariesTest.class);
096: return suite;
097: }
098:
099: @Override
100: protected void setUp() throws Exception {
101: super .setUp();
102: librariesUrls = new HashMap<String, Set<String>>();
103:
104: }
105:
106: // -------------------------------------------------------------------------
107:
108: /**
109: * This test fails only for golden files that differ from current state
110: * in content (ignored are differnces related to lines order).
111: *
112: */
113: public void testDefaultLibrariesIgnoreOrder() throws IOException {
114: init1();
115: LibraryManager libMan = LibraryManager.getDefault();
116: Library[] libs = libMan.getLibraries();
117: String errorString = "";
118:
119: PrintWriter initMethodBuff = new PrintWriter(new FileWriter(
120: new File(getWorkDir(),
121: "testDefaultLibrariesIgnoreOrder.txt")));
122: try {
123: for (int i = 0; i < libs.length; i++) {
124:
125: // names of files are based on library name
126: String baseName = libs[i].getName();
127:
128: Set<String> urls = new TreeSet<String>();
129: addURLs(urls, libs[i].getContent("classpath"));
130: addURLs(urls, libs[i].getContent("javadoc"));
131: addURLs(urls, libs[i].getContent("src"));
132: generateInitDefaultLibrariesIngoreOrder(initMethodBuff,
133: urls, baseName);
134: assertEquals(
135: "Following files differ other way than lines order:",
136: librariesUrls.get(baseName), urls);
137:
138: }
139: } finally {
140: initMethodBuff.close();
141: }
142: }
143:
144: public void init1() {
145: Set<String> set = new TreeSet<String>();
146: set
147: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/FastInfoset.jar!/");
148: set
149: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/activation.jar!/");
150: set
151: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jax-qname.jar!/");
152: set
153: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxp-api.jar!/");
154: set
155: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-api.jar!/");
156: set
157: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-impl.jar!/");
158: set
159: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jaxrpc-spi.jar!/");
160: set
161: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/jsr173_api.jar!/");
162: set
163: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/mail.jar!/");
164: set
165: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/relaxngDatatype.jar!/");
166: set
167: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/saaj-api.jar!/");
168: set
169: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/saaj-impl.jar!/");
170: set
171: .add("jar:nbinst://org.netbeans.modules.websvc.jaxrpc16/modules/ext/jaxrpc16/xsdlib.jar!/");
172: librariesUrls.put("jaxrpc16", set);
173: set = new TreeSet<String>();
174: set.add("jar:nbinst:///docs/javaee5-doc-api.zip!/");
175: set
176: .add("jar:nbinst:///modules/ext/toplink/toplink-essentials-agent.jar!/");
177: set
178: .add("jar:nbinst:///modules/ext/toplink/toplink-essentials.jar!/");
179: librariesUrls.put("toplink", set);
180: set = new TreeSet<String>();
181: set
182: .add("jar:nbinst://org.netbeans.modules.form/modules/ext/AbsoluteLayout.jar!/");
183: librariesUrls.put("absolutelayout", set);
184: set = new TreeSet<String>();
185: set.add("jar:nbinst:///modules/ext/jaxws21/FastInfoset.jar!/");
186: set.add("jar:nbinst:///modules/ext/jaxws21/activation.jar!/");
187: set.add("jar:nbinst:///modules/ext/jaxws21/api/jaxb-api.jar!/");
188: set
189: .add("jar:nbinst:///modules/ext/jaxws21/api/jaxws-api.jar!/");
190: set
191: .add("jar:nbinst:///modules/ext/jaxws21/api/jsr173_api.jar!/");
192: set
193: .add("jar:nbinst:///modules/ext/jaxws21/api/jsr181-api.jar!/");
194: set.add("jar:nbinst:///modules/ext/jaxws21/api/saaj-api.jar!/");
195: set.add("jar:nbinst:///modules/ext/jaxws21/http.jar!/");
196: set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-impl.jar!/");
197: set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-xjc.jar!/");
198: set.add("jar:nbinst:///modules/ext/jaxws21/jaxws-rt.jar!/");
199: set.add("jar:nbinst:///modules/ext/jaxws21/jaxws-tools.jar!/");
200: set.add("jar:nbinst:///modules/ext/jaxws21/jsr250-api.jar!/");
201: set.add("jar:nbinst:///modules/ext/jaxws21/saaj-impl.jar!/");
202: set.add("jar:nbinst:///modules/ext/jaxws21/sjsxp.jar!/");
203: set.add("jar:nbinst:///modules/ext/jaxws21/stax-ex.jar!/");
204: set.add("jar:nbinst:///modules/ext/jaxws21/streambuffer.jar!/");
205: librariesUrls.put("jaxws20", set);
206: set = new TreeSet<String>();
207: set.add("jar:nbinst:///docs/org-netbeans-libs-javacapi.zip!/");
208: set.add("jar:nbinst:///modules/ext/javac-api-nb-7.0-b07.jar!/");
209: librariesUrls.put("javac-api", set);
210: set = new TreeSet<String>();
211: set.add("jar:nbinst:///docs/javaee5-doc-api.zip!/");
212: set
213: .add("jar:nbinst://org.netbeans.modules.web.jstl11/modules/ext/jstl.jar!/");
214: set
215: .add("jar:nbinst://org.netbeans.modules.web.jstl11/modules/ext/standard.jar!/");
216: librariesUrls.put("jstl11", set);
217: set = new TreeSet<String>();
218: set.add("jar:nbinst:///modules/ext/jaxws21/activation.jar!/");
219: set.add("jar:nbinst:///modules/ext/jaxws21/api/jaxb-api.jar!/");
220: set
221: .add("jar:nbinst:///modules/ext/jaxws21/api/jsr173_api.jar!/");
222: set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-impl.jar!/");
223: set.add("jar:nbinst:///modules/ext/jaxws21/jaxb-xjc.jar!/");
224: librariesUrls.put("jaxb20", set);
225: set = new TreeSet<String>();
226: set
227: .add("jar:nbinst://org.netbeans.modules.swingapp/docs/appframework-0.30-doc.zip!/");
228: set
229: .add("jar:nbinst://org.netbeans.modules.swingapp/modules/ext/appframework-0.30.jar!/");
230: set
231: .add("jar:nbinst://org.netbeans.modules.swingapp/modules/ext/swing-worker.jar!/");
232: librariesUrls.put("swing-app-framework", set);
233: set = new TreeSet<String>();
234: set.add("jar:nbinst:///docs/struts-1.2.9-javadoc.zip!/");
235: set
236: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/antlr.jar!/");
237: set
238: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-beanutils.jar!/");
239: set
240: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-digester.jar!/");
241: set
242: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-fileupload.jar!/");
243: set
244: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-logging.jar!/");
245: set
246: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/commons-validator.jar!/");
247: set
248: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/jakarta-oro.jar!/");
249: set
250: .add("jar:nbinst://org.netbeans.modules.web.struts/modules/ext/struts/struts.jar!/");
251: librariesUrls.put("struts", set);
252: set = new TreeSet<String>();
253: set
254: .add("jar:nbinst://org.netbeans.modules.junit/docs/junit-3.8.2-api.zip!/");
255: set
256: .add("jar:nbinst://org.netbeans.modules.junit/modules/ext/junit-3.8.2.jar!/");
257: set
258: .add("jar:nbinst://org.netbeans.modules.junit/modules/ext/junit-4.1.jar!/");
259: librariesUrls.put("junit", set);
260: set = new TreeSet<String>();
261: set
262: .add("jar:nbinst://org.jdesktop.layout/docs/swing-layout-1.0.2-doc.zip!/");
263: set
264: .add("jar:nbinst://org.jdesktop.layout/docs/swing-layout-1.0.2-src.zip!/");
265: set
266: .add("jar:nbinst://org.jdesktop.layout/modules/ext/swing-layout-1.0.2.jar!/");
267: librariesUrls.put("swing-layout", set);
268: set = new TreeSet<String>();
269: set
270: .add("jar:nbinst://org.netbeans.modules.java.j2seproject/ant/extra/org-netbeans-modules-java-j2seproject-copylibstask.jar!/");
271: librariesUrls.put("CopyLibs", set);
272: set = new TreeSet<String>();
273: set.add("jar:nbinst:///modules/ext/beansbinding-0.5.jar!/");
274: librariesUrls.put("beans-binding", set);
275: set = new TreeSet<String>();
276: set.add("jar:nbinst:///docs/javaee5-doc-api.zip!/");
277: set
278: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-beanutils.jar!/");
279: set
280: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-collections.jar!/");
281: set
282: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-digester.jar!/");
283: set
284: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/commons-logging.jar!/");
285: set
286: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/jsf-api.jar!/");
287: set
288: .add("jar:nbinst://org.netbeans.modules.web.jsf/modules/ext/jsf/jsf-impl.jar!/");
289: librariesUrls.put("jsf", set);
290:
291: }
292:
293: private void generateInitDefaultLibrariesIngoreOrder(
294: PrintWriter buff, Set<String> urls, String baseName) {
295: buff.println("set = new TreeSet<String>();");
296: for (String url : urls) {
297: buff.println("set.add(\"" + url + "\");");
298: }
299: buff.println("librariesUrls.put(\"" + baseName + "\", set);");
300: }
301:
302: private void addURLs(Set<String> urls, Iterable<URL> sourceUrls) {
303: for (URL url : sourceUrls) {
304: urls.add(url.toExternalForm());
305: }
306: }
307:
308: /**
309: * This test fails if golden files differ from current state even if there are
310: * only differnces in lines order.
311: */
312: // public void testDefaultLibraries() {
313: //
314: // LibraryManager libMan = LibraryManager.getDefault();
315: // Library [] libs = libMan.getLibraries();
316: //
317: // for (int i = 0; i < libs.length; i++) {
318: // System.out.println("**********" + libs[i].getName() + "**********");
319: // File refFile = null;
320: // PrintStream ps = null;
321: // // names of files are based on library name
322: // String baseName = libs[i].getName();
323: // String refFileName = baseName + "_ref.txt";
324: // String goldenFileName = baseName + "_pass.txt";
325: // String diffFileName = baseName + "_diff.txt";
326: //
327: // // write library data to ref file
328: // try {
329: // ps = new PrintStream(new java.io.FileOutputStream(new File(getWorkDir(), refFileName)));
330: // } catch (Exception exc) {
331: // fail(exc.getMessage());
332: // }
333: //
334: // List listOfClasspaths = libs[i].getContent("classpath");
335: // dumpList("", listOfClasspaths, ps);
336: // List listOfJavadocs = libs[i].getContent("javadoc");
337: // dumpList("", listOfJavadocs, ps);
338: // List listOfSrcs = libs[i].getContent("src");
339: // dumpList("", listOfSrcs, ps);
340: // ps.close();
341: //
342: // try {
343: // compareReferenceFiles(refFileName, goldenFileName, diffFileName);
344: // } catch (Exception exc) {
345: // fail(exc.getMessage());
346: // }
347: // }
348: //
349: // }
350: public void __testCreateLibrary() {
351:
352: // learn hostname
353: String hostName = null;
354: try {
355: hostName = InetAddress.getLocalHost().getHostName();
356: } catch (UnknownHostException uhe) {
357: fail("Cannot get hostname: " + uhe.getMessage()); // NOI18N
358: }
359: hostName = hostName.replace('-', '_');
360:
361: // load platforms.properties file
362: InputStream is = this .getClass().getResourceAsStream(
363: "libraries.properties");
364: Properties props = new Properties();
365: try {
366: props.load(is);
367: } catch (java.io.IOException ioe) {
368: fail("Cannot load platforms properties: "
369: + ioe.getMessage()); // NOI18N
370: }
371:
372: String[] libCp = getTokensAsArray(props.getProperty(hostName
373: + "library1_cp"));
374: String[] libSrc = getTokensAsArray(props.getProperty(hostName
375: + "library1_src"));
376: String[] libJdoc = getTokensAsArray(props.getProperty(hostName
377: + "library1_jdoc"));
378:
379: TestProjectUtils.addLibrary(props.getProperty(hostName
380: + "library1_name"), libCp, libSrc, libJdoc);
381: }
382:
383: public void __testListDefaultLibraries() {
384: listDefaultLibs("e:\\work\\libs\\");
385: }
386:
387: // -------------------------------------------------------------------------
388:
389: /* This method is intended only for generation of golden files from Beta2 release build
390: */
391: private void listDefaultLibs(String folder) {
392:
393: PrintStream pw = null;
394: LibraryManager libMan = LibraryManager.getDefault();
395: Library[] libs = libMan.getLibraries();
396:
397: for (int i = 0; i < libs.length; i++) {
398: try {
399: pw = new PrintStream(new FileOutputStream(folder
400: + libs[i].getName() + ".txt"));
401: } catch (FileNotFoundException fnfe) {
402: fnfe.printStackTrace();
403: }
404:
405: System.out.println("Display name: "
406: + libs[i].getDisplayName());
407: System.out.println("Name: " + libs[i].getName());
408: List listOfClasspaths = libs[i].getContent("classpath");
409: dumpList("Classpath: ", listOfClasspaths, System.out);
410: dumpList("", listOfClasspaths, pw);
411: List listOfJavadocs = libs[i].getContent("javadoc");
412: dumpList("Javadoc: ", listOfJavadocs, System.out);
413: dumpList("", listOfJavadocs, pw);
414: List listOfSrcs = libs[i].getContent("src");
415: dumpList("Sources: ", listOfSrcs, System.out);
416: dumpList("", listOfSrcs, pw);
417: pw.close();
418:
419: }
420:
421: }
422:
423: public String[] getGoldenFileLines(String goldenFileName) {
424: File goldenFile = null;
425: BufferedReader reader = null;
426: try {
427: goldenFile = getGoldenFile(goldenFileName);
428: reader = new BufferedReader(new FileReader(goldenFile));
429: } catch (Exception e) {
430: fail("Unable to open following golden file '"
431: + goldenFile.toString() + "'"); // NOI18N
432: }
433: List<String> linesList = new ArrayList<String>();
434: String line;
435: try {
436: while ((line = reader.readLine()) != null) {
437: System.out.println(line);
438: if (line.trim() != "")
439: linesList.add(line);
440: }
441: reader.close();
442: } catch (Exception e) {
443: }
444: String[] lines = (String[]) linesList.toArray(new String[0]);
445: Arrays.sort(lines);
446: return lines;
447: }
448:
449: private void dumpList(String prefix, List list, PrintStream ps) {
450: Iterator iter = list.iterator();
451: while (iter.hasNext()) {
452: ps.println(prefix + iter.next());
453: }
454: }
455:
456: private String[] getTokensAsArray(String str) {
457: StringTokenizer st = new StringTokenizer(str, ",");
458: String[] array = new String[st.countTokens()];
459: int index = 0;
460: while (st.hasMoreTokens()) {
461: array[index++] = st.nextToken().trim();
462: }
463: return array;
464: }
465:
466: }
|