KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > FixCrLfTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.*;
21
22 import junit.framework.AssertionFailedError;
23 import org.apache.tools.ant.BuildFileTest;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.util.JavaEnvUtils;
26
27 /**
28  */

29 public class FixCrLfTest extends BuildFileTest {
30
31     public FixCrLfTest(String JavaDoc name) {
32         super(name);
33     }
34
35     public void setUp() {
36         configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml");
37     }
38
39     public void tearDown() {
40         executeTarget("cleanup");
41     }
42
43     public void test1() throws IOException {
44         executeTarget("test1");
45         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk1.java"),
46                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk1.java"));
47     }
48
49     public void test2() throws IOException {
50         executeTarget("test2");
51         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk2.java"),
52                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk2.java"));
53     }
54
55     public void test3() throws IOException {
56         executeTarget("test3");
57         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk3.java"),
58                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk3.java"));
59     }
60
61     public void test4() throws IOException {
62         executeTarget("test4");
63         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk4.java"),
64                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk4.java"));
65     }
66
67     public void test5() throws IOException {
68         executeTarget("test5");
69         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk5.java"),
70                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk5.java"));
71     }
72
73     public void test6() throws IOException {
74         executeTarget("test6");
75         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk6.java"),
76                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk6.java"));
77     }
78
79     public void test7() throws IOException {
80         executeTarget("test7");
81         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk7.java"),
82                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk7.java"));
83     }
84
85     public void test8() throws IOException {
86         executeTarget("test8");
87         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk8.java"),
88                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk8.java"));
89     }
90
91     public void test9() throws IOException {
92         executeTarget("test9");
93         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Junk9.java"),
94                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk9.java"));
95     }
96
97     public void testMacLines() throws IOException {
98         executeTarget("testMacLines");
99         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/Mac2Unix"),
100                            new File("src/etc/testcases/taskdefs/fixcrlf/result/Mac2Unix"));
101     }
102
103     public void testNoOverwrite() throws IOException {
104         executeTarget("test1");
105         File result =
106             new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk1.java");
107         long modTime = result.lastModified();
108
109         /*
110          * Sleep for some time to make sure a newer file would get a
111          * more recent timestamp according to the file system's
112          * granularity (should be > 2s to account for Windows FAT).
113          */

114         try {
115             Thread.currentThread().sleep(5000);
116         } catch (InterruptedException JavaDoc ie) {
117             fail(ie.getMessage());
118         } // end of try-catch
119

120         /*
121          * make sure we get a new Project instance or the target won't get run
122          * a second time.
123          */

124         configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml");
125
126         executeTarget("test1");
127         result =
128             new File("src/etc/testcases/taskdefs/fixcrlf/result/Junk1.java");
129         assertEquals(modTime, result.lastModified());
130     }
131
132     public void testEncoding() throws IOException {
133         if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
134             // UTF16 is not supported in JDK 1.1
135
return;
136         }
137         executeTarget("testEncoding");
138         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16"),
139                            new File("src/etc/testcases/taskdefs/fixcrlf/result/input.crlf.utf16"));
140     }
141
142     public void testLongLines() throws IOException {
143         executeTarget("testLongLines");
144         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/longlines.lf"),
145                            new File("src/etc/testcases/taskdefs/fixcrlf/result/longlines.crlf"));
146     }
147
148     public void testCrCrLfSequenceUnix() throws IOException {
149         executeTarget("testCrCrLfSequence-unix");
150         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/crcrlf.unix"),
151                            new File("src/etc/testcases/taskdefs/fixcrlf/result/crcrlf"));
152     }
153
154     public void testCrCrLfSequenceDos() throws IOException {
155         executeTarget("testCrCrLfSequence-dos");
156         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/crcrlf.dos"),
157                            new File("src/etc/testcases/taskdefs/fixcrlf/result/crcrlf"));
158     }
159
160     public void testCrCrLfSequenceMac() throws IOException {
161         executeTarget("testCrCrLfSequence-mac");
162         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/crcrlf.mac"),
163                            new File("src/etc/testcases/taskdefs/fixcrlf/result/crcrlf"));
164     }
165
166     public void testFixlastDos() throws IOException {
167         executeTarget("testFixlastDos");
168         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/fixlast.dos"),
169                            new File("src/etc/testcases/taskdefs/fixcrlf/result/fixlastfalse.lf"));
170     }
171
172     public void testFixlastFalseMac() throws IOException {
173         executeTarget("testFixlastFalseMac");
174         assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/fixlastfalse.mac"),
175                            new File("src/etc/testcases/taskdefs/fixcrlf/result/fixlastfalse.lf"));
176     }
177
178     /**
179      * Bugzilla Report 20840
180      *
181      * Will fail with an exception if the parent directories do not
182      * get created.
183      */

184     public void testCreateParentDirs() {
185         executeTarget("createParentDirs");
186     }
187
188     public void assertEqualContent(File expect, File result)
189         throws AssertionFailedError, IOException {
190         if (!result.exists()) {
191             fail("Expected file "+result+" doesn\'t exist");
192         }
193
194         InputStream inExpect = null;
195         InputStream inResult = null;
196         try {
197             inExpect = new BufferedInputStream(new FileInputStream(expect));
198             inResult = new BufferedInputStream(new FileInputStream(result));
199
200             int expectedByte = inExpect.read();
201             while (expectedByte != -1) {
202                 assertEquals(expectedByte, inResult.read());
203                 expectedByte = inExpect.read();
204             }
205             assertEquals("End of file", -1, inResult.read());
206         } finally {
207             if (inResult != null) {
208                 inResult.close();
209             }
210             if (inExpect != null) {
211                 inExpect.close();
212             }
213         }
214     }
215
216 }
217
Popular Tags