1 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 ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.net.URLStreamHandler ; 33 import java.net.URLStreamHandlerFactory ; 34 35 36 43 public abstract class AbstractTestCase extends TestCase { 44 protected static TestURLHandler testURLHandler; 45 protected static final File resourceBaseDir; 46 47 static { 48 String projectBaseDir = System.getProperty("basedir"); 50 51 if (projectBaseDir == null) { 52 projectBaseDir = "core"; 53 } 54 resourceBaseDir = new File (projectBaseDir, "src/test"); 55 56 URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory () { 58 public URLStreamHandler createURLStreamHandler(final String protocol) { 59 if ("tst".equals(protocol)) { 60 return new URLStreamHandler () { 61 protected URLConnection openConnection(final URL u) { 62 return new URLConnection (u) { 63 public void connect() { 64 } 65 66 public OutputStream getOutputStream() throws IOException { 67 return testURLHandler.getOutputStream(u); 68 } 69 70 public InputStream getInputStream() throws IOException { 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 99 protected URL getResource(final String path) { 100 try { 101 return IOUtils.toUrl(new File (resourceBaseDir, path)); 102 } catch (MalformedURLException e) { 103 throw new IllegalStateException (e.getMessage(), e); 104 } 105 } 106 107 protected EtlExecutor newEtlExecutor() { 108 String name = getClass().getSimpleName()+".xml"; 109 return newEtlExecutor(name); 110 } 111 112 protected EtlExecutor newEtlExecutor(final String 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 path) { 121 ConfigurationFactory cf = newConfigurationFactory(); 122 final URL resource = getClass().getResource(path); 123 124 if (resource == null) { 125 throw new IllegalStateException ("Resource " + path + " not found"); 126 } 127 128 cf.setResourceURL(resource); 129 130 return cf.createConfiguration(); 131 } 132 133 136 protected ConfigurationFactory newConfigurationFactory() { 137 return new ConfigurationFactory(); 138 } 139 140 146 protected String removeExtraWhitespaces(String s) { 147 return s.replaceAll("\\s+", " ").trim(); 148 } 149 150 protected static interface TestURLHandler { 151 public InputStream getInputStream(final URL u) 152 throws IOException ; 153 public OutputStream getOutputStream(final URL u) 154 throws IOException ; 155 156 public int getContentLength(final URL u); 157 } 158 159 164 protected static String asString(final Resource content) { 165 try { 166 return IOUtils.toString(content.open()); 167 } catch (IOException e) { 168 throw new IllegalStateException (e); 169 } 170 } 171 172 } 173 | Popular Tags |