01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.io.BufferedOutputStream;
18: import java.io.File;
19: import java.io.FileOutputStream;
20: import java.io.IOException;
21: import java.io.OutputStream;
22: import java.io.OutputStreamWriter;
23: import java.net.URL;
24:
25: import org.apache.tapestry.internal.test.InternalBaseTestCase;
26: import org.apache.tapestry.services.ResourceDigestGenerator;
27: import org.testng.annotations.Test;
28:
29: public class ResourceDigestGeneratorImplTest extends
30: InternalBaseTestCase {
31: @Test
32: public void typical_file() throws Exception {
33: URL url = getClass().getResource(
34: "ResourceDigestGeneratorImplTest.class");
35:
36: ResourceDigestGenerator g = new ResourceDigestGeneratorImpl();
37:
38: String digest = g.generateDigest(url);
39:
40: assertTrue(digest.length() > 0);
41:
42: String checksum2 = g.generateDigest(url);
43:
44: assertEquals(checksum2, digest);
45: }
46:
47: @Test
48: public void digest_changes_with_changes_to_file() throws Exception {
49: File file = File.createTempFile("digest.", ".dat");
50:
51: URL url = file.toURL();
52:
53: ResourceDigestGenerator g = new ResourceDigestGeneratorImpl();
54:
55: String prevDigest = g.generateDigest(url);
56:
57: for (int i = 0; i < 5; i++) {
58: writeJunkTofile(file);
59:
60: String digest = g.generateDigest(url);
61:
62: assertFalse(digest.equals(prevDigest));
63:
64: prevDigest = digest;
65:
66: }
67: }
68:
69: private void writeJunkTofile(File file) throws IOException {
70: OutputStream os = new BufferedOutputStream(
71: new FileOutputStream(file, true));
72:
73: OutputStreamWriter writer = new OutputStreamWriter(os);
74:
75: for (int i = 0; i < 1000; i++)
76: writer
77: .write("All work and no play makes Jack a dull boy. ");
78:
79: writer.close();
80: }
81: }
|