KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > taskdefs > TestOptionsFile


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestOptionsFile.java,v 1.9 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.taskdefs;
21
22 // import junit specifics
23
import junit.framework.TestCase;
24
25 // import logging specifics
26
import org.apache.log4j.Logger;
27
28 // import java specifics
29
import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.enhydra.xml.xmlc.taskdef.*;
33
34 import org.enhydra.barracuda.testbed.*;
35
36 /**
37  * Unit tests for getOptionFiles() method of DefaultXmlcUtilsImpl
38  *
39  */

40 public class TestOptionsFile extends DefaultTestCase {
41
42     //===========================================================================
43
// CONSTRUCTORS
44
//===========================================================================
45

46     /**
47      * Default constructor
48      *
49      * @param theName The name of the test
50      */

51     public TestOptionsFile( String JavaDoc theName) {
52         super( theName );
53     }
54
55     /**
56      * Sets up the fixture, for example, open a network connection.
57      * This method is called before a test is executed.
58      */

59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         // create the test directory if required
62
if ( !this.myBaseTestDir.exists() ) {
63           assertTrue("Cannot create base test directory:" + this.myBaseTestDir.getAbsolutePath(),
64                  this.myBaseTestDir.mkdir() );
65         }
66     }
67
68     /**
69      * Tears down the fixture, for example, close a network connection.
70      * This method is called after a test is executed.
71      */

72     protected void tearDown() throws Exception JavaDoc {
73         super.tearDown();
74         // delete any xmlc in parent directory of base
75
File JavaDoc[] files = this.myBaseTestDir.getAbsoluteFile().getParentFile().listFiles();
76         for (int i = 0; i < files.length; i++) {
77           File JavaDoc 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     //===========================================================================
86
// TEST METHODS
87
//===========================================================================
88
public void testDefaultNoOptionsFile() {
89         XmlcUtils xmlcUtils = XmlcUtils.create();
90
91         String JavaDoc[] 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 JavaDoc[] 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 JavaDoc)null );
102         assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length );
103
104         foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir, (String JavaDoc[])null );
105         assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length );
106
107         String JavaDoc[] 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 JavaDoc[] dirs = { null, new File JavaDoc("test1"), null, new File JavaDoc("test2") };
112         foundOptionFiles = xmlcUtils.getOptionFiles( dirs, (String JavaDoc)null );
113         assertEquals("Incorrect no. of option files in empty directory", 0, foundOptionFiles.length );
114
115         foundOptionFiles = xmlcUtils.getOptionFiles( dirs, (String JavaDoc[])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 JavaDoc {
123         XmlcUtils xmlcUtils = XmlcUtils.create();
124
125         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME );
126         assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(),
127                expectedOptionFile.createNewFile() );
128         String JavaDoc[] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir );
129         assertEquals("Incorrect no. of option files", 1, foundOptionFiles.length );
130     }
131
132     public void testDefaultOptionsFileIgnoresDirectories() throws IOException JavaDoc {
133         XmlcUtils xmlcUtils = XmlcUtils.create();
134
135         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME );
136         assertTrue("Cannot create option directory:" + expectedOptionFile.getAbsolutePath(),
137                expectedOptionFile.mkdir() );
138
139         String JavaDoc[] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir );
140         assertEquals("Incorrect no. of option files", 0, foundOptionFiles.length );
141     }
142
143     public void testDefaultOptionsFileUserDir() throws IOException JavaDoc {
144         XmlcUtils xmlcUtils = XmlcUtils.create();
145
146         File JavaDoc expectedOptionFile = new File JavaDoc( XmlcUtils.DEFAULT_OPTIONS_FILENAME );
147         assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(),
148                expectedOptionFile.createNewFile() );
149         String JavaDoc[] foundOptionFiles = xmlcUtils.getOptionFiles( this.myBaseTestDir );
150         assertEquals("Incorrect no. of option files", 1, foundOptionFiles.length );
151     }
152
153     public void testDefaultOptionsFileUserAndSpecified() throws IOException JavaDoc {
154         XmlcUtils xmlcUtils = XmlcUtils.create();
155
156         File JavaDoc userDirOptionFile = new File JavaDoc( XmlcUtils.DEFAULT_OPTIONS_FILENAME );
157         assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(),
158                userDirOptionFile.createNewFile() );
159
160         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME );
161         assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(),
162                expectedOptionFile.createNewFile() );
163         String JavaDoc[] 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 JavaDoc {
173         String JavaDoc overrideOptionsFilename = "override.xmlc";
174         XmlcUtils xmlcUtils = XmlcUtils.create();
175
176         File JavaDoc userDirOptionFile = new File JavaDoc( overrideOptionsFilename );
177         assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(),
178                userDirOptionFile.createNewFile() );
179
180         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, overrideOptionsFilename );
181         assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(),
182                expectedOptionFile.createNewFile() );
183         String JavaDoc[] 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 JavaDoc {
193         String JavaDoc overrideOptionsFilename = XmlcUtils.DEFAULT_OPTIONS_FILENAME;
194         XmlcUtils xmlcUtils = XmlcUtils.create();
195
196         File JavaDoc userDirOptionFile = new File JavaDoc( overrideOptionsFilename );
197         assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(),
198                userDirOptionFile.createNewFile() );
199
200         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, overrideOptionsFilename );
201         assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(),
202                expectedOptionFile.createNewFile() );
203         String JavaDoc[] 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 JavaDoc {
213         String JavaDoc currFile = "test";
214         XmlcUtils xmlcUtils = XmlcUtils.create();
215         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, currFile + xmlcUtils.DEFAULT_OPTIONS_FILETYPE );
216         assertTrue("Cannot create option file:" + expectedOptionFile.getAbsolutePath(),
217                expectedOptionFile.createNewFile() );
218
219         String JavaDoc[] 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 JavaDoc {
233         XmlcUtils xmlcUtils = XmlcUtils.create();
234
235         File JavaDoc defaultUserDirOptionFile = new File JavaDoc( XmlcUtils.DEFAULT_OPTIONS_FILENAME );
236         assertTrue("Cannot create default user dir option file:" + defaultUserDirOptionFile.getAbsolutePath(),
237                defaultUserDirOptionFile.createNewFile() );
238
239         File JavaDoc defaultExpectedOptionFile = new File JavaDoc( this.myBaseTestDir, XmlcUtils.DEFAULT_OPTIONS_FILENAME );
240         assertTrue("Cannot create default base option file:" + defaultExpectedOptionFile.getAbsolutePath(),
241                defaultExpectedOptionFile.createNewFile() );
242
243         String JavaDoc overrideOptionsFilename = "override.xmlc";
244         File JavaDoc userDirOptionFile = new File JavaDoc( overrideOptionsFilename );
245         assertTrue("Cannot create user dir option file:" + userDirOptionFile.getAbsolutePath(),
246                userDirOptionFile.createNewFile() );
247
248         File JavaDoc expectedOptionFile = new File JavaDoc( this.myBaseTestDir, overrideOptionsFilename );
249         assertTrue("Cannot create base option file:" + expectedOptionFile.getAbsolutePath(),
250                expectedOptionFile.createNewFile() );
251
252         String JavaDoc[] 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 JavaDoc {
266         File JavaDoc expectedOptionFile1 = new File JavaDoc( this.myBaseTestDir, "test1.xmlc" );
267         assertTrue("Cannot create option file:" + expectedOptionFile1.getAbsolutePath(),
268                expectedOptionFile1.createNewFile() );
269
270         File JavaDoc expectedOptionFile2 = new File JavaDoc( this.myBaseTestDir, "test2.xmlc" );
271         assertTrue("Cannot create option file:" + expectedOptionFile2.getAbsolutePath(),
272                expectedOptionFile2.createNewFile() );
273
274         File JavaDoc expectedOptionFile3 = new File JavaDoc( this.myBaseTestDir, "test3.xmlc" );
275         assertTrue("Cannot create option file:" + expectedOptionFile3.getAbsolutePath(),
276                expectedOptionFile3.createNewFile() );
277
278         String JavaDoc[] expectedOptionFiles = new String JavaDoc[3];
279         expectedOptionFiles[0] = expectedOptionFile3.getName();
280         expectedOptionFiles[1] = expectedOptionFile1.getName();
281         expectedOptionFiles[2] = expectedOptionFile2.getName();
282
283         XmlcUtils xmlcUtils = XmlcUtils.create();
284         String JavaDoc[] 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 JavaDoc {
297         File JavaDoc dir1 = new File JavaDoc( this.myBaseTestDir, "Dir1" );
298         assertTrue("Cannot create test directory:" + dir1.getAbsolutePath(), dir1.mkdir() );
299
300         File JavaDoc dir2 = new File JavaDoc( this.myBaseTestDir, "Dir2" );
301         assertTrue("Cannot create test directory:" + dir2.getAbsolutePath(), dir2.mkdir() );
302
303         File JavaDoc dir3 = new File JavaDoc( this.myBaseTestDir, "Dir1/Dir3" );
304         assertTrue("Cannot create test directory:" + dir3.getAbsolutePath(), dir3.mkdir() );
305
306         String JavaDoc optionsFilename = "test.xmlc";
307         File JavaDoc expectedOptionFileDir1 = new File JavaDoc( dir1, optionsFilename );
308         assertTrue("Cannot create option file:" + expectedOptionFileDir1.getAbsolutePath(),
309                expectedOptionFileDir1.createNewFile() );
310
311         File JavaDoc expectedOptionFileDir2 = new File JavaDoc( dir2, optionsFilename );
312         assertTrue("Cannot create option file:" + expectedOptionFileDir2.getAbsolutePath(),
313                expectedOptionFileDir2.createNewFile() );
314
315         File JavaDoc expectedOptionFileDir3 = new File JavaDoc( dir3, optionsFilename );
316         assertTrue("Cannot create option file:" + expectedOptionFileDir3.getAbsolutePath(),
317                expectedOptionFileDir3.createNewFile() );
318
319         File JavaDoc[] dirs = new File JavaDoc[3];
320         dirs[0] = dir1;
321         dirs[1] = dir2;
322         dirs[2] = dir3;
323
324         XmlcUtils xmlcUtils = XmlcUtils.create();
325         String JavaDoc[] 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         // make sure filters out duplicates and uses specified order
335
dirs = new File JavaDoc[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     //===========================================================================
354
// HELPER METHODS
355
//===========================================================================
356

357     private void recursiveDelete( File JavaDoc theFile ) {
358         if ( theFile.isDirectory() ) {
359             File JavaDoc[] 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     //===========================================================================
369
// DATA MEMBERS
370
//===========================================================================
371
private File JavaDoc myBaseTestDir = new File JavaDoc( TEST_DIR );
372
373     //===========================================================================
374
// STATIC DATA MEMBERS
375
//===========================================================================
376
private static final String JavaDoc TEST_DIR="_TestOptionsFile";
377
378     // setup the loggger for this class
379
private static final Logger theirLogger = Logger.getLogger( TestOptionsFile.class.getName() );
380 }
381
Popular Tags