1 22 23 package net.sourceforge.cobertura.util; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 28 import junit.framework.*; 29 30 public class FileFinderTest extends TestCase { 31 private FileFinder fileFinder; 32 private FileFixture fileFixture; 33 34 public void testGetSourceDirectoryList() { 35 assertEquals(4, fileFinder.getSourceDirectoryList().size()); 36 assertTrue(new FileFinder().getSourceDirectoryList().isEmpty()); 37 38 FileFinder ff = new FileFinder(); 39 ff.addSourceDirectory( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0]); 40 assertEquals( 1, ff.getSourceDirectoryList().size()); 41 ff.addSourceDirectory( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]); 42 assertEquals( 2, ff.getSourceDirectoryList().size()); 43 44 ff = new FileFinder(); 45 ff.addSourceFile( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0], "com/example/Sample1.java"); 46 ff.addSourceFile( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0], "com/example/Sample2.java"); 47 assertEquals( 1, ff.getSourceDirectoryList().size()); 48 ff.addSourceFile( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[1], "com/example/Sample3.java"); 49 assertEquals( 2, ff.getSourceDirectoryList().size()); 50 ff.addSourceDirectory( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]); 51 assertEquals( 3, ff.getSourceDirectoryList().size()); 52 } 53 54 private void checkFile( String fileName, String baseName, int sourceNum) throws IOException { 55 File file = fileFinder.getFileForSource( fileName); 56 assertTrue( file.getAbsolutePath(), file.getAbsolutePath().indexOf(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[sourceNum])!=-1); 57 assertTrue( baseName.equals( file.getName())); 58 assertTrue( file.exists()); 59 assertTrue( file.isFile()); 60 } 61 62 public void testFindFile() throws IOException { 63 checkFile("com/example/Sample1.java", "Sample1.java", 0); 64 checkFile("com\\example\\Sample2.java", "Sample2.java", 0); 65 checkFile("com/example\\Sample3.java", "Sample3.java", 1); 66 checkFile("com/example/Sample4.java", "Sample4.java", 1); 67 checkFile("com/example/Sample5.java", "Sample5.java", 2); 68 checkFile("com/example/Sample6.java", "Sample6.java", 2); 69 checkFile("com\\example/Sample7.java", "Sample7.java", 3); 70 } 71 72 public void testFindFile_NotFound() { 73 try { 74 fileFinder.getFileForSource("com/example/Sample19.java"); 75 fail( "IOException expected"); 76 } catch( IOException ex) {} 77 try { 78 fileFinder.getFileForSource("com/example/Sample1.jav"); 79 fail( "IOException expected"); 80 } catch( IOException ex) {} 81 try { 82 fileFinder.getFileForSource("com/example/Sample7.java2"); 83 fail( "IOException expected"); 84 } catch( IOException ex) {} 85 try { 86 fileFinder.getFileForSource("Sample3.java"); 87 fail( "IOException expected"); 88 } catch( IOException ex) {} 89 try { 90 fileFinder.getFileForSource("com/example/Sample8.java"); 92 fail( "IOException expected"); 93 } catch( IOException ex) {} 94 } 95 96 public void testFindFile_null() throws IOException { 97 try { 98 fileFinder.getFileForSource(null); 99 fail( "NullPointerException expected"); 100 } catch( NullPointerException ex) {} 101 } 102 103 public void testAddSourceDirectory_null() { 104 try { 105 fileFinder.addSourceDirectory(null); 106 fail( "NullPointerException expected"); 107 } catch( NullPointerException ex) {} 108 } 109 110 public void testAddSourceFile_null() { 111 try { 112 fileFinder.addSourceFile(null,"com/example/Sample1.java"); 113 fail( "NullPointerException expected"); 114 } catch( NullPointerException ex) {} 115 try { 116 fileFinder.addSourceFile( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0], null); 117 fail( "NullPointerException expected"); 118 } catch( NullPointerException ex) {} 119 } 120 121 protected void setUp() throws Exception { 122 super.setUp(); 123 fileFixture = new FileFixture(); 124 fileFixture.setUp(); 125 126 fileFinder = new FileFinder(); 127 fileFinder.addSourceDirectory(fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0]).toString()); 128 fileFinder.addSourceDirectory(fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[1]).toString()); 129 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(), "com/example\\Sample5.java"); 130 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[2]).toString(), "com/example/Sample6.java"); 131 fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(), "com/example/Sample7.java"); 132 133 } 136 137 protected void tearDown() throws Exception { 138 super.tearDown(); 139 fileFixture.tearDown(); 140 } 141 142 } 143 | Popular Tags |