KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > tidy > ant > JTidyTaskTest


1 /*
2  * Java HTML Tidy - JTidy
3  * HTML parser and pretty printer
4  *
5  * Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6  * Institute of Technology, Institut National de Recherche en
7  * Informatique et en Automatique, Keio University). All Rights
8  * Reserved.
9  *
10  * Contributing Author(s):
11  *
12  * Dave Raggett <dsr@w3.org>
13  * Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14  * Gary L Peskin <garyp@firstech.com> (Java development)
15  * Sami Lempinen <sami@lempinen.net> (release management)
16  * Fabrizio Giustina <fgiust at users.sourceforge.net>
17  *
18  * The contributing author(s) would like to thank all those who
19  * helped with testing, bug fixes, and patience. This wouldn't
20  * have been possible without all of you.
21  *
22  * COPYRIGHT NOTICE:
23  *
24  * This software and documentation is provided "as is," and
25  * the copyright holders and contributing author(s) make no
26  * representations or warranties, express or implied, including
27  * but not limited to, warranties of merchantability or fitness
28  * for any particular purpose or that the use of the software or
29  * documentation will not infringe any third party patents,
30  * copyrights, trademarks or other rights.
31  *
32  * The copyright holders and contributing author(s) will not be
33  * liable for any direct, indirect, special or consequential damages
34  * arising out of any use of the software or documentation, even if
35  * advised of the possibility of such damage.
36  *
37  * Permission is hereby granted to use, copy, modify, and distribute
38  * this source code, or portions hereof, documentation and executables,
39  * for any purpose, without fee, subject to the following restrictions:
40  *
41  * 1. The origin of this source code must not be misrepresented.
42  * 2. Altered versions must be plainly marked as such and must
43  * not be misrepresented as being the original source.
44  * 3. This Copyright notice may not be removed or altered from any
45  * source or altered source distribution.
46  *
47  * The copyright holders and contributing author(s) specifically
48  * permit, without fee, and encourage the use of this source code
49  * as a component for supporting the Hypertext Markup Language in
50  * commercial products. If you use this source code in a product,
51  * acknowledgment is not required but would be appreciated.
52  *
53  */

54 package org.w3c.tidy.ant;
55
56 import java.io.File JavaDoc;
57 import java.io.FileReader JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.Reader JavaDoc;
60
61 import junit.framework.TestCase;
62
63 import org.apache.tools.ant.BuildException;
64 import org.apache.tools.ant.Project;
65 import org.apache.tools.ant.types.FileSet;
66 import org.apache.tools.ant.types.Parameter;
67 import org.apache.tools.ant.util.FileUtils;
68
69
70 /**
71  * @author Fabrizio Giustina
72  * @version $Revision: 1.3 $ ($Author: fgiust $)
73  */

74 public class JTidyTaskTest extends TestCase
75 {
76
77     /**
78      * test instance.
79      */

80     private JTidyTask task;
81
82     /**
83      * Temp dir used for output.
84      */

85     private String JavaDoc tempDir;
86
87     /**
88      * Test dir.
89      */

90     private String JavaDoc testDir;
91
92     /**
93      * @see junit.framework.TestCase#setUp()
94      */

95     protected void setUp() throws Exception JavaDoc
96     {
97         super.setUp();
98         task = new JTidyTask();
99         Project p = new Project();
100         task.setProject(p);
101         task.init();
102         tempDir = System.getProperty("java.io.tmpdir");
103         testDir = new File JavaDoc(getClass().getClassLoader().getResource("test.dir").getPath()).getParent();
104     }
105
106     /**
107      * Test with invalid parameters.
108      */

109     public void testExceptionMissingParameters()
110     {
111         try
112         {
113             task.execute();
114             fail("Invalid parameters not detected");
115         }
116         catch (BuildException e)
117         {
118             // ok
119
}
120     }
121
122     /**
123      * Test with invalid parameters.
124      */

125     public void testExceptionBothSrcfileAndFilesets()
126     {
127         try
128         {
129             task.setSrcfile(new File JavaDoc("."));
130             task.addFileset(new FileSet());
131             task.validateParameters();
132             fail("Invalid parameters not detected");
133         }
134         catch (BuildException e)
135         {
136             // ok
137
}
138     }
139
140     /**
141      * Test with invalid parameters.
142      */

143     public void testDestFileAndDestDirNull()
144     {
145         try
146         {
147             task.setSrcfile(new File JavaDoc("."));
148             task.validateParameters();
149             fail("Invalid parameters not detected");
150         }
151         catch (BuildException e)
152         {
153             // ok
154
}
155     }
156
157     /**
158      * Test with invalid parameters.
159      */

160     public void testDestFileAndFilesets()
161     {
162         try
163         {
164             task.addFileset(new FileSet());
165             task.setDestfile(new File JavaDoc("."));
166             task.validateParameters();
167             fail("Invalid parameters not detected");
168         }
169         catch (BuildException e)
170         {
171             // ok
172
}
173     }
174
175     /**
176      * Test with invalid parameters.
177      */

178     public void testScrFileIsADir()
179     {
180         try
181         {
182             task.setSrcfile(new File JavaDoc("/"));
183             task.setDestfile(new File JavaDoc("test.out"));
184             task.validateParameters();
185             fail("Invalid parameters not detected");
186         }
187         catch (BuildException e)
188         {
189             // ok
190
}
191     }
192
193     /**
194      * Test with invalid parameters.
195      */

196     public void testScrFileDoesntExist()
197     {
198
199         task.setSrcfile(new File JavaDoc("xyz.123"));
200         task.setDestfile(new File JavaDoc("test.out"));
201         task.validateParameters();
202         try
203         {
204             task.execute();
205             fail("Missing source file not detected");
206         }
207         catch (BuildException e)
208         {
209             // ok
210
}
211     }
212
213     /**
214      * Test with invalid parameters.
215      */

216     public void testInvalidProperties()
217     {
218         try
219         {
220             task.setSrcfile(new File JavaDoc("test.in"));
221             task.setDestfile(new File JavaDoc("test.out"));
222             task.setProperties(new File JavaDoc("x2ui34"));
223             task.validateParameters();
224             fail("Invalid parameters not detected");
225         }
226         catch (BuildException e)
227         {
228             // ok
229
}
230     }
231
232     /**
233      * Test with a fileset.
234      */

235     public void testFileset()
236     {
237         FileSet fileset = new FileSet();
238         fileset.setDir(new File JavaDoc(testDir, "ant"));
239
240         task.addFileset(fileset);
241         task.setDestdir(new File JavaDoc(tempDir));
242
243         task.execute();
244
245         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file1.html").exists());
246         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file2.html").exists());
247
248         new File JavaDoc(tempDir, "file1.html").delete();
249         new File JavaDoc(tempDir, "file2.html").delete();
250     }
251
252     /**
253      * Test with a fileset.
254      */

255     public void testFilesetWithDirStructure()
256     {
257         FileSet fileset = new FileSet();
258         fileset.setDir(new File JavaDoc(testDir));
259         fileset.setIncludes("ant/*.html");
260
261         task.addFileset(fileset);
262         task.setDestdir(new File JavaDoc(tempDir));
263
264         task.execute();
265
266         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "ant/file1.html").exists());
267         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "ant/file2.html").exists());
268
269         new File JavaDoc(tempDir, "ant/file1.html").delete();
270         new File JavaDoc(tempDir, "ant/file2.html").delete();
271         new File JavaDoc(tempDir, "ant").delete();
272     }
273
274     /**
275      * Test with a fileset.
276      */

277     public void testFilesetWithDirStructureFlatten()
278     {
279         FileSet fileset = new FileSet();
280         fileset.setDir(new File JavaDoc(testDir));
281         fileset.setIncludes("ant/*.html");
282
283         task.addFileset(fileset);
284         task.setDestdir(new File JavaDoc(tempDir));
285         task.setFlatten(true);
286
287         task.execute();
288
289         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file1.html").exists());
290         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file2.html").exists());
291
292         new File JavaDoc(tempDir, "file1.html").delete();
293         new File JavaDoc(tempDir, "file2.html").delete();
294     }
295
296     /**
297      * Test nested parameter element.
298      */

299     public void testWithParameters()
300     {
301         FileSet fileset = new FileSet();
302         fileset.setDir(new File JavaDoc(testDir));
303         fileset.setIncludes("ant/*1.html");
304
305         task.addFileset(fileset);
306         task.setDestdir(new File JavaDoc(tempDir));
307         task.setFlatten(true);
308         Parameter parameter = new Parameter();
309         parameter.setName("tidy-mark");
310         parameter.setValue("false");
311         task.addConfiguredParameter(parameter);
312         task.execute();
313
314         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file1.html").exists());
315
316         try
317         {
318             Reader JavaDoc reader = new FileReader JavaDoc(new File JavaDoc(tempDir, "file1.html"));
319             String JavaDoc output = FileUtils.readFully(reader);
320             reader.close();
321
322             // output file should not contain "generator"
323
assertTrue("Configured parameter doesn't have effect on output.", output.indexOf("generator") == -1);
324         }
325         catch (IOException JavaDoc e)
326         {
327             fail("Unable to read generated file.");
328         }
329
330         new File JavaDoc(tempDir, "file1.html").delete();
331     }
332
333     /**
334      * Test with a properties file.
335      */

336     public void testWithProperties()
337     {
338         FileSet fileset = new FileSet();
339         fileset.setDir(new File JavaDoc(testDir));
340         fileset.setIncludes("ant/*1.html");
341
342         task.addFileset(fileset);
343         task.setDestdir(new File JavaDoc(tempDir));
344         task.setFlatten(true);
345         task.setProperties(new File JavaDoc(testDir, "default.cfg"));
346
347         task.execute();
348
349         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file1.html").exists());
350
351         try
352         {
353             Reader JavaDoc reader = new FileReader JavaDoc(new File JavaDoc(tempDir, "file1.html"));
354             String JavaDoc output = FileUtils.readFully(reader);
355             reader.close();
356
357             // output file should not contain "generator"
358
assertTrue("Configured parameter doesn't have effect on output.", output.indexOf("generator") == -1);
359         }
360         catch (IOException JavaDoc e)
361         {
362             fail("Unable to read generated file.");
363         }
364
365         new File JavaDoc(tempDir, "file1.html").delete();
366     }
367
368     /**
369      * Test with a fileset.
370      */

371     public void testFailonerrorFalse()
372     {
373         task.setSrcfile(new File JavaDoc(testDir, "ant/file3.html"));
374         task.setDestdir(new File JavaDoc(tempDir));
375         task.setFailonerror(false);
376
377         task.execute();
378
379         // ok if no buildexception is thrown
380
}
381
382     /**
383      * Test with a fileset.
384      */

385     public void testFailonerrorTrue()
386     {
387         task.setSrcfile(new File JavaDoc(testDir, "ant/file3.html"));
388         task.setDestdir(new File JavaDoc(tempDir));
389         task.setFailonerror(true);
390
391         try
392         {
393             task.execute();
394             fail("Expected BuildException not thrown.");
395         }
396         catch (BuildException e)
397         {
398             // ok if buildexception IS thrown
399
}
400     }
401
402     /**
403      * Test with srcfile/destdir.
404      */

405     public void testSrcfileDestDir()
406     {
407         task.setSrcfile(new File JavaDoc(testDir, "ant/file1.html"));
408         task.setDestdir(new File JavaDoc(tempDir));
409         task.setFailonerror(true);
410
411         task.execute();
412
413         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "file1.html").exists());
414
415         new File JavaDoc(tempDir, "file1.html").delete();
416     }
417
418     /**
419      * Test with srcfile/destfile.
420      */

421     public void testSrcfileDestFile()
422     {
423         task.setSrcfile(new File JavaDoc(testDir, "ant/file1.html"));
424         task.setDestfile(new File JavaDoc(tempDir, "newfile.html"));
425         task.setFailonerror(true);
426
427         task.execute();
428
429         assertTrue("Expected output file not created", new File JavaDoc(tempDir, "newfile.html").exists());
430         assertFalse("Expected output file is a dir!", new File JavaDoc(tempDir, "newfile.html").isDirectory());
431
432         new File JavaDoc(tempDir, "newfile.html").delete();
433     }
434
435 }
Popular Tags