001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.BuildFileTest;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.util.FileUtils;
024:
025: import java.io.File;
026:
027: public class TouchTest extends BuildFileTest {
028:
029: private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest";
030:
031: /** Utilities used for file operations */
032: private static final FileUtils FILE_UTILS = FileUtils
033: .getFileUtils();
034:
035: public TouchTest(String name) {
036: super (name);
037: }
038:
039: public void setUp() {
040: configureProject("src/etc/testcases/taskdefs/touch.xml");
041: }
042:
043: public void tearDown() {
044: executeTarget("cleanup");
045: }
046:
047: public long getTargetTime() {
048:
049: File file = new File(System.getProperty("root"), TOUCH_FILE);
050: if (!file.exists()) {
051: throw new BuildException("failed to touch file " + file);
052: }
053: return file.lastModified();
054: }
055:
056: /**
057: * No real test, simply checks whether the dateformat without
058: * seconds is accepted - by erroring out otherwise.
059: */
060: public void testNoSeconds() {
061: executeTarget("noSeconds");
062: long time = getTargetTime();
063: }
064:
065: /**
066: * No real test, simply checks whether the dateformat with
067: * seconds is accepted - by erroring out otherwise.
068: */
069: public void testSeconds() {
070: executeTarget("seconds");
071: long time = getTargetTime();
072: }
073:
074: /**
075: * verify that the millis test sets things up
076: */
077: public void testMillis() {
078: touchFile("testMillis", 662256000000L);
079: }
080:
081: /**
082: * verify that the default value defaults to now
083: */
084: public void testNow() {
085: long now = System.currentTimeMillis();
086: executeTarget("testNow");
087: long time = getTargetTime();
088: assertTimesNearlyMatch(time, now, 5000);
089: }
090:
091: /**
092: * verify that the millis test sets things up
093: */
094: public void test2000() {
095: touchFile("test2000", 946080000000L);
096: }
097:
098: /**
099: * test the file list
100: */
101: public void testFilelist() {
102: touchFile("testFilelist", 662256000000L);
103: }
104:
105: /**
106: * test the file set
107: */
108: public void testFileset() {
109: touchFile("testFileset", 946080000000L);
110: }
111:
112: /**
113: * test the resource collection
114: */
115: public void testResourceCollection() {
116: touchFile("testResourceCollection", 1662256000000L);
117: }
118:
119: /**
120: * test the mapped file set
121: */
122: public void testMappedFileset() {
123: executeTarget("testMappedFileset");
124: }
125:
126: /**
127: * test the explicit mapped file set
128: */
129: public void testExplicitMappedFileset() {
130: executeTarget("testExplicitMappedFileset");
131: }
132:
133: /**
134: * test the mapped file list
135: */
136: public void testMappedFilelist() {
137: executeTarget("testMappedFilelist");
138: }
139:
140: /**
141: * test the pattern attribute
142: */
143: public void testGoodPattern() {
144: executeTarget("testGoodPattern");
145: }
146:
147: /**
148: * test the pattern attribute again
149: */
150: public void testBadPattern() {
151: expectBuildExceptionContaining("testBadPattern",
152: "No parsing exception thrown", "Unparseable");
153: }
154:
155: /**
156: * run a target to touch the test file; verify the timestamp is as expected
157: * @param targetName
158: * @param timestamp
159: */
160: private void touchFile(String targetName, long timestamp) {
161: executeTarget(targetName);
162: long time = getTargetTime();
163: assertTimesNearlyMatch(timestamp, time);
164: }
165:
166: /**
167: * assert that two times are within the current FS granularity;
168: * @param timestamp
169: * @param time
170: */
171: public void assertTimesNearlyMatch(long timestamp, long time) {
172: long granularity = FILE_UTILS.getFileTimestampGranularity();
173: assertTimesNearlyMatch(timestamp, time, granularity);
174: }
175:
176: /**
177: * assert that two times are within a specified range
178: * @param timestamp
179: * @param time
180: * @param range
181: */
182: private void assertTimesNearlyMatch(long timestamp, long time,
183: long range) {
184: assertTrue("Time " + timestamp + " is not within " + range
185: + " ms of " + time,
186: (Math.abs(time - timestamp) <= range));
187: }
188: }
|