001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.util;
016:
017: import java.io.File;
018: import java.net.URL;
019:
020: import org.apache.tapestry.test.TapestryTestCase;
021: import org.testng.annotations.Test;
022:
023: public class URLChangeTrackerTest extends TapestryTestCase {
024: @Test
025: public void contains_change_when_empty() {
026: URLChangeTracker t = new URLChangeTracker();
027:
028: assertFalse(t.containsChanges());
029: }
030:
031: @Test
032: public void contains_changes() throws Exception {
033: URLChangeTracker t = new URLChangeTracker();
034:
035: File f = File.createTempFile("changetracker0", ".tmp");
036: URL url = f.toURL();
037:
038: t.add(url);
039:
040: assertEquals(t.trackedFileCount(), 1);
041:
042: assertFalse(t.containsChanges());
043:
044: boolean changed = false;
045:
046: // Because of clock accuracy, we need to try a couple of times
047: // to ensure that the change to the file is visible in the
048: // lastUpdated time stamp on the URL.
049:
050: for (int i = 0; i < 10 && !changed; i++) {
051: Thread.sleep(100);
052:
053: touch(f);
054:
055: changed = t.containsChanges();
056: }
057:
058: assertTrue(changed);
059:
060: // And, once a change has been observed ...
061:
062: assertFalse(t.containsChanges());
063: }
064:
065: @Test
066: public void non_file_URLs_are_ignored() throws Exception {
067: URLChangeTracker t = new URLChangeTracker();
068:
069: URL url = new URL("ftp://breeblebrox.com");
070:
071: t.add(url);
072:
073: assertEquals(t.trackedFileCount(), 0);
074: }
075:
076: @Test
077: public void caching() throws Exception {
078: URLChangeTracker t = new URLChangeTracker();
079:
080: File f = File.createTempFile("changetracker0", ".tmp");
081: URL url = f.toURL();
082:
083: long initial = t.add(url);
084:
085: touch(f);
086:
087: long current = t.add(url);
088:
089: assertEquals(current, initial);
090:
091: assertTrue(t.containsChanges());
092:
093: t.clear();
094:
095: current = t.add(url);
096:
097: assertFalse(current == initial);
098: }
099:
100: @Test
101: public void deleted_files_show_as_changes() throws Exception {
102: File f = File.createTempFile("changetracker0", ".tmp");
103: URL url = f.toURL();
104:
105: URLChangeTracker t = new URLChangeTracker();
106:
107: long timeModified = t.add(url);
108:
109: assertTrue(timeModified > 0);
110:
111: assertEquals(t.trackedFileCount(), 1);
112:
113: assertFalse(t.containsChanges());
114:
115: assertTrue(f.delete());
116:
117: assertTrue(t.containsChanges());
118: }
119:
120: @Test
121: public void second_level_granularity() throws Exception {
122: URLChangeTracker t = new URLChangeTracker(true);
123:
124: File f = File.createTempFile("changetracker0", ".tmp");
125: URL url = f.toURL();
126:
127: touch(f);
128: long timestamp1 = t.add(url);
129: assertEquals(0, timestamp1 % 1000);
130: assertFalse(t.containsChanges());
131:
132: Thread.sleep(1500);
133:
134: touch(f);
135: long timestamp2 = t.add(url);
136: assertEquals(0, timestamp2 % 1000);
137: assertTrue(t.containsChanges());
138: }
139:
140: }
|