1 15 package org.apache.tapestry.enhance; 16 17 import java.util.Map ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.test.HiveMindTestCase; 21 import org.apache.tapestry.IComponent; 22 import org.apache.tapestry.IRender; 23 import org.easymock.MockControl; 24 25 29 public class TestEnhanceUtils extends HiveMindTestCase 30 { 31 public void testTypeUnspecifiedWithNoExistingProperty() 32 { 33 MockControl opc = newControl(EnhancementOperation.class); 34 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 35 36 op.getPropertyType("wilma"); 37 opc.setReturnValue(null); 38 39 replayControls(); 40 41 Class result = EnhanceUtils.extractPropertyType(op, "wilma", null); 42 43 assertEquals(Object .class, result); 44 45 verifyControls(); 46 } 47 48 public void testTypeUnspecifiedButExistingProperty() 49 { 50 MockControl opc = newControl(EnhancementOperation.class); 51 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 52 53 op.getPropertyType("fred"); 54 opc.setReturnValue(Map .class); 55 56 replayControls(); 57 58 Class result = EnhanceUtils.extractPropertyType(op, "fred", null); 59 60 assertEquals(Map .class, result); 61 62 verifyControls(); 63 } 64 65 public void testTypeSpecified() 66 { 67 MockControl opc = newControl(EnhancementOperation.class); 68 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 69 70 op.convertTypeName("int[]"); 71 opc.setReturnValue(int[].class); 72 73 op.validateProperty("betty", int[].class); 74 75 replayControls(); 76 77 Class result = EnhanceUtils.extractPropertyType(op, "betty", "int[]"); 78 79 assertEquals(int[].class, result); 80 81 verifyControls(); 82 } 83 84 public void testCreateUnwrapForPrimitive() 85 { 86 EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class); 87 88 replayControls(); 89 90 String result = EnhanceUtils.createUnwrapExpression(op, "mybinding", int.class); 91 92 assertEquals("org.apache.tapestry.enhance.EnhanceUtils.toInt(mybinding)", result); 93 94 verifyControls(); 95 } 96 97 public void testCreateUnwrapForObjectType() 98 { 99 MockControl opc = newControl(EnhancementOperation.class); 100 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 101 102 op.getClassReference(String .class); 103 opc.setReturnValue("_$class$String"); 104 105 replayControls(); 106 107 String result = EnhanceUtils.createUnwrapExpression(op, "thebinding", String .class); 108 109 assertEquals("(java.lang.String) thebinding.getObject(_$class$String)", result); 110 111 verifyControls(); 112 } 113 114 public void testVerifyPropertyTypeNoProperty() 115 { 116 MockControl opc = newControl(EnhancementOperation.class); 117 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 118 119 op.getPropertyType("foo"); 120 opc.setReturnValue(null); 121 122 replayControls(); 123 124 assertEquals(Object .class, EnhanceUtils.verifyPropertyType(op, "foo", String .class)); 125 126 verifyControls(); 127 } 128 129 public void testVerifyPropertyTypeSuccess() 130 { 131 MockControl opc = newControl(EnhancementOperation.class); 132 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 133 134 op.getPropertyType("foo"); 135 opc.setReturnValue(Object .class); 136 137 replayControls(); 138 139 assertEquals(Object .class, EnhanceUtils.verifyPropertyType(op, "foo", String .class)); 140 141 verifyControls(); 142 } 143 144 public void testVerifyPropertyTypeWithDeclaredPropertyType() 145 { 146 MockControl opc = newControl(EnhancementOperation.class); 147 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 148 149 op.getPropertyType("foo"); 150 opc.setReturnValue(IRender.class); 151 152 replayControls(); 153 154 assertEquals(IRender.class, EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class)); 155 156 verifyControls(); 157 158 } 159 160 public void testVerifyPropertyTypeFailure() 161 { 162 MockControl opc = newControl(EnhancementOperation.class); 163 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 164 165 op.getPropertyType("foo"); 166 opc.setReturnValue(String .class); 167 168 replayControls(); 169 170 try 171 { 172 EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class); 173 } 174 catch (ApplicationRuntimeException ex) 175 { 176 assertEquals( 177 "Property foo is type java.lang.String, which is not compatible with org.apache.tapestry.IComponent.", 178 ex.getMessage()); 179 } 180 181 verifyControls(); 182 183 } 184 185 } | Popular Tags |