[OLINGO-377] Code clean up

This commit is contained in:
mibo 2014-07-26 11:41:54 +02:00
parent d343506700
commit 49a38afc4e
2 changed files with 130 additions and 33 deletions

View File

@ -59,7 +59,7 @@
<dependency> <dependency>
<groupId>org.apache.cxf</groupId> <groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId> <artifactId>cxf-rt-rs-client</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.cxf</groupId> <groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-security-oauth2</artifactId> <artifactId>cxf-rt-rs-security-oauth2</artifactId>
@ -137,7 +137,6 @@
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
@ -161,8 +160,6 @@
</configuration> </configuration>
</plugin> </plugin>
<!--
-->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId> <artifactId>maven-dependency-plugin</artifactId>
@ -200,8 +197,7 @@
</instrumentation> </instrumentation>
</configuration> </configuration>
</plugin> </plugin>
<!--
-->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId> <artifactId>maven-invoker-plugin</artifactId>
@ -220,15 +216,43 @@
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
<id>integration-test</id> <id>pojogen-integration-test</id>
<phase>integration-test</phase>
<goals> <goals>
<!--<goal>integration-test</goal>--> <goal>integration-test</goal>
<!--<goal>verify</goal>--> <goal>verify</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*ITCase.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.apache.olingo.fit.server.TomcatTestServer</mainClass>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
@ -256,19 +280,6 @@
</configuration> </configuration>
</plugin> </plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<systemPropertyVariables>
<propertyName>org.slf4j.simpleLogger.defaultLogLevel</propertyName>
<org.slf4j.simpleLogger.defaultLogLevel>DEBUG</org.slf4j.simpleLogger.defaultLogLevel>
</systemPropertyVariables>
</configuration>
</plugin>
-->
</plugins> </plugins>
</build> </build>

View File

@ -26,10 +26,17 @@ import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.naming.directory.DirContext; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
/** /**
@ -48,6 +55,52 @@ public class TomcatTestServer {
this.tomcat = tomcat; this.tomcat = tomcat;
} }
public static void main(String[] params) {
try {
LOG.trace("Start tomcat embedded server from main()");
TomcatTestServer server = TomcatTestServer.init(9080)
.addStaticContent("/stub/StaticService/V30/Static.svc/$metadata", "V30/metadata.xml")
.addStaticContent("/stub/StaticService/V30/ActionOverloading.svc/$metadata",
"V30/actionOverloadingMetadata.xml")
.addStaticContent("/stub/StaticService/V30/OpenType.svc/$metadata", "V30/openTypeMetadata.xml")
.addStaticContent("/stub/StaticService/V30/PrimitiveKeys.svc/$metadata", "V30/primitiveKeysMetadata.xml")
.addStaticContent("/stub/StaticService/V40/OpenType.svc/$metadata", "V40/openTypeMetadata.xml")
.addStaticContent("/stub/StaticService/V40/Demo.svc/$metadata", "V40/demoMetadata.xml")
.addStaticContent("/stub/StaticService/V40/Static.svc/$metadata", "V40/metadata.xml")
.start();
} catch (Exception e) {
throw new RuntimeException("Failed to start Tomcat server from main method.", e);
}
}
public static class StaticContent extends HttpServlet {
private final String uri;
private final String resource;
public StaticContent(String uri, String resource) {
this.uri = uri;
this.resource = resource;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
StringHelper.Stream st;
File resourcePath = new File(resource);
if(resourcePath.exists() && resourcePath.isFile()) {
FileInputStream fin = new FileInputStream(resourcePath);
st = StringHelper.toStream(fin);
LOG.info("Mapped uri '{}' to resource '{}'.", uri, resource);
LOG.trace("Resource content {\n\n{}\n\n}", st.asString());
} else {
LOG.debug("Unable to load resource for path {} as stream.", uri);
st = StringHelper.toStream("<html><head/><body>No resource for path found</body>");
}
resp.getOutputStream().write(st.asString().getBytes());
}
}
private static TestServerBuilder builder; private static TestServerBuilder builder;
public static TestServerBuilder init(int port) { public static TestServerBuilder init(int port) {
if(builder == null) { if(builder == null) {
@ -59,13 +112,14 @@ public class TomcatTestServer {
public static class TestServerBuilder { public static class TestServerBuilder {
private final Tomcat tomcat; private final Tomcat tomcat;
private final File baseDir; private final File baseDir;
private final File projectTarget;
private TomcatTestServer server; private TomcatTestServer server;
private TestServerBuilder(int fixedPort) { private TestServerBuilder(int fixedPort) {
tomcat = new Tomcat(); tomcat = new Tomcat();
tomcat.setPort(fixedPort); tomcat.setPort(fixedPort);
//baseDir = new File(System.getProperty("java.io.tmpdir"), "tomcat-test"); //baseDir = new File(System.getProperty("java.io.tmpdir"), "tomcat-test");
File projectTarget = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile()); projectTarget = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile());
// projectTarget == ...fit/target/test-classes // projectTarget == ...fit/target/test-classes
baseDir = new File(projectTarget, "../emb-tom-fit"); baseDir = new File(projectTarget, "../emb-tom-fit");
if(!baseDir.exists() && !baseDir.mkdirs()) { if(!baseDir.exists() && !baseDir.mkdirs()) {
@ -95,15 +149,28 @@ public class TomcatTestServer {
FileUtils.copyDirectory(webAppProjectDir, webAppDir); FileUtils.copyDirectory(webAppProjectDir, webAppDir);
File libDir = new File(webAppDir, "WEB-INF/lib"); File libDir = new File(webAppDir, "WEB-INF/lib");
File classesDir = new File(webAppDir, "WEB-INF/classes"); File classesDir = new File(webAppDir, "WEB-INF/classes");
String[] libs = new String[]{"olingo-client-proxy-0.1.0-SNAPSHOT.jar", String[] libsToExtract = new String[]{
"olingo-client-proxy-0.1.0-SNAPSHOT.jar",
"olingo-commons-api-0.1.0-SNAPSHOT.jar", "olingo-commons-api-0.1.0-SNAPSHOT.jar",
"olingo-commons-core-0.1.0-SNAPSHOT.jar"}; "olingo-commons-core-0.1.0-SNAPSHOT.jar"
for (String lib : libs) { };
for (String lib : libsToExtract) {
File libFile = new File(libDir, lib); File libFile = new File(libDir, lib);
extract(libFile, classesDir); extract(libFile, classesDir);
FileUtils.forceDelete(libFile); FileUtils.forceDelete(libFile);
} }
String[] libsToRemove = new String[]{
"javax.ws.rs-api-2.0.jar",
"maven-scm-api-1.4.jar",
"maven-scm-provider-svn-commons-1.4.jar",
"maven-scm-provider-svnexe-1.4.jar",
"tomcat-embed-logging-juli-7.0.54.jar",
"tomcat-embed-core-7.0.54.jar"};
for (String lib : libsToRemove) {
FileUtils.forceDelete(new File(libDir, lib));
}
String contextPath = "/stub"; // contextFile.getName() String contextPath = "/stub"; // contextFile.getName()
tomcat.addWebapp(tomcat.getHost(), contextPath, webAppDir.getAbsolutePath()); tomcat.addWebapp(tomcat.getHost(), contextPath, webAppDir.getAbsolutePath());
LOG.info("Webapp {} at context {}.", webAppDir.getName(), contextPath); LOG.info("Webapp {} at context {}.", webAppDir.getName(), contextPath);
@ -125,6 +192,25 @@ public class TomcatTestServer {
return this; return this;
} }
public TestServerBuilder addStaticContent(String uri, String resourceName) throws Exception {
String resource = new File(projectTarget, resourceName).getAbsolutePath();
LOG.info("Added static content from '{}' at uri '{}'.", resource, uri);
StaticContent staticContent = new StaticContent(uri, resource);
return addServlet(staticContent, String.valueOf(uri.hashCode()), uri);
}
public TestServerBuilder addServlet(HttpServlet httpServlet, String name, String path) throws Exception {
if(server != null) {
return this;
}
Context cxt = getContext();
Tomcat.addServlet(cxt, name, httpServlet);
cxt.addServletMapping(path, name);
//
LOG.info("Added servlet {} at context {}.", name, path);
return this;
}
private Context baseContext = null; private Context baseContext = null;
private Context getContext() { private Context getContext() {
@ -191,17 +277,17 @@ public class TomcatTestServer {
} }
private static void extract(File jarFile, File destDir) throws IOException { private static void extract(File jarFile, File destDir) throws IOException {
JarFile jar = new java.util.jar.JarFile(jarFile); JarFile jar = new JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries(); Enumeration<JarEntry> enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) { while (enumEntries.hasMoreElements()) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); JarEntry file = enumEntries.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); File f = new File(destDir + File.separator + file.getName());
if (file.isDirectory()) { // if its a directory, create it if (file.isDirectory()) { // if its a directory, create it
f.mkdir(); f.mkdir();
continue; continue;
} }
java.io.InputStream is = jar.getInputStream(file); // get the input stream InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f); FileOutputStream fos = new FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos' while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read()); fos.write(is.read());
} }