1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.ContextConfig;
import org.apache.catalina.startup.Tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "application-servlet.xml", "applicationContext.xml", "securityContext.xml" })
public abstract class AbstractMainHTTPTestCase {
private static final Logger LOG = LoggerFactory.getLogger(AbstractMainHTTPTestCase.class);
private static String hostname = "localhost";
private static int PORT = 8081;
private static String protocol = "org.apache.coyote.http11.Http11Protocol";
private static String contextPath = "/MyApp";
private Tomcat tomcat;
private String baseDir = new java.io.File("").getAbsolutePath();
private String appPath;
private String WebXmlPath;
public Client client;
public WebResource webResource;
public String response;
@Before
public final void setUp() throws Exception {
this.startHTTPServer();
}
public final void startHTTPServer() throws Exception {
// Create temporary folders
appPath = new java.io.File(this.baseDir + "/src/main/webapp").getAbsolutePath();
WebXmlPath = new java.io.File(this.appPath + "/WEB-INF/web.xml").getAbsolutePath();
File tempDir = new File(this.baseDir + "/target", "webtest");
if (!tempDir.exists() && !tempDir.mkdirs()) {
LOG.error("Unable to create webtest TEMP directory for test:{}", tempDir.getAbsolutePath());
}
File appBase = new File(this.baseDir + "/target/webtest/", "webapps");
if (!appBase.exists() && !appBase.mkdir()) {
LOG.error("Unable to create appBase for test:{}", appBase.getAbsolutePath());
}
// Log parameters values
LOG.debug("baseDir: {}", this.baseDir);
LOG.debug("appPath: {}", this.appPath);
LOG.debug("WebXmlPath: {}", this.WebXmlPath);
LOG.debug("tempDir: {}", tempDir.getAbsolutePath());
LOG.debug("appBase: {}", appBase.getAbsolutePath());
try {
this.tomcat = new Tomcat();
this.tomcat.setHostname(AbstractMainHTTPTestCase.hostname);
this.tomcat.setBaseDir(tempDir.getAbsolutePath());
Connector connector = new Connector(protocol);
connector.setPort(AbstractMainHTTPTestCase.PORT);
connector.setAttribute("connectionTimeout", "3000");
this.tomcat.getService().addConnector(connector);
this.tomcat.setConnector(connector);
this.tomcat.getHost().setAppBase(appBase.getAbsolutePath());
StandardContext ctxt = (StandardContext) tomcat.addContext(AbstractMainHTTPTestCase.contextPath, this.appPath);
ctxt.setDefaultWebXml(this.WebXmlPath);
ctxt.addLifecycleListener(new ContextConfig());
ctxt.setDocBase(tempDir.getAbsolutePath());
ctxt.setPath(AbstractMainHTTPTestCase.contextPath);
LOG.debug("Tomcat 7 server starting");
this.tomcat.start();
this.tomcat.getServer().await();
LOG.debug("Tomcat 7 server started (PORT: {})",
AbstractMainHTTPTestCase.PORT);
this.client = Client.create();
this.webResource = this.client.resource("http://localhost:" + AbstractMainHTTPTestCase.PORT);
} catch (Exception ex) {
LOG.error("Error while starting tomcat server");
this.tearDown();
}
}
@After
public final void tearDown() throws Exception {
if (tomcat.getServer() != null
&& tomcat.getServer().getState() != LifecycleState.DESTROYED) {
if (tomcat.getServer().getState() != LifecycleState.STOPPED) {
tomcat.stop();
}
tomcat.destroy();
}
}
public int getPORT() {
return AbstractMainHTTPTestCase.PORT;
}
} |
Partager