001    // Copyright 2004, 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.util.Collections;
020    import java.util.List;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.hivemind.Location;
024    import org.apache.hivemind.ServiceImplementationFactoryParameters;
025    import org.apache.hivemind.lib.DefaultImplementationBuilder;
026    import org.apache.tapestry.BaseComponentTestCase;
027    import org.apache.tapestry.spec.IApplicationSpecification;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Tests {@link org.apache.tapestry.services.impl.ExtensionLookupFactory}.
032     * 
033     * @author Howard Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestExtensionLookupFactory extends BaseComponentTestCase
038    {
039        private List createParameters(String extensionName)
040        {
041            return createParameters(extensionName, null);
042        }
043    
044        private List createParameters(String extensionName, Object defaultValue)
045        {
046            ExtensionLookupParameter p = new ExtensionLookupParameter();
047    
048            p.setExtensionName(extensionName);
049            p.setDefault(defaultValue);
050    
051            return Collections.singletonList(p);
052        }
053    
054        public void testInSpecification()
055        {
056            IApplicationSpecification spec = newMock(IApplicationSpecification.class);
057    
058            Runnable r = newMock(Runnable.class);
059            
060            ServiceImplementationFactoryParameters fp = newMock(ServiceImplementationFactoryParameters.class);
061    
062            // Training
063    
064            expect(fp.getParameters()).andReturn(createParameters("foo.bar"));
065    
066            expect(fp.getServiceInterface()).andReturn(Runnable.class);
067    
068            expect(spec.checkExtension("foo.bar")).andReturn(true);
069    
070            expect(spec.getExtension("foo.bar", Runnable.class)).andReturn(r);
071    
072            replay();
073    
074            ExtensionLookupFactory f = new ExtensionLookupFactory();
075            f.setSpecification(spec);
076    
077            Object actual = f.createCoreServiceImplementation(fp);
078    
079            assertSame(r, actual);
080    
081            verify();
082        }
083    
084        public void testSyntheticDefault()
085        {
086            IApplicationSpecification spec = newMock(IApplicationSpecification.class);
087            
088            DefaultImplementationBuilder dib = newMock(DefaultImplementationBuilder.class);
089    
090            Runnable r = newMock(Runnable.class);
091    
092            ServiceImplementationFactoryParameters fp = newMock(ServiceImplementationFactoryParameters.class);
093    
094            // Training
095    
096            expect(fp.getParameters()).andReturn(createParameters("foo.bar"));
097    
098            expect(fp.getServiceInterface()).andReturn(Runnable.class);
099            
100            expect(spec.checkExtension("foo.bar")).andReturn(false);
101    
102            expect(dib.buildDefaultImplementation(Runnable.class)).andReturn(r);
103    
104            replay();
105    
106            ExtensionLookupFactory f = new ExtensionLookupFactory();
107            f.setSpecification(spec);
108            f.setDefaultBuilder(dib);
109    
110            Object actual = f.createCoreServiceImplementation(fp);
111    
112            assertSame(r, actual);
113    
114            verify();
115        }
116    
117        public void testConfigurationDefault()
118        {
119            IApplicationSpecification spec = newMock(IApplicationSpecification.class);
120    
121            Runnable r = newMock(Runnable.class);
122    
123            ServiceImplementationFactoryParameters fp = newMock(ServiceImplementationFactoryParameters.class);
124    
125            // Training
126    
127            expect(fp.getParameters()).andReturn(createParameters("foo.bar", r));
128    
129            expect(fp.getServiceInterface()).andReturn(Runnable.class);
130    
131            expect(spec.checkExtension("foo.bar")).andReturn(false);
132    
133            replay();
134    
135            ExtensionLookupFactory f = new ExtensionLookupFactory();
136            f.setSpecification(spec);
137    
138            Object actual = f.createCoreServiceImplementation(fp);
139    
140            assertSame(r, actual);
141    
142            verify();
143        }
144    
145        public void testFailure()
146        {
147            Location l = fabricateLocation(264);
148            ExtensionLookupParameter p = new ExtensionLookupParameter();
149    
150            p.setLocation(l);
151            p.setExtensionName("gnip.gnop");
152    
153            ServiceImplementationFactoryParameters fp = newMock(ServiceImplementationFactoryParameters.class);
154    
155            expect(fp.getParameters()).andReturn(Collections.singletonList(p));
156    
157            expect(fp.getServiceInterface()).andReturn(null);
158    
159            ExtensionLookupFactory f = new ExtensionLookupFactory();
160    
161            replay();
162    
163            try
164            {
165                f.createCoreServiceImplementation(fp);
166    
167                unreachable();
168            }
169            catch (ApplicationRuntimeException ex)
170            {
171                assertSame(l, ex.getLocation());
172            }
173    
174            verify();
175        }
176    }