KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > FileFinderTest


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 James Seigel
5  * Copyright (C) 2005 Grzegorz Lukasik
6  *
7  * Cobertura is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License,
10  * or (at your option) any later version.
11  *
12  * Cobertura is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Cobertura; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22
23 package net.sourceforge.cobertura.util;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
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 JavaDoc fileName, String JavaDoc baseName, int sourceNum) throws IOException JavaDoc {
55         File JavaDoc 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 JavaDoc {
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 JavaDoc ex) {}
77         try {
78             fileFinder.getFileForSource("com/example/Sample1.jav");
79             fail( "IOException expected");
80         } catch( IOException JavaDoc ex) {}
81         try {
82             fileFinder.getFileForSource("com/example/Sample7.java2");
83             fail( "IOException expected");
84         } catch( IOException JavaDoc ex) {}
85         try {
86             fileFinder.getFileForSource("Sample3.java");
87             fail( "IOException expected");
88         } catch( IOException JavaDoc ex) {}
89         try {
90             // This file exist, but is not added to fileFinder
91
fileFinder.getFileForSource("com/example/Sample8.java");
92             fail( "IOException expected");
93         } catch( IOException JavaDoc ex) {}
94     }
95
96     public void testFindFile_null() throws IOException JavaDoc {
97         try {
98             fileFinder.getFileForSource(null);
99             fail( "NullPointerException expected");
100         } catch( NullPointerException JavaDoc ex) {}
101     }
102
103     public void testAddSourceDirectory_null() {
104         try {
105             fileFinder.addSourceDirectory(null);
106             fail( "NullPointerException expected");
107         } catch( NullPointerException JavaDoc 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 JavaDoc ex) {}
115         try {
116             fileFinder.addSourceFile( FileFixture.SOURCE_DIRECTORY_IDENTIFIER[0], null);
117             fail( "NullPointerException expected");
118         } catch( NullPointerException JavaDoc ex) {}
119     }
120
121     protected void setUp() throws Exception JavaDoc {
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         // Do not add com/example/Sample8.java
134
// fileFinder.addSourceFile( fileFixture.sourceDirectory(FileFixture.SOURCE_DIRECTORY_IDENTIFIER[3]).toString(), "com/example/Sample8.java");
135
}
136
137     protected void tearDown() throws Exception JavaDoc {
138         super.tearDown();
139         fileFixture.tearDown();
140     }
141
142 }
143
Popular Tags