You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyTest.java 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import org.apache.tools.ant.BuildFileTest;
  20. import org.apache.tools.ant.util.FileUtils;
  21. import java.io.File;
  22. import java.io.FileReader;
  23. import java.io.IOException;
  24. /**
  25. * Tests FileSet using the Copy task.
  26. *
  27. */
  28. public class CopyTest extends BuildFileTest {
  29. /** Utilities used for file operations */
  30. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  31. public CopyTest(String name) {
  32. super(name);
  33. }
  34. public void setUp() {
  35. configureProject("src/etc/testcases/taskdefs/copy.xml");
  36. }
  37. public void test1() {
  38. executeTarget("test1");
  39. File f = new File(getProjectDir(), "copytest1.tmp");
  40. if ( !f.exists()) {
  41. fail("Copy failed");
  42. }
  43. }
  44. public void tearDown() {
  45. executeTarget("cleanup");
  46. }
  47. public void test2() {
  48. executeTarget("test2");
  49. File f = new File(getProjectDir(), "copytest1dir/copy.xml");
  50. if ( !f.exists()) {
  51. fail("Copy failed");
  52. }
  53. }
  54. public void test3() {
  55. executeTarget("test3");
  56. File file3 = new File(getProjectDir(), "copytest3.tmp");
  57. assertTrue(file3.exists());
  58. File file3a = new File(getProjectDir(), "copytest3a.tmp");
  59. assertTrue(file3a.exists());
  60. File file3b = new File(getProjectDir(), "copytest3b.tmp");
  61. assertTrue(file3b.exists());
  62. File file3c = new File(getProjectDir(), "copytest3c.tmp");
  63. assertTrue(file3c.exists());
  64. //file length checks rely on touch generating a zero byte file
  65. if(file3.length()==0) {
  66. fail("could not overwrite an existing, older file");
  67. }
  68. if(file3c.length()!=0) {
  69. fail("could not force overwrite an existing, newer file");
  70. }
  71. if(file3b.length()==0) {
  72. fail("unexpectedly overwrote an existing, newer file");
  73. }
  74. //file time checks for java1.2+
  75. assertTrue(file3a.lastModified()==file3.lastModified());
  76. assertTrue(file3c.lastModified()<file3a.lastModified());
  77. }
  78. public void testFilterTest() {
  79. executeTarget("filtertest");
  80. assertTrue(getOutput().indexOf("loop in tokens") == -1);
  81. }
  82. public void testInfiniteFilter() {
  83. executeTarget("infinitetest");
  84. assertTrue(getOutput().indexOf("loop in tokens") != -1);
  85. }
  86. public void testFilterSet() throws IOException {
  87. executeTarget("testFilterSet");
  88. File tmp = new File(getProjectDir(), "copy.filterset.tmp");
  89. File check = new File(getProjectDir(), "expected/copy.filterset.filtered");
  90. assertTrue(tmp.exists());
  91. assertTrue(FILE_UTILS.contentEquals(tmp, check));
  92. }
  93. public void testFilterChain() throws IOException {
  94. executeTarget("testFilterChain");
  95. File tmp = new File(getProjectDir(), "copy.filterchain.tmp");
  96. File check = new File(getProjectDir(), "expected/copy.filterset.filtered");
  97. assertTrue(tmp.exists());
  98. assertTrue(FILE_UTILS.contentEquals(tmp, check));
  99. }
  100. public void testSingleFileFileset() {
  101. executeTarget("test_single_file_fileset");
  102. File file = new File(getProjectDir(),
  103. "copytest_single_file_fileset.tmp");
  104. assertTrue(file.exists());
  105. }
  106. public void testSingleFilePath() {
  107. executeTarget("test_single_file_path");
  108. File file = new File(getProjectDir(),
  109. "copytest_single_file_path.tmp");
  110. assertTrue(file.exists());
  111. }
  112. public void testTranscoding() throws IOException {
  113. executeTarget("testTranscoding");
  114. File f1 = getProject().resolveFile("copy/expected/utf-8");
  115. File f2 = getProject().resolveFile("copytest1.tmp");
  116. assertTrue(FILE_UTILS.contentEquals(f1, f2));
  117. }
  118. public void testMissingFileIgnore() {
  119. expectLogContaining("testMissingFileIgnore",
  120. "Warning: Could not find file ");
  121. }
  122. public void testMissingFileBail() {
  123. expectBuildException("testMissingFileBail", "not-there doesn't exist");
  124. assertTrue(getBuildException().getMessage()
  125. .startsWith("Warning: Could not find file "));
  126. }
  127. public void testMissingDirIgnore() {
  128. expectLogContaining("testMissingDirIgnore", "Warning: ");
  129. }
  130. public void testMissingDirBail() {
  131. expectBuildException("testMissingDirBail", "not-there doesn't exist");
  132. assertTrue(getBuildException().getMessage().endsWith(" not found."));
  133. }
  134. public void testFileResourcePlain() {
  135. executeTarget("testFileResourcePlain");
  136. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt");
  137. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt");
  138. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt");
  139. assertTrue(file1.exists());
  140. assertTrue(file2.exists());
  141. assertTrue(file3.exists());
  142. }
  143. public void _testFileResourceWithMapper() {
  144. executeTarget("testFileResourceWithMapper");
  145. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt.bak");
  146. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt.bak");
  147. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt.bak");
  148. assertTrue(file1.exists());
  149. assertTrue(file2.exists());
  150. assertTrue(file3.exists());
  151. }
  152. public void testFileResourceWithFilter() {
  153. executeTarget("testFileResourceWithFilter");
  154. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/fileNR.txt");
  155. assertTrue(file1.exists());
  156. try {
  157. String file1Content = FILE_UTILS.readFully(new FileReader(file1));
  158. assertEquals("This is file 42", file1Content);
  159. } catch (IOException e) {
  160. // no-op: not a real business error
  161. }
  162. }
  163. public void testPathAsResource() {
  164. executeTarget("testPathAsResource");
  165. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt");
  166. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt");
  167. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt");
  168. assertTrue(file1.exists());
  169. assertTrue(file2.exists());
  170. assertTrue(file3.exists());
  171. }
  172. public void testZipfileset() {
  173. executeTarget("testZipfileset");
  174. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt");
  175. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt");
  176. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt");
  177. assertTrue(file1.exists());
  178. assertTrue(file2.exists());
  179. assertTrue(file3.exists());
  180. }
  181. public void testDirset() {
  182. executeTarget("testDirset");
  183. }
  184. public void _testResourcePlain() {
  185. executeTarget("testResourcePlain");
  186. }
  187. public void _testResourcePlainWithMapper() {
  188. executeTarget("testResourcePlainWithMapper");
  189. }
  190. public void _testResourcePlainWithFilter() {
  191. executeTarget("testResourcePlainWithFilter");
  192. }
  193. public void _testOnlineResources() {
  194. executeTarget("testOnlineResources");
  195. }
  196. }