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.

TouchTest.java 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2003-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.BuildException;
  20. import org.apache.tools.ant.util.FileUtils;
  21. import java.io.File;
  22. public class TouchTest extends BuildFileTest {
  23. private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest";
  24. /** Utilities used for file operations */
  25. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  26. public TouchTest(String name) {
  27. super(name);
  28. }
  29. public void setUp() {
  30. configureProject("src/etc/testcases/taskdefs/touch.xml");
  31. }
  32. public void tearDown() {
  33. executeTarget("cleanup");
  34. }
  35. public long getTargetTime() {
  36. File file = new File(System.getProperty("root"), TOUCH_FILE);
  37. if(!file.exists()) {
  38. throw new BuildException("failed to touch file " + file);
  39. }
  40. return file.lastModified();
  41. }
  42. /**
  43. * No real test, simply checks whether the dateformat without
  44. * seconds is accepted - by erroring out otherwise.
  45. */
  46. public void testNoSeconds() {
  47. executeTarget("noSeconds");
  48. long time = getTargetTime();
  49. }
  50. /**
  51. * No real test, simply checks whether the dateformat with
  52. * seconds is accepted - by erroring out otherwise.
  53. */
  54. public void testSeconds() {
  55. executeTarget("seconds");
  56. long time=getTargetTime();
  57. }
  58. /**
  59. * verify that the millis test sets things up
  60. */
  61. public void testMillis() {
  62. touchFile("testMillis", 662256000000L);
  63. }
  64. /**
  65. * verify that the default value defaults to now
  66. */
  67. public void testNow() {
  68. long now=System.currentTimeMillis();
  69. executeTarget("testNow");
  70. long time = getTargetTime();
  71. assertTimesNearlyMatch(time,now,5000);
  72. }
  73. /**
  74. * verify that the millis test sets things up
  75. */
  76. public void test2000() {
  77. touchFile("test2000", 946080000000L);
  78. }
  79. /**
  80. * test the file list
  81. */
  82. public void testFilelist() {
  83. touchFile("testFilelist", 662256000000L);
  84. }
  85. /**
  86. * test the file set
  87. */
  88. public void testFileset() {
  89. touchFile("testFileset", 946080000000L);
  90. }
  91. /**
  92. * test the resource collection
  93. */
  94. public void testResourceCollection() {
  95. touchFile("testResourceCollection", 1662256000000L);
  96. }
  97. /**
  98. * test the mapped file set
  99. */
  100. public void testMappedFileset() {
  101. executeTarget("testMappedFileset");
  102. }
  103. /**
  104. * test the explicit mapped file set
  105. */
  106. public void testExplicitMappedFileset() {
  107. executeTarget("testExplicitMappedFileset");
  108. }
  109. /**
  110. * test the mapped file list
  111. */
  112. public void testMappedFilelist() {
  113. executeTarget("testMappedFilelist");
  114. }
  115. /**
  116. * test the pattern attribute
  117. */
  118. public void testGoodPattern() {
  119. executeTarget("testGoodPattern");
  120. }
  121. /**
  122. * test the pattern attribute again
  123. */
  124. public void testBadPattern() {
  125. expectBuildExceptionContaining("testBadPattern",
  126. "No parsing exception thrown", "Unparseable");
  127. }
  128. /**
  129. * run a target to touch the test file; verify the timestamp is as expected
  130. * @param targetName
  131. * @param timestamp
  132. */
  133. private void touchFile(String targetName, long timestamp) {
  134. executeTarget(targetName);
  135. long time = getTargetTime();
  136. assertTimesNearlyMatch(timestamp, time);
  137. }
  138. /**
  139. * assert that two times are within the current FS granularity;
  140. * @param timestamp
  141. * @param time
  142. */
  143. public void assertTimesNearlyMatch(long timestamp,long time) {
  144. long granularity= FILE_UTILS.getFileTimestampGranularity();
  145. assertTimesNearlyMatch(timestamp, time, granularity);
  146. }
  147. /**
  148. * assert that two times are within a specified range
  149. * @param timestamp
  150. * @param time
  151. * @param range
  152. */
  153. private void assertTimesNearlyMatch(long timestamp, long time, long range) {
  154. assertTrue("Time " + timestamp + " is not within " + range + " ms of "
  155. + time, (Math.abs(time - timestamp) <= range));
  156. }
  157. }