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.

ConcatTest.java 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. import java.io.Reader;
  25. /**
  26. * A test class for the 'concat' task, used to concatenate a series of
  27. * files into a single stream.
  28. *
  29. */
  30. public class ConcatTest
  31. extends BuildFileTest {
  32. /**
  33. * The name of the temporary file.
  34. */
  35. private static final String tempFile = "concat.tmp";
  36. /**
  37. * The name of the temporary file.
  38. */
  39. private static final String tempFile2 = "concat.tmp.2";
  40. /** Utilities used for file operations */
  41. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  42. /**
  43. * Required constructor.
  44. */
  45. public ConcatTest(String name) {
  46. super(name);
  47. }
  48. /**
  49. * Test set up, called by the unit test framework prior to each
  50. * test.
  51. */
  52. public void setUp() {
  53. configureProject("src/etc/testcases/taskdefs/concat.xml");
  54. }
  55. /**
  56. * Test tear down, called by the unit test framework prior to each
  57. * test.
  58. */
  59. public void tearDown() {
  60. executeTarget("cleanup");
  61. }
  62. /**
  63. * Expect an exception when insufficient information is provided.
  64. */
  65. public void test1() {
  66. expectBuildException("test1", "Insufficient information.");
  67. }
  68. /**
  69. * Expect an exception when the destination file is invalid.
  70. */
  71. public void test2() {
  72. expectBuildException("test2", "Invalid destination file.");
  73. }
  74. /**
  75. * Cats the string 'Hello, World!' to a temporary file.
  76. */
  77. public void test3() {
  78. File file = new File(getProjectDir(), tempFile);
  79. if (file.exists()) {
  80. file.delete();
  81. }
  82. executeTarget("test3");
  83. assertTrue(file.exists());
  84. }
  85. /**
  86. * Cats the file created in test3 three times.
  87. */
  88. public void test4() {
  89. test3();
  90. File file = new File(getProjectDir(), tempFile);
  91. final long origSize = file.length();
  92. executeTarget("test4");
  93. File file2 = new File(getProjectDir(), tempFile2);
  94. final long newSize = file2.length();
  95. assertEquals(origSize * 3, newSize);
  96. }
  97. /**
  98. * Cats the string 'Hello, World!' to the console.
  99. */
  100. public void test5() {
  101. expectLog("test5", "Hello, World!");
  102. }
  103. public void test6() {
  104. String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist"
  105. .replace('/', File.separatorChar);
  106. expectLogContaining("test6", filename +" does not exist.");
  107. }
  108. public void testConcatNoNewline() {
  109. expectLog("testConcatNoNewline", "ab");
  110. }
  111. public void testConcatNoNewlineEncoding() {
  112. expectLog("testConcatNoNewlineEncoding", "ab");
  113. }
  114. public void testPath() {
  115. test3();
  116. File file = new File(getProjectDir(), tempFile);
  117. final long origSize = file.length();
  118. executeTarget("testPath");
  119. File file2 = new File(getProjectDir(), tempFile2);
  120. final long newSize = file2.length();
  121. assertEquals(origSize, newSize);
  122. }
  123. public void testAppend() {
  124. test3();
  125. File file = new File(getProjectDir(), tempFile);
  126. final long origSize = file.length();
  127. executeTarget("testAppend");
  128. File file2 = new File(getProjectDir(), tempFile2);
  129. final long newSize = file2.length();
  130. assertEquals(origSize*2, newSize);
  131. }
  132. public void testFilter() {
  133. executeTarget("testfilter");
  134. assertTrue(getLog().indexOf("REPLACED") > -1);
  135. }
  136. public void testNoOverwrite() {
  137. executeTarget("testnooverwrite");
  138. File file2 = new File(getProjectDir(), tempFile2);
  139. long size = file2.length();
  140. assertEquals(size, 0);
  141. }
  142. public void testheaderfooter() {
  143. test3();
  144. expectLog("testheaderfooter", "headerHello, World!footer");
  145. }
  146. public void testfileheader() {
  147. test3();
  148. expectLog("testfileheader", "Hello, World!Hello, World!");
  149. }
  150. /**
  151. * Expect an exception when attempting to cat an file to itself
  152. */
  153. public void testsame() {
  154. expectBuildException("samefile", "output file same as input");
  155. }
  156. /**
  157. * Check if filter inline works
  158. */
  159. public void testfilterinline() {
  160. executeTarget("testfilterinline");
  161. assertTrue(getLog().indexOf("REPLACED") > -1);
  162. }
  163. /**
  164. * Check if multireader works
  165. */
  166. public void testmultireader() {
  167. executeTarget("testmultireader");
  168. assertTrue(getLog().indexOf("Bye") > -1);
  169. assertTrue(getLog().indexOf("Hello") == -1);
  170. }
  171. /**
  172. * Check if fixlastline works
  173. */
  174. public void testfixlastline()
  175. throws IOException
  176. {
  177. expectFileContains(
  178. "testfixlastline", "concat.line4",
  179. "end of line" + System.getProperty("line.separator")
  180. + "This has");
  181. }
  182. /**
  183. * Check if fixlastline works with eol
  184. */
  185. public void testfixlastlineeol()
  186. throws IOException
  187. {
  188. expectFileContains(
  189. "testfixlastlineeol", "concat.linecr",
  190. "end of line\rThis has");
  191. }
  192. // ------------------------------------------------------
  193. // Helper methods - should be in BuildFileTest
  194. // -----------------------------------------------------
  195. private String getFileString(String filename)
  196. throws IOException
  197. {
  198. Reader r = null;
  199. try {
  200. r = new FileReader(getProject().resolveFile(filename));
  201. return FileUtils.readFully(r);
  202. }
  203. finally {
  204. FileUtils.close(r);
  205. }
  206. }
  207. private String getFileString(String target, String filename)
  208. throws IOException
  209. {
  210. executeTarget(target);
  211. return getFileString(filename);
  212. }
  213. private void expectFileContains(
  214. String target, String filename, String contains)
  215. throws IOException
  216. {
  217. String content = getFileString(target, filename);
  218. assertTrue(
  219. "expecting file " + filename + " to contain " +
  220. contains +
  221. " but got " + content, content.indexOf(contains) > -1);
  222. }
  223. public void testTranscoding() throws IOException {
  224. executeTarget("testTranscoding");
  225. File f1 = getProject().resolveFile("copy/expected/utf-8");
  226. File f2 = getProject().resolveFile("concat.utf8");
  227. assertTrue(FILE_UTILS.contentEquals(f1, f2));
  228. }
  229. public void testResources() {
  230. executeTarget("testResources");
  231. }
  232. }