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 java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.impl.DefaultClassResolver;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.testng.annotations.Test;
025    
026    /**
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    @Test
031    public class TestNamedClassStateObjectFactory extends BaseComponentTestCase
032    {
033        public void testSuccess()
034        {
035            NamedClassStateObjectFactory f = new NamedClassStateObjectFactory();
036            f.setClassResolver(new DefaultClassResolver());
037            f.setClassName(HashMap.class.getName());
038    
039            assertTrue(f.createStateObject() instanceof HashMap);
040        }
041    
042        public void testFailure()
043        {
044            Location l = fabricateLocation(99);
045    
046            NamedClassStateObjectFactory f = new NamedClassStateObjectFactory();
047            f.setClassResolver(new DefaultClassResolver());
048            f.setClassName(Map.class.getName());
049            f.setLocation(l);
050    
051            try
052            {
053                f.createStateObject();
054                unreachable();
055            }
056            catch (ApplicationRuntimeException ex)
057            {
058                assertExceptionSubstring(ex, "Unable to instantiate an instance of java.util.Map");
059    
060                assertSame(l, ex.getLocation());
061    
062            }
063    
064        }
065    
066    }