001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.request;
003:
004: import java.net.SocketTimeoutException;
005:
006: import org.apache.log4j.Level;
007: import org.apache.log4j.Logger;
008: import org.apache.tools.ant.Project;
009:
010: import com.agical.rmock.extension.junit.RMockTestCase;
011: import com.canoo.webtest.engine.Configuration;
012: import com.canoo.webtest.engine.Context;
013: import com.canoo.webtest.engine.StepExecutionException;
014: import com.canoo.webtest.engine.StepFailedException;
015: import com.canoo.webtest.interfaces.IConnectionInitializer;
016: import com.canoo.webtest.interfaces.IPropertyHandler;
017: import com.canoo.webtest.security.ConnectionInitializationException;
018: import com.canoo.webtest.self.BufferingAppender;
019: import com.canoo.webtest.self.ContextStub;
020: import com.canoo.webtest.self.TestBlock;
021: import com.canoo.webtest.self.ThrowAssert;
022: import com.canoo.webtest.steps.AbstractBrowserAction;
023: import com.canoo.webtest.steps.BaseStepTestCase;
024: import com.canoo.webtest.steps.StepUtil;
025:
026: /**
027: * @author Paul King
028: * @author Marc Guillemot
029: */
030: public class TargetHelperTest extends RMockTestCase {
031: private static final String OK_CUSTOM_INITIALIZER_CLASS_NAME = "com.canoo.webtest.steps.request.TargetHelperTest$NullConnectionInitializer";
032: private static final String BAD_CUSTOM_INITIALIZER_CLASS_NAME = "com.canoo.webtest.steps.request.TargetHelperTest$ExceptionConnectionInitializer";
033:
034: private AbstractBrowserAction fBrowserAction = new AbstractBrowserAction() {
035: public void doExecute() {
036: }
037: };
038: private TargetHelper fTargetHelper = new TargetHelper(
039: fBrowserAction);
040: private final Context fTestContext = createContextStupWithoutSteps();
041: private final BufferingAppender fBuffAppender = new BufferingAppender();
042:
043: static ContextStub createContextStupWithoutSteps() {
044: return new ContextStub() {
045: protected void initTestStepSequence(Project project) {
046: // nothing, we want to skip it here
047: }
048: };
049: }
050:
051: protected void setUp() throws Exception {
052: final Configuration testConfig = new Configuration();
053: testConfig.setShowhtmlparseroutput(false);
054: final IPropertyHandler handler = (IPropertyHandler) mock(
055: IPropertyHandler.class, "handler");
056: testConfig.setPropertyHandler(handler);
057: fTestContext.getWebtest().addConfig(testConfig);
058:
059: BaseStepTestCase.executeStep(fBrowserAction); // coverage
060:
061: Logger.getRootLogger().setLevel(Level.INFO); // ensure that the logged event we want to catch is not discarded
062: Logger.getRootLogger().addAppender(fBuffAppender);
063: }
064:
065: protected void tearDown() throws Exception {
066: super .tearDown();
067: Logger.getRootLogger().removeAppender(fBuffAppender);
068: }
069:
070: public void testGotoTargetByUrl() throws Exception {
071: startVerification();
072: fBuffAppender.getEvents().clear();
073: try {
074: fTargetHelper.getResponse(fTestContext, "targetUrl");
075: } catch (final Exception e) {
076: /* ignore */
077: }
078: assertTrue("should be called by url", fBuffAppender
079: .allMessagesToString().indexOf("url") > -1);
080: }
081:
082: /**
083: * Tests that SocketTimeoutException is catched and that a StepFailedException is rethrown
084: * @throws Exception
085: */
086: public void testSocketTimeoutException() throws Exception {
087: final String message = ThrowAssert.assertThrows(
088: StepFailedException.class, new TestBlock() {
089: public void call() throws Exception {
090: final Throwable t = new SocketTimeoutException(
091: "blah SocketTimeoutException message");
092: StepUtil.handleException(t);
093: }
094: });
095: assertEquals(
096: "Server took to long to answer: blah SocketTimeoutException message",
097: message);
098: }
099:
100: public void testInvokeCustomInitializer() throws Exception {
101: ThrowAssert.assertThrows(StepExecutionException.class,
102: new TestBlock() {
103: public void call() throws Exception {
104: fTargetHelper.invokeCustomInitializer(
105: fTestContext, "no.such.class");
106: }
107: });
108: final String message = ThrowAssert.assertThrows(
109: StepExecutionException.class, new TestBlock() {
110: public void call() throws Exception {
111: fTargetHelper.invokeCustomInitializer(
112: fTestContext,
113: BAD_CUSTOM_INITIALIZER_CLASS_NAME);
114: }
115: });
116: assertTrue(message
117: .startsWith("ConnectionInitializer raised exception"));
118: }
119:
120: public void testPrepareConversationWithProperCustomInitializer()
121: throws Exception {
122: final IPropertyHandler customHandler = (IPropertyHandler) mock(
123: IPropertyHandler.class, "customHandler");
124: customHandler
125: .getProperty(TargetHelper.CONNECTION_INITIALIZER_KEY);
126: modify().returnValue(OK_CUSTOM_INITIALIZER_CLASS_NAME);
127: startVerification();
128: fTestContext.getConfig().setPropertyHandler(customHandler);
129: ThrowAssert.assertPasses("normal pass", new TestBlock() {
130: public void call() throws Exception {
131: fTargetHelper.prepareConversationIfNeeded(fTestContext);
132: }
133: });
134: }
135:
136: public static class ExceptionConnectionInitializer implements
137: IConnectionInitializer {
138: public void initializeConnection(final Configuration config)
139: throws ConnectionInitializationException {
140: throw new ConnectionInitializationException("xxx");
141: }
142: }
143:
144: public static class NullConnectionInitializer implements
145: IConnectionInitializer {
146: public void initializeConnection(final Configuration config)
147: throws ConnectionInitializationException {
148: // just pass
149: }
150: }
151:
152: }
|