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.enhance;
016    
017    import org.apache.hivemind.ErrorLog;
018    import org.apache.hivemind.Location;
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.html.BasePage;
021    import org.apache.tapestry.spec.IComponentSpecification;
022    import org.apache.tapestry.spec.InjectSpecification;
023    import org.apache.tapestry.spec.InjectSpecificationImpl;
024    import static org.easymock.EasyMock.expect;
025    import static org.easymock.EasyMock.expectLastCall;
026    import org.testng.annotations.Test;
027    
028    import java.util.Collections;
029    import java.util.Map;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.enhance.DispatchToInjectWorker}.
033     *
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class TestDispatchToInjectWorker extends BaseComponentTestCase
039    {
040        private InjectSpecification newInjectSpecification(String propertyName, String type,
041                                                           String object)
042        {
043            return newInjectSpecification(propertyName, type, object, null);
044        }
045    
046        private InjectSpecification newInjectSpecification(String propertyName, String type,
047                                                           String object, Location location)
048        {
049            InjectSpecification result = new InjectSpecificationImpl();
050            result.setProperty(propertyName);
051            result.setType(type);
052            result.setObject(object);
053            result.setLocation(location);
054    
055            return result;
056        }
057    
058        private IComponentSpecification newSpec(InjectSpecification injectSpec)
059        {
060            IComponentSpecification spec = newSpec();
061    
062            expect(spec.getInjectSpecifications()).andReturn(Collections.singletonList(injectSpec));
063    
064            return spec;
065        }
066    
067        private Map newMap(String key, Object value)
068        {
069            return Collections.singletonMap(key, value);
070        }
071    
072        public void test_Success()
073        {
074            EnhancementOperation op = newOp();
075            InjectSpecification is = newInjectSpecification("property", "object", "service:Foo");
076            InjectEnhancementWorker worker = newWorker();
077            Map map = newMap("object", worker);
078            IComponentSpecification spec = newSpec(is);
079    
080            worker.performEnhancement(op, is);
081    
082            replay();
083    
084            DispatchToInjectWorker d = new DispatchToInjectWorker();
085            d.setInjectWorkers(map);
086    
087            d.performEnhancement(op, spec);
088    
089            verify();
090        }
091    
092        public void test_Unknown_Type()
093        {
094            Location l = newLocation();
095            EnhancementOperation op = newOp();
096            InjectSpecification is = newInjectSpecification(
097              "injectedProperty",
098              "object",
099              "service:Foo",
100              l);
101            IComponentSpecification spec = newSpec(is);
102            ErrorLog log = newErrorLog();
103    
104            log.error(EnhanceMessages.unknownInjectType("injectedProperty", "object"), l, null);
105    
106            replay();
107    
108            DispatchToInjectWorker d = new DispatchToInjectWorker();
109            d.setInjectWorkers(Collections.EMPTY_MAP);
110            d.setErrorLog(log);
111    
112            d.performEnhancement(op, spec);
113    
114            verify();
115        }
116    
117        public void test_Failure()
118        {
119            Location l = newLocation();
120    
121            EnhancementOperation op = newOp();
122            InjectSpecification is = newInjectSpecification("myProperty", "object", "service:Foo", l);
123            InjectEnhancementWorker worker = newMock(InjectEnhancementWorker.class);
124    
125            Map map = newMap("object", worker);
126            IComponentSpecification spec = newSpec(is);
127    
128            Throwable t = new RuntimeException("Simulated failure.");
129            ErrorLog log = newErrorLog();
130    
131            worker.performEnhancement(op, is);
132            expectLastCall().andThrow(t);
133    
134            expect(op.getBaseClass()).andReturn(BasePage.class);
135    
136            log
137              .error(
138                "Error adding property myProperty to class org.apache.tapestry.html.BasePage: Simulated failure.",
139                l,
140                t);
141    
142            replay();
143    
144            DispatchToInjectWorker d = new DispatchToInjectWorker();
145            d.setInjectWorkers(map);
146            d.setErrorLog(log);
147    
148            d.performEnhancement(op, spec);
149    
150            verify();
151        }
152    
153        private InjectEnhancementWorker newWorker()
154        {
155            return newMock(InjectEnhancementWorker.class);
156        }
157    
158        private ErrorLog newErrorLog()
159        {
160            return newMock(ErrorLog.class);
161        }
162    
163        private EnhancementOperation newOp()
164        {
165            return newMock(EnhancementOperation.class);
166        }
167    }