001 /*--------------------------------------------------------------------------+ 002 $Id: CCSMTestCaseBase.java 28629 2010-06-23 08:57:09Z deissenb $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.test; 019 020 import java.io.File; 021 import java.io.IOException; 022 import java.io.PrintWriter; 023 024 import junit.framework.TestCase; 025 import edu.tum.cs.commons.filesystem.CanonicalFile; 026 import edu.tum.cs.commons.filesystem.FileSystemUtils; 027 028 /** 029 * Base class for test cases that access test data files. This class provides a 030 * simple mechanism for accessing test data files in a specified directory and 031 * provides statistics on test file usage and non-usage. 032 * <p> 033 * The test files a test case accesses must reside in the following location: 034 * 035 * <pre> 036 * test-data/<Name of the package the test case resides in> 037 * </pre> 038 * 039 * For example if a test case is defined in package <code>demo.test</code> the 040 * test files it accesses must be located in directory 041 * <code>test-data/demo.test</code>. 042 * 043 * @author Florian Deissenboeck 044 * @author $Author: deissenb $ 045 * @version $Rev: 28629 $ 046 * @levd.rating GREEN Hash: AEAC92C20B9EDCFB4A8E5E0A2C9E844A 047 */ 048 public abstract class CCSMTestCaseBase extends TestCase { 049 050 /** Test data directory. */ 051 private final static File TEST_DATA_ROOT_DIRECTORY = new File("test-data"); 052 053 /** Tmp directory. */ 054 private final static File TEST_TMP_ROOT_DIRECTORY = new File("test-tmp"); 055 056 /** Test data manager for this test case. */ 057 private final TestDataManager testDataManager = TestDataManager 058 .getInstance(new File(TEST_DATA_ROOT_DIRECTORY, getClass() 059 .getPackage().getName())); 060 061 /** Tmp directory. */ 062 private final File tmpDirectory = new File(TEST_TMP_ROOT_DIRECTORY, 063 getClass().getPackage().getName()); 064 065 /** Default constructor */ 066 public CCSMTestCaseBase() { 067 super(); 068 } 069 070 /** 071 * Constructs a test case with the given name. 072 * 073 * @param name 074 * Name of the test method that gets called 075 */ 076 public CCSMTestCaseBase(String name) { 077 super(name); 078 } 079 080 /** 081 * Use test file. This method does not actually access the file, so no IO 082 * exception can be raised. This method uses a {@link TestDataManager} to 083 * log access to test data files. 084 * 085 * @param filename 086 * Name of the file 087 * @return the file. 088 */ 089 protected File useTestFile(String filename) { 090 return testDataManager.getTestFile(filename, this); 091 } 092 093 /** 094 * Create a temporary file in a subdirectory of the test temp directory. 095 * Directories are created as needed. 096 * 097 * @param filename 098 * name of the file 099 * @param content 100 * content 101 * @return the file 102 * @throws IOException 103 * if an IO exception occurrs 104 */ 105 protected File createTmpFile(String filename, String content) 106 throws IOException { 107 File file = new File(tmpDirectory, filename); 108 FileSystemUtils.writeFile(file, content); 109 return file; 110 } 111 112 /** Get temporary directory. */ 113 protected File getTmpDirectory() { 114 return tmpDirectory; 115 } 116 117 /** Delete temporary directory. */ 118 protected void deleteTmpDirectory() { 119 if (tmpDirectory.isDirectory()) { 120 FileSystemUtils.deleteRecursively(tmpDirectory); 121 } 122 } 123 124 /** 125 * Print report about used test files. 126 */ 127 protected void printUsedFiles() { 128 testDataManager.printUsedFiles(new PrintWriter(System.out)); 129 } 130 131 /** 132 * Print report about unused test files. 133 */ 134 protected void printUnusedFiles() { 135 testDataManager.printUnusedFiles(new PrintWriter(System.out)); 136 } 137 138 /** 139 * Print report about used and unused test files. 140 */ 141 protected void printStatistics() { 142 printUsedFiles(); 143 printUnusedFiles(); 144 } 145 146 /** 147 * Same as {@link #useTestFile(String)} but returns a {@link CanonicalFile}. 148 * If canonization fails, this makes the current test fail. 149 */ 150 protected CanonicalFile useCanonicalTestFile(String filename) { 151 try { 152 return new CanonicalFile(useTestFile(filename)); 153 } catch (IOException e) { 154 fail("Problem canonizing file: " + filename + ": " + e.getMessage()); 155 return null; 156 } 157 } 158 159 /** 160 * Same as {@link #createTmpFile(String, String)} but returns a 161 * {@link CanonicalFile}. If canonization fails, this makes the current test 162 * fail. 163 */ 164 protected CanonicalFile createCanonicalTmpFile(String filename, 165 String content) throws IOException { 166 return canonize(createTmpFile(filename, content)); 167 } 168 169 /** 170 * Canonize file. If canonization fails, this makes the current test fail. 171 */ 172 protected CanonicalFile canonize(File file) { 173 try { 174 return new CanonicalFile(file); 175 } catch (IOException e) { 176 fail("Problem canonizing file: " + file + ": " + e.getMessage()); 177 return null; 178 } 179 } 180 181 /** Checks if we run on a 64 bit VM */ 182 public static boolean is64BitVM() { 183 return System.getProperty("os.arch").contains("64"); 184 } 185 }