1 16 package org.apache.commons; 17 18 import junit.framework.TestCase; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.util.Messages; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 26 32 public abstract class AbstractVfsTestCase 33 extends TestCase 34 { 35 private static File baseDir; 36 37 43 public static String getPackageName(final Class clazz) 44 { 45 final Package pkg = clazz.getPackage(); 46 if (null != pkg) 47 { 48 return pkg.getName(); 49 } 50 51 final String name = clazz.getName(); 52 if (-1 == name.lastIndexOf(".")) 53 { 54 return ""; 55 } 56 else 57 { 58 return name.substring(0, name.lastIndexOf(".")); 59 } 60 } 61 62 67 public static File getTestResource(final String name) 68 { 69 return getTestResource(name, true); 70 } 71 72 77 public static File getTestResource(final String name, final boolean mustExist) 78 { 79 File file = new File (getTestDirectoryFile(), name); 80 file = getCanonicalFile(file); 81 if (mustExist) 82 { 83 assertTrue("Test file \"" + file + "\" does not exist.", file.exists()); 84 } 85 else 86 { 87 assertTrue("Test file \"" + file + "\" should not exist.", !file.exists()); 88 } 89 90 return file; 91 } 92 93 96 public static File getTestDirectoryFile() 97 { 98 if (baseDir == null) 99 { 100 final String baseDirProp = getTestDirectory(); 102 baseDir = getCanonicalFile(new File (baseDirProp)); 103 } 104 return baseDir; 105 } 106 107 public static String getTestDirectory() 108 { 109 return System.getProperty("test.basedir"); 110 118 } 119 120 125 public static File getTestDirectory(final String name) 126 { 127 File file = new File (getTestDirectoryFile(), name); 128 file = getCanonicalFile(file); 129 assertTrue("Test directory \"" + file + "\" does not exist or is not a directory.", 130 file.isDirectory() || file.mkdirs()); 131 return file; 132 } 133 134 137 public static File getCanonicalFile(final File file) 138 { 139 try 140 { 141 return file.getCanonicalFile(); 142 } 143 catch (IOException e) 144 { 145 return file.getAbsoluteFile(); 146 } 147 } 148 149 155 public static void assertSameMessage(final String [] messages, final Throwable throwable) 156 { 157 Throwable current = throwable; 158 for (int i = 0; i < messages.length; i++) 159 { 160 String message = messages[i]; 161 assertNotNull(current); 162 if (message != null) 163 { 164 assertEquals(message, current.getMessage()); 165 } 166 167 current = getCause(current); 169 } 170 } 171 172 175 public static Throwable getCause(Throwable throwable) 176 { 177 try 178 { 179 Method method = throwable.getClass().getMethod("getCause", null); 180 return (Throwable ) method.invoke(throwable, null); 181 } 182 catch (Exception e) 183 { 184 return null; 185 } 186 } 187 188 191 public static void assertSameMessage(final String code, 192 final Throwable throwable) 193 { 194 assertSameMessage(code, new Object [0], throwable); 195 } 196 197 200 public static void assertSameMessage(final String code, 201 final Object [] params, 202 final Throwable throwable) 203 { 204 if (throwable instanceof FileSystemException) 205 { 206 final FileSystemException fse = (FileSystemException) throwable; 207 208 assertEquals(code, fse.getCode()); 210 assertEquals(params.length, fse.getInfo().length); 211 for (int i = 0; i < params.length; i++) 212 { 213 final Object param = params[i]; 214 assertEquals(String.valueOf(param), fse.getInfo()[i]); 215 } 216 } 217 218 final String message = Messages.getString(code, params); 220 assertEquals(message, throwable.getMessage()); 221 } 222 223 226 public static void assertSameMessage(final String code, 227 final Object param, 228 final Throwable throwable) 229 { 230 assertSameMessage(code, new Object []{param}, throwable); 231 } 232 233 237 public static boolean equals(final Object o1, final Object o2) 238 { 239 if (o1 == null && o2 == null) 240 { 241 return true; 242 } 243 if (o1 == null || o2 == null) 244 { 245 return false; 246 } 247 return o1.equals(o2); 248 } 249 } 250 | Popular Tags |