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.tapestry.BaseComponentTestCase; 022 import org.apache.tapestry.IEngine; 023 import org.apache.tapestry.services.EngineFactory; 024 import org.apache.tapestry.services.ObjectPool; 025 import org.apache.tapestry.services.RequestLocaleManager; 026 import org.testng.annotations.Test; 027 028 /** 029 * Tests for {@link org.apache.tapestry.services.impl.EngineManagerImpl}. 030 * 031 * @author Howard Lewis Ship 032 * @since 4.0 033 */ 034 @Test(sequential=true) 035 public class TestEngineManager extends BaseComponentTestCase 036 { 037 038 public void testGetFromPool() 039 { 040 RequestLocaleManager extractor = newMock(RequestLocaleManager.class); 041 042 ObjectPool pool = newMock(ObjectPool.class); 043 044 // Training 045 046 expect(extractor.extractLocaleForCurrentRequest()).andReturn(Locale.CHINESE); 047 048 IEngine engine = newMock(IEngine.class); 049 050 expect(pool.get(Locale.CHINESE)).andReturn(engine); 051 052 replay(); 053 054 EngineManagerImpl m = new EngineManagerImpl(); 055 056 m.setEnginePool(pool); 057 m.setLocaleManager(extractor); 058 059 IEngine actual = m.getEngineInstance(); 060 061 assertSame(engine, actual); 062 063 verify(); 064 } 065 066 public void testGetNotInPool() 067 { 068 RequestLocaleManager extractor = newMock(RequestLocaleManager.class); 069 070 ObjectPool pool = newMock(ObjectPool.class); 071 072 // Training 073 074 expect(extractor.extractLocaleForCurrentRequest()).andReturn(Locale.CHINESE); 075 076 IEngine engine = newMock(IEngine.class); 077 078 expect(pool.get(Locale.CHINESE)).andReturn(null); 079 080 EngineFactory factory = newMock(EngineFactory.class); 081 082 expect(factory.constructNewEngineInstance(Locale.CHINESE)).andReturn(engine); 083 084 replay(); 085 086 EngineManagerImpl m = new EngineManagerImpl(); 087 088 m.setEnginePool(pool); 089 m.setLocaleManager(extractor); 090 m.setEngineFactory(factory); 091 092 IEngine actual = m.getEngineInstance(); 093 094 assertSame(engine, actual); 095 096 verify(); 097 } 098 099 public void testStoreNoSession() 100 { 101 IEngine engine = newMock(IEngine.class); 102 103 ObjectPool pool = newMock(ObjectPool.class); 104 105 // Training 106 107 expect(engine.getLocale()).andReturn(Locale.KOREAN); 108 109 pool.store(Locale.KOREAN, engine); 110 111 replay(); 112 113 EngineManagerImpl m = new EngineManagerImpl(); 114 115 m.setEnginePool(pool); 116 117 m.storeEngineInstance(engine); 118 119 verify(); 120 } 121 122 }