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.Locale;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.ErrorLog;
023    import org.apache.hivemind.impl.DefaultClassResolver;
024    import org.apache.tapestry.BaseComponentTestCase;
025    import org.apache.tapestry.IEngine;
026    import org.apache.tapestry.engine.AbstractEngine;
027    import org.apache.tapestry.engine.BaseEngine;
028    import org.apache.tapestry.spec.IApplicationSpecification;
029    import org.testng.annotations.Test;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.services.impl.EngineFactoryImpl}.
033     * 
034     * @author Howard Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class EngineFactoryTest extends BaseComponentTestCase
039    {
040        public void testUseDefault()
041        {
042            IApplicationSpecification spec = newAppSpec();
043    
044            // Training
045    
046            trainGetEngineClassName(spec, null);
047    
048            EngineFactoryImpl f = new EngineFactoryImpl();
049    
050            f.setApplicationSpecification(spec);
051            f.setClassResolver(new DefaultClassResolver());
052            f.setDefaultEngineClassName(BaseEngine.class.getName());
053    
054            replay();
055    
056            f.initializeService();
057    
058            IEngine result = f.constructNewEngineInstance(Locale.CANADA_FRENCH);
059    
060            assertTrue(result instanceof BaseEngine);
061            assertEquals(Locale.CANADA_FRENCH, result.getLocale());
062    
063            verify();
064        }
065    
066        private void trainGetEngineClassName(IApplicationSpecification spec, String engineClassName)
067        {
068            expect(spec.getEngineClassName()).andReturn(engineClassName);
069        }
070    
071        private IApplicationSpecification newAppSpec()
072        {
073            return newMock(IApplicationSpecification.class);
074        }
075    
076        public void testDefinedInSpec()
077        {
078            IApplicationSpecification spec = newAppSpec();
079    
080            trainGetEngineClassName(spec, EngineFixture.class.getName());
081    
082            EngineFactoryImpl f = new EngineFactoryImpl();
083    
084            f.setApplicationSpecification(spec);
085            f.setClassResolver(new DefaultClassResolver());
086    
087            replay();
088    
089            f.initializeService();
090    
091            IEngine result = f.constructNewEngineInstance(Locale.CHINESE);
092    
093            assertTrue(result instanceof EngineFixture);
094            assertEquals(Locale.CHINESE, result.getLocale());
095    
096            verify();
097        }
098    
099        public void testUnableToInstantiate()
100        {
101            IApplicationSpecification spec = newAppSpec();
102    
103            // Training
104    
105            trainGetEngineClassName(spec, AbstractEngine.class.getName());
106    
107            EngineFactoryImpl f = new EngineFactoryImpl();
108    
109            f.setApplicationSpecification(spec);
110            f.setClassResolver(new DefaultClassResolver());
111    
112            replay();
113    
114            f.initializeService();
115    
116            try
117            {
118                f.constructNewEngineInstance(Locale.CHINESE);
119                unreachable();
120            }
121            catch (ApplicationRuntimeException ex)
122            {
123                assertExceptionSubstring(
124                        ex,
125                        "Unable to instantiate engine as instance of class org.apache.tapestry.engine.AbstractEngine");
126            }
127    
128            verify();
129        }
130    
131        public void testInvalidClass()
132        {
133            IApplicationSpecification spec = newAppSpec();
134    
135            trainGetEngineClassName(spec, "foo.XyzzYx");
136    
137            ErrorLog log = newMock(ErrorLog.class);
138    
139            log.error("Engine class 'foo.XyzzYx' not found.", null, null);
140    
141            EngineFactoryImpl f = new EngineFactoryImpl();
142    
143            f.setApplicationSpecification(spec);
144            f.setClassResolver(new DefaultClassResolver());
145            f.setErrorLog(log);
146            f.setDefaultEngineClassName(BaseEngine.class.getName());
147    
148            replay();
149    
150            f.initializeService();
151    
152            IEngine result = f.constructNewEngineInstance(Locale.CANADA_FRENCH);
153    
154            assertTrue(result instanceof BaseEngine);
155    
156            verify();
157        }
158    }