1 20 package org.enhydra.barracuda.taskdefs; 21 22 import junit.framework.TestCase; 24 25 import org.apache.log4j.Logger; 27 28 import java.io.File ; 30 import java.io.IOException ; 31 32 import org.enhydra.xml.xmlc.taskdef.*; 33 34 import org.enhydra.barracuda.testbed.*; 35 36 40 public class TestOptionsFile extends DefaultTestCase { 41 42 46 51 public TestOptionsFile( String theName) { 52 super( theName ); 53 } 54 55 59 protected void setUp() throws Exception { 60 super.setUp(); 61 if ( !this.myBaseTestDir.exists() ) { 63 assertTrue("Cannot create base test directory:" + this.myBaseTestDir.getAbsolutePath(), 64 this.myBaseTestDir.mkdir() ); 65 } 66 } 67 68 72 protected void tearDown() throws Exception { 73 super.tearDown(); 74 File [] files = this.myBaseTestDir.getAbsoluteFile().getParentFile().listFiles(); 76 for (int i = 0; i < files.length; i++) { 77 File currFile = files[i]; 78 if ( currFile.getName().endsWith(XmlcUtils.DEFAULT_OPTIONS_FILETYPE) ) { 79 assertTrue("Cannot delete: " + currFile.getAbsolutePath(), currFile.delete() ); 80 } 81 } 82 this.recursiveDelete( this.myBaseTestDir ); 83 } 84 85 public void testDefaultNoOptionsFile() { 89 XmlcUtils xmlcUtils = XmlcUtils.create(); 90 91 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir ); 92 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 93 } 94 95 public void testOptionsFileNulls() { 96 XmlcUtils xmlcUtils = XmlcUtils.create(); 97 98 String [] foundOptionFiles = xmlcUtils.getOptionFiles( null ); 99 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 100 101 foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, (String )null ); 102 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 103 104 foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, (String [])null ); 105 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 106 107 String [] files = { "test1", null, "test2" }; 108 foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, files ); 109 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 110 111 File [] dirs = { null, new File ("test1"), null, new File ("test2") }; 112 foundOptionFiles = xmlcUtils.getOptionFiles( dirs, (String )null ); 113 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 114 115 foundOptionFiles = xmlcUtils.getOptionFiles( dirs, (String [])null ); 116 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 117 118 foundOptionFiles = xmlcUtils.getOptionFiles( dirs, files ); 119 assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length ); 120 } 121 122 public void testDefaultOneOptionsFile() throws IOException { 123 XmlcUtils xmlcUtils = XmlcUtils.create(); 124 125 File expectedOptionFile = new File ( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 126 assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(), 127 expectedOptionFile.createNewFile() ); 128 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir ); 129 assertEquals("Incorrect no. of option files", 1, foundOptionFiles.length ); 130 } 131 132 public void testDefaultOptionsFileIgnoresDirectories() throws IOException { 133 XmlcUtils xmlcUtils = XmlcUtils.create(); 134 135 File expectedOptionFile = new File ( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 136 assertTrue("Cannot create option directory:" + expectedOptionFile.getAbsolutePath(), 137 expectedOptionFile.mkdir() ); 138 139 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir ); 140 assertEquals("Incorrect no. of option files", 0, foundOptionFiles.length ); 141 } 142 143 public void testDefaultOptionsFileUserDir() throws IOException { 144 XmlcUtils xmlcUtils = XmlcUtils.create(); 145 146 File expectedOptionFile = new File ( XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 147 assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(), 148 expectedOptionFile.createNewFile() ); 149 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir ); 150 assertEquals("Incorrect no. of option files", 1, foundOptionFiles.length ); 151 } 152 153 public void testDefaultOptionsFileUserAndSpecified() throws IOException { 154 XmlcUtils xmlcUtils = XmlcUtils.create(); 155 156 File userDirOptionFile = new File ( XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 157 assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(), 158 userDirOptionFile.createNewFile() ); 159 160 File expectedOptionFile = new File ( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 161 assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(), 162 expectedOptionFile.createNewFile() ); 163 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir ); 164 165 assertEquals("Incorrect no. of option files", 2, foundOptionFiles.length ); 166 assertEquals( "Incorrect expected filename: ", 167 expectedOptionFile.getAbsolutePath(), foundOptionFiles[0] ); 168 assertEquals( "Incorrect user dir filename: ", 169 userDirOptionFile.getAbsolutePath(), foundOptionFiles[1] ); 170 } 171 172 public void testOverrideOptionsFileUserAndSpecified() throws IOException { 173 String overrideOptionsFilename = "override.xmlc"; 174 XmlcUtils xmlcUtils = XmlcUtils.create(); 175 176 File userDirOptionFile = new File ( overrideOptionsFilename ); 177 assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(), 178 userDirOptionFile.createNewFile() ); 179 180 File expectedOptionFile = new File ( this.myBaseTestDir, overrideOptionsFilename ); 181 assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(), 182 expectedOptionFile.createNewFile() ); 183 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, overrideOptionsFilename ); 184 185 assertEquals("Incorrect no. of option files", 2, foundOptionFiles.length ); 186 assertEquals( "Incorrect expected filename: ", 187 expectedOptionFile.getAbsolutePath(), foundOptionFiles[0] ); 188 assertEquals( "Incorrect user dir filename: ", 189 userDirOptionFile.getAbsolutePath(), foundOptionFiles[1] ); 190 } 191 192 public void testOverrideOptionsFileUserAndSpecifiedUsingDefaultName() throws IOException { 193 String overrideOptionsFilename = XmlcUtils.DEFAULT_OPTIONS_FILENAME; 194 XmlcUtils xmlcUtils = XmlcUtils.create(); 195 196 File userDirOptionFile = new File ( overrideOptionsFilename ); 197 assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(), 198 userDirOptionFile.createNewFile() ); 199 200 File expectedOptionFile = new File ( this.myBaseTestDir, overrideOptionsFilename ); 201 assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(), 202 expectedOptionFile.createNewFile() ); 203 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, overrideOptionsFilename ); 204 205 assertEquals("Incorrect no. of option files", 2, foundOptionFiles.length ); 206 assertEquals( "Incorrect expected filename: ", 207 expectedOptionFile.getAbsolutePath(), foundOptionFiles[0] ); 208 assertEquals( "Incorrect user dir filename: ", 209 userDirOptionFile.getAbsolutePath(), foundOptionFiles[1] ); 210 } 211 212 public void testOptionsFilename() throws IOException { 213 String currFile = "test"; 214 XmlcUtils xmlcUtils = XmlcUtils.create(); 215 File expectedOptionFile = new File ( this.myBaseTestDir, currFile + xmlcUtils.DEFAULT_OPTIONS_FILETYPE ); 216 assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(), 217 expectedOptionFile.createNewFile() ); 218 219 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, currFile ); 220 assertEquals("Incorrect no. of option files for " + currFile, 1, foundOptionFiles.length ); 221 assertEquals("Found option filename should match expected for " + currFile, 222 expectedOptionFile.getAbsolutePath(), 223 foundOptionFiles[0] ); 224 225 foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, expectedOptionFile.getAbsolutePath() ); 226 assertEquals("Incorrect no. of option files for " + currFile, 1, foundOptionFiles.length ); 227 assertEquals("Found option filename should match expected for " + currFile, 228 expectedOptionFile.getAbsolutePath(), 229 foundOptionFiles[0] ); 230 } 231 232 public void testOverrideAndDefaultOptionsFiles() throws IOException { 233 XmlcUtils xmlcUtils = XmlcUtils.create(); 234 235 File defaultUserDirOptionFile = new File ( XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 236 assertTrue("Cannot create default user dir option file:" + defaultUserDirOptionFile.getAbsolutePath(), 237 defaultUserDirOptionFile.createNewFile() ); 238 239 File defaultExpectedOptionFile = new File ( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME ); 240 assertTrue("Cannot create default base option file:" + defaultExpectedOptionFile.getAbsolutePath(), 241 defaultExpectedOptionFile.createNewFile() ); 242 243 String overrideOptionsFilename = "override.xmlc"; 244 File userDirOptionFile = new File ( overrideOptionsFilename ); 245 assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(), 246 userDirOptionFile.createNewFile() ); 247 248 File expectedOptionFile = new File ( this.myBaseTestDir, overrideOptionsFilename ); 249 assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(), 250 expectedOptionFile.createNewFile() ); 251 252 String [] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, overrideOptionsFilename ); 253 254 assertEquals("Incorrect no. of option files", 4, foundOptionFiles.length ); 255 assertEquals( "Incorrect expected filename: ", 256 expectedOptionFile.getAbsolutePath(), foundOptionFiles[0] ); 257 assertEquals( "Incorrect default expected filename: ", 258 defaultExpectedOptionFile.getAbsolutePath(), foundOptionFiles[1] ); 259 assertEquals( "Incorrect user dir filename: ", 260 userDirOptionFile.getAbsolutePath(), foundOptionFiles[2] ); 261 assertEquals( "Incorrect defaultuser dir filename: ", 262 defaultUserDirOptionFile.getAbsolutePath(), foundOptionFiles[3] ); 263 } 264 265 public void testMultipleOptionsFiles() throws IOException { 266 File expectedOptionFile1 = new File ( this.myBaseTestDir, "test1.xmlc" ); 267 assertTrue("Cannot create option file:" + expectedOptionFile1.getAbsolutePath(), 268 expectedOptionFile1.createNewFile() ); 269 270 File expectedOptionFile2 = new File ( this.myBaseTestDir, "test2.xmlc" ); 271 assertTrue("Cannot create option file:" + expectedOptionFile2.getAbsolutePath(), 272 expectedOptionFile2.createNewFile() ); 273 274 File expectedOptionFile3 = new File ( this.myBaseTestDir, "test3.xmlc" ); 275 assertTrue("Cannot create option file:" + expectedOptionFile3.getAbsolutePath(), 276 expectedOptionFile3.createNewFile() ); 277 278 String [] expectedOptionFiles = new String [3]; 279 expectedOptionFiles[0] = expectedOptionFile3.getName(); 280 expectedOptionFiles[1] = expectedOptionFile1.getName(); 281 expectedOptionFiles[2] = expectedOptionFile2.getName(); 282 283 XmlcUtils xmlcUtils = XmlcUtils.create(); 284 String [] foundOptionFiles = 285 xmlcUtils.getOptionFiles( this.myBaseTestDir, expectedOptionFiles ); 286 assertEquals("Incorrect no. of option files", 287 expectedOptionFiles.length, foundOptionFiles.length ); 288 assertEquals( "Incorrect expected filename: ", 289 expectedOptionFile3.getAbsolutePath(), foundOptionFiles[0] ); 290 assertEquals( "Incorrect expected filename: ", 291 expectedOptionFile1.getAbsolutePath(), foundOptionFiles[1] ); 292 assertEquals( "Incorrect expected filename: ", 293 expectedOptionFile2.getAbsolutePath(), foundOptionFiles[2] ); 294 } 295 296 public void testMultipleDirOptionsFiles() throws IOException { 297 File dir1 = new File ( this.myBaseTestDir, "Dir1" ); 298 assertTrue("Cannot create test directory:" + dir1.getAbsolutePath(), dir1.mkdir() ); 299 300 File dir2 = new File ( this.myBaseTestDir, "Dir2" ); 301 assertTrue("Cannot create test directory:" + dir2.getAbsolutePath(), dir2.mkdir() ); 302 303 File dir3 = new File ( this.myBaseTestDir, "Dir1/Dir3" ); 304 assertTrue("Cannot create test directory:" + dir3.getAbsolutePath(), dir3.mkdir() ); 305 306 String optionsFilename = "test.xmlc"; 307 File expectedOptionFileDir1 = new File ( dir1, optionsFilename ); 308 assertTrue("Cannot create option file:" + expectedOptionFileDir1.getAbsolutePath(), 309 expectedOptionFileDir1.createNewFile() ); 310 311 File expectedOptionFileDir2 = new File ( dir2, optionsFilename ); 312 assertTrue("Cannot create option file:" + expectedOptionFileDir2.getAbsolutePath(), 313 expectedOptionFileDir2.createNewFile() ); 314 315 File expectedOptionFileDir3 = new File ( dir3, optionsFilename ); 316 assertTrue("Cannot create option file:" + expectedOptionFileDir3.getAbsolutePath(), 317 expectedOptionFileDir3.createNewFile() ); 318 319 File [] dirs = new File [3]; 320 dirs[0] = dir1; 321 dirs[1] = dir2; 322 dirs[2] = dir3; 323 324 XmlcUtils xmlcUtils = XmlcUtils.create(); 325 String [] foundOptionFiles = xmlcUtils.getOptionFiles( dirs, optionsFilename ); 326 assertEquals("Incorrect no. of option files", dirs.length, foundOptionFiles.length ); 327 assertEquals( "Incorrect expected filename: ", 328 expectedOptionFileDir1.getAbsolutePath(), foundOptionFiles[0] ); 329 assertEquals( "Incorrect expected filename: ", 330 expectedOptionFileDir2.getAbsolutePath(), foundOptionFiles[1] ); 331 assertEquals( "Incorrect expected filename: ", 332 expectedOptionFileDir3.getAbsolutePath(), foundOptionFiles[2] ); 333 334 dirs = new File [6]; 336 dirs[0] = dir3; 337 dirs[1] = dir2; 338 dirs[2] = dir2; 339 dirs[3] = dir1; 340 dirs[4] = dir3; 341 dirs[5] = dir1; 342 343 foundOptionFiles = xmlcUtils.getOptionFiles( dirs, optionsFilename ); 344 assertEquals("Incorrect no. of option files", 3, foundOptionFiles.length ); 345 assertEquals( "Incorrect expected filename: ", 346 expectedOptionFileDir3.getAbsolutePath(), foundOptionFiles[0] ); 347 assertEquals( "Incorrect expected filename: ", 348 expectedOptionFileDir2.getAbsolutePath(), foundOptionFiles[1] ); 349 assertEquals( "Incorrect expected filename: ", 350 expectedOptionFileDir1.getAbsolutePath(), foundOptionFiles[2] ); 351 } 352 353 357 private void recursiveDelete( File theFile ) { 358 if ( theFile.isDirectory() ) { 359 File [] files = theFile.listFiles(); 360 for (int i = 0; i < files.length; i++) { 361 this.recursiveDelete( files[i] ); 362 } 363 } 364 assertTrue("Cannot delete: " + theFile.getAbsolutePath(), 365 theFile.delete() ); 366 } 367 368 private File myBaseTestDir = new File ( TEST_DIR ); 372 373 private static final String TEST_DIR="_TestOptionsFile"; 377 378 private static final Logger theirLogger = Logger.getLogger( TestOptionsFile.class.getName() ); 380 } 381 | Popular Tags |