1 15 package org.apache.tapestry.services.impl; 16 17 import java.lang.reflect.Method ; 18 import java.util.Collections ; 19 import java.util.Date ; 20 21 import ognl.TypeConverter; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.test.HiveMindTestCase; 25 import org.apache.tapestry.Tapestry; 26 import org.apache.tapestry.services.ExpressionCache; 27 import org.apache.tapestry.services.ExpressionEvaluator; 28 import org.apache.tapestry.spec.IApplicationSpecification; 29 import org.easymock.AbstractMatcher; 30 import org.easymock.MockControl; 31 32 36 public class TestExpressionEvaluator extends HiveMindTestCase 37 { 38 private ExpressionEvaluator create() 39 { 40 ExpressionCache cache = new ExpressionCacheImpl(); 41 42 ExpressionEvaluatorImpl result = new ExpressionEvaluatorImpl(); 43 44 result.setExpressionCache(cache); 45 46 return result; 47 } 48 49 private static class NullMeansIgnoreMatcher extends AbstractMatcher 50 { 51 public boolean matches(Object [] expected, Object [] actual) 52 { 53 for (int i = 0; i < expected.length; i++) 54 { 55 if (expected[i] == null) 56 continue; 57 58 if (!argumentMatches(expected[i], actual[i])) 59 return false; 60 } 61 62 return true; 63 } 64 65 } 66 67 public static class Fixture 68 { 69 private String _value; 70 71 public Fixture() 72 { 73 } 74 75 public Fixture(String value) 76 { 77 _value = value; 78 } 79 80 public String getValue() 81 { 82 return _value; 83 } 84 85 public void setValue(String value) 86 { 87 _value = value; 88 } 89 } 90 91 public void testRead() 92 { 93 Fixture f = new Fixture("Foo"); 94 95 ExpressionEvaluator ee = create(); 96 97 assertEquals("Foo", ee.read(f, "value")); 98 } 99 100 public void testReadFail() 101 { 102 Fixture f = new Fixture(); 103 104 ExpressionEvaluator ee = create(); 105 106 try 107 { 108 ee.read(f, "bar"); 109 unreachable(); 110 } 111 catch (ApplicationRuntimeException ex) 112 { 113 assertExceptionSubstring(ex, "Unable to read OGNL expression"); 114 assertSame(f, ex.getComponent()); 115 } 116 } 117 118 public void testWrite() 119 { 120 Fixture f = new Fixture("Foo"); 121 122 ExpressionEvaluator ee = create(); 123 124 ee.write(f, "value", "Bar"); 125 126 assertEquals("Bar", f.getValue()); 127 } 128 129 public void testWriteFail() 130 { 131 Fixture f = new Fixture(); 132 133 ExpressionEvaluator ee = create(); 134 135 try 136 { 137 ee.write(f, "class", "Foo"); 138 unreachable(); 139 } 140 catch (ApplicationRuntimeException ex) 141 { 142 assertExceptionSubstring(ex, "Unable to update OGNL expression"); 143 assertSame(f, ex.getComponent()); 144 } 145 } 146 147 public void testIsConstant() 148 { 149 ExpressionEvaluator ee = create(); 150 151 assertEquals(true, ee.isConstant("true")); 152 assertEquals(true, ee.isConstant("'OGNL'")); 153 assertEquals(false, ee.isConstant("foo.bar")); 154 assertEquals(false, ee.isConstant("bar()")); 155 assertEquals(true, ee.isConstant("@org.apache.tapestry.Tapestry@HOME_SERVICE")); 156 } 157 158 public void testIsConstantFail() 159 { 160 ExpressionEvaluator ee = create(); 161 162 try 163 { 164 ee.isConstant("@foo@BAR"); 165 unreachable(); 166 } 167 catch (ApplicationRuntimeException ex) 168 { 169 assertExceptionSubstring(ex, "Error evaluating OGNL expression"); 170 } 171 172 } 173 174 public void testTypeConverter() throws Exception 175 { 176 MockControl asc = newControl(IApplicationSpecification.class); 177 IApplicationSpecification as = (IApplicationSpecification) asc.getMock(); 178 179 MockControl tcc = newControl(TypeConverter.class); 180 TypeConverter tc = (TypeConverter) tcc.getMock(); 181 182 184 as.checkExtension(Tapestry.OGNL_TYPE_CONVERTER); 185 asc.setReturnValue(true); 186 187 as.getExtension(Tapestry.OGNL_TYPE_CONVERTER, TypeConverter.class); 188 asc.setReturnValue(tc); 189 190 replayControls(); 191 192 ExpressionCache cache = new ExpressionCacheImpl(); 193 194 ExpressionEvaluatorImpl ee = new ExpressionEvaluatorImpl(); 195 196 ee.setExpressionCache(cache); 197 ee.setApplicationSpecification(as); 198 ee.setContributions(Collections.EMPTY_LIST); 199 200 ee.initializeService(); 201 202 verifyControls(); 203 204 Fixture f = new Fixture(); 205 206 Method m = Fixture.class.getMethod("setValue", new Class [] 207 { String .class }); 208 209 Date d = new Date (); 210 211 213 tc.convertValue(null, f, m, "value", d, String .class); 216 tcc.setMatcher(new NullMeansIgnoreMatcher()); 217 218 tcc.setReturnValue("FROM-TYPE-CONVERTER"); 219 220 replayControls(); 221 222 ee.write(f, "value", d); 223 224 assertEquals("FROM-TYPE-CONVERTER", f.getValue()); 225 226 verifyControls(); 227 } 228 } | Popular Tags |