001 // Copyright 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.portlet.bindings; 016 017 import static org.easymock.EasyMock.*; 018 019 import java.util.HashMap; 020 import java.util.Map; 021 022 import org.apache.hivemind.ApplicationRuntimeException; 023 import org.apache.hivemind.Location; 024 import org.apache.tapestry.BaseComponentTestCase; 025 import org.apache.tapestry.IBinding; 026 import org.apache.tapestry.coerce.ValueConverter; 027 import org.testng.annotations.Test; 028 029 import javax.portlet.PortletRequest; 030 031 /** 032 * Tests for {@link org.apache.tapestry.portlet.bindings.UserAttributeBinding} and 033 * {@link org.apache.tapestry.portlet.bindings.UserAttributeBindingFactory}. 034 * 035 * @author Howard M. Lewis Ship 036 * @since 4.0 037 */ 038 @Test(sequential=true) 039 public class TestUserAttributeBinding extends BaseComponentTestCase 040 { 041 private IBinding newBinding(String bindingDescription, ValueConverter converter, 042 Location location, PortletRequest request, String attributeName) 043 { 044 UserAttributeBindingFactory factory = new UserAttributeBindingFactory(); 045 factory.setValueConverter(converter); 046 factory.setRequest(request); 047 048 return factory.createBinding(null, bindingDescription, attributeName, location); 049 } 050 051 private Map newMap(String key, String value) 052 { 053 Map map = newMock(Map.class); 054 checkOrder(map, false); 055 056 expect(map.get(key)).andReturn(value); 057 058 return map; 059 } 060 061 private ValueConverter newConverter() 062 { 063 return newMock(ValueConverter.class); 064 } 065 066 private PortletRequest newRequest(Map userInfo) 067 { 068 PortletRequest request = newMock(PortletRequest.class); 069 070 expect(request.getAttribute(PortletRequest.USER_INFO)).andReturn(userInfo); 071 072 return request; 073 } 074 075 public void testGetObject() 076 { 077 Map map = newMap("foo.bar", "baz"); 078 PortletRequest request = newRequest(map); 079 ValueConverter vc = newConverter(); 080 Location l = newLocation(); 081 082 replay(); 083 084 IBinding b = newBinding("description", vc, l, request, "foo.bar"); 085 086 assertSame(l, b.getLocation()); 087 assertEquals("description", b.getDescription()); 088 assertEquals(false, b.isInvariant()); 089 assertEquals("baz", b.getObject()); 090 091 verify(); 092 } 093 094 public void testGetObjectNoUserInfo() 095 { 096 ValueConverter vc = newConverter(); 097 PortletRequest request = newRequest(null); 098 Location l = newLocation(); 099 100 replay(); 101 102 IBinding b = newBinding("description", vc, l, request, "foo.bar"); 103 104 try 105 { 106 b.getObject(); 107 unreachable(); 108 } 109 catch (ApplicationRuntimeException ex) 110 { 111 assertEquals(BindingsMessages.noUserInfo(), ex.getMessage()); 112 assertSame(l, ex.getLocation()); 113 } 114 115 verify(); 116 } 117 118 public void testSetObject() 119 { 120 Object newValue = new Object(); 121 String valueConverted = "CONVERTED"; 122 123 Map map = new HashMap(); 124 125 ValueConverter converter = newMock(ValueConverter.class); 126 Location l = newLocation(); 127 128 expect(converter.coerceValue(newValue, String.class)).andReturn(valueConverted); 129 130 PortletRequest request = newRequest(map); 131 132 replay(); 133 134 IBinding b = newBinding("description", converter, l, request, "foo.bar"); 135 136 b.setObject(newValue); 137 138 assertSame(valueConverted, map.get("foo.bar")); 139 140 verify(); 141 } 142 143 }