KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > bindings > TestUserAttributeBinding


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.portlet.bindings;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.portlet.PortletRequest;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.test.HiveMindTestCase;
25 import org.apache.tapestry.IBinding;
26 import org.apache.tapestry.coerce.ValueConverter;
27 import org.apache.tapestry.portlet.bindings.UserAttributeBindingFactory;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.portlet.bindings.UserAttributeBinding} and
32  * {@link org.apache.tapestry.portlet.bindings.UserAttributeBindingFactory}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestUserAttributeBinding extends HiveMindTestCase
38 {
39     private IBinding newBinding(String JavaDoc bindingDescription, ValueConverter converter,
40             Location location, PortletRequest request, String JavaDoc attributeName)
41     {
42         UserAttributeBindingFactory factory = new UserAttributeBindingFactory();
43         factory.setValueConverter(converter);
44         factory.setRequest(request);
45
46         return factory.createBinding(null, bindingDescription, attributeName, location);
47     }
48
49     private Map JavaDoc newMap(String JavaDoc key, String JavaDoc value)
50     {
51         MockControl control = newControl(Map JavaDoc.class);
52         Map JavaDoc map = (Map JavaDoc) control.getMock();
53
54         map.get(key);
55         control.setReturnValue(value);
56
57         return map;
58     }
59
60     private ValueConverter newConverter()
61     {
62         return (ValueConverter) newMock(ValueConverter.class);
63     }
64
65     private PortletRequest newRequest(Map JavaDoc userInfo)
66     {
67         MockControl control = newControl(PortletRequest.class);
68         PortletRequest request = (PortletRequest) control.getMock();
69
70         request.getAttribute(PortletRequest.USER_INFO);
71         control.setReturnValue(userInfo);
72
73         return request;
74     }
75
76     public void testGetObject()
77     {
78         Map JavaDoc map = newMap("foo.bar", "baz");
79         ValueConverter vc = newConverter();
80         PortletRequest request = newRequest(map);
81         Location l = newLocation();
82
83         replayControls();
84
85         IBinding b = newBinding("description", vc, l, request, "foo.bar");
86
87         assertSame(l, b.getLocation());
88         assertEquals("description", b.getDescription());
89         assertEquals(false, b.isInvariant());
90         assertEquals("baz", b.getObject());
91
92         verifyControls();
93     }
94
95     public void testGetObjectNoUserInfo()
96     {
97         ValueConverter vc = newConverter();
98         PortletRequest request = newRequest(null);
99         Location l = newLocation();
100
101         replayControls();
102
103         IBinding b = newBinding("description", vc, l, request, "foo.bar");
104
105         try
106         {
107             b.getObject();
108             unreachable();
109         }
110         catch (ApplicationRuntimeException ex)
111         {
112             assertEquals(BindingsMessages.noUserInfo(), ex.getMessage());
113             assertSame(l, ex.getLocation());
114         }
115
116         verifyControls();
117     }
118
119     public void testSetObject()
120     {
121         Object JavaDoc newValue = new Object JavaDoc();
122         String JavaDoc valueConverted = "CONVERTED";
123
124         Map JavaDoc map = new HashMap JavaDoc();
125
126         MockControl converterc = newControl(ValueConverter.class);
127         ValueConverter converter = (ValueConverter) converterc.getMock();
128         PortletRequest request = newRequest(map);
129         Location l = newLocation();
130
131         converter.coerceValue(newValue, String JavaDoc.class);
132         converterc.setReturnValue(valueConverted);
133
134         replayControls();
135
136         IBinding b = newBinding("description", converter, l, request, "foo.bar");
137
138         b.setObject(newValue);
139
140         assertSame(valueConverted, map.get("foo.bar"));
141
142         verifyControls();
143     }
144
145 }
146
Popular Tags