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