KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > AbstractTestCase


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella;
17
18 import junit.framework.TestCase;
19 import scriptella.configuration.ConfigurationEl;
20 import scriptella.configuration.ConfigurationFactory;
21 import scriptella.execution.EtlExecutor;
22 import scriptella.spi.Resource;
23 import scriptella.util.IOUtils;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.net.URLStreamHandler JavaDoc;
33 import java.net.URLStreamHandlerFactory JavaDoc;
34
35
36 /**
37  * TODO: Add documentation
38  * TODO: add integration testcase subclass.
39  *
40  * @author Fyodor Kupolov
41  * @version 1.0
42  */

43 public abstract class AbstractTestCase extends TestCase {
44     protected static TestURLHandler testURLHandler;
45     protected static final File JavaDoc resourceBaseDir;
46
47     static {
48         //If running under maven/ant - we use basedir
49
String JavaDoc projectBaseDir = System.getProperty("basedir");
50
51         if (projectBaseDir == null) {
52             projectBaseDir = "core";
53         }
54         resourceBaseDir = new File JavaDoc(projectBaseDir, "src/test");
55         
56         //Registering tst URL, subclasses should set testUrlStreamHandler in test method if they use "tst" url
57
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory JavaDoc() {
58             public URLStreamHandler JavaDoc createURLStreamHandler(final String JavaDoc protocol) {
59                 if ("tst".equals(protocol)) {
60                     return new URLStreamHandler JavaDoc() {
61                         protected URLConnection JavaDoc openConnection(final URL JavaDoc u) {
62                             return new URLConnection JavaDoc(u) {
63                                 public void connect() {
64                                 }
65
66                                 public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
67                                     return testURLHandler.getOutputStream(u);
68                                 }
69
70                                 public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
71                                     return testURLHandler.getInputStream(u);
72                                 }
73
74                                 public int getContentLength() {
75                                     return testURLHandler.getContentLength(u);
76                                 }
77                             };
78                         }
79                     };
80                 }
81
82                 return null;
83             }
84         });
85     }
86
87     public AbstractTestCase() {
88         setName(getClass().getSimpleName());
89         testURLHandler=null;
90     }
91
92
93     /**
94      * This method returns file with path relative to project's src/test directory
95      *
96      * @param path file path relative to project's src/test directory
97      * @return file with path relative to project's src/test directory
98      */

99     protected URL JavaDoc getResource(final String JavaDoc path) {
100         try {
101             return IOUtils.toUrl(new File JavaDoc(resourceBaseDir, path));
102         } catch (MalformedURLException JavaDoc e) {
103             throw new IllegalStateException JavaDoc(e.getMessage(), e);
104         }
105     }
106
107     protected EtlExecutor newEtlExecutor() {
108         String JavaDoc name = getClass().getSimpleName()+".xml";
109         return newEtlExecutor(name);
110     }
111
112     protected EtlExecutor newEtlExecutor(final String JavaDoc path) {
113         return newEtlExecutor(loadConfiguration(path));
114     }
115
116     protected EtlExecutor newEtlExecutor(final ConfigurationEl configuration) {
117         return new EtlExecutor(configuration);
118     }
119
120     protected ConfigurationEl loadConfiguration(final String JavaDoc path) {
121         ConfigurationFactory cf = newConfigurationFactory();
122         final URL JavaDoc resource = getClass().getResource(path);
123
124         if (resource == null) {
125             throw new IllegalStateException JavaDoc("Resource " + path + " not found");
126         }
127
128         cf.setResourceURL(resource);
129
130         return cf.createConfiguration();
131     }
132
133     /**
134      * Overridable method to allow factory customization.
135      */

136     protected ConfigurationFactory newConfigurationFactory() {
137         return new ConfigurationFactory();
138     }
139
140     /**
141      * Removes extra whitespace characters.
142      *
143      * @param s string to replace.
144      * @return s with extra whitespace chars removed.
145      */

146     protected String JavaDoc removeExtraWhitespaces(String JavaDoc s) {
147         return s.replaceAll("\\s+", " ").trim();
148     }
149
150     protected static interface TestURLHandler {
151         public InputStream JavaDoc getInputStream(final URL JavaDoc u)
152                 throws IOException JavaDoc;
153         public OutputStream JavaDoc getOutputStream(final URL JavaDoc u)
154                 throws IOException JavaDoc;
155
156         public int getContentLength(final URL JavaDoc u);
157     }
158
159     /**
160      * Converts a specified resource to String.
161      * @param content content to convert.
162      * @return resource content as String.
163      */

164     protected static String JavaDoc asString(final Resource content) {
165         try {
166             return IOUtils.toString(content.open());
167         } catch (IOException JavaDoc e) {
168             throw new IllegalStateException JavaDoc(e);
169         }
170     }
171
172 }
173
Popular Tags