1 19 20 26 27 package projects; 28 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.FileReader ; 34 import java.io.InputStream ; 35 import java.io.PrintStream ; 36 import java.net.InetAddress ; 37 import java.net.URL ; 38 import java.net.UnknownHostException ; 39 import java.util.ArrayList ; 40 import java.util.Arrays ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Properties ; 44 import java.util.StringTokenizer ; 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 54 public class TestLibraries extends JellyTestCase { 55 56 public TestLibraries(java.lang.String testName) { 57 super(testName); 58 } 59 60 public static void main(java.lang.String [] 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 71 76 public void testDefaultLibrariesIgnoreOrder() { 77 78 LibraryManager libMan = LibraryManager.getDefault(); 79 Library [] libs = libMan.getLibraries(); 80 String errorString = ""; 81 82 for (int i = 0; i < libs.length; i++) { 83 84 String baseName = libs[i].getName(); 86 String goldenFileName = baseName + "_pass.txt"; 87 88 List <URL > listOfClasspaths = libs[i].getContent("classpath"); 89 List <URL > listOfJavadocs = libs[i].getContent("javadoc"); 90 List <URL > listOfSrcs = libs[i].getContent("src"); 91 92 List <String > allLists = new ArrayList <String >(); 93 for (Iterator <URL > it = listOfClasspaths.iterator(); it.hasNext();) { 94 String item = it.next().toString(); 95 allLists.add(item); 96 System.out.println(item); 97 } 98 for (Iterator <URL > it = listOfJavadocs.iterator(); it.hasNext();) { 99 String item = it.next().toString(); 100 allLists.add(item); 101 System.out.println(item); 102 } 103 for (Iterator <URL > it = listOfSrcs.iterator(); it.hasNext();) { 104 String item = it.next().toString(); 105 allLists.add(item); 106 System.out.println(item); 107 } 108 109 String [] goldenFileLines = getGoldenFileLines(goldenFileName); 110 String [] lines = (String []) allLists.toArray(new String [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 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 refFile = null; 133 PrintStream ps = null; 134 String baseName = libs[i].getName(); 136 String refFileName = baseName + "_ref.txt"; 137 String goldenFileName = baseName + "_pass.txt"; 138 String diffFileName = baseName + "_diff.txt"; 139 140 try { 142 ps = new PrintStream (new java.io.FileOutputStream (new File (getWorkDir(), refFileName))); 143 } catch (Exception exc) { 144 fail(exc.getMessage()); 145 } 146 147 List listOfClasspaths = libs[i].getContent("classpath"); 148 dumpList("", listOfClasspaths, ps); 149 List listOfJavadocs = libs[i].getContent("javadoc"); 150 dumpList("", listOfJavadocs, ps); 151 List listOfSrcs = libs[i].getContent("src"); 152 dumpList("", listOfSrcs, ps); 153 ps.close(); 154 155 try { 156 compareReferenceFiles(refFileName, goldenFileName, diffFileName); 157 } catch (Exception exc) { 158 fail(exc.getMessage()); 159 } 160 } 161 162 } 163 164 public void __testCreateLibrary() { 165 166 String hostName = null; 168 try { 169 hostName = InetAddress.getLocalHost().getHostName(); 170 } catch (UnknownHostException uhe) { 171 fail("Cannot get hostname: " + uhe.getMessage()); } 173 hostName = hostName.replace('-', '_'); 174 175 InputStream is = this.getClass().getResourceAsStream("libraries.properties"); 177 Properties props = new Properties (); 178 try { 179 props.load(is); 180 } catch (java.io.IOException ioe) { 181 fail("Cannot load platforms properties: " + ioe.getMessage()); } 183 184 String [] libCp = getTokensAsArray(props.getProperty(hostName + "library1_cp")); 185 String [] libSrc = getTokensAsArray(props.getProperty(hostName + "library1_src")); 186 String [] 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 198 200 private void listDefaultLibs(String folder) { 201 202 PrintStream 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 (new FileOutputStream (folder + libs[i].getName() + ".txt")); 209 } catch (FileNotFoundException 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 listOfClasspaths = libs[i].getContent("classpath"); 216 dumpList("Classpath: ", listOfClasspaths, System.out); 217 dumpList("", listOfClasspaths, pw); 218 List listOfJavadocs = libs[i].getContent("javadoc"); 219 dumpList("Javadoc: ", listOfJavadocs, System.out); 220 dumpList("", listOfJavadocs, pw); 221 List 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 [] getGoldenFileLines(String goldenFileName){ 230 File goldenFile = null; 231 BufferedReader reader = null; 232 try { 233 goldenFile = getGoldenFile(goldenFileName); 234 reader = new BufferedReader (new FileReader (goldenFile)); 235 } catch (Exception e) { 236 fail("Unable to open following golden file '" + goldenFile.toString() + "'"); } 238 List <String > linesList = new ArrayList <String >(); 239 String 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 e){} 248 String [] lines = (String []) linesList.toArray(new String [0]); 249 Arrays.sort(lines); 250 return lines; 251 } 252 253 private void dumpList(String prefix, List list, PrintStream ps) { 254 Iterator iter = list.iterator(); 255 while (iter.hasNext()) { 256 ps.println(prefix + iter.next()); 257 } 258 } 259 260 private String [] getTokensAsArray(String str) { 261 StringTokenizer st = new StringTokenizer (str, ","); 262 String [] array = new String [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 |