KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > binding > TestLiteralBinding


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.binding;
16
17 import java.util.Date JavaDoc;
18
19 import org.apache.hivemind.Location;
20 import org.apache.tapestry.BindingException;
21 import org.apache.tapestry.coerce.ValueConverter;
22 import org.easymock.MockControl;
23
24 /**
25  * Tests for {@link org.apache.tapestry.binding.LiteralBinding}. It also tests some common
26  * behaviors provided by {@link org.apache.tapestry.binding.AbstractBinding}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestLiteralBinding extends BindingTestCase
32 {
33     public void testGetObject()
34     {
35         Location l = fabricateLocation(22);
36         ValueConverter vc = newValueConverter();
37
38         replayControls();
39
40         LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value");
41
42         assertSame(l, b.getLocation());
43         assertEquals("parameter foo", b.getDescription());
44         assertEquals("literal-value", b.getObject());
45
46         assertEquals(true, b.isInvariant());
47         assertNull(b.getComponent());
48
49         verifyControls();
50     }
51
52     public void testToString()
53     {
54         Location l = fabricateLocation(22);
55         ValueConverter vc = newValueConverter();
56
57         replayControls();
58
59         LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value");
60
61         assertEquals("StaticBinding[literal-value]", b.toString());
62
63         verifyControls();
64     }
65
66     public void testGetObjectWithClass()
67     {
68         MockControl control = newControl(ValueConverter.class);
69         ValueConverter vc = (ValueConverter) control.getMock();
70
71         Date JavaDoc date = new Date JavaDoc();
72
73         vc.coerceValue("my-literal", Date JavaDoc.class);
74         control.setReturnValue(date);
75
76         replayControls();
77
78         LiteralBinding b = new LiteralBinding("parameter foo", vc, fabricateLocation(99),
79                 "my-literal");
80
81         assertSame(date, b.getObject(Date JavaDoc.class));
82
83         verifyControls();
84     }
85
86     public void testGetObjectException()
87     {
88         MockControl control = newControl(ValueConverter.class);
89         ValueConverter vc = (ValueConverter) control.getMock();
90
91         Exception JavaDoc innerException = new RuntimeException JavaDoc("Failure");
92
93         vc.coerceValue("my-literal", Date JavaDoc.class);
94         control.setThrowable(innerException);
95
96         replayControls();
97
98         Location location = fabricateLocation(99);
99         LiteralBinding b = new LiteralBinding("parameter foo", vc, location, "my-literal");
100
101         try
102         {
103             b.getObject(Date JavaDoc.class);
104             unreachable();
105         }
106         catch (BindingException ex)
107         {
108             assertEquals("Error converting value for parameter foo: Failure", ex.getMessage());
109             assertSame(innerException, ex.getRootCause());
110             assertSame(location, ex.getLocation());
111             assertSame(b, ex.getBinding());
112         }
113
114         verifyControls();
115     }
116
117     public void testSetObject()
118     {
119         Location l = fabricateLocation(22);
120         ValueConverter vc = newValueConverter();
121
122         replayControls();
123
124         LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value");
125
126         try
127         {
128             b.setObject("fred");
129             unreachable();
130         }
131         catch (BindingException ex)
132         {
133             assertEquals(
134                     "Binding for parameter foo (StaticBinding[literal-value]) may not be updated.",
135                     ex.getMessage());
136             assertSame(b, ex.getBinding());
137             assertSame(l, ex.getLocation());
138         }
139
140     }
141 }
Popular Tags