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.binding;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.apache.tapestry.IBinding;
023    import org.apache.tapestry.coerce.ValueConverter;
024    import org.apache.tapestry.engine.state.ApplicationStateManager;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.binding.StateBinding}and
029     * {@link org.apache.tapestry.binding.StateBindingFactory}.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    @Test
035    public class StateBindingTest extends BaseComponentTestCase
036    {
037    
038        private ValueConverter newValueConverter()
039        {
040            return newMock(ValueConverter.class);
041        }
042    
043        private IBinding newBinding(String objectName, ValueConverter vc, ApplicationStateManager asm,
044                Location location)
045        {
046            StateBindingFactory f = new StateBindingFactory();
047            f.setValueConverter(vc);
048            f.setApplicationStateManager(asm);
049    
050            return f.createBinding(null, "binding description", objectName, location);
051        }
052    
053        public void testSuccess()
054        {
055            ApplicationStateManager asm = newMock(ApplicationStateManager.class);
056            
057            expect(asm.exists("fred")).andReturn(true);
058            
059            ValueConverter vc = newValueConverter();
060    
061            replay();
062    
063            IBinding b = newBinding("fred", vc, asm, null);
064    
065            assertEquals(Boolean.TRUE, b.getObject());
066            assertEquals(false, b.isInvariant());
067    
068            verify();
069        }
070    
071        public void testFailure()
072        {
073            Location l = fabricateLocation(22);
074    
075            Throwable t = new RuntimeException("Nested exception.");
076            ApplicationStateManager asm = newMock(ApplicationStateManager.class);
077    
078            expect(asm.exists("fred")).andThrow(t);
079            
080            ValueConverter vc = newValueConverter();
081    
082            replay();
083    
084            IBinding b = newBinding("fred", vc, asm, l);
085    
086            try
087            {
088                b.getObject();
089                unreachable();
090            }
091            catch (ApplicationRuntimeException ex)
092            {
093                assertEquals("Nested exception.", ex.getMessage());
094                assertSame(l, ex.getLocation());
095                assertSame(t, ex.getRootCause());
096            }
097    
098            verify();
099        }
100    
101    }