01: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
02: package com.canoo.webtest.boundary;
03:
04: import com.canoo.webtest.steps.Step;
05: import com.canoo.webtest.engine.StepExecutionException;
06: import org.apache.log4j.Logger;
07:
08: import java.net.MalformedURLException;
09: import java.net.URL;
10: import java.io.File;
11:
12: /**
13: * Helper class for working with URLs.
14: *
15: * @author Paul King
16: */
17: public final class UrlBoundary {
18: private static final Logger LOG = Logger
19: .getLogger(UrlBoundary.class);
20:
21: private UrlBoundary() {
22: }
23:
24: /**
25: * Helper method to create URLs.
26: *
27: * @param urlStr
28: * @return the created URL
29: * @throws java.lang.RuntimeException if the URL could not be created
30: */
31: public static URL tryCreateUrl(final String urlStr) {
32: try {
33: return new URL(urlStr);
34: } catch (MalformedURLException e) {
35: LOG.error("Creating URL '" + urlStr + "' failed: "
36: + e.getMessage());
37: throw new RuntimeException(e);
38: }
39: }
40:
41: /**
42: * Helper method to create URLs.
43: *
44: * @param urlStr
45: * @return the created URL
46: * @throws java.lang.RuntimeException if the URL could not be created
47: */
48: public static URL tryCreateUrlWithError(final URL resource,
49: final String urlStr, final Step step) {
50: try {
51: return new URL(resource, urlStr);
52: } catch (MalformedURLException e) {
53: LOG.error("Creating URL '" + urlStr + "' failed: "
54: + e.getMessage());
55: throw new StepExecutionException(e.getMessage(), step);
56: }
57: }
58:
59: /**
60: * Helper method to create URLs.
61: *
62: * @param urlFile
63: * @return the created URL
64: */
65: public static URL tryCreateUrlFromFileWithError(final File urlFile,
66: final Step step) {
67: try {
68: return urlFile.toURI().toURL();
69: } catch (MalformedURLException e) {
70: LOG.error("Creating URL for File '" + urlFile.getName()
71: + "' failed: " + e.getMessage());
72: throw new StepExecutionException(e.getMessage(), step);
73: }
74: }
75: }
|