001 // Copyright 2004, 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.enhance; 016 017 import static org.easymock.EasyMock.expect; 018 019 import java.util.Map; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.tapestry.BaseComponentTestCase; 023 import org.apache.tapestry.IBinding; 024 import org.apache.tapestry.IComponent; 025 import org.apache.tapestry.IRender; 026 import org.testng.annotations.Test; 027 028 /** 029 * @author Howard M. Lewis Ship 030 * @since 4.0 031 */ 032 @Test 033 public class TestEnhanceUtils extends BaseComponentTestCase 034 { 035 protected EnhancementOperation newOp() 036 { 037 return newMock(EnhancementOperation.class); 038 } 039 040 public void testTypeUnspecifiedWithNoExistingProperty() 041 { 042 EnhancementOperation op = newOp(); 043 044 expect(op.getPropertyType("wilma")).andReturn(null); 045 046 replay(); 047 048 Class result = EnhanceUtils.extractPropertyType(op, "wilma", null); 049 050 assertEquals(Object.class, result); 051 052 verify(); 053 } 054 055 public void testTypeUnspecifiedButExistingProperty() 056 { 057 EnhancementOperation op = newOp(); 058 059 expect(op.getPropertyType("fred")).andReturn(Map.class); 060 061 replay(); 062 063 Class result = EnhanceUtils.extractPropertyType(op, "fred", null); 064 065 assertEquals(Map.class, result); 066 067 verify(); 068 } 069 070 public void testTypeSpecified() 071 { 072 EnhancementOperation op = newOp(); 073 074 expect(op.convertTypeName("int[]")).andReturn(int[].class); 075 076 op.validateProperty("betty", int[].class); 077 078 replay(); 079 080 Class result = EnhanceUtils.extractPropertyType(op, "betty", "int[]"); 081 082 assertEquals(int[].class, result); 083 084 verify(); 085 } 086 087 public void testCreateUnwrapForPrimitive() 088 { 089 EnhancementOperation op = newOp(); 090 091 replay(); 092 093 String result = EnhanceUtils.createUnwrapExpression(op, "mybinding", int.class); 094 095 assertEquals("org.apache.tapestry.enhance.EnhanceUtils.toInt(mybinding)", result); 096 097 verify(); 098 } 099 100 public void testCreateUnwrapForObjectType() 101 { 102 EnhancementOperation op = newOp(); 103 104 expect(op.getClassReference(String.class)).andReturn("_$class$String"); 105 106 replay(); 107 108 String result = EnhanceUtils.createUnwrapExpression(op, "thebinding", String.class); 109 110 assertEquals("(java.lang.String) thebinding.getObject(_$class$String)", result); 111 112 verify(); 113 } 114 115 public void testVerifyPropertyTypeNoProperty() 116 { 117 EnhancementOperation op = newOp(); 118 119 expect(op.getPropertyType("foo")).andReturn(null); 120 121 replay(); 122 123 assertEquals(Object.class, EnhanceUtils.verifyPropertyType(op, "foo", String.class)); 124 125 verify(); 126 } 127 128 public void testVerifyPropertyTypeSuccess() 129 { 130 EnhancementOperation op = newOp(); 131 132 expect(op.getPropertyType("foo")).andReturn(Object.class); 133 134 replay(); 135 136 assertEquals(Object.class, EnhanceUtils.verifyPropertyType(op, "foo", String.class)); 137 138 verify(); 139 } 140 141 public void testVerifyPropertyTypeWithDeclaredPropertyType() 142 { 143 EnhancementOperation op = newOp(); 144 145 expect(op.getPropertyType("foo")).andReturn(IRender.class); 146 147 replay(); 148 149 assertEquals(IRender.class, EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class)); 150 151 verify(); 152 153 } 154 155 public void testVerifyPropertyTypeFailure() 156 { 157 EnhancementOperation op = newOp(); 158 159 expect(op.getPropertyType("foo")).andReturn(String.class); 160 161 replay(); 162 163 try 164 { 165 EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class); 166 } 167 catch (ApplicationRuntimeException ex) 168 { 169 assertEquals( 170 "Property foo is type java.lang.String, which is not compatible with org.apache.tapestry.IComponent.", 171 ex.getMessage()); 172 } 173 174 verify(); 175 } 176 177 protected IBinding newBinding(Class expectedType, Object value) 178 { 179 IBinding binding = newMock(IBinding.class); 180 181 expect(binding.getObject(expectedType)).andReturn(value); 182 183 return binding; 184 } 185 186 public void testToBooleanNull() 187 { 188 IBinding binding = newBinding(Boolean.class, null); 189 190 replay(); 191 192 assertEquals(false, EnhanceUtils.toBoolean(binding)); 193 194 verify(); 195 } 196 197 public void testToBoolean() 198 { 199 IBinding binding = newBinding(Boolean.class, Boolean.TRUE); 200 201 replay(); 202 203 assertEquals(true, EnhanceUtils.toBoolean(binding)); 204 205 verify(); 206 } 207 208 public void testToByteNull() 209 { 210 IBinding binding = newBinding(Byte.class, null); 211 212 replay(); 213 214 assertEquals(0, EnhanceUtils.toByte(binding)); 215 216 verify(); 217 } 218 219 public void testToByte() 220 { 221 IBinding binding = newBinding(Byte.class, new Byte((byte) 37)); 222 223 replay(); 224 225 assertEquals(37, EnhanceUtils.toByte(binding)); 226 227 verify(); 228 } 229 230 public void testToCharNull() 231 { 232 IBinding binding = newBinding(Character.class, null); 233 234 replay(); 235 236 assertEquals(0, EnhanceUtils.toChar(binding)); 237 238 verify(); 239 } 240 241 public void testToChar() 242 { 243 IBinding binding = newBinding(Character.class, new Character('q')); 244 245 replay(); 246 247 assertEquals('q', EnhanceUtils.toChar(binding)); 248 249 verify(); 250 } 251 252 public void testToShortNull() 253 { 254 IBinding binding = newBinding(Short.class, null); 255 256 replay(); 257 258 assertEquals(0, EnhanceUtils.toShort(binding)); 259 260 verify(); 261 } 262 263 public void testToShort() 264 { 265 IBinding binding = newBinding(Short.class, new Short((short) 99)); 266 267 replay(); 268 269 assertEquals(99, EnhanceUtils.toShort(binding)); 270 271 verify(); 272 } 273 274 public void testToIntNull() 275 { 276 IBinding binding = newBinding(Integer.class, null); 277 278 replay(); 279 280 assertEquals(0, EnhanceUtils.toInt(binding)); 281 282 verify(); 283 } 284 285 public void testToInt() 286 { 287 IBinding binding = newBinding(Integer.class, new Integer(107)); 288 289 replay(); 290 291 assertEquals(107, EnhanceUtils.toInt(binding)); 292 293 verify(); 294 } 295 296 public void testToLongNull() 297 { 298 IBinding binding = newBinding(Long.class, null); 299 300 replay(); 301 302 assertEquals(0, EnhanceUtils.toLong(binding)); 303 304 verify(); 305 } 306 307 public void testToLong() 308 { 309 IBinding binding = newBinding(Long.class, new Long(90000)); 310 311 replay(); 312 313 assertEquals(90000, EnhanceUtils.toLong(binding)); 314 315 verify(); 316 } 317 318 public void testToFloatNull() 319 { 320 IBinding binding = newBinding(Float.class, null); 321 322 replay(); 323 324 assertEquals(0.0f, EnhanceUtils.toFloat(binding), 0.0f); 325 326 verify(); 327 } 328 329 public void testToFloat() 330 { 331 IBinding binding = newBinding(Float.class, new Float(2.5f)); 332 333 replay(); 334 335 assertEquals(2.5f, EnhanceUtils.toFloat(binding), 0.0f); 336 337 verify(); 338 } 339 340 public void testToDoubleNull() 341 { 342 IBinding binding = newBinding(Double.class, null); 343 344 replay(); 345 346 assertEquals(0.0d, EnhanceUtils.toDouble(binding), 0.0d); 347 348 verify(); 349 } 350 351 public void testToDouble() 352 { 353 IBinding binding = newBinding(Double.class, new Double(2.5d)); 354 355 replay(); 356 357 assertEquals(2.5d, EnhanceUtils.toDouble(binding), 0.0d); 358 359 verify(); 360 } 361 362 }