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 org.apache.hivemind.Location; 018 import org.apache.tapestry.BindingException; 019 import org.apache.tapestry.coerce.ValueConverter; 020 import static org.easymock.EasyMock.expect; 021 import org.testng.annotations.Test; 022 023 import java.util.Date; 024 025 /** 026 * Tests for {@link org.apache.tapestry.binding.LiteralBinding}. It also tests some common 027 * behaviors provided by {@link org.apache.tapestry.binding.AbstractBinding}. 028 * 029 * @author Howard M. Lewis Ship 030 * @since 4.0 031 */ 032 @Test 033 public class TestLiteralBinding extends BindingTestCase 034 { 035 public void test_Get_Object() 036 { 037 Location l = fabricateLocation(22); 038 ValueConverter vc = newValueConverter(); 039 040 replay(); 041 042 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 043 044 assertSame(l, b.getLocation()); 045 assertEquals(b.getDescription(), "literal-value"); 046 assertEquals("literal-value", b.getObject()); 047 048 assertEquals(true, b.isInvariant()); 049 assertNull(b.getComponent()); 050 051 verify(); 052 } 053 054 public void test_To_String() 055 { 056 Location l = fabricateLocation(22); 057 ValueConverter vc = newValueConverter(); 058 059 replay(); 060 061 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 062 063 assertEquals("StaticBinding[literal-value]", b.toString()); 064 065 verify(); 066 } 067 068 public void test_Get_Object_With_Class() 069 { 070 ValueConverter vc = newMock(ValueConverter.class); 071 Location l = fabricateLocation(99); 072 073 Date date = new Date(); 074 075 expect(vc.coerceValue("my-literal", Date.class)).andReturn(date); 076 077 replay(); 078 079 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "my-literal"); 080 081 assertSame(date, b.getObject(Date.class)); 082 083 verify(); 084 } 085 086 public void test_Get_Object_Exception() 087 { 088 ValueConverter vc = newMock(ValueConverter.class); 089 Location location = fabricateLocation(99); 090 091 Exception innerException = new RuntimeException("Failure"); 092 093 expect(vc.coerceValue("my-literal", Date.class)).andThrow(innerException); 094 095 replay(); 096 097 LiteralBinding b = new LiteralBinding("parameter foo", vc, location, "my-literal"); 098 099 try 100 { 101 b.getObject(Date.class); 102 unreachable(); 103 } 104 catch (BindingException ex) 105 { 106 assertEquals(ex.getMessage(), "Error converting value for my-literal: Failure"); 107 assertSame(innerException, ex.getRootCause()); 108 assertSame(location, ex.getLocation()); 109 assertSame(b, ex.getBinding()); 110 } 111 112 verify(); 113 } 114 115 public void test_Set_Object() 116 { 117 Location l = fabricateLocation(22); 118 ValueConverter vc = newValueConverter(); 119 120 replay(); 121 122 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 123 124 try 125 { 126 b.setObject("fred"); 127 unreachable(); 128 } 129 catch (BindingException ex) 130 { 131 assertEquals(ex.getMessage() , 132 "Binding with value literal-value (StaticBinding[literal-value]) may not be updated."); 133 assertSame(b, ex.getBinding()); 134 assertSame(l, ex.getLocation()); 135 } 136 137 } 138 }