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.form.translator; 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.hivemind.lib.BeanFactory; 022 import org.apache.tapestry.IBinding; 023 import org.apache.tapestry.IComponent; 024 import org.apache.tapestry.binding.BindingTestCase; 025 import org.apache.tapestry.coerce.ValueConverter; 026 import org.testng.annotations.Test; 027 028 /** 029 * Tests for {@link org.apache.tapestry.form.translator.TranslatorBinding} and 030 * {@link org.apache.tapestry.form.translator.TranslatorBindingFactory}. 031 * 032 * @author Howard Lewis Ship 033 * @since 4.0 034 */ 035 @Test(sequential = true) 036 public class TestTranslatorBinding extends BindingTestCase 037 { 038 public void test_Create() 039 { 040 Location l = newLocation(); 041 ValueConverter vc = newValueConverter(); 042 IComponent component = newComponent(); 043 044 BeanFactory bf = newMock(BeanFactory.class); 045 046 Translator translator =newMock(Translator.class); 047 048 expect(bf.get("string")).andReturn(translator); 049 050 replay(); 051 052 TranslatorBindingFactory f = new TranslatorBindingFactory(); 053 f.setValueConverter(vc); 054 f.setTranslatorBeanFactory(bf); 055 056 IBinding binding = f.createBinding(component, "description", "string", l); 057 058 assertSame(translator, binding.getObject()); 059 assertSame(l, binding.getLocation()); 060 assertTrue(binding.isInvariant()); 061 assertEquals("description", binding.getDescription()); 062 063 verify(); 064 } 065 066 public void test_Failure() 067 { 068 Location l = newLocation(); 069 IComponent component = newComponent(); 070 071 BeanFactory bf = newMock(BeanFactory.class); 072 073 Throwable t = new RuntimeException("Boom!"); 074 075 expect(bf.get("string")).andThrow(t); 076 077 replay(); 078 079 TranslatorBindingFactory f = new TranslatorBindingFactory(); 080 f.setTranslatorBeanFactory(bf); 081 082 try 083 { 084 f.createBinding(component, "description", "string", l); 085 unreachable(); 086 } 087 catch (ApplicationRuntimeException ex) 088 { 089 assertEquals("Boom!", ex.getMessage()); 090 assertSame(t, ex.getRootCause()); 091 assertSame(l, ex.getLocation()); 092 } 093 094 verify(); 095 096 } 097 }