KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 // Creates structre:
32
// src0/
33
// com/
34
// example/
35
// Sample1.java
36
// Sample2.java
37
// src1/
38
// com/
39
// example/
40
// Sample3.java
41
// Sample4.java
42
// src2/
43
// com/
44
// example/
45
// Sample5.java
46
// Sample6.java
47
// src3/
48
// com/
49
// example/
50
// Sample7.java
51
// Sample8.java
52
public class FileFixture {
53     public static final String JavaDoc[] SOURCE_DIRECTORY_IDENTIFIER = { "src0", "src1", "src2", "src3" };
54     public HashMap JavaDoc sourceDirectories;
55
56     public void setUp() throws IOException JavaDoc {
57         File JavaDoc tempFile = File.createTempFile("temp", ".tmp");
58         tempFile.deleteOnExit();
59         sourceDirectories = new HashMap JavaDoc();
60
61         for( int i=0; i<SOURCE_DIRECTORY_IDENTIFIER.length; i++) {
62             File JavaDoc directory = new File JavaDoc(tempFile.getParent(), SOURCE_DIRECTORY_IDENTIFIER[i]);
63             directory.mkdir();
64             filesUnderSourceDir(directory, 1+i*2);
65             sourceDirectories.put(SOURCE_DIRECTORY_IDENTIFIER[i], directory);
66         }
67     }
68
69     private void filesUnderSourceDir(File JavaDoc srcDirectory, int number) throws IOException JavaDoc, FileNotFoundException JavaDoc {
70         File JavaDoc sub = new File JavaDoc(srcDirectory, "com");
71         sub.mkdir();
72         File JavaDoc temp = new File JavaDoc(sub, "example");
73         temp.mkdir();
74         File JavaDoc sample1 = new File JavaDoc(temp, "Sample" + number + ".java");
75         sample1.createNewFile();
76         FileWriter JavaDoc writer1 = new FileWriter JavaDoc(sample1);
77         writer1.write( "package com.example;\n");
78         writer1.write( "public class Sample" + number + " {\n");
79         writer1.write( " public void someMethod(int v) {\n");
80         writer1.write( " if(v<0) System.out.println();\n");
81         writer1.write( " else System.out.println('x');\n");
82         writer1.write( " }\n");
83         writer1.write( "}\n");
84         writer1.close();
85         
86         File JavaDoc sample2 = new File JavaDoc(temp, "Sample" + (number + 1) + ".java");
87         sample2.createNewFile();
88         FileWriter JavaDoc writer2 = new FileWriter JavaDoc(sample2);
89         writer2.write( "package com.example;\n");
90         writer2.write( "class Sample" + (number + 1) + " {\n");
91         writer2.write( " private String otherMethod() {\n");
92         writer2.write( " return \"OtherValue\";\n");
93         writer2.write( " }\n");
94         writer2.write( "}\n");
95         writer2.close();
96     }
97
98     protected void deleteTree(File JavaDoc fileRoot) {
99         if (fileRoot.isFile()) {
100             fileRoot.delete();
101             return;
102         }
103
104         File JavaDoc[] files = fileRoot.listFiles();
105         for (int i = 0; i < files.length; i++) {
106             deleteTree(files[i]);
107         }
108         fileRoot.delete();
109     }
110
111     public void tearDown() {
112         for( int i=0; i<SOURCE_DIRECTORY_IDENTIFIER.length; i++) {
113             deleteTree(sourceDirectory(SOURCE_DIRECTORY_IDENTIFIER[i]));
114         }
115     }
116
117     public File JavaDoc sourceDirectory(String JavaDoc directoryIdentifier) {
118         return (File JavaDoc) sourceDirectories.get(directoryIdentifier);
119     }
120 }
121
Popular Tags