Minor changes for faster (default) FIT execution

This commit is contained in:
mibo 2014-10-14 14:46:10 +02:00
parent ca4449f2b2
commit 0be567b9cd
3 changed files with 53 additions and 37 deletions

View File

@ -55,19 +55,6 @@ public class TomcatTestServer {
private TomcatTestServer(Tomcat tomcat) { private TomcatTestServer(Tomcat tomcat) {
this.tomcat = tomcat; this.tomcat = tomcat;
enableLogging();
}
private void enableLogging() {
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("");
try {
Handler fileHandler = new FileHandler(tomcat.getHost().getAppBase() + "/catalina.out", true);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(Level.ALL);
logger.addHandler(fileHandler);
} catch (IOException e) {
throw new RuntimeException("Unable to configure embedded tomcat logging.");
}
} }
public static void main(String[] params) { public static void main(String[] params) {
@ -179,9 +166,10 @@ public class TomcatTestServer {
tomcat = new Tomcat(); tomcat = new Tomcat();
tomcat.setBaseDir(baseDir.getParentFile().getAbsolutePath()); tomcat.setBaseDir(baseDir.getParentFile().getAbsolutePath());
tomcat.setPort(fixedPort); tomcat.setPort(fixedPort);
tomcat.getHost().setAutoDeploy(true);
tomcat.getHost().setAppBase(baseDir.getAbsolutePath()); tomcat.getHost().setAppBase(baseDir.getAbsolutePath());
tomcat.getHost().setDeployOnStartup(true); tomcat.getHost().setDeployOnStartup(true);
tomcat.getConnector().setSecure(false);
tomcat.setSilent(true);
tomcat.addUser("odatajclient", "odatajclient"); tomcat.addUser("odatajclient", "odatajclient");
tomcat.addRole("odatajclient", "odatajclient"); tomcat.addRole("odatajclient", "odatajclient");
} }
@ -198,22 +186,43 @@ public class TomcatTestServer {
} }
} }
public void enableLogging(Level level) {
tomcat.setSilent(false);
try {
Handler fileHandler = new FileHandler(tomcat.getHost().getAppBase() + "/catalina.out", true);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(level);
java.util.logging.Logger.getLogger("").addHandler(fileHandler);
} catch (IOException e) {
throw new RuntimeException("Unable to configure embedded tomcat logging.");
}
}
public void atPort(int port) { public void atPort(int port) {
tomcat.setPort(port); tomcat.setPort(port);
} }
public TestServerBuilder addWebApp() throws IOException { public TestServerBuilder addWebApp() throws IOException {
return addWebApp(true);
}
public TestServerBuilder addWebApp(boolean copy) throws IOException {
if (server != null) { if (server != null) {
return this; return this;
} }
File webAppProjectDir = getFileForDirProperty(PROJECT_WEB_APP_DIR); File webAppProjectDir = getFileForDirProperty(PROJECT_WEB_APP_DIR);
File webAppDir = new File(baseDir, webAppProjectDir.getName()); final File webAppDir;
if(copy) {
webAppDir = new File(baseDir, webAppProjectDir.getName());
FileUtils.deleteDirectory(webAppDir); FileUtils.deleteDirectory(webAppDir);
if (!webAppDir.mkdirs()) { if (!webAppDir.mkdirs()) {
throw new RuntimeException("Unable to create temporary directory at {" + webAppDir.getAbsolutePath() + "}"); throw new RuntimeException("Unable to create temporary directory at {" + webAppDir.getAbsolutePath() + "}");
} }
FileUtils.copyDirectory(webAppProjectDir, webAppDir); FileUtils.copyDirectory(webAppProjectDir, webAppDir);
} else {
webAppDir = webAppProjectDir;
}
String contextPath = "/stub"; String contextPath = "/stub";
Context context = tomcat.addWebapp(tomcat.getHost(), contextPath, webAppDir.getAbsolutePath()); Context context = tomcat.addWebapp(tomcat.getHost(), contextPath, webAppDir.getAbsolutePath());
@ -244,14 +253,14 @@ public class TomcatTestServer {
if (server != null) { if (server != null) {
return this; return this;
} }
String odataServlet = factoryClass.getName(); String servletClassname = factoryClass.getName();
HttpServlet httpServlet = (HttpServlet) Class.forName(odataServlet).newInstance(); HttpServlet httpServlet = (HttpServlet) Class.forName(servletClassname).newInstance();
Context cxt = getContext(); Context cxt = getContext();
String randomServletId = UUID.randomUUID().toString(); String randomServletId = UUID.randomUUID().toString();
Tomcat.addServlet(cxt, randomServletId, httpServlet); Tomcat.addServlet(cxt, randomServletId, httpServlet);
cxt.addServletMapping(path, randomServletId); cxt.addServletMapping(path, randomServletId);
// //
LOG.info("Added servlet {} at context {} (mapping id={}).", odataServlet, path, randomServletId); LOG.info("Added servlet {} at context {} (mapping id={}).", servletClassname, path, randomServletId);
return this; return this;
} }
@ -263,6 +272,11 @@ public class TomcatTestServer {
return addServlet(staticContent, String.valueOf(uri.hashCode()), uri); return addServlet(staticContent, String.valueOf(uri.hashCode()), uri);
} }
public TestServerBuilder addServlet(HttpServlet httpServlet, String path) throws IOException {
String name = UUID.randomUUID().toString();
return addServlet(httpServlet, name, path);
}
public TestServerBuilder addServlet(HttpServlet httpServlet, String name, String path) throws IOException { public TestServerBuilder addServlet(HttpServlet httpServlet, String name, String path) throws IOException {
if (server != null) { if (server != null) {
return this; return this;

View File

@ -46,7 +46,7 @@ under the License.
</logger> </logger>
<root> <root>
<level value="DEBUG"/> <level value="INFO"/>
<appender-ref ref="fitlog"/> <appender-ref ref="fitlog"/>
</root> </root>

View File

@ -57,9 +57,10 @@ public abstract class AbstractBaseTestITCase {
throws LifecycleException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { throws LifecycleException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
TomcatTestServer.init(9080) TomcatTestServer.init(9080)
.addServlet(TechnicalServlet.class, "/odata-server-tecsvc/odata.svc/*") .addServlet(TechnicalServlet.class, "/odata-server-tecsvc/odata.svc/*")
.addServlet(StaticContent.class, "/odata-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml") .addServlet(StaticContent.create("org-odata-core-v1.xml"),
.addServlet(MetadataContent.class, "/odata-metadata/$metadata") "/odata-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml")
.addWebApp() .addServlet(StaticContent.create("metadata-ref.xml"), "/odata-metadata/$metadata")
.addWebApp(false)
.start(); .start();
} }
@ -130,19 +131,20 @@ public abstract class AbstractBaseTestITCase {
public static class StaticContent extends HttpServlet { public static class StaticContent extends HttpServlet {
private static final long serialVersionUID = -6663569573355398997L; private static final long serialVersionUID = -6663569573355398997L;
@Override private final String resourceName;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getOutputStream().write(IOUtils.toByteArray( public StaticContent(String resourceName) {
Thread.currentThread().getContextClassLoader().getResourceAsStream("org-odata-core-v1.xml"))); this.resourceName = resourceName;
} }
public static HttpServlet create(String resourceName) {
return new StaticContent(resourceName);
} }
public static class MetadataContent extends HttpServlet {
private static final long serialVersionUID = -6663569573355398997L;
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getOutputStream().write(IOUtils.toByteArray( resp.getOutputStream().write(IOUtils.toByteArray(
Thread.currentThread().getContextClassLoader().getResourceAsStream("metadata-ref.xml"))); Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName)));
} }
} }
} }