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.services.impl;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.net.URL;
020    
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.Resource;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.IAsset;
025    import org.apache.tapestry.asset.AssetSource;
026    import org.apache.tapestry.engine.ISpecificationSource;
027    import org.apache.tapestry.services.NamespaceResources;
028    import org.apache.tapestry.spec.IComponentSpecification;
029    import org.apache.tapestry.spec.ILibrarySpecification;
030    import org.testng.annotations.Test;
031    
032    /**
033     * Tests for {@link org.apache.tapestry.services.impl.NamespaceResourcesImpl}.
034     * 
035     * @author Howard M. Lewis Ship
036     * @since 4.0
037     */
038    @Test
039    public class TestNamespaceResources extends BaseComponentTestCase
040    {
041        protected Resource newResource()
042        {
043            return newMock(Resource.class);
044        }
045    
046        protected void trainGetRelativeResource(Resource parent, String path, Resource child)
047        {
048            expect(parent.getRelativeResource(path)).andReturn(child);
049        }
050    
051        protected void trainGetResourceURL(Resource resource, URL url)
052        {
053            expect(resource.getResourceURL()).andReturn(url);
054        }
055    
056        protected ISpecificationSource newSource()
057        {
058            return newMock(ISpecificationSource.class);
059        }
060    
061        protected ILibrarySpecification newLSpec()
062        {
063            return newMock(ILibrarySpecification.class);
064        }
065    
066        protected AssetSource newAssetSource()
067        {
068            return newMock(AssetSource.class);
069        }
070    
071        public void testFindChildLibrarySpecification()
072        {
073            Resource parent = newResource();
074            Resource child = newResource();
075            ISpecificationSource source = newSource();
076            ILibrarySpecification spec = newLSpec();
077            AssetSource assetSource = newAssetSource();
078            Location l = newLocation();
079    
080            trainResolveChildResource(assetSource, parent, "foo/bar.library", l, child);
081    
082            trainGetLibrarySpecification(source, child, spec);
083    
084            replay();
085    
086            NamespaceResources nr = new NamespaceResourcesImpl(source, assetSource);
087    
088            assertSame(spec, nr.findChildLibrarySpecification(parent, "foo/bar.library", l));
089    
090            verify();
091    
092        }
093    
094        protected void trainResolveChildResource(AssetSource assetSource, Resource parent,
095                String childPath, Location location, Resource child)
096        {
097            IAsset asset = newAsset();
098    
099            expect(assetSource.findAsset(parent, childPath, null, location)).andReturn(asset);
100    
101            expect(asset.getResourceLocation()).andReturn(child);
102        }
103    
104        protected IAsset newAsset()
105        {
106            return newMock(IAsset.class);
107        }
108    
109        protected URL newURL()
110        {
111            return getClass().getResource("TestNamespaceResources.class");
112        }
113    
114        protected void trainGetLibrarySpecification(ISpecificationSource source, Resource resource,
115                ILibrarySpecification spec)
116        {
117            expect(source.getLibrarySpecification(resource)).andReturn(spec);
118        }
119    
120        protected IComponentSpecification newComponentSpec()
121        {
122            return newMock(IComponentSpecification.class);
123        }
124    
125        public void test_Get_Page_Specification()
126        {
127            Resource libraryResource = newResource();
128            Resource specResource = newResource();
129            IComponentSpecification spec = newComponentSpec();
130            ISpecificationSource source = newSource();
131            AssetSource assetSource = newAssetSource();
132            Location l = newLocation();
133    
134            trainResolveChildResource(assetSource, libraryResource, "Foo.page", l, specResource);
135    
136            expect(source.getPageSpecification(specResource)).andReturn(spec);
137    
138            replay();
139    
140            NamespaceResources nr = new NamespaceResourcesImpl(source, assetSource);
141    
142            assertSame(spec, nr.getPageSpecification(libraryResource, "Foo.page", l));
143    
144            verify();
145        }
146    
147        public void test_Get_Component_Specification()
148        {
149            Resource libraryResource = newResource();
150            Resource specResource = newResource();
151            IComponentSpecification spec = newComponentSpec();
152            ISpecificationSource source = newSource();
153            AssetSource assetSource = newAssetSource();
154            Location l = newLocation();
155    
156            trainResolveChildResource(assetSource, libraryResource, "Foo.jwc", l, specResource);
157    
158            expect(source.getComponentSpecification(specResource)).andReturn(spec);
159    
160            replay();
161    
162            NamespaceResources nr = new NamespaceResourcesImpl(source, assetSource);
163    
164            assertSame(spec, nr.getComponentSpecification(libraryResource, "Foo.jwc", l));
165    
166            verify();
167        }
168    }