KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projects > TestLibraries


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * TestDefaultLibraries.java
22  * NetBeans JUnit based test
23  *
24  * Created on 06 September 2004, 15:37
25  */

26
27 package projects;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.PrintStream JavaDoc;
36 import java.net.InetAddress JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.net.UnknownHostException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45 import junit.framework.*;
46 import org.netbeans.jellytools.JellyTestCase;
47 import org.netbeans.junit.*;
48 import org.netbeans.api.project.libraries.LibraryManager;
49 import org.netbeans.api.project.libraries.Library;
50
51 /**
52  *
53  */

54 public class TestLibraries extends JellyTestCase {
55     
56     public TestLibraries(java.lang.String JavaDoc testName) {
57         super(testName);
58     }
59     
60     public static void main(java.lang.String JavaDoc[] args) {
61         junit.textui.TestRunner.run(suite());
62     }
63     
64     public static Test suite() {
65         TestSuite suite = new NbTestSuite(TestLibraries.class);
66         return suite;
67     }
68     
69     // -------------------------------------------------------------------------
70

71     /**
72      * This test fails only for golden files that differ from current state
73      * in content (ignored are differnces related to lines order).
74      *
75      */

76     public void testDefaultLibrariesIgnoreOrder() {
77         
78         LibraryManager libMan = LibraryManager.getDefault();
79         Library [] libs = libMan.getLibraries();
80         String JavaDoc errorString = "";
81         
82         for (int i = 0; i < libs.length; i++) {
83             
84             // names of files are based on library name
85
String JavaDoc baseName = libs[i].getName();
86             String JavaDoc goldenFileName = baseName + "_pass.txt";
87             
88             List JavaDoc<URL JavaDoc> listOfClasspaths = libs[i].getContent("classpath");
89             List JavaDoc<URL JavaDoc> listOfJavadocs = libs[i].getContent("javadoc");
90             List JavaDoc<URL JavaDoc> listOfSrcs = libs[i].getContent("src");
91             
92             List JavaDoc<String JavaDoc> allLists = new ArrayList JavaDoc<String JavaDoc>();
93             for (Iterator JavaDoc<URL JavaDoc> it = listOfClasspaths.iterator(); it.hasNext();) {
94                 String JavaDoc item = it.next().toString();
95                 allLists.add(item);
96                 System.out.println(item);
97             }
98             for (Iterator JavaDoc<URL JavaDoc> it = listOfJavadocs.iterator(); it.hasNext();) {
99                 String JavaDoc item = it.next().toString();
100                 allLists.add(item);
101                 System.out.println(item);
102             }
103             for (Iterator JavaDoc<URL JavaDoc> it = listOfSrcs.iterator(); it.hasNext();) {
104                 String JavaDoc item = it.next().toString();
105                 allLists.add(item);
106                 System.out.println(item);
107             }
108             
109             String JavaDoc[] goldenFileLines = getGoldenFileLines(goldenFileName);
110             String JavaDoc[] lines = (String JavaDoc[]) allLists.toArray(new String JavaDoc[0]);
111             
112             Arrays.sort(lines);
113             boolean equality = Arrays.equals(lines, goldenFileLines);
114             if(!equality){
115                 errorString += libs[i].getName() + "; ";
116             }
117         }
118         assertTrue("Following files differ other way than lines order:" + errorString, errorString.length() == 0);
119     }
120     
121    /**
122      * This test fails if golden files differ from current state even if there are
123      * only differnces in lines order.
124      */

125     public void testDefaultLibraries() {
126         
127         LibraryManager libMan = LibraryManager.getDefault();
128         Library [] libs = libMan.getLibraries();
129         
130         for (int i = 0; i < libs.length; i++) {
131             System.out.println("**********" + libs[i].getName() + "**********");
132             File JavaDoc refFile = null;
133             PrintStream JavaDoc ps = null;
134             // names of files are based on library name
135
String JavaDoc baseName = libs[i].getName();
136             String JavaDoc refFileName = baseName + "_ref.txt";
137             String JavaDoc goldenFileName = baseName + "_pass.txt";
138             String JavaDoc diffFileName = baseName + "_diff.txt";
139             
140             // write library data to ref file
141
try {
142                 ps = new PrintStream JavaDoc(new java.io.FileOutputStream JavaDoc(new File JavaDoc(getWorkDir(), refFileName)));
143             } catch (Exception JavaDoc exc) {
144                 fail(exc.getMessage());
145             }
146             
147             List JavaDoc listOfClasspaths = libs[i].getContent("classpath");
148             dumpList("", listOfClasspaths, ps);
149             List JavaDoc listOfJavadocs = libs[i].getContent("javadoc");
150             dumpList("", listOfJavadocs, ps);
151             List JavaDoc listOfSrcs = libs[i].getContent("src");
152             dumpList("", listOfSrcs, ps);
153             ps.close();
154             
155             try {
156                 compareReferenceFiles(refFileName, goldenFileName, diffFileName);
157             } catch (Exception JavaDoc exc) {
158                 fail(exc.getMessage());
159             }
160         }
161         
162     }
163     
164     public void __testCreateLibrary() {
165         
166         // learn hostname
167
String JavaDoc hostName = null;
168         try {
169             hostName = InetAddress.getLocalHost().getHostName();
170         } catch (UnknownHostException JavaDoc uhe) {
171             fail("Cannot get hostname: " + uhe.getMessage()); // NOI18N
172
}
173         hostName = hostName.replace('-', '_');
174         
175         // load platforms.properties file
176
InputStream JavaDoc is = this.getClass().getResourceAsStream("libraries.properties");
177         Properties JavaDoc props = new Properties JavaDoc();
178         try {
179             props.load(is);
180         } catch (java.io.IOException JavaDoc ioe) {
181             fail("Cannot load platforms properties: " + ioe.getMessage()); // NOI18N
182
}
183         
184         String JavaDoc[] libCp = getTokensAsArray(props.getProperty(hostName + "library1_cp"));
185         String JavaDoc[] libSrc = getTokensAsArray(props.getProperty(hostName + "library1_src"));
186         String JavaDoc[] libJdoc = getTokensAsArray(props.getProperty(hostName + "library1_jdoc"));
187         
188         TestProjectUtils.addLibrary(props.getProperty(hostName + "library1_name"),
189                 libCp, libSrc, libJdoc);
190     }
191     
192     public void __testListDefaultLibraries() {
193         listDefaultLibs("e:\\work\\libs\\");
194     }
195     
196     // -------------------------------------------------------------------------
197

198     /* This method is intended only for generation of golden files from Beta2 release build
199      */

200     private void listDefaultLibs(String JavaDoc folder) {
201         
202         PrintStream JavaDoc pw = null;
203         LibraryManager libMan = LibraryManager.getDefault();
204         Library [] libs = libMan.getLibraries();
205         
206         for (int i = 0; i < libs.length; i++) {
207             try {
208                 pw = new PrintStream JavaDoc(new FileOutputStream JavaDoc(folder + libs[i].getName() + ".txt"));
209             } catch (FileNotFoundException JavaDoc fnfe) {
210                 fnfe.printStackTrace();
211             }
212             
213             System.out.println("Display name: " + libs[i].getDisplayName());
214             System.out.println("Name: " + libs[i].getName());
215             List JavaDoc listOfClasspaths = libs[i].getContent("classpath");
216             dumpList("Classpath: ", listOfClasspaths, System.out);
217             dumpList("", listOfClasspaths, pw);
218             List JavaDoc listOfJavadocs = libs[i].getContent("javadoc");
219             dumpList("Javadoc: ", listOfJavadocs, System.out);
220             dumpList("", listOfJavadocs, pw);
221             List JavaDoc listOfSrcs = libs[i].getContent("src");
222             dumpList("Sources: ", listOfSrcs, System.out);
223             dumpList("", listOfSrcs, pw);
224             pw.close();
225             
226         }
227         
228     }
229     public String JavaDoc[] getGoldenFileLines(String JavaDoc goldenFileName){
230         File JavaDoc goldenFile = null;
231         BufferedReader JavaDoc reader = null;
232         try {
233             goldenFile = getGoldenFile(goldenFileName);
234             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(goldenFile));
235         } catch (Exception JavaDoc e) {
236             fail("Unable to open following golden file '" + goldenFile.toString() + "'"); // NOI18N
237
}
238         List JavaDoc<String JavaDoc> linesList = new ArrayList JavaDoc<String JavaDoc>();
239         String JavaDoc line;
240         try{
241             while((line = reader.readLine()) != null){
242                 System.out.println(line);
243                 if(line.trim()!="")
244                 linesList.add(line);
245             }
246             reader.close();
247         } catch(Exception JavaDoc e){}
248         String JavaDoc[] lines = (String JavaDoc[]) linesList.toArray(new String JavaDoc[0]);
249         Arrays.sort(lines);
250         return lines;
251     }
252     
253     private void dumpList(String JavaDoc prefix, List JavaDoc list, PrintStream JavaDoc ps) {
254         Iterator JavaDoc iter = list.iterator();
255         while (iter.hasNext()) {
256             ps.println(prefix + iter.next());
257         }
258     }
259     
260     private String JavaDoc[] getTokensAsArray(String JavaDoc str) {
261         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, ",");
262         String JavaDoc[] array = new String JavaDoc[st.countTokens()];
263         int index = 0;
264         while (st.hasMoreTokens()) {
265             array[index++] = st.nextToken().trim();
266         }
267         return array;
268     }
269     
270 }
271
Popular Tags