因此,是否可以使用Java语句启动和停止容器?
您的用例看起来很奇怪(必须在两次测试之间重新启动容器),但我们不要讨论这个。要回答您的问题,可以,可以使用Cargo的Java
API完成。
要启动Tomcat容器并部署您的战争,您可以在
setUp()方法中执行以下操作:
// (1) Optional step to install the container from a URL pointing to its distributionInstaller installer = new ZipURLInstaller(new URL("http://www.apache.org/dist/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip"));installer.install();// (2) Create the Cargo Container instance wrapping our physical containerLocalConfiguration configuration = (LocalConfiguration) new DefaultConfigurationFactory() .createConfiguration("tomcat6x"), ContainerType.INSTALLED, ConfigurationType.STANDALONE);container = (InstalledLocalContainer) new DefaultContainerFactory() .createContainer("tomcat6x", ContainerType.INSTALLED, configuration);container.setHome(installer.getHome());// (3) Statically deploy some WAR (optional)WAR deployable = new WAR("./webapp-testing-webapp/target/webapp-testing-webapp-1.0.war");deployable.setContext("ROOT");configuration.addDeployable(deployable);// (4) Start the containercontainer.start();并停止该
tearDown()方法。
// (6) Stop the containercontainer.stop();



