001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.extension.applet;
003:
004: import java.io.IOException;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007:
008: import org.apache.tools.ant.Project;
009: import org.apache.tools.ant.types.CommandlineJava;
010: import org.apache.tools.ant.types.Environment;
011: import org.apache.tools.ant.types.Path;
012: import org.jaxen.JaxenException;
013: import org.netbeans.jemmy.JemmyException;
014: import org.xml.sax.SAXException;
015:
016: import com.canoo.webtest.engine.Configuration;
017: import com.canoo.webtest.engine.Context;
018: import com.canoo.webtest.engine.StepExecutionException;
019: import com.canoo.webtest.engine.StepFailedException;
020: import com.canoo.webtest.interfaces.IPropertyHandler;
021: import com.canoo.webtest.self.ContextStub;
022: import com.canoo.webtest.self.TestBlock;
023: import com.canoo.webtest.self.ThrowAssert;
024: import com.canoo.webtest.steps.BaseStepTestCase;
025: import com.canoo.webtest.steps.Step;
026: import com.gargoylesoftware.htmlunit.Page;
027: import com.gargoylesoftware.htmlunit.WebRequestSettings;
028: import com.gargoylesoftware.htmlunit.html.HtmlElement;
029: import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
030:
031: /**
032: * @author Denis N. Antonioli
033: * @author Marc Guillemot
034: */
035: public class AppletRunnerStepTest extends BaseStepTestCase {
036:
037: public static class AppletRunnerStepStub extends AppletRunnerStep {
038: private String fUrl;
039:
040: public String getUrl() {
041: return fUrl;
042: }
043:
044: public AppletRunnerStepStub() {
045: }
046:
047: public AppletRunnerStepStub(final String xpath) {
048: setXpath(xpath);
049: setScenario("AJemmyTest");
050: }
051:
052: public Page findTargetWithResults(
053: final AppletPluginResults results, final Context context)
054: throws IOException, SAXException {
055: return super .findTargetWithResults(results, context);
056: }
057:
058: public Page findTarget() throws JaxenException, IOException,
059: SAXException {
060: return super .findTarget();
061: }
062:
063: protected Page getResponse(final WebRequestSettings _settings) {
064: fUrl = _settings.getURL().toExternalForm();
065: return null;
066: }
067: }
068:
069: private static final String DOCUMENT = "<html><head></head><body>"
070: + "<applet\n"
071: + "\tcode=\"Applet.class\"\n"
072: + "\tarchive=\"SwingApplet.jar\"\n"
073: + "\twidth=\"600\"\n"
074: + "\theight=\"95\"\n"
075: + "\t>\n"
076: + "\t\t<param name=\"p1\" value=\"1\" />\n"
077: + "Your browser is completely ignoring the <APPLET> tag!\n"
078: + "</applet>" + "</body></html>";
079: private static final String XPATH = "//input[@id='id']/@value";
080:
081: protected Step createStep() {
082: return new AppletRunnerStep();
083: }
084:
085: public static void testVerifyParametersXpath() throws Exception {
086: final AppletRunnerStep step = new AppletRunnerStepStub();
087: step.setScenario("foo");
088: assertStepRejectsNullParam("xpath", new TestBlock() {
089: public void call() throws Exception {
090: executeStep(step);
091: }
092: });
093: step.setXpath(XPATH);
094: assertEquals("Xpath attribute should be set", XPATH, step
095: .getXpath());
096: }
097:
098: public static void testVerifyParametersScenarioMissing()
099: throws Exception {
100: final AppletRunnerStep step = new AppletRunnerStepStub();
101:
102: step.setXpath("foo");
103: assertStepRejectsNullParam("scenario", new TestBlock() {
104: public void call() throws Exception {
105: executeStep(step);
106: }
107: });
108: step.setScenario("foo");
109: assertEquals("Scenario attribute should be set", "foo", step
110: .getScenario());
111: }
112:
113: public void testRejectsNullCurrentResponse() throws Exception {
114: final AppletRunnerStep step = new AppletRunnerStepStub(XPATH);
115: assertStepRejectsNullResponse(step);
116: }
117:
118: public void testRejectsNonHtmlResponse() throws Exception {
119: final AppletRunnerStep step = new AppletRunnerStepStub(XPATH);
120: getContext().setDefaultResponse("<foo></foo>", "text/xml");
121:
122: final TestBlock block = new TestBlock() {
123: public void call() throws Exception {
124: executeStep(step);
125: }
126: };
127: ThrowAssert.assertThrows(StepExecutionException.class,
128: "Current response is not an HTML page", block);
129: }
130:
131: public void testExceptionIfNoMatch() throws Exception {
132: final AppletRunnerStep step = new AppletRunnerStepStub(
133: "//input[@id='id']/@value");
134: getContext().setDefaultResponse(DOCUMENT);
135:
136: ThrowAssert.assertThrows(StepFailedException.class,
137: "The specified element <", new TestBlock() {
138: public void call() throws Exception {
139: executeStep(step);
140: }
141: });
142: }
143:
144: public void testExceptionIfXpathError() throws Exception {
145: final AppletRunnerStep step = new AppletRunnerStepStub(
146: "//input[@id='id'/@value");
147: getContext().setDefaultResponse(DOCUMENT);
148:
149: ThrowAssert.assertThrows(StepExecutionException.class,
150: "Xpath error for '", new TestBlock() {
151: public void call() throws Exception {
152: executeStep(step);
153: }
154: });
155: }
156:
157: public static void testExtractAppletParameter() throws Exception {
158: final URL url = new URL("http://localhost:9090/index.html");
159: final Page currentResponse = getContextForDocument(
160: "<html><body>"
161: + "<applet id='0' />\n"
162: + "<applet code=\"Applet.class\" width=\"600\" height=\"95\" id='2' />\n"
163: + "</body></html>").getCurrentResponse();
164: final AppletRunnerStep step = new AppletRunnerStepStub();
165:
166: ThrowAssert.assertThrows(StepFailedException.class,
167: "Don't know how to handle element <body>.",
168: new TestBlock() {
169: public void call() throws Exception {
170: step
171: .extractAppletParameter(
172: url,
173: (HtmlElement) new HtmlUnitXPath(
174: "/html/body")
175: .selectSingleNode(currentResponse));
176: }
177: });
178: ThrowAssert.assertThrows(StepFailedException.class,
179: "Applet element does not define the code.",
180: new TestBlock() {
181: public void call() throws Exception {
182: step
183: .extractAppletParameter(
184: url,
185: (HtmlElement) new HtmlUnitXPath(
186: "/html/body/applet[@id='0']")
187: .selectSingleNode(currentResponse));
188: }
189: });
190:
191: final Object ap = step.extractAppletParameter(url,
192: (HtmlElement) new HtmlUnitXPath(
193: "/html/body/applet[@id='2']")
194: .selectSingleNode(currentResponse));
195:
196: assertNotNull(ap);
197: assertTrue(ap instanceof AppletTag);
198: }
199:
200: /**
201: * @deprecated to remove when {@link AppletRunnerStep#addParam(Parameter)} is removed
202: * @noinspection Deprecation
203: */
204: public static void testAddParam() {
205:
206: final AppletRunnerStep step = new AppletRunnerStepStub();
207: final Parameter param = new Parameter("fooo", "foo");
208: step.addParam(param);
209:
210: assertSame(param, step.getParameters().next());
211: }
212:
213: /*
214: * TODO: expansion is made by PropertyHelper: test really needed?
215: public void testParameters() {
216: final AppletRunnerStep step = new AppletRunnerStepStub();
217: configureStep(step);
218:
219: final String key1 = "withoutExpansion";
220: final String value1 = "value";
221: final String key2 = "withExpansion";
222: final String value2 = "expandedValue";
223:
224: step.addParameter(new Parameter(key1, value1));
225: step.addParameter(new Parameter(key2, "#{x}"));
226: step.setWebtestProperty("x", value2);
227:
228: final Iterator iter = step.getParameters();
229: assertTrue(iter.hasNext());
230: Parameter param = (Parameter) iter.next();
231:
232: assertEquals(key1, param.getName());
233: assertEquals(value1, param.getValue());
234: param = (Parameter) iter.next();
235: assertEquals(key2, param.getName());
236: assertEquals(value2, param.getValue());
237: assertFalse(iter.hasNext());
238: }
239: */
240: public static void testTarget() {
241: final AppletRunnerStep step = new AppletRunnerStepStub();
242: assertNull(step.getTarget());
243: final String aTarget = "_top";
244: step.setTarget(aTarget);
245: assertSame(aTarget, step.getTarget());
246: }
247:
248: public void testFollowTargetNoTargetNoShow() throws Exception {
249: final AppletPluginResults results = new AppletPluginResults();
250: final AppletRunnerStep step = new AppletRunnerStepStub();
251: assertNull(step.findTargetWithResults(results, getContext()));
252: }
253:
254: public void testFollowTargetTargetNoShow() throws Exception {
255: final AppletRunnerStep step = new AppletRunnerStepStub();
256: step.setTarget("_blank");
257: ThrowAssert.assertThrows(StepFailedException.class,
258: new TestBlock() {
259: public void call() throws Exception {
260: final AppletPluginResults results = new AppletPluginResults();
261: step.findTargetWithResults(results,
262: getContext());
263: }
264: });
265: }
266:
267: public void testFollowTargetTargetShowWrong() throws Exception {
268: final AppletPluginResults apr = new AppletPluginResults();
269: apr.setFrame("_blank", new URL("http://webtest.canoo.com/"));
270: final AppletRunnerStep step = new AppletRunnerStepStub();
271: step.setTarget("_top");
272: ThrowAssert.assertThrows(StepFailedException.class,
273: new TestBlock() {
274: public void call() throws Exception {
275: step.findTargetWithResults(apr, getContext());
276: }
277: });
278: }
279:
280: public void testFollowTargetTargetShow() throws Exception {
281: final ContextStub context = new ContextStub() {
282: protected void initConfig(final Project project) {
283: final Configuration myConfig = new Configuration();
284: myConfig.setShowhtmlparseroutput(false);
285: getWebtest().addConfig(myConfig);
286: }
287: };
288: final IPropertyHandler handler = (IPropertyHandler) mock(
289: IPropertyHandler.class, "handler");
290: context.getConfig().setPropertyHandler(handler);
291: final AppletPluginResults apr = new AppletPluginResults();
292: apr.setFrame("_blank", new URL("http://webtest.canoo.com/"));
293: final AppletRunnerStepStub step = new AppletRunnerStepStub();
294: step.setTarget("_blank");
295: step.findTargetWithResults(apr, context);
296: assertEquals("http://webtest.canoo.com/", step.getUrl());
297: }
298:
299: public void testFollowTargetNoTargetShow() throws Exception {
300: final AppletPluginResults apr = new AppletPluginResults();
301: apr.setFrame("_blank", new URL("http://webtest.canoo.com/"));
302: final AppletRunnerStep step = new AppletRunnerStepStub();
303: assertNull(step.findTargetWithResults(apr, getContext()));
304: }
305:
306: public static void testVerifyAppletResultNoError() throws Exception {
307: final AppletPluginResults apr = new AppletPluginResults();
308: apr.setReturnValue(AppletRunnerStep.ZERO);
309: final AppletRunnerStep step = new AppletRunnerStepStub();
310: step.verifyAppletResult(apr);
311: }
312:
313: public static void testVerifyAppletResultException()
314: throws Exception {
315: final AppletPluginResults apr = new AppletPluginResults();
316: apr.setReturnValue(AppletRunnerStep.ZERO);
317: apr.setException(new ArrayIndexOutOfBoundsException());
318: final AppletRunnerStep step = new AppletRunnerStepStub();
319: ThrowAssert.assertThrows(StepFailedException.class,
320: new TestBlock() {
321: public void call() throws Exception {
322: step.verifyAppletResult(apr);
323: }
324: });
325: }
326:
327: public static void testVerifyAppletResultJemmyException()
328: throws Exception {
329: final AppletPluginResults apr = new AppletPluginResults();
330: apr.setReturnValue(AppletRunnerStep.ZERO);
331: apr.setJemmyException(new JemmyException("dummy"));
332: final AppletRunnerStep step = new AppletRunnerStepStub();
333: ThrowAssert.assertThrows(StepFailedException.class,
334: new TestBlock() {
335: public void call() throws Exception {
336: step.verifyAppletResult(apr);
337: }
338: });
339: }
340:
341: public static void testVerifyAppletResultErrorCode()
342: throws Exception {
343: final AppletPluginResults apr = new AppletPluginResults();
344: apr.setReturnValue(new Integer(42));
345: final AppletRunnerStep step = new AppletRunnerStepStub();
346: ThrowAssert.assertThrows(StepFailedException.class,
347: new TestBlock() {
348: public void call() throws Exception {
349: step.verifyAppletResult(apr);
350: }
351: });
352: }
353:
354: public static void testSetupLog4jOverrides() throws Exception {
355: final String saveInitOverride = System
356: .getProperty(AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES);
357: System.setProperty(
358: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES, "true");
359:
360: final CommandlineJava cmd = new CommandlineJava();
361: assertEquals(0, cmd.getSystemProperties().size());
362: AppletRunnerStep.setupLog4j(cmd);
363:
364: assertEquals(1, cmd.getSystemProperties().size());
365: assertEquals("-D"
366: + AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES
367: + "=true", cmd.getSystemProperties().getVariables()[0]);
368:
369: restoreSystemPropertyState(
370: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES,
371: saveInitOverride);
372: }
373:
374: public static void testSetupLog4jPlain() throws Exception {
375: final String saveInitOverride = System
376: .getProperty(AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES);
377: System.setProperty(
378: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES, "false");
379: final String saveConfig = System
380: .getProperty(AppletRunnerStep.LOG4J_CONFIGURATION);
381: System.getProperties().remove(
382: AppletRunnerStep.LOG4J_CONFIGURATION);
383:
384: final CommandlineJava cmd = new CommandlineJava();
385: assertEquals(0, cmd.getSystemProperties().size());
386: AppletRunnerStep.setupLog4j(cmd);
387:
388: final String[] variables = cmd.getSystemProperties()
389: .getVariables();
390: assertEquals(2, variables.length);
391: assertTrue(variables[0].startsWith("-D"
392: + AppletRunnerStep.LOG4J_CONFIGURATION));
393: assertTrue(variables[0].endsWith("log4j.properties"));
394: assertEquals("-D"
395: + AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES
396: + "=false", variables[1]);
397:
398: restoreSystemPropertyState(
399: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES,
400: saveInitOverride);
401: restoreSystemPropertyState(
402: AppletRunnerStep.LOG4J_CONFIGURATION, saveConfig);
403: }
404:
405: public static void testSetupLog4jOwnConfig() throws Exception {
406: final String saveInitOverride = System
407: .getProperty(AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES);
408: System.setProperty(
409: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES, "false");
410: final String saveConfig = System
411: .getProperty(AppletRunnerStep.LOG4J_CONFIGURATION);
412: System.setProperty(AppletRunnerStep.LOG4J_CONFIGURATION,
413: "This resource doesn't exist");
414:
415: final CommandlineJava cmd = new CommandlineJava();
416: assertEquals(0, cmd.getSystemProperties().size());
417: AppletRunnerStep.setupLog4j(cmd);
418:
419: final String[] variables = cmd.getSystemProperties()
420: .getVariables();
421: assertEquals(1, variables.length);
422: assertEquals("-D"
423: + AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES
424: + "=true", variables[0]);
425:
426: restoreSystemPropertyState(
427: AppletRunnerStep.LOG4J_DEFAULT_INIT_OVERRIDES,
428: saveInitOverride);
429: restoreSystemPropertyState(
430: AppletRunnerStep.LOG4J_CONFIGURATION, saveConfig);
431: }
432:
433: public static void testGetProtocolHandler() throws Exception {
434: final String saveHandlers = System
435: .getProperty(AppletRunnerStep.PROTOCOL_HANDLER_LIST);
436:
437: System.setProperty(AppletRunnerStep.PROTOCOL_HANDLER_LIST, "");
438: assertVariable(AppletRunnerStep.RUNNER_PACKAGE);
439:
440: final String pseudoHandlers = "dummy.handler.package";
441: System.setProperty(AppletRunnerStep.PROTOCOL_HANDLER_LIST,
442: pseudoHandlers);
443: assertVariable(AppletRunnerStep.RUNNER_PACKAGE + "|"
444: + pseudoHandlers);
445:
446: System.getProperties().remove(
447: AppletRunnerStep.PROTOCOL_HANDLER_LIST);
448: assertVariable(AppletRunnerStep.RUNNER_PACKAGE);
449:
450: restoreSystemPropertyState(
451: AppletRunnerStep.PROTOCOL_HANDLER_LIST, saveHandlers);
452: }
453:
454: private static void restoreSystemPropertyState(final String key,
455: final String value) {
456: if (value == null) {
457: System.getProperties().remove(key);
458: } else {
459: System.setProperty(key, value);
460: }
461: }
462:
463: private static void assertVariable(final String value) {
464: final Environment.Variable env = AppletRunnerStep
465: .getProtocolHandler();
466: assertEquals(AppletRunnerStep.PROTOCOL_HANDLER_LIST, env
467: .getKey());
468: assertEquals(value, env.getValue());
469: }
470:
471: public void testGetUrlForClass() throws MalformedURLException {
472: assertNull(AppletRunnerStep
473: .getUrlForClass("com.canoo.webtest.extension.applet.ThisClassDoesNotExist"));
474: assertNotNull(AppletRunnerStep.getUrlForClass(getClass()
475: .getName()));
476: }
477:
478: public static void testExtractClasspathEntryFromURL() {
479: assertEquals("/user/home/java/lib/", AppletRunnerStep
480: .extractClasspathEntry(
481: "/user/home/java/lib/com/foo/Bar.class",
482: "com/foo/Bar.class"));
483: assertEquals("/user/home/java/lib.jar", AppletRunnerStep
484: .extractClasspathEntry(
485: "/user/home/java/lib.jar!/com/foo/Bar.class",
486: "com/foo/Bar.class"));
487: }
488:
489: public static void testAppendOptionalToClasspathFail() {
490: final AppletRunnerStep step = new AppletRunnerStepStub();
491: final Path classpath = new Path(step.getProject());
492: assertEquals(0, classpath.list().length);
493: assertFalse(step
494: .appendOptionalToClasspath(
495: "com.canoo.webtest.extension.applet.ThisClassDoesNotExist",
496: classpath));
497: assertEquals(0, classpath.list().length);
498: }
499:
500: public void testAppendOptionalToClasspathOk() {
501: final AppletRunnerStep step = new AppletRunnerStepStub();
502: final Path classpath = new Path(step.getProject());
503: assertEquals(0, classpath.list().length);
504: assertTrue(step.appendOptionalToClasspath(getClass().getName(),
505: classpath));
506: assertEquals(1, classpath.list().length);
507: }
508:
509: public static void testAppendToClasspathFail() {
510: final AppletRunnerStep step = new AppletRunnerStepStub() {
511: boolean appendOptionalToClasspath(final String aClassName,
512: final Path classpath) {
513: return !AppletRunnerStep.class.getName().equals(
514: aClassName);
515: }
516: };
517: final Path classpath = new Path(step.getProject());
518:
519: assertEquals(0, classpath.list().length);
520: ThrowAssert.assertThrows(StepFailedException.class,
521: new TestBlock() {
522: public void call() throws Exception {
523: step.appendToClasspath(AppletRunnerStep.class,
524: classpath);
525: }
526: });
527: assertEquals(0, classpath.list().length);
528: }
529:
530: public void testAppendToClasspathOk() {
531: final AppletRunnerStep step = new AppletRunnerStepStub();
532: final Path classpath = new Path(step.getProject());
533:
534: assertEquals(0, classpath.list().length);
535: step.appendToClasspath(getClass(), classpath);
536: assertEquals(1, classpath.list().length);
537: }
538:
539: public static void testMapPathToURL() {
540: final AppletRunnerStep step = new AppletRunnerStepStub();
541: assertEquals(0, step.convertPathToURL(null).length);
542: assertEquals(0, step.convertPathToURL(new Path(step
543: .getProject())).length);
544: }
545:
546: public void testCreateAppletPluginArguments() {
547: final AppletRunnerStep step = new AppletRunnerStepStub();
548:
549: final String testName = "testName";
550:
551: getContext().getWebtest().setName(testName);
552:
553: AppletPluginArguments appletPluginArguments = step
554: .createAppletPluginArguments();
555: assertEquals(testName, appletPluginArguments
556: .getBaseWindowName());
557:
558: final String description = "desc";
559: step.setDescription(description);
560: appletPluginArguments = step.createAppletPluginArguments();
561: assertEquals(testName + " - " + description,
562: appletPluginArguments.getBaseWindowName());
563:
564: }
565: }
|