KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 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 org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.test.HiveMindTestCase;
20 import org.apache.tapestry.IBinding;
21 import org.apache.tapestry.coerce.ValueConverter;
22 import org.apache.tapestry.engine.state.ApplicationStateManager;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests for {@link org.apache.tapestry.binding.StateBinding}and
27  * {@link org.apache.tapestry.binding.StateBindingFactory}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 4.0
31  */

32 public class TestStateBinding extends HiveMindTestCase
33 {
34
35     private ValueConverter newValueConverter()
36     {
37         return (ValueConverter) newMock(ValueConverter.class);
38     }
39
40     private IBinding newBinding(String JavaDoc objectName, ValueConverter vc, ApplicationStateManager asm,
41             Location location)
42     {
43         StateBindingFactory f = new StateBindingFactory();
44         f.setValueConverter(vc);
45         f.setApplicationStateManager(asm);
46
47         return f.createBinding(null, "binding description", objectName, location);
48     }
49
50     public void testSuccess()
51     {
52         MockControl asmc = newControl(ApplicationStateManager.class);
53         ApplicationStateManager asm = (ApplicationStateManager) asmc.getMock();
54
55         asm.exists("fred");
56         asmc.setReturnValue(true);
57
58         ValueConverter vc = newValueConverter();
59
60         replayControls();
61
62         IBinding b = newBinding("fred", vc, asm, null);
63
64         assertEquals(Boolean.TRUE, b.getObject());
65
66         verifyControls();
67     }
68
69     public void testFailure()
70     {
71         Location l = fabricateLocation(22);
72
73         Throwable JavaDoc t = new RuntimeException JavaDoc("Nested exception.");
74
75         MockControl asmc = newControl(ApplicationStateManager.class);
76         ApplicationStateManager asm = (ApplicationStateManager) asmc.getMock();
77
78         asm.exists("fred");
79         asmc.setThrowable(t);
80
81         ValueConverter vc = newValueConverter();
82
83         replayControls();
84
85         IBinding b = newBinding("fred", vc, asm, l);
86
87         
88         try
89         {
90             b.getObject();
91             unreachable();
92         }
93         catch (ApplicationRuntimeException ex)
94         {
95             assertEquals("Nested exception.", ex.getMessage());
96             assertSame(l, ex.getLocation());
97             assertSame(t, ex.getRootCause());
98         }
99
100         verifyControls();
101     }
102
103 }
Popular Tags