001    // Copyright 2005 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.asset;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.net.URL;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.ClassResolver;
023    import org.apache.hivemind.impl.DefaultClassResolver;
024    import org.apache.tapestry.BaseComponentTestCase;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.asset.ResourceDigestSourceImpl}.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestResourceDigestSource extends BaseComponentTestCase
035    {
036        public void testSuccess()
037        {
038            ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
039            s.setClassResolver(new DefaultClassResolver());
040    
041            assertEquals("a5f4663532ea3efe22084df086482290", s
042                    .getDigestForResource("/org/apache/tapestry/asset/tapestry-in-action.png"));
043        }
044    
045        public void testMissing()
046        {
047            ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
048            s.setClassResolver(new DefaultClassResolver());
049    
050            try
051            {
052                s.getDigestForResource("/foo/bar/baz");
053                unreachable();
054            }
055            catch (ApplicationRuntimeException ex)
056            {
057                assertEquals("Classpath resource '/foo/bar/baz' does not exist.", ex.getMessage());
058            }
059        }
060    
061        public void testFolderInJar()
062        {
063            ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
064            s.setClassResolver(new DefaultClassResolver());
065            // the next will throw a NPE inside JarURLInputStream.close
066            // there doesn't seem to be a way to prevent this - so users
067            // of this method should be aware
068            try
069            {
070                s.getDigestForResource("/org/apache/hivemind");
071                fail("digests for packages in jars are known to throw NPE");
072            }
073            catch (NullPointerException e)
074            {
075                // ignore
076            }
077        }
078    
079        public void testCache()
080        {
081            ClassResolver resolver = newMock(ClassResolver.class);
082    
083            URL url = getClass().getResource("tapestry-in-action.png");
084    
085            expect(resolver.getResource("/foo")).andReturn(url);
086    
087            replay();
088    
089            ResourceDigestSourceImpl s = new ResourceDigestSourceImpl();
090            s.setClassResolver(resolver);
091    
092            assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
093    
094            // Try it in the cache; note that the class resolver is not
095            // invoked this time.
096    
097            assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
098    
099            verify();
100    
101            expect(resolver.getResource("/foo")).andReturn(url);
102    
103            replay();
104    
105            // This clears the cache
106    
107            s.resetEventDidOccur();
108    
109            // So this goes to the ClassResolver
110    
111            assertEquals("a5f4663532ea3efe22084df086482290", s.getDigestForResource("/foo"));
112    
113            verify();
114    
115        }
116    }