[14982] | 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 |
|
---|
| 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 | /** Utilities used for file operations */
|
---|
| 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 | /**
|
---|
| 55 | * No real test, simply checks whether the dateformat without
|
---|
| 56 | * seconds is accepted - by erroring out otherwise.
|
---|
| 57 | */
|
---|
| 58 | public void testNoSeconds() {
|
---|
| 59 | executeTarget("noSeconds");
|
---|
| 60 | long time = getTargetTime();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * No real test, simply checks whether the dateformat with
|
---|
| 65 | * seconds is accepted - by erroring out otherwise.
|
---|
| 66 | */
|
---|
| 67 | public void testSeconds() {
|
---|
| 68 | executeTarget("seconds");
|
---|
| 69 | long time=getTargetTime();
|
---|
| 70 | }
|
---|
| 71 | /**
|
---|
| 72 | * verify that the millis test sets things up
|
---|
| 73 | */
|
---|
| 74 | public void testMillis() {
|
---|
| 75 | touchFile("testMillis", 662256000000L);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * verify that the default value defaults to now
|
---|
| 80 | */
|
---|
| 81 | public void testNow() {
|
---|
| 82 | long now=System.currentTimeMillis();
|
---|
| 83 | executeTarget("testNow");
|
---|
| 84 | long time = getTargetTime();
|
---|
| 85 | assertTimesNearlyMatch(time,now,5000);
|
---|
| 86 | }
|
---|
| 87 | /**
|
---|
| 88 | * verify that the millis test sets things up
|
---|
| 89 | */
|
---|
| 90 | public void test2000() {
|
---|
| 91 | touchFile("test2000", 946080000000L);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * test the file list
|
---|
| 96 | */
|
---|
| 97 | public void testFilelist() {
|
---|
| 98 | touchFile("testFilelist", 662256000000L);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * test the file set
|
---|
| 103 | */
|
---|
| 104 | public void testFileset() {
|
---|
| 105 | touchFile("testFileset", 946080000000L);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * test the mapped file set
|
---|
| 110 | */
|
---|
| 111 | public void testMappedFileset() {
|
---|
| 112 | executeTarget("testMappedFileset");
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * test the explicit mapped file set
|
---|
| 117 | */
|
---|
| 118 | public void testExplicitMappedFileset() {
|
---|
| 119 | executeTarget("testExplicitMappedFileset");
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * test the mapped file list
|
---|
| 124 | */
|
---|
| 125 | public void testMappedFilelist() {
|
---|
| 126 | executeTarget("testMappedFilelist");
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * test the pattern attribute
|
---|
| 131 | */
|
---|
| 132 | public void testGoodPattern() {
|
---|
| 133 | executeTarget("testGoodPattern");
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * test the pattern attribute again
|
---|
| 138 | */
|
---|
| 139 | public void testBadPattern() {
|
---|
| 140 | expectBuildExceptionContaining("testBadPattern",
|
---|
| 141 | "No parsing exception thrown", "Unparseable");
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * run a target to touch the test file; verify the timestamp is as expected
|
---|
| 146 | * @param targetName
|
---|
| 147 | * @param timestamp
|
---|
| 148 | */
|
---|
| 149 | private void touchFile(String targetName, long timestamp) {
|
---|
| 150 | executeTarget(targetName);
|
---|
| 151 | long time = getTargetTime();
|
---|
| 152 | assertTimesNearlyMatch(timestamp, time);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * assert that two times are within the current FS granularity;
|
---|
| 157 | * @param timestamp
|
---|
| 158 | * @param time
|
---|
| 159 | */
|
---|
| 160 | public void assertTimesNearlyMatch(long timestamp,long time) {
|
---|
| 161 | long granularity= FILE_UTILS.getFileTimestampGranularity();
|
---|
| 162 | assertTimesNearlyMatch(timestamp, time, granularity);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * assert that two times are within a specified range
|
---|
| 167 | * @param timestamp
|
---|
| 168 | * @param time
|
---|
| 169 | * @param range
|
---|
| 170 | */
|
---|
| 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 | }
|
---|