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 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2000-2005 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. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.BuildFileTest;
  19. import org.apache.tools.ant.util.FileUtils;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23. /**
  24. * Tests FileSet using the Copy task.
  25. *
  26. */
  27. public class CopyTest extends BuildFileTest {
  28. /** Utilities used for file operations */
  29. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  30. public CopyTest(String name) {
  31. super(name);
  32. }
  33. public void setUp() {
  34. configureProject("src/etc/testcases/taskdefs/copy.xml");
  35. }
  36. public void test1() {
  37. executeTarget("test1");
  38. File f = new File(getProjectDir(), "copytest1.tmp");
  39. if ( !f.exists()) {
  40. fail("Copy failed");
  41. }
  42. }
  43. public void tearDown() {
  44. executeTarget("cleanup");
  45. }
  46. public void test2() {
  47. executeTarget("test2");
  48. File f = new File(getProjectDir(), "copytest1dir/copy.xml");
  49. if ( !f.exists()) {
  50. fail("Copy failed");
  51. }
  52. }
  53. public void test3() {
  54. executeTarget("test3");
  55. File file3 = new File(getProjectDir(), "copytest3.tmp");
  56. assertTrue(file3.exists());
  57. File file3a = new File(getProjectDir(), "copytest3a.tmp");
  58. assertTrue(file3a.exists());
  59. File file3b = new File(getProjectDir(), "copytest3b.tmp");
  60. assertTrue(file3b.exists());
  61. File file3c = new File(getProjectDir(), "copytest3c.tmp");
  62. assertTrue(file3c.exists());
  63. //file length checks rely on touch generating a zero byte file
  64. if(file3.length()==0) {
  65. fail("could not overwrite an existing, older file");
  66. }
  67. if(file3c.length()!=0) {
  68. fail("could not force overwrite an existing, newer file");
  69. }
  70. if(file3b.length()==0) {
  71. fail("unexpectedly overwrote an existing, newer file");
  72. }
  73. //file time checks for java1.2+
  74. assertTrue(file3a.lastModified()==file3.lastModified());
  75. assertTrue(file3c.lastModified()<file3a.lastModified());
  76. }
  77. public void testFilterTest() {
  78. executeTarget("filtertest");
  79. assertTrue(getOutput().indexOf("loop in tokens") == -1);
  80. }
  81. public void testInfiniteFilter() {
  82. executeTarget("infinitetest");
  83. assertTrue(getOutput().indexOf("loop in tokens") != -1);
  84. }
  85. public void testFilterSet() throws IOException {
  86. executeTarget("testFilterSet");
  87. File tmp = new File(getProjectDir(), "copy.filterset.tmp");
  88. File check = new File(getProjectDir(), "expected/copy.filterset.filtered");
  89. assertTrue(tmp.exists());
  90. assertTrue(FILE_UTILS.contentEquals(tmp, check));
  91. }
  92. public void testFilterChain() throws IOException {
  93. executeTarget("testFilterChain");
  94. File tmp = new File(getProjectDir(), "copy.filterchain.tmp");
  95. File check = new File(getProjectDir(), "expected/copy.filterset.filtered");
  96. assertTrue(tmp.exists());
  97. assertTrue(FILE_UTILS.contentEquals(tmp, check));
  98. }
  99. public void testSingleFileFileset() {
  100. executeTarget("test_single_file_fileset");
  101. File file = new File(getProjectDir(),
  102. "copytest_single_file_fileset.tmp");
  103. assertTrue(file.exists());
  104. }
  105. public void testSingleFilePath() {
  106. executeTarget("test_single_file_path");
  107. File file = new File(getProjectDir(),
  108. "copytest_single_file_path.tmp");
  109. assertTrue(file.exists());
  110. }
  111. public void testTranscoding() throws IOException {
  112. executeTarget("testTranscoding");
  113. File f1 = getProject().resolveFile("copy/expected/utf-8");
  114. File f2 = getProject().resolveFile("copytest1.tmp");
  115. assertTrue(FILE_UTILS.contentEquals(f1, f2));
  116. }
  117. public void testMissingFileIgnore() {
  118. expectLogContaining("testMissingFileIgnore",
  119. "Warning: Could not find file ");
  120. }
  121. public void testMissingFileBail() {
  122. expectBuildException("testMissingFileBail", "not-there doesn't exist");
  123. assertTrue(getBuildException().getMessage()
  124. .startsWith("Warning: Could not find file "));
  125. }
  126. public void testMissingDirIgnore() {
  127. expectLogContaining("testMissingDirIgnore", "Warning: ");
  128. }
  129. public void testMissingDirBail() {
  130. expectBuildException("testMissingDirBail", "not-there doesn't exist");
  131. assertTrue(getBuildException().getMessage().endsWith(" not found."));
  132. }
  133. public void testFileResourcePlain() {
  134. executeTarget("testFileResourcePlain");
  135. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt");
  136. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt");
  137. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt");
  138. assertTrue(file1.exists());
  139. assertTrue(file2.exists());
  140. assertTrue(file3.exists());
  141. }
  142. public void _testFileResourceWithMapper() {
  143. executeTarget("testFileResourceWithMapper");
  144. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt.bak");
  145. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt.bak");
  146. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt.bak");
  147. assertTrue(file1.exists());
  148. assertTrue(file2.exists());
  149. assertTrue(file3.exists());
  150. }
  151. public void testFileResourceWithFilter() {
  152. executeTarget("testFileResourceWithFilter");
  153. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/fileNR.txt");
  154. assertTrue(file1.exists());
  155. try {
  156. String file1Content = FILE_UTILS.readFully(new FileReader(file1));
  157. assertEquals("This is file 42", file1Content);
  158. } catch (IOException e) {
  159. // no-op: not a real business error
  160. }
  161. }
  162. public void testPathAsResource() {
  163. executeTarget("testPathAsResource");
  164. File file1 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file1.txt");
  165. File file2 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file2.txt");
  166. File file3 = new File(getProjectDir(), getProject().getProperty("to.dir")+"/file3.txt");
  167. assertTrue(file1.exists());
  168. assertTrue(file2.exists());
  169. assertTrue(file3.exists());
  170. }
  171. public void _testResourcePlain() {
  172. executeTarget("testResourcePlain");
  173. }
  174. public void _testResourcePlainWithMapper() {
  175. executeTarget("testResourcePlainWithMapper");
  176. }
  177. public void _testResourcePlainWithFilter() {
  178. executeTarget("testResourcePlainWithFilter");
  179. }
  180. public void _testOnlineResources() {
  181. executeTarget("testOnlineResources");
  182. }
  183. }