001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.mock;
006:
007: import com.opensymphony.xwork.interceptor.Interceptor;
008: import com.opensymphony.xwork.ActionInvocation;
009: import junit.framework.Assert;
010:
011: /**
012: * Mock for an {@link com.opensymphony.xwork.interceptor.Interceptor}.
013: *
014: * @author Jason Carreira
015: */
016: public class MockInterceptor implements Interceptor {
017:
018: private static final long serialVersionUID = 2692551676567227756L;
019:
020: public static final String DEFAULT_FOO_VALUE = "fooDefault";
021:
022: private String expectedFoo = DEFAULT_FOO_VALUE;
023: private String foo = DEFAULT_FOO_VALUE;
024: private boolean executed = false;
025:
026: public boolean isExecuted() {
027: return executed;
028: }
029:
030: public void setExpectedFoo(String expectedFoo) {
031: this .expectedFoo = expectedFoo;
032: }
033:
034: public String getExpectedFoo() {
035: return expectedFoo;
036: }
037:
038: public void setFoo(String foo) {
039: this .foo = foo;
040: }
041:
042: public String getFoo() {
043: return foo;
044: }
045:
046: /**
047: * Called to let an interceptor clean up any resources it has allocated.
048: */
049: public void destroy() {
050: }
051:
052: public boolean equals(Object o) {
053: if (this == o) {
054: return true;
055: }
056:
057: if (!(o instanceof MockInterceptor)) {
058: return false;
059: }
060:
061: final MockInterceptor testInterceptor = (MockInterceptor) o;
062:
063: if (executed != testInterceptor.executed) {
064: return false;
065: }
066:
067: if ((expectedFoo != null) ? (!expectedFoo
068: .equals(testInterceptor.expectedFoo))
069: : (testInterceptor.expectedFoo != null)) {
070: return false;
071: }
072:
073: if ((foo != null) ? (!foo.equals(testInterceptor.foo))
074: : (testInterceptor.foo != null)) {
075: return false;
076: }
077:
078: return true;
079: }
080:
081: public int hashCode() {
082: int result;
083: result = ((expectedFoo != null) ? expectedFoo.hashCode() : 0);
084: result = (29 * result) + ((foo != null) ? foo.hashCode() : 0);
085: result = (29 * result) + (executed ? 1 : 0);
086:
087: return result;
088: }
089:
090: /**
091: * Called after an Interceptor is created, but before any requests are processed using the intercept() methodName. This
092: * gives the Interceptor a chance to initialize any needed resources.
093: */
094: public void init() {
095: }
096:
097: /**
098: * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
099: * request by the DefaultActionInvocation or to short-circuit the processing and just return a String return code.
100: */
101: public String intercept(ActionInvocation invocation)
102: throws Exception {
103: executed = true;
104: Assert.assertNotSame(DEFAULT_FOO_VALUE, foo);
105: Assert.assertEquals(expectedFoo, foo);
106:
107: return invocation.invoke();
108: }
109: }
|