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.engine.state;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.util.Collections;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.hivemind.ApplicationRuntimeException;
024    import org.apache.hivemind.ErrorLog;
025    import org.apache.hivemind.Location;
026    import org.apache.tapestry.BaseComponentTestCase;
027    import org.testng.annotations.Test;
028    
029    /**
030     * Tests {@link TestSOMRegistry}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestSOMRegistry extends BaseComponentTestCase
037    {
038        public void testInitializeAndGet()
039        {
040            Object stateObject = new Object();
041            
042            StateObjectPersistenceManager pm = newMock(StateObjectPersistenceManager.class);
043    
044            StateObjectFactory f = newMock(StateObjectFactory.class);
045    
046            expect(pm.get("fred", f)).andReturn(stateObject);
047    
048            StateObjectContribution c = new StateObjectContribution();
049            c.setName("fred");
050            c.setScope("wierd");
051            c.setFactory(f);
052    
053            Map applicationContributions = new HashMap();
054            applicationContributions.put("fred", c);
055    
056            Map persistenceManagers = new HashMap();
057            persistenceManagers.put("wierd", pm);
058    
059            replay();
060    
061            SOMRegistryImpl r = new SOMRegistryImpl();
062            r.setApplicationContributions(applicationContributions);
063            r.setFactoryContributions(Collections.EMPTY_MAP);
064            r.setPersistenceManagers(persistenceManagers);
065            r.initializeService();
066    
067            StateObjectManager som = r.get("fred");
068    
069            assertSame(stateObject, som.get());
070    
071            verify();
072        }
073    
074        public void testInitializeUnknownScope()
075        {
076            Location l = fabricateLocation(55);
077            ErrorLog log = newMock(ErrorLog.class);
078    
079            StateObjectContribution c = new StateObjectContribution();
080            c.setName("fred");
081            c.setScope("wierd");
082            c.setLocation(l);
083    
084            Map applicationContributions = new HashMap();
085            applicationContributions.put("fred", c);
086    
087            log.error(StateMessages.unknownScope("fred", "wierd"), l, null);
088    
089            replay();
090    
091            SOMRegistryImpl r = new SOMRegistryImpl();
092            r.setApplicationContributions(applicationContributions);
093            r.setFactoryContributions(Collections.EMPTY_MAP);
094            r.setPersistenceManagers(Collections.EMPTY_MAP);
095            r.setErrorLog(log);
096            r.initializeService();
097    
098            verify();
099        }
100    
101        public void testGetUnknownObjectName()
102        {
103            SOMRegistryImpl r = new SOMRegistryImpl();
104    
105            try
106            {
107                r.get("angel");
108                unreachable();
109            }
110            catch (ApplicationRuntimeException ex)
111            {
112                assertEquals(StateMessages.unknownStateObjectName("angel"), ex.getMessage());
113            }
114        }
115    }