1 19 20 package org.netbeans.modules.java.source; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileFilter ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.util.Enumeration ; 34 import java.util.Iterator ; 35 import java.util.Properties ; 36 import java.util.zip.ZipEntry ; 37 import java.util.zip.ZipFile ; 38 import junit.framework.TestCase; 39 import org.netbeans.junit.NbTestCase; 40 41 42 46 public class TestUtil { 47 48 public static final String RT_JAR = "jre/lib/rt.jar"; 49 public static final String SRC_ZIP = "src.zip"; 50 51 52 private TestUtil() { 53 } 54 55 public static void copyFiles( File destDir, String ... resourceNames ) throws IOException { 56 copyFiles(getDataDir(), destDir, resourceNames); 57 } 58 59 public static void copyFiles( File sourceDir, File destDir, String ... resourceNames ) throws IOException { 60 61 for( String resourceName : resourceNames ) { 62 63 File src = new File ( sourceDir, resourceName ); 64 65 if ( !src.canRead() ) { 66 TestCase.fail( "The test requires the file: " + resourceName + " to be readable and stored in: " + sourceDir ); 67 } 68 69 InputStream is = new FileInputStream ( src ); 70 BufferedInputStream bis = new BufferedInputStream ( is ); 71 72 File dest = new File ( destDir, resourceName ); 73 File parent = dest.getParentFile(); 74 75 if ( !parent.exists() ) { 76 parent.mkdirs(); 77 } 78 79 dest.createNewFile(); 80 BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( dest ) ); 81 82 copyFile( bis, bos ); 83 } 84 } 85 86 public static void unzip( ZipFile zip, File dest ) throws IOException { 87 88 for( Enumeration <? extends ZipEntry > e = zip.entries(); e.hasMoreElements(); ) { 89 ZipEntry entry = e.nextElement(); 90 File f = new File ( dest, entry.getName() ); 91 if ( entry.isDirectory() ) { 92 f.mkdirs(); 93 } 94 else { 95 f.getParentFile().mkdirs(); 96 f.createNewFile(); 97 BufferedInputStream bis = new BufferedInputStream ( zip.getInputStream( entry ) ); 98 BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( f ) ); 99 copyFile( bis, bos ); 100 } 101 } 102 103 } 104 105 public static File createWorkFolder() throws IOException { 106 File tempFile = File.createTempFile( "TestWorkDir", null ); 107 tempFile.delete(); 108 tempFile.mkdir(); 109 return tempFile; 110 } 111 112 public static FileFilter createExtensionFilter( boolean folders, final String ... extensions ) { 113 return new ExtensionFileFilter( folders, extensions ); 114 } 115 116 public static void removeWorkFolder( File file ) { 117 deleteRecursively( file ); 118 } 119 120 123 public static String collectionDiff( Iterable c1, Iterable c2 ) { 124 return collectionDiff( c1.iterator(), c2.iterator() ); 125 } 126 127 public static String collectionDiff( Iterator it1, Iterator it2 ) { 128 129 StringBuilder sb = new StringBuilder (); 130 131 int index = 0; 132 boolean printing = false; 133 while( it1.hasNext() ) { 134 135 Object o1 = it1.next(); 136 137 Object o2 = it2.hasNext() ? it2.next() : null ; 138 139 if ( !o1.equals( o2 ) ) { 140 if ( !printing ) { 141 printing = true; 142 sb.append("\n"); 143 } 144 sb.append( index + " " + o1 + " -> " + ( o2 == null ? "NULL" : o2 ) + "\n" ); 145 } 146 else if ( printing ) { 147 printing = false; 148 } 149 150 index++; 151 } 152 153 if ( it2.hasNext() ) { 154 sb.append( "\n" ); 155 } 156 while( it2.hasNext() ) { 157 sb.append( index + " [NULL]" + " -> " + it2.next() + "\n" ); 158 index ++; 159 } 160 161 return sb.toString(); 162 } 163 164 public static String fileToString( File file ) throws IOException { 165 166 BufferedReader reader = new BufferedReader ( new FileReader ( file ) ); 167 StringBuffer sb = new StringBuffer (); 168 169 for( String line = reader.readLine(); line != null; line = reader.readLine() ) { 170 sb.append( line ).append( "\n" ); 172 } 173 174 return sb.toString(); 175 } 176 177 179 public static File getDataDir() { 180 return TemporaryTestCase.getDataFolder(); 181 } 182 183 185 public static File getJdkDir() { 186 187 Properties p = System.getProperties(); 188 String javaHomeProp = p.getProperty( "java.home" ); 189 190 if ( javaHomeProp == null ) { 191 throw new IllegalStateException ( "Can't find java.home property "); 192 } 193 else { 194 File jre = new File ( javaHomeProp ); 195 if ( !jre.canRead() ) { 196 throw new IllegalStateException ( "Can't read " + jre ); 197 } 198 File dir = jre.getParentFile(); 199 if ( !jre.canRead() ) { 200 throw new IllegalStateException ( "Can't read " + dir); 201 } 202 return dir; 203 } 204 } 205 206 207 212 public static File getJdkFile( String path ) { 213 File dir = getJdkDir(); 214 215 File f = new File ( dir, path ); 216 217 if ( f.canRead() ) { 218 return f; 219 } 220 else { 221 throw new IllegalArgumentException ( "Can't read file " + f ); 222 } 223 224 } 225 226 227 229 private static int BLOCK_SIZE = 16384; 230 231 private static void copyFile( InputStream is, OutputStream os ) throws IOException { 232 byte[] b = new byte[ BLOCK_SIZE ]; 233 int count = is.read(b); 234 235 while (count != -1) 236 { 237 os.write(b, 0, count); 238 count = is.read(b); 239 } 240 241 is.close(); 242 os.close(); 243 } 244 245 247 private static void deleteRecursively( File file ) { 248 249 if ( file.isDirectory() ) { 250 File [] files = file.listFiles(); 251 for( File f : files ) { 252 deleteRecursively( f ); 253 } 254 } 255 256 file.delete(); 257 } 258 259 260 303 304 306 private static class ExtensionFileFilter implements FileFilter { 307 308 private boolean folders; 309 private String [] extensions; 310 311 public ExtensionFileFilter( boolean folders, String ... extensions ) { 312 this.folders = folders; 313 this.extensions = extensions; 314 } 315 316 317 public boolean accept( File file ) { 318 319 if ( folders && file.isDirectory() ) { 320 return true; 321 } 322 323 for( String ext : extensions ) { 324 if ( file.getName().endsWith( ext ) ) { 325 return true; 326 } 327 } 328 329 return false; 330 } 331 332 333 } 334 335 private static class TemporaryTestCase extends NbTestCase { 336 337 private static TemporaryTestCase INSTANCE = new TemporaryTestCase(); 338 339 TemporaryTestCase() { 340 super( TemporaryTestCase.class.toString() ); 341 } 342 343 public static File getDataFolder() { 344 return INSTANCE.getDataDir(); 345 } 346 347 } 348 349 350 } 351 | Popular Tags |