001 package org.trails.test;
002
003 import org.apache.tapestry.IRequestCycle;
004 import org.apache.tapestry.services.Infrastructure;
005 import org.apache.tapestry.web.WebRequest;
006 import org.apache.tapestry.web.WebResponse;
007 import org.jmock.Mock;
008 import org.jmock.MockObjectTestCase;
009 import org.jmock.core.InvocationMatcher;
010 import org.jmock.core.Stub;
011 import org.jmock.core.constraint.IsEqual;
012 import org.jmock.core.constraint.IsInstanceOf;
013 import org.springframework.test.AbstractTransactionalSpringContextTests;
014 import org.trails.component.ComponentTest;
015 import org.trails.persistence.PersistenceService;
016
017 public abstract class MockableTransactionalTestCase extends AbstractTransactionalSpringContextTests {
018 protected PersistenceService persistenceService;
019
020 private MockObjectTestCase mockDelegate = new MockObjectTestCase() {};
021
022 protected Mock cycleMock = mock(IRequestCycle.class);
023 protected Mock infrastructureMock = mock(Infrastructure.class);
024 protected Mock webRequestMock = mock(WebRequest.class);
025 protected Mock webResponseMock = mock(WebResponse.class);
026
027 protected String[] getConfigLocations(){
028 return new String[]{"applicationContext-test.xml", "seed-data-test.xml"};
029 }
030
031 public void onSetUpBeforeTransaction() throws Exception {
032 persistenceService = (PersistenceService) applicationContext.getBean("persistenceService");
033 }
034
035 protected void onTearDownAfterTransaction() {
036 verify();
037 }
038
039
040 /**
041 * Operations delegation to componentTestDelegate
042 */
043 protected <T> T buildTrailsPage(Class<T> pageClass)
044 {
045 return getComponentTest().buildTrailsPage(pageClass);
046 }
047
048 // Operations delegating to mockDelegate
049
050 public Mock mock(Class mockedType) {return mockDelegate.mock(mockedType);}
051
052 public InvocationMatcher once() {return mockDelegate.once();}
053 public InvocationMatcher atLeastOnce() {return mockDelegate.atLeastOnce();}
054
055 public IsInstanceOf isA(Class operandClass){return mockDelegate.isA(operandClass);}
056
057 public void verify() {mockDelegate.verify();}
058
059 public Stub returnValue(Object o) {return mockDelegate.returnValue(o);}
060 public Stub returnValue(boolean result){return mockDelegate.returnValue(result);}
061 public Stub returnValue(byte result){return mockDelegate.returnValue(result);}
062 public Stub returnValue(char result){return mockDelegate.returnValue(result);}
063 public Stub returnValue(short result){return mockDelegate.returnValue(result);}
064 public Stub returnValue(int result){return mockDelegate.returnValue(result);}
065 public Stub returnValue(long result){return mockDelegate.returnValue(result);}
066 public Stub returnValue(float result){return mockDelegate.returnValue(result);}
067 public Stub returnValue(double result){return mockDelegate.returnValue(result);}
068
069 public IsEqual eq(Object operand) {return mockDelegate.eq(operand);}
070 public IsEqual eq(boolean operand) {return mockDelegate.eq(operand);}
071 public IsEqual eq(byte operand) {return mockDelegate.eq(operand);}
072 public IsEqual eq(char operand) {return mockDelegate.eq(operand);}
073 public IsEqual eq(short operand) {return mockDelegate.eq(operand);}
074 public IsEqual eq(int operand) {return mockDelegate.eq(operand);}
075 public IsEqual eq(long operand) {return mockDelegate.eq(operand);}
076 public IsEqual eq(float operand) {return mockDelegate.eq(operand);}
077 public IsEqual eq(double operand) {return mockDelegate.eq(operand);}
078
079
080 /**
081 * Little hook to override the default ComponentTest delegate.
082 *
083 * @return
084 */
085 protected ComponentTest getComponentTest() {
086 return new ComponentTest(){};
087 }
088 }