001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella;
017:
018: import junit.framework.TestCase;
019: import scriptella.configuration.ConfigurationEl;
020: import scriptella.configuration.ConfigurationFactory;
021: import scriptella.execution.EtlExecutor;
022: import scriptella.spi.Resource;
023: import scriptella.util.IOUtils;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.net.URLConnection;
032: import java.net.URLStreamHandler;
033: import java.net.URLStreamHandlerFactory;
034:
035: /**
036: * TODO: Add documentation
037: * TODO: add integration testcase subclass.
038: *
039: * @author Fyodor Kupolov
040: * @version 1.0
041: */
042: public abstract class AbstractTestCase extends TestCase {
043: protected static TestURLHandler testURLHandler;
044: protected static final File resourceBaseDir;
045:
046: static {
047: //If running under maven/ant - we use basedir
048: String projectBaseDir = System.getProperty("basedir");
049:
050: if (projectBaseDir == null) {
051: projectBaseDir = "core";
052: }
053: resourceBaseDir = new File(projectBaseDir, "src/test");
054:
055: //Registering tst URL, subclasses should set testUrlStreamHandler in test method if they use "tst" url
056: URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
057: public URLStreamHandler createURLStreamHandler(
058: final String protocol) {
059: if ("tst".equals(protocol)) {
060: return new URLStreamHandler() {
061: protected URLConnection openConnection(
062: final URL u) {
063: return new URLConnection(u) {
064: public void connect() {
065: }
066:
067: public OutputStream getOutputStream()
068: throws IOException {
069: return testURLHandler
070: .getOutputStream(u);
071: }
072:
073: public InputStream getInputStream()
074: throws IOException {
075: return testURLHandler
076: .getInputStream(u);
077: }
078:
079: public int getContentLength() {
080: return testURLHandler
081: .getContentLength(u);
082: }
083: };
084: }
085: };
086: }
087:
088: return null;
089: }
090: });
091: }
092:
093: public AbstractTestCase() {
094: setName(getClass().getSimpleName());
095: testURLHandler = null;
096: }
097:
098: /**
099: * This method returns file with path relative to project's src/test directory
100: *
101: * @param path file path relative to project's src/test directory
102: * @return file with path relative to project's src/test directory
103: */
104: protected URL getResource(final String path) {
105: try {
106: return IOUtils.toUrl(new File(resourceBaseDir, path));
107: } catch (MalformedURLException e) {
108: throw new IllegalStateException(e.getMessage(), e);
109: }
110: }
111:
112: protected EtlExecutor newEtlExecutor() {
113: String name = getClass().getSimpleName() + ".xml";
114: return newEtlExecutor(name);
115: }
116:
117: protected EtlExecutor newEtlExecutor(final String path) {
118: return newEtlExecutor(loadConfiguration(path));
119: }
120:
121: protected EtlExecutor newEtlExecutor(
122: final ConfigurationEl configuration) {
123: return new EtlExecutor(configuration);
124: }
125:
126: protected ConfigurationEl loadConfiguration(final String path) {
127: ConfigurationFactory cf = newConfigurationFactory();
128: final URL resource = getClass().getResource(path);
129:
130: if (resource == null) {
131: throw new IllegalStateException("Resource " + path
132: + " not found");
133: }
134:
135: cf.setResourceURL(resource);
136:
137: return cf.createConfiguration();
138: }
139:
140: /**
141: * Overridable method to allow factory customization.
142: */
143: protected ConfigurationFactory newConfigurationFactory() {
144: return new ConfigurationFactory();
145: }
146:
147: /**
148: * Removes extra whitespace characters.
149: *
150: * @param s string to replace.
151: * @return s with extra whitespace chars removed.
152: */
153: protected String removeExtraWhitespaces(String s) {
154: return s.replaceAll("\\s+", " ").trim();
155: }
156:
157: protected static interface TestURLHandler {
158: public InputStream getInputStream(final URL u)
159: throws IOException;
160:
161: public OutputStream getOutputStream(final URL u)
162: throws IOException;
163:
164: public int getContentLength(final URL u);
165: }
166:
167: /**
168: * Converts a specified resource to String.
169: * @param content content to convert.
170: * @return resource content as String.
171: */
172: protected static String asString(final Resource content) {
173: try {
174: return IOUtils.toString(content.open());
175: } catch (IOException e) {
176: throw new IllegalStateException(e);
177: }
178: }
179:
180: }
|