001: /*
002: * Java HTML Tidy - JTidy
003: * HTML parser and pretty printer
004: *
005: * Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
006: * Institute of Technology, Institut National de Recherche en
007: * Informatique et en Automatique, Keio University). All Rights
008: * Reserved.
009: *
010: * Contributing Author(s):
011: *
012: * Dave Raggett <dsr@w3.org>
013: * Andy Quick <ac.quick@sympatico.ca> (translation to Java)
014: * Gary L Peskin <garyp@firstech.com> (Java development)
015: * Sami Lempinen <sami@lempinen.net> (release management)
016: * Fabrizio Giustina <fgiust at users.sourceforge.net>
017: *
018: * The contributing author(s) would like to thank all those who
019: * helped with testing, bug fixes, and patience. This wouldn't
020: * have been possible without all of you.
021: *
022: * COPYRIGHT NOTICE:
023: *
024: * This software and documentation is provided "as is," and
025: * the copyright holders and contributing author(s) make no
026: * representations or warranties, express or implied, including
027: * but not limited to, warranties of merchantability or fitness
028: * for any particular purpose or that the use of the software or
029: * documentation will not infringe any third party patents,
030: * copyrights, trademarks or other rights.
031: *
032: * The copyright holders and contributing author(s) will not be
033: * liable for any direct, indirect, special or consequential damages
034: * arising out of any use of the software or documentation, even if
035: * advised of the possibility of such damage.
036: *
037: * Permission is hereby granted to use, copy, modify, and distribute
038: * this source code, or portions hereof, documentation and executables,
039: * for any purpose, without fee, subject to the following restrictions:
040: *
041: * 1. The origin of this source code must not be misrepresented.
042: * 2. Altered versions must be plainly marked as such and must
043: * not be misrepresented as being the original source.
044: * 3. This Copyright notice may not be removed or altered from any
045: * source or altered source distribution.
046: *
047: * The copyright holders and contributing author(s) specifically
048: * permit, without fee, and encourage the use of this source code
049: * as a component for supporting the Hypertext Markup Language in
050: * commercial products. If you use this source code in a product,
051: * acknowledgment is not required but would be appreciated.
052: *
053: */
054: package org.w3c.tidy.ant;
055:
056: import java.io.File;
057: import java.io.FileReader;
058: import java.io.IOException;
059: import java.io.Reader;
060:
061: import junit.framework.TestCase;
062:
063: import org.apache.tools.ant.BuildException;
064: import org.apache.tools.ant.Project;
065: import org.apache.tools.ant.types.FileSet;
066: import org.apache.tools.ant.types.Parameter;
067: import org.apache.tools.ant.util.FileUtils;
068:
069: /**
070: * @author Fabrizio Giustina
071: * @version $Revision: 1.3 $ ($Author: fgiust $)
072: */
073: public class JTidyTaskTest extends TestCase {
074:
075: /**
076: * test instance.
077: */
078: private JTidyTask task;
079:
080: /**
081: * Temp dir used for output.
082: */
083: private String tempDir;
084:
085: /**
086: * Test dir.
087: */
088: private String testDir;
089:
090: /**
091: * @see junit.framework.TestCase#setUp()
092: */
093: protected void setUp() throws Exception {
094: super .setUp();
095: task = new JTidyTask();
096: Project p = new Project();
097: task.setProject(p);
098: task.init();
099: tempDir = System.getProperty("java.io.tmpdir");
100: testDir = new File(getClass().getClassLoader().getResource(
101: "test.dir").getPath()).getParent();
102: }
103:
104: /**
105: * Test with invalid parameters.
106: */
107: public void testExceptionMissingParameters() {
108: try {
109: task.execute();
110: fail("Invalid parameters not detected");
111: } catch (BuildException e) {
112: // ok
113: }
114: }
115:
116: /**
117: * Test with invalid parameters.
118: */
119: public void testExceptionBothSrcfileAndFilesets() {
120: try {
121: task.setSrcfile(new File("."));
122: task.addFileset(new FileSet());
123: task.validateParameters();
124: fail("Invalid parameters not detected");
125: } catch (BuildException e) {
126: // ok
127: }
128: }
129:
130: /**
131: * Test with invalid parameters.
132: */
133: public void testDestFileAndDestDirNull() {
134: try {
135: task.setSrcfile(new File("."));
136: task.validateParameters();
137: fail("Invalid parameters not detected");
138: } catch (BuildException e) {
139: // ok
140: }
141: }
142:
143: /**
144: * Test with invalid parameters.
145: */
146: public void testDestFileAndFilesets() {
147: try {
148: task.addFileset(new FileSet());
149: task.setDestfile(new File("."));
150: task.validateParameters();
151: fail("Invalid parameters not detected");
152: } catch (BuildException e) {
153: // ok
154: }
155: }
156:
157: /**
158: * Test with invalid parameters.
159: */
160: public void testScrFileIsADir() {
161: try {
162: task.setSrcfile(new File("/"));
163: task.setDestfile(new File("test.out"));
164: task.validateParameters();
165: fail("Invalid parameters not detected");
166: } catch (BuildException e) {
167: // ok
168: }
169: }
170:
171: /**
172: * Test with invalid parameters.
173: */
174: public void testScrFileDoesntExist() {
175:
176: task.setSrcfile(new File("xyz.123"));
177: task.setDestfile(new File("test.out"));
178: task.validateParameters();
179: try {
180: task.execute();
181: fail("Missing source file not detected");
182: } catch (BuildException e) {
183: // ok
184: }
185: }
186:
187: /**
188: * Test with invalid parameters.
189: */
190: public void testInvalidProperties() {
191: try {
192: task.setSrcfile(new File("test.in"));
193: task.setDestfile(new File("test.out"));
194: task.setProperties(new File("x2ui34"));
195: task.validateParameters();
196: fail("Invalid parameters not detected");
197: } catch (BuildException e) {
198: // ok
199: }
200: }
201:
202: /**
203: * Test with a fileset.
204: */
205: public void testFileset() {
206: FileSet fileset = new FileSet();
207: fileset.setDir(new File(testDir, "ant"));
208:
209: task.addFileset(fileset);
210: task.setDestdir(new File(tempDir));
211:
212: task.execute();
213:
214: assertTrue("Expected output file not created", new File(
215: tempDir, "file1.html").exists());
216: assertTrue("Expected output file not created", new File(
217: tempDir, "file2.html").exists());
218:
219: new File(tempDir, "file1.html").delete();
220: new File(tempDir, "file2.html").delete();
221: }
222:
223: /**
224: * Test with a fileset.
225: */
226: public void testFilesetWithDirStructure() {
227: FileSet fileset = new FileSet();
228: fileset.setDir(new File(testDir));
229: fileset.setIncludes("ant/*.html");
230:
231: task.addFileset(fileset);
232: task.setDestdir(new File(tempDir));
233:
234: task.execute();
235:
236: assertTrue("Expected output file not created", new File(
237: tempDir, "ant/file1.html").exists());
238: assertTrue("Expected output file not created", new File(
239: tempDir, "ant/file2.html").exists());
240:
241: new File(tempDir, "ant/file1.html").delete();
242: new File(tempDir, "ant/file2.html").delete();
243: new File(tempDir, "ant").delete();
244: }
245:
246: /**
247: * Test with a fileset.
248: */
249: public void testFilesetWithDirStructureFlatten() {
250: FileSet fileset = new FileSet();
251: fileset.setDir(new File(testDir));
252: fileset.setIncludes("ant/*.html");
253:
254: task.addFileset(fileset);
255: task.setDestdir(new File(tempDir));
256: task.setFlatten(true);
257:
258: task.execute();
259:
260: assertTrue("Expected output file not created", new File(
261: tempDir, "file1.html").exists());
262: assertTrue("Expected output file not created", new File(
263: tempDir, "file2.html").exists());
264:
265: new File(tempDir, "file1.html").delete();
266: new File(tempDir, "file2.html").delete();
267: }
268:
269: /**
270: * Test nested parameter element.
271: */
272: public void testWithParameters() {
273: FileSet fileset = new FileSet();
274: fileset.setDir(new File(testDir));
275: fileset.setIncludes("ant/*1.html");
276:
277: task.addFileset(fileset);
278: task.setDestdir(new File(tempDir));
279: task.setFlatten(true);
280: Parameter parameter = new Parameter();
281: parameter.setName("tidy-mark");
282: parameter.setValue("false");
283: task.addConfiguredParameter(parameter);
284: task.execute();
285:
286: assertTrue("Expected output file not created", new File(
287: tempDir, "file1.html").exists());
288:
289: try {
290: Reader reader = new FileReader(new File(tempDir,
291: "file1.html"));
292: String output = FileUtils.readFully(reader);
293: reader.close();
294:
295: // output file should not contain "generator"
296: assertTrue(
297: "Configured parameter doesn't have effect on output.",
298: output.indexOf("generator") == -1);
299: } catch (IOException e) {
300: fail("Unable to read generated file.");
301: }
302:
303: new File(tempDir, "file1.html").delete();
304: }
305:
306: /**
307: * Test with a properties file.
308: */
309: public void testWithProperties() {
310: FileSet fileset = new FileSet();
311: fileset.setDir(new File(testDir));
312: fileset.setIncludes("ant/*1.html");
313:
314: task.addFileset(fileset);
315: task.setDestdir(new File(tempDir));
316: task.setFlatten(true);
317: task.setProperties(new File(testDir, "default.cfg"));
318:
319: task.execute();
320:
321: assertTrue("Expected output file not created", new File(
322: tempDir, "file1.html").exists());
323:
324: try {
325: Reader reader = new FileReader(new File(tempDir,
326: "file1.html"));
327: String output = FileUtils.readFully(reader);
328: reader.close();
329:
330: // output file should not contain "generator"
331: assertTrue(
332: "Configured parameter doesn't have effect on output.",
333: output.indexOf("generator") == -1);
334: } catch (IOException e) {
335: fail("Unable to read generated file.");
336: }
337:
338: new File(tempDir, "file1.html").delete();
339: }
340:
341: /**
342: * Test with a fileset.
343: */
344: public void testFailonerrorFalse() {
345: task.setSrcfile(new File(testDir, "ant/file3.html"));
346: task.setDestdir(new File(tempDir));
347: task.setFailonerror(false);
348:
349: task.execute();
350:
351: // ok if no buildexception is thrown
352: }
353:
354: /**
355: * Test with a fileset.
356: */
357: public void testFailonerrorTrue() {
358: task.setSrcfile(new File(testDir, "ant/file3.html"));
359: task.setDestdir(new File(tempDir));
360: task.setFailonerror(true);
361:
362: try {
363: task.execute();
364: fail("Expected BuildException not thrown.");
365: } catch (BuildException e) {
366: // ok if buildexception IS thrown
367: }
368: }
369:
370: /**
371: * Test with srcfile/destdir.
372: */
373: public void testSrcfileDestDir() {
374: task.setSrcfile(new File(testDir, "ant/file1.html"));
375: task.setDestdir(new File(tempDir));
376: task.setFailonerror(true);
377:
378: task.execute();
379:
380: assertTrue("Expected output file not created", new File(
381: tempDir, "file1.html").exists());
382:
383: new File(tempDir, "file1.html").delete();
384: }
385:
386: /**
387: * Test with srcfile/destfile.
388: */
389: public void testSrcfileDestFile() {
390: task.setSrcfile(new File(testDir, "ant/file1.html"));
391: task.setDestfile(new File(tempDir, "newfile.html"));
392: task.setFailonerror(true);
393:
394: task.execute();
395:
396: assertTrue("Expected output file not created", new File(
397: tempDir, "newfile.html").exists());
398: assertFalse("Expected output file is a dir!", new File(tempDir,
399: "newfile.html").isDirectory());
400:
401: new File(tempDir, "newfile.html").delete();
402: }
403:
404: }
|