001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.BuildFileTest;
022: import org.apache.tools.ant.util.FileUtils;
023: import java.io.File;
024: import java.io.FileReader;
025: import java.io.IOException;
026:
027: /**
028: * Tests FileSet using the Copy task.
029: *
030: */
031: public class CopyTest extends BuildFileTest {
032:
033: /** Utilities used for file operations */
034: private static final FileUtils FILE_UTILS = FileUtils
035: .getFileUtils();
036:
037: public CopyTest(String name) {
038: super (name);
039: }
040:
041: public void setUp() {
042: configureProject("src/etc/testcases/taskdefs/copy.xml");
043: }
044:
045: public void test1() {
046: executeTarget("test1");
047: File f = new File(getProjectDir(), "copytest1.tmp");
048: if (!f.exists()) {
049: fail("Copy failed");
050: }
051: }
052:
053: public void tearDown() {
054: executeTarget("cleanup");
055: }
056:
057: public void test2() {
058: executeTarget("test2");
059: File f = new File(getProjectDir(), "copytest1dir/copy.xml");
060: if (!f.exists()) {
061: fail("Copy failed");
062: }
063: }
064:
065: public void test3() {
066: executeTarget("test3");
067: File file3 = new File(getProjectDir(), "copytest3.tmp");
068: assertTrue(file3.exists());
069: File file3a = new File(getProjectDir(), "copytest3a.tmp");
070: assertTrue(file3a.exists());
071: File file3b = new File(getProjectDir(), "copytest3b.tmp");
072: assertTrue(file3b.exists());
073: File file3c = new File(getProjectDir(), "copytest3c.tmp");
074: assertTrue(file3c.exists());
075:
076: //file length checks rely on touch generating a zero byte file
077: if (file3.length() == 0) {
078: fail("could not overwrite an existing, older file");
079: }
080: if (file3c.length() != 0) {
081: fail("could not force overwrite an existing, newer file");
082: }
083: if (file3b.length() == 0) {
084: fail("unexpectedly overwrote an existing, newer file");
085: }
086:
087: //file time checks for java1.2+
088: assertTrue(file3a.lastModified() == file3.lastModified());
089: assertTrue(file3c.lastModified() < file3a.lastModified());
090:
091: }
092:
093: public void testFilterTest() {
094: executeTarget("filtertest");
095: assertTrue(getOutput().indexOf("loop in tokens") == -1);
096: }
097:
098: public void testInfiniteFilter() {
099: executeTarget("infinitetest");
100: assertTrue(getOutput().indexOf("loop in tokens") != -1);
101: }
102:
103: public void testFilterSet() throws IOException {
104: executeTarget("testFilterSet");
105: File tmp = new File(getProjectDir(), "copy.filterset.tmp");
106: File check = new File(getProjectDir(),
107: "expected/copy.filterset.filtered");
108: assertTrue(tmp.exists());
109: assertTrue(FILE_UTILS.contentEquals(tmp, check));
110: }
111:
112: public void testFilterChain() throws IOException {
113: executeTarget("testFilterChain");
114: File tmp = new File(getProjectDir(), "copy.filterchain.tmp");
115: File check = new File(getProjectDir(),
116: "expected/copy.filterset.filtered");
117: assertTrue(tmp.exists());
118: assertTrue(FILE_UTILS.contentEquals(tmp, check));
119: }
120:
121: public void testSingleFileFileset() {
122: executeTarget("test_single_file_fileset");
123: File file = new File(getProjectDir(),
124: "copytest_single_file_fileset.tmp");
125: assertTrue(file.exists());
126: }
127:
128: public void testSingleFilePath() {
129: executeTarget("test_single_file_path");
130: File file = new File(getProjectDir(),
131: "copytest_single_file_path.tmp");
132: assertTrue(file.exists());
133: }
134:
135: public void testTranscoding() throws IOException {
136: executeTarget("testTranscoding");
137: File f1 = getProject().resolveFile("copy/expected/utf-8");
138: File f2 = getProject().resolveFile("copytest1.tmp");
139: assertTrue(FILE_UTILS.contentEquals(f1, f2));
140: }
141:
142: public void testMissingFileIgnore() {
143: expectLogContaining("testMissingFileIgnore",
144: "Warning: Could not find file ");
145: }
146:
147: public void testMissingFileBail() {
148: expectBuildException("testMissingFileBail",
149: "not-there doesn't exist");
150: assertTrue(getBuildException().getMessage().startsWith(
151: "Warning: Could not find file "));
152: }
153:
154: public void testMissingDirIgnore() {
155: expectLogContaining("testMissingDirIgnore", "Warning: ");
156: }
157:
158: public void testMissingDirBail() {
159: expectBuildException("testMissingDirBail",
160: "not-there doesn't exist");
161: assertTrue(getBuildException().getMessage().endsWith(
162: " not found."));
163: }
164:
165: public void testFileResourcePlain() {
166: executeTarget("testFileResourcePlain");
167: File file1 = new File(getProjectDir(), getProject()
168: .getProperty("to.dir")
169: + "/file1.txt");
170: File file2 = new File(getProjectDir(), getProject()
171: .getProperty("to.dir")
172: + "/file2.txt");
173: File file3 = new File(getProjectDir(), getProject()
174: .getProperty("to.dir")
175: + "/file3.txt");
176: assertTrue(file1.exists());
177: assertTrue(file2.exists());
178: assertTrue(file3.exists());
179: }
180:
181: public void _testFileResourceWithMapper() {
182: executeTarget("testFileResourceWithMapper");
183: File file1 = new File(getProjectDir(), getProject()
184: .getProperty("to.dir")
185: + "/file1.txt.bak");
186: File file2 = new File(getProjectDir(), getProject()
187: .getProperty("to.dir")
188: + "/file2.txt.bak");
189: File file3 = new File(getProjectDir(), getProject()
190: .getProperty("to.dir")
191: + "/file3.txt.bak");
192: assertTrue(file1.exists());
193: assertTrue(file2.exists());
194: assertTrue(file3.exists());
195: }
196:
197: public void testFileResourceWithFilter() {
198: executeTarget("testFileResourceWithFilter");
199: File file1 = new File(getProjectDir(), getProject()
200: .getProperty("to.dir")
201: + "/fileNR.txt");
202: assertTrue(file1.exists());
203: try {
204: String file1Content = FILE_UTILS.readFully(new FileReader(
205: file1));
206: assertEquals("This is file 42", file1Content);
207: } catch (IOException e) {
208: // no-op: not a real business error
209: }
210: }
211:
212: public void testPathAsResource() {
213: executeTarget("testPathAsResource");
214: File file1 = new File(getProjectDir(), getProject()
215: .getProperty("to.dir")
216: + "/file1.txt");
217: File file2 = new File(getProjectDir(), getProject()
218: .getProperty("to.dir")
219: + "/file2.txt");
220: File file3 = new File(getProjectDir(), getProject()
221: .getProperty("to.dir")
222: + "/file3.txt");
223: assertTrue(file1.exists());
224: assertTrue(file2.exists());
225: assertTrue(file3.exists());
226: }
227:
228: public void testZipfileset() {
229: executeTarget("testZipfileset");
230: File file1 = new File(getProjectDir(), getProject()
231: .getProperty("to.dir")
232: + "/file1.txt");
233: File file2 = new File(getProjectDir(), getProject()
234: .getProperty("to.dir")
235: + "/file2.txt");
236: File file3 = new File(getProjectDir(), getProject()
237: .getProperty("to.dir")
238: + "/file3.txt");
239: assertTrue(file1.exists());
240: assertTrue(file2.exists());
241: assertTrue(file3.exists());
242: }
243:
244: public void testDirset() {
245: executeTarget("testDirset");
246: }
247:
248: public void _testResourcePlain() {
249: executeTarget("testResourcePlain");
250: }
251:
252: public void _testResourcePlainWithMapper() {
253: executeTarget("testResourcePlainWithMapper");
254: }
255:
256: public void _testResourcePlainWithFilter() {
257: executeTarget("testResourcePlainWithFilter");
258: }
259:
260: public void _testOnlineResources() {
261: executeTarget("testOnlineResources");
262: }
263:
264: }
|