001    // Copyright 2006 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 org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.tapestry.IComponent;
020    import org.apache.tapestry.coerce.ValueConverter;
021    import static org.easymock.EasyMock.expect;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests for {@link org.apache.tapestry.binding.ClientIdBinding}. 
026     */
027    @Test
028    public class TestClientIdBinding extends BindingTestCase
029    {
030        public void test_Get_Object()
031        {
032            IComponent nested = newComponent();
033            IComponent component = newMock(IComponent.class);
034            
035            expect(component.getComponent("foo")).andReturn(nested);
036            expect(nested.getClientId()).andReturn("id");
037    
038            ValueConverter vc = newValueConverter();
039    
040            Location l = newLocation();
041    
042            replay();
043    
044            ClientIdBinding b = new ClientIdBinding("param", vc, l, component, "foo");
045    
046            assertSame(component, b.getComponent());
047            assertSame("id", b.getObject());
048    
049            verify();
050        }
051    
052        public void test_Get_Object_Failure()
053        {
054            IComponent component = newMock(IComponent.class);
055    
056            Throwable t = new ApplicationRuntimeException("No such component.");
057    
058            expect(component.getComponent("foo")).andThrow(t);
059    
060            ValueConverter vc = newValueConverter();
061    
062            Location l = newLocation();
063    
064            replay();
065    
066            ClientIdBinding b = new ClientIdBinding("param", vc, l, component, "foo");
067    
068            try
069            {
070                b.getObject();
071                unreachable();
072            }
073            catch (ApplicationRuntimeException ex)
074            {
075                assertEquals(t.getMessage(), ex.getMessage());
076                assertSame(t, ex.getRootCause());
077                assertSame(l, ex.getLocation());
078            }
079    
080            verify();
081        }
082    }