1 15 package org.apache.tapestry.record; 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Collections ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.ErrorLog; 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.Resource; 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.apache.tapestry.IComponent; 27 import org.apache.tapestry.IPage; 28 import org.apache.tapestry.IRequestCycle; 29 import org.apache.tapestry.event.ObservedChangeEvent; 30 import org.apache.tapestry.spec.IComponentSpecification; 31 import org.apache.tapestry.spec.IPropertySpecification; 32 import org.apache.tapestry.test.Creator; 33 import org.easymock.MockControl; 34 35 41 public class TestPageRecorder extends HiveMindTestCase 42 { 43 private IRequestCycle newCycle() 44 { 45 return (IRequestCycle) newMock(IRequestCycle.class); 46 } 47 48 private ErrorLog newLog() 49 { 50 return (ErrorLog) newMock(ErrorLog.class); 51 } 52 53 public void testGetChanges() 54 { 55 IRequestCycle cycle = newCycle(); 56 ErrorLog log = newLog(); 57 58 MockControl sourcec = newControl(PropertyPersistenceStrategySource.class); 59 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) sourcec 60 .getMock(); 61 62 Collection expected = new ArrayList (); 63 64 source.getAllStoredChanges("Foo", cycle); 65 sourcec.setReturnValue(expected); 66 67 replayControls(); 68 69 PageRecorderImpl pr = new PageRecorderImpl("Foo", cycle, source, log); 70 71 Collection actual = pr.getChanges(); 72 73 assertSame(expected, actual); 74 75 verifyControls(); 76 } 77 78 private IComponentSpecification newSpec(String propertyName, String persistence) 79 { 80 MockControl specc = newControl(IComponentSpecification.class); 81 IComponentSpecification spec = (IComponentSpecification) specc.getMock(); 82 83 MockControl psc = newControl(IPropertySpecification.class); 84 IPropertySpecification ps = (IPropertySpecification) psc.getMock(); 85 86 spec.getPropertySpecification(propertyName); 87 specc.setReturnValue(ps); 88 89 ps.getPersistence(); 90 psc.setReturnValue(persistence); 91 92 return spec; 93 } 94 95 public void testObserveChange() 96 { 97 IRequestCycle cycle = newCycle(); 98 ErrorLog log = newLog(); 99 100 MockControl pagec = newControl(IPage.class); 101 IPage page = (IPage) pagec.getMock(); 102 103 IComponentSpecification spec = newSpec("foobar", "session"); 104 105 page.getSpecification(); 106 pagec.setReturnValue(spec); 107 108 page.getIdPath(); 109 pagec.setReturnValue(null); 110 111 MockControl sourcec = newControl(PropertyPersistenceStrategySource.class); 112 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) sourcec 113 .getMock(); 114 115 PropertyPersistenceStrategy strategy = (PropertyPersistenceStrategy) newMock(PropertyPersistenceStrategy.class); 116 117 source.getStrategy("session"); 118 sourcec.setReturnValue(strategy); 119 120 Object newValue = new Object (); 121 122 strategy.store("Foo", null, "foobar", newValue); 123 124 replayControls(); 125 126 PageRecorderImpl pr = new PageRecorderImpl("Foo", cycle, source, log); 127 128 ObservedChangeEvent event = new ObservedChangeEvent(page, "foobar", newValue); 129 130 pr.observeChange(event); 131 132 verifyControls(); 133 } 134 135 public void testUnknownStategy() 136 { 137 Location l = fabricateLocation(12); 138 Throwable inner = new ApplicationRuntimeException("Simulated error."); 139 ErrorLog log = newLog(); 140 141 IRequestCycle cycle = newCycle(); 142 143 MockControl sourcec = newControl(PropertyPersistenceStrategySource.class); 144 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) sourcec 145 .getMock(); 146 147 MockControl componentc = newControl(IComponent.class); 148 IComponent component = (IComponent) componentc.getMock(); 149 150 MockControl specc = newControl(IComponentSpecification.class); 151 IComponentSpecification spec = (IComponentSpecification) specc.getMock(); 152 153 MockControl psc = newControl(IPropertySpecification.class); 154 IPropertySpecification ps = (IPropertySpecification) psc.getMock(); 155 156 component.getSpecification(); 157 componentc.setReturnValue(spec); 158 159 spec.getPropertySpecification("zip"); 160 specc.setReturnValue(ps); 161 162 ps.getPersistence(); 163 psc.setReturnValue("unknown"); 164 165 source.getStrategy("unknown"); 166 sourcec.setThrowable(inner); 167 168 ps.getLocation(); 169 psc.setReturnValue(l); 170 171 log.error("Simulated error.", l, inner); 172 173 replayControls(); 174 175 PageRecorderImpl pr = new PageRecorderImpl("SomePage", cycle, source, log); 176 177 assertNull(pr.findStrategy(component, "zip")); 178 179 verifyControls(); 180 } 181 182 public void testRollbackPageProperty() 183 { 184 IRequestCycle cycle = newCycle(); 185 ErrorLog log = newLog(); 186 187 Creator creator = new Creator(); 188 189 PageFixture page = (PageFixture) creator.newInstance(PageFixture.class); 190 191 MockControl sourcec = newControl(PropertyPersistenceStrategySource.class); 192 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) sourcec 193 .getMock(); 194 195 PropertyChange pc = new PropertyChangeImpl(null, "cartoonName", "Dexter's Laboratory"); 196 197 source.getAllStoredChanges("MyPage", cycle); 198 sourcec.setReturnValue(Collections.singletonList(pc)); 199 200 replayControls(); 201 202 PageRecorderImpl pr = new PageRecorderImpl("MyPage", cycle, source, log); 203 204 pr.rollback(page); 205 206 assertEquals("Dexter's Laboratory", page.getCartoonName()); 207 208 verifyControls(); 209 } 210 211 public void testRollbackComponentProperty() 212 { 213 IRequestCycle cycle = newCycle(); 214 ErrorLog log = newLog(); 215 216 MockControl pagec = newControl(IPage.class); 217 IPage page = (IPage) pagec.getMock(); 218 219 IComponent component = (IComponent) newMock(IComponent.class); 220 221 MockControl sourcec = newControl(PropertyPersistenceStrategySource.class); 222 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) sourcec 223 .getMock(); 224 225 PropertyChange pc = new PropertyChangeImpl("fred.barney", "id", "ziff"); 226 227 source.getAllStoredChanges("MyPage", cycle); 228 sourcec.setReturnValue(Collections.singletonList(pc)); 229 230 page.getNestedComponent("fred.barney"); 231 pagec.setReturnValue(component); 232 233 component.setId("ziff"); 234 235 replayControls(); 236 237 PageRecorderImpl pr = new PageRecorderImpl("MyPage", cycle, source, log); 238 239 pr.rollback(page); 240 241 verifyControls(); 242 } 243 244 public void testChangeWhileLocked() 245 { 246 IRequestCycle cycle = newCycle(); 247 ErrorLog log = newLog(); 248 249 MockControl pagec = newControl(IPage.class); 250 IPage page = (IPage) pagec.getMock(); 251 252 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) newMock(PropertyPersistenceStrategySource.class); 253 254 page.getExtendedId(); 255 pagec.setReturnValue("MyPage"); 256 257 log 258 .error( 259 "Change to persistent property foobar of MyPage has been ignored." 260 + " Persistent properties may only be changed prior to the rendering of the response page.", 261 null, 262 null); 263 264 replayControls(); 265 266 PageRecorderImpl pr = new PageRecorderImpl("MyPage", cycle, source, log); 267 268 pr.commit(); 269 270 ObservedChangeEvent event = new ObservedChangeEvent(page, "foobar", new Object ()); 271 272 pr.observeChange(event); 273 274 verifyControls(); 275 } 276 277 public void testChangeToNonSpecifiedProperty() 278 { 279 Resource r = fabricateLocation(99).getResource(); 280 IRequestCycle cycle = newCycle(); 281 ErrorLog log = newLog(); 282 283 MockControl pagec = newControl(IPage.class); 284 IPage page = (IPage) pagec.getMock(); 285 286 MockControl specc = newControl(IComponentSpecification.class); 287 IComponentSpecification spec = (IComponentSpecification) specc.getMock(); 288 289 PropertyPersistenceStrategySource source = (PropertyPersistenceStrategySource) newMock(PropertyPersistenceStrategySource.class); 290 291 page.getSpecification(); 292 pagec.setReturnValue(spec); 293 294 spec.getPropertySpecification("foobar"); 295 specc.setReturnValue(null); 296 297 page.getExtendedId(); 298 pagec.setReturnValue("TestPage"); 299 300 page.getSpecification(); 301 pagec.setReturnValue(spec); 302 303 spec.getSpecificationLocation(); 304 specc.setReturnValue(r); 305 306 log.error( 307 "A property change event for property foobar of TestPage was observed, " 308 + "but no such property is identified in the specification (" + r + ").", 309 null, 310 null); 311 312 replayControls(); 313 314 PageRecorderImpl pr = new PageRecorderImpl("TestPage", cycle, source, log); 315 316 ObservedChangeEvent event = new ObservedChangeEvent(page, "foobar", new Object ()); 317 318 pr.observeChange(event); 319 320 verifyControls(); 321 } 322 } | Popular Tags |