1 15 package org.apache.tapestry.portlet.bindings; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 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 37 public class TestUserAttributeBinding extends HiveMindTestCase 38 { 39 private IBinding newBinding(String bindingDescription, ValueConverter converter, 40 Location location, PortletRequest request, String 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 newMap(String key, String value) 50 { 51 MockControl control = newControl(Map .class); 52 Map map = (Map ) 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 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 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 newValue = new Object (); 122 String valueConverted = "CONVERTED"; 123 124 Map map = new HashMap (); 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 .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 |