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.services;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.net.URL;
020:
021: import org.apache.tapestry.internal.events.InvalidationListener;
022: import org.apache.tapestry.internal.test.InternalBaseTestCase;
023: import org.apache.tapestry.ioc.Resource;
024: import org.apache.tapestry.services.ResourceDigestGenerator;
025: import org.testng.annotations.Test;
026:
027: public class ResourceCacheImplTest extends InternalBaseTestCase {
028: private static final String PATH = "foo/Bar.gif";
029:
030: private static final String DIGEST = "abc123";
031:
032: @Test
033: public void properties_for_simple_resource() throws Exception {
034: ResourceDigestGenerator generator = mockResourceDigestGenerator();
035:
036: File f = createTestFile();
037: URL url = f.toURL();
038: Resource r = mockResource();
039:
040: long lastUpdated = f.lastModified();
041: lastUpdated -= lastUpdated % 1000;
042:
043: train_getPath(r, PATH);
044: train_toURL(r, url);
045:
046: train_requiresDigest(generator, PATH, false);
047:
048: replay();
049:
050: ResourceCacheImpl cache = new ResourceCacheImpl(generator);
051:
052: assertEquals(cache.requiresDigest(r), false);
053: assertEquals(cache.getTimeModified(r), lastUpdated);
054: assertEquals(cache.getDigest(r), null);
055:
056: verify();
057: }
058:
059: @Test
060: public void properties_for_missing_resource() throws Exception {
061: ResourceDigestGenerator generator = mockResourceDigestGenerator();
062:
063: Resource r = mockResource();
064:
065: train_getPath(r, PATH);
066: train_toURL(r, null);
067:
068: train_requiresDigest(generator, PATH, true);
069:
070: replay();
071:
072: ResourceCacheImpl cache = new ResourceCacheImpl(generator);
073:
074: assertEquals(cache.requiresDigest(r), true);
075: assertEquals(cache.getTimeModified(r),
076: ResourceCacheImpl.MISSING_RESOURCE_TIME_MODIFIED);
077: assertEquals(cache.getDigest(r), null);
078:
079: verify();
080: }
081:
082: @Test
083: public void properties_for_protected_resource() throws Exception {
084: ResourceDigestGenerator generator = mockResourceDigestGenerator();
085:
086: File f = createTestFile();
087: URL url = f.toURL();
088: Resource r = mockResource();
089:
090: long lastUpdated = f.lastModified();
091: lastUpdated -= lastUpdated % 1000;
092:
093: train_getPath(r, PATH);
094: train_toURL(r, url);
095:
096: train_requiresDigest(generator, PATH, true);
097: train_generateChecksum(generator, url, DIGEST);
098:
099: replay();
100:
101: ResourceCacheImpl cache = new ResourceCacheImpl(generator);
102:
103: assertEquals(cache.requiresDigest(r), true);
104: assertEquals(cache.getTimeModified(r), lastUpdated);
105: assertEquals(cache.getDigest(r), DIGEST);
106:
107: verify();
108: }
109:
110: @Test
111: public void caching_and_invalidation() throws Exception {
112: ResourceDigestGenerator generator = mockResourceDigestGenerator();
113: InvalidationListener listener = mockInvalidationListener();
114: File f = createTestFile();
115: URL url = f.toURL();
116: Resource r = mockResource();
117:
118: long lastUpdated = f.lastModified();
119: lastUpdated -= lastUpdated % 1000;
120:
121: train_getPath(r, PATH);
122: train_toURL(r, url);
123:
124: train_requiresDigest(generator, PATH, true);
125: train_generateChecksum(generator, url, DIGEST);
126:
127: replay();
128:
129: ResourceCacheImpl cache = new ResourceCacheImpl(generator);
130: cache.addInvalidationListener(listener);
131:
132: assertEquals(cache.requiresDigest(r), true);
133: assertEquals(cache.getTimeModified(r), lastUpdated);
134: assertEquals(cache.getDigest(r), DIGEST);
135:
136: // No updates yet.
137:
138: cache.checkForUpdates();
139:
140: verify();
141:
142: Thread.sleep(1500);
143: touch(f);
144:
145: lastUpdated = f.lastModified();
146: lastUpdated -= lastUpdated % 1000;
147:
148: String expectedDigest = "FREDBARNEY";
149:
150: train_getPath(r, PATH);
151: train_toURL(r, url);
152: train_requiresDigest(generator, PATH, true);
153: train_generateChecksum(generator, url, expectedDigest);
154:
155: listener.objectWasInvalidated();
156:
157: replay();
158:
159: cache.checkForUpdates();
160:
161: assertEquals(cache.requiresDigest(r), true);
162: assertEquals(cache.getTimeModified(r), lastUpdated);
163: assertEquals(cache.getDigest(r), expectedDigest);
164:
165: verify();
166: }
167:
168: private File createTestFile() throws IOException {
169: return File.createTempFile("ResourceCacheImplTest.", ".tst");
170: }
171: }
|