1 /* 2 * Copyright (c) 2002-2003 The XDoclet Team. 3 * All rights reserved. 4 */ 5 6 package xdoclet.util; 7 8 import java.io.File; 9 import java.io.BufferedReader; 10 import java.io.InputStream; 11 import java.io.InputStreamReader; 12 import java.util.Locale; 13 14 import junit.framework.TestCase; 15 import junit.framework.AssertionFailedError; 16 17 /*** 18 * Test Case for the FileUtils class 19 * 20 * @author <a href="mailto:andreas.schaefer@madplanet.com">Andreas Schaefer</a> 21 * @version $Revision: 1.7 $ 22 */ 23 public class FileUtilsTest extends TestCase { 24 25 /*** Default TestCase Constructor */ 26 public FileUtilsTest(String s) { 27 super(s); 28 } 29 30 /*** 31 * Checks if {@link FileUtils#isJar FIleUtils.isJar()} can indentify 32 * a Jar file 33 */ 34 public void testIsJar() 35 throws Exception { 36 File rootDirectory = FileUtils.getRoot(getClass()).getParentFile().getParentFile(); 37 File jarFile = new File( 38 rootDirectory, 39 "src/testdata/util/EmptyJarFile.jar" 40 ); 41 assertTrue("The given JAR file is not recognized as such", FileUtils.isJar(jarFile)); 42 File lTextFile = new File( 43 rootDirectory, 44 "src/testdata/util/EmptyFile.txt" 45 ); 46 assertFalse("The given Text file is wrongly accepted as JAR archive", FileUtils.isJar(lTextFile)); 47 } 48 49 /*** 50 * Checks if {@link FileUtils#getResourceAsStream FileUtils.getResourceAsStream()} 51 * can return an Input Stream for a resource in a directory or Jar archive 52 */ 53 public void testGetResourceAsStream() 54 throws Exception { 55 File rootDirectory = FileUtils.getRoot(getClass()).getParentFile().getParentFile(); 56 // Now check a Resource from a JAR archive 57 File jarFile = new File( 58 rootDirectory, 59 "src/testdata/util/EmptyJarFile.jar" 60 ); 61 InputStream inputStream = FileUtils.getResourceAsStream(jarFile, "EmptyFile.txt"); 62 // Check that the Stream returns 'Empty' which is the content of the file 63 BufferedReader reader = new BufferedReader( 64 new InputStreamReader(inputStream) 65 ); 66 String line = reader.readLine(); 67 assertTrue("The JAR archive resource did not contain 'Empty' but: " + line, "Empty".equals(line)); 68 // Now check a Resource from a Directory 69 File directory = new File( 70 rootDirectory, 71 "src/testdata/util" 72 ); 73 inputStream = FileUtils.getResourceAsStream(directory, "EmptyFile.txt"); 74 // Check that the Stream returns 'Empty' which is the content of the file 75 reader = new BufferedReader( 76 new InputStreamReader(inputStream) 77 ); 78 line = reader.readLine(); 79 assertTrue("The directory resource did not contain 'Empty' but: " + line, "Empty".equals(line)); 80 } 81 82 /*** 83 * Checks if {@link FileUtils#getRoot FileUtils.getRootDir()} 84 * can find the root directory of a class in a directory hierarchy 85 */ 86 public void testGetRootFromJar() 87 throws Exception { 88 // Now check only if the method works 89 Class fileClass = Thread.currentThread().getContextClassLoader().loadClass("java.io.File"); 90 File rootDirectory = FileUtils.getRoot(fileClass); 91 92 String runtimeJarPath = null; 93 String os = System.getProperty( "os.name" ).toLowerCase( Locale.US ); 94 if( os.indexOf("mac os x") != -1 ) { 95 runtimeJarPath = "../Classes/classes.jar"; 96 } else { 97 // Assume that all other OSes' JDK have the runtime here. If that ain't right, 98 // just add a new test as the mac one above. 99 runtimeJarPath = "lib/rt.jar"; 100 } 101 File javaHome = new File( System.getProperty("java.home"), runtimeJarPath ); 102 try { 103 assertEquals(javaHome.getCanonicalFile(), rootDirectory.getCanonicalFile()); 104 } catch( AssertionFailedError e ) { 105 System.err.println("You should update this test's source for your OS/JDK. Your os: " + os); 106 throw e; 107 } 108 } 109 }

This page was automatically generated by Maven