1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.BuildFileTest; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.util.FileUtils; 23 24 import java.io.File ; 25 26 public class TouchTest extends BuildFileTest { 27 28 private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest"; 29 30 31 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 32 33 public TouchTest(String name) { 34 super(name); 35 } 36 37 public void setUp() { 38 configureProject("src/etc/testcases/taskdefs/touch.xml"); 39 } 40 41 public void tearDown() { 42 executeTarget("cleanup"); 43 } 44 45 public long getTargetTime() { 46 47 File file = new File (System.getProperty("root"), TOUCH_FILE); 48 if(!file.exists()) { 49 throw new BuildException("failed to touch file " + file); 50 } 51 return file.lastModified(); 52 } 53 54 58 public void testNoSeconds() { 59 executeTarget("noSeconds"); 60 long time = getTargetTime(); 61 } 62 63 67 public void testSeconds() { 68 executeTarget("seconds"); 69 long time=getTargetTime(); 70 } 71 74 public void testMillis() { 75 touchFile("testMillis", 662256000000L); 76 } 77 78 81 public void testNow() { 82 long now=System.currentTimeMillis(); 83 executeTarget("testNow"); 84 long time = getTargetTime(); 85 assertTimesNearlyMatch(time,now,5000); 86 } 87 90 public void test2000() { 91 touchFile("test2000", 946080000000L); 92 } 93 94 97 public void testFilelist() { 98 touchFile("testFilelist", 662256000000L); 99 } 100 101 104 public void testFileset() { 105 touchFile("testFileset", 946080000000L); 106 } 107 108 111 public void testMappedFileset() { 112 executeTarget("testMappedFileset"); 113 } 114 115 118 public void testExplicitMappedFileset() { 119 executeTarget("testExplicitMappedFileset"); 120 } 121 122 125 public void testMappedFilelist() { 126 executeTarget("testMappedFilelist"); 127 } 128 129 132 public void testGoodPattern() { 133 executeTarget("testGoodPattern"); 134 } 135 136 139 public void testBadPattern() { 140 expectBuildExceptionContaining("testBadPattern", 141 "No parsing exception thrown", "Unparseable"); 142 } 143 144 149 private void touchFile(String targetName, long timestamp) { 150 executeTarget(targetName); 151 long time = getTargetTime(); 152 assertTimesNearlyMatch(timestamp, time); 153 } 154 155 160 public void assertTimesNearlyMatch(long timestamp,long time) { 161 long granularity= FILE_UTILS.getFileTimestampGranularity(); 162 assertTimesNearlyMatch(timestamp, time, granularity); 163 } 164 165 171 private void assertTimesNearlyMatch(long timestamp, long time, long range) { 172 assertTrue("Time " + timestamp + " is not within " + range + " ms of " 173 + time, (Math.abs(time - timestamp) <= range)); 174 } 175 } 176 | Popular Tags |