1 22 23 package net.sourceforge.cobertura.util; 24 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 31 public class FileFixture { 53 public static final String [] SOURCE_DIRECTORY_IDENTIFIER = { "src0", "src1", "src2", "src3" }; 54 public HashMap sourceDirectories; 55 56 public void setUp() throws IOException { 57 File tempFile = File.createTempFile("temp", ".tmp"); 58 tempFile.deleteOnExit(); 59 sourceDirectories = new HashMap (); 60 61 for( int i=0; i<SOURCE_DIRECTORY_IDENTIFIER.length; i++) { 62 File directory = new File (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 srcDirectory, int number) throws IOException , FileNotFoundException { 70 File sub = new File (srcDirectory, "com"); 71 sub.mkdir(); 72 File temp = new File (sub, "example"); 73 temp.mkdir(); 74 File sample1 = new File (temp, "Sample" + number + ".java"); 75 sample1.createNewFile(); 76 FileWriter writer1 = new FileWriter (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 sample2 = new File (temp, "Sample" + (number + 1) + ".java"); 87 sample2.createNewFile(); 88 FileWriter writer2 = new FileWriter (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 fileRoot) { 99 if (fileRoot.isFile()) { 100 fileRoot.delete(); 101 return; 102 } 103 104 File [] 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 sourceDirectory(String directoryIdentifier) { 118 return (File ) sourceDirectories.get(directoryIdentifier); 119 } 120 } 121 | Popular Tags |