KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > asset > TestResourceDigestSource


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.asset;
16
17 import java.net.URL JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ClassResolver;
21 import org.apache.hivemind.impl.DefaultClassResolver;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests for {@link org.apache.tapestry.asset.ResourceDigestSourceImpl}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestResourceDigestSource extends HiveMindTestCase
32 {
33     public void testSuccess()
34     {
35         ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
36         s.setClassResolver(new DefaultClassResolver());
37
38         assertEquals("a5f4663532ea3efe22084df086482290", s
39                 .getDigestForResource("/org/apache/tapestry/asset/tapestry-in-action.png"));
40     }
41
42     public void testMissing()
43     {
44         ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
45         s.setClassResolver(new DefaultClassResolver());
46
47         try
48         {
49             s.getDigestForResource("/foo/bar/baz");
50             unreachable();
51         }
52         catch (ApplicationRuntimeException ex)
53         {
54             assertEquals("Classpath resource '/foo/bar/baz' does not exist.", ex.getMessage());
55         }
56     }
57
58     public void testCache()
59     {
60         MockControl control = newControl(ClassResolver.class);
61         ClassResolver resolver = (ClassResolver) control.getMock();
62
63         URL JavaDoc url = getClass().getResource("tapestry-in-action.png");
64
65         resolver.getResource("/foo");
66         control.setReturnValue(url);
67
68         replayControls();
69
70         ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
71         s.setClassResolver(resolver);
72
73         assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
74
75         // Try it in the cache; note that the class resolver is not
76
// invoked this time.
77

78         assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
79
80         verifyControls();
81
82         resolver.getResource("/foo");
83         control.setReturnValue(url);
84
85         replayControls();
86
87         // This clears the cache
88

89         s.resetEventDidOccur();
90
91         // So this goes to the ClassResolver
92

93         assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
94
95         verifyControls();
96
97     }
98 }
Popular Tags