private static void initIdp() { try { idpServer = new Tomcat(); idpServer.setPort(0); String currentDir = new File(".").getCanonicalPath(); idpServer.setBaseDir(currentDir + File.separator + "target"); idpServer.getHost().setAppBase("tomcat/idp/webapps"); idpServer.getHost().setAutoDeploy(true); idpServer.getHost().setDeployOnStartup(true); Connector httpsConnector = new Connector(); httpsConnector.setPort(Integer.parseInt(idpHttpsPort)); httpsConnector.setSecure(true); httpsConnector.setScheme("https"); // httpsConnector.setAttribute("keyAlias", keyAlias); httpsConnector.setAttribute("keystorePass", "tompass"); httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("truststorePass", "tompass"); httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("clientAuth", "want"); // httpsConnector.setAttribute("clientAuth", "false"); httpsConnector.setAttribute("sslProtocol", "TLS"); httpsConnector.setAttribute("SSLEnabled", true); idpServer.getService().addConnector(httpsConnector); idpServer.addWebapp("/fediz-idp-sts", "fediz-idp-sts"); idpServer.addWebapp("/fediz-idp", "fediz-idp"); idpServer.start(); } catch (Exception e) { e.printStackTrace(); } }
public void launch() throws Exception { this.configurarProperties(); final Tomcat server = new Tomcat(); server.setPort(this.puerto); server.setBaseDir(this.baseDir); server.getHost().setAppBase(this.appBase); server.getHost().setAutoDeploy(true); server.getHost().setDeployOnStartup(true); server.addWebapp(this.nameSpace, this.warName + ".war"); server.start(); }
@Before @Override public void setUp() throws Exception { super.setUp(); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); File appBase = new File(getTemporaryDirectory(), "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } tomcat = new TomcatWithFastSessionIDs(); String protocol = getProtocol(); Connector connector = new Connector(protocol); // Listen only on localhost connector.setAttribute("address", InetAddress.getByName("localhost").getHostAddress()); // Use random free port connector.setPort(0); // Mainly set to reduce timeouts during async tests connector.setAttribute("connectionTimeout", "3000"); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); // Add AprLifecycleListener if we are using the Apr connector if (protocol.contains("Apr")) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } File catalinaBase = getTemporaryDirectory(); tomcat.setBaseDir(catalinaBase.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean(System.getProperty("tomcat.test.accesslog", "false")); if (accessLogEnabled) { AccessLogValve alv = new AccessLogValve(); alv.setDirectory(getBuildDirectory() + "/logs"); alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D"); tomcat.getHost().getPipeline().addValve(alv); } // Cannot delete the whole tempDir, because logs are there, // but delete known subdirectories of it. addDeleteOnTearDown(new File(catalinaBase, "webapps")); addDeleteOnTearDown(new File(catalinaBase, "work")); }
@Test public void testFlagFailCtxIfServletStartFails() throws Exception { Tomcat tomcat = getTomcatInstance(); File docBase = new File(System.getProperty("java.io.tmpdir")); StandardContext context = (StandardContext) tomcat.addContext("", docBase.getAbsolutePath()); // first we test the flag itself, which can be set on the Host and // Context assertFalse(context.getComputedFailCtxIfServletStartFails()); StandardHost host = (StandardHost) tomcat.getHost(); host.setFailCtxIfServletStartFails(true); assertTrue(context.getComputedFailCtxIfServletStartFails()); context.setFailCtxIfServletStartFails(Boolean.FALSE); assertFalse( "flag on Context should override Host config", context.getComputedFailCtxIfServletStartFails()); // second, we test the actual effect of the flag on the startup Wrapper servlet = Tomcat.addServlet(context, "myservlet", new FailingStartupServlet()); servlet.setLoadOnStartup(1); tomcat.start(); assertTrue("flag false should not fail deployment", context.getState().isAvailable()); tomcat.stop(); assertFalse(context.getState().isAvailable()); host.removeChild(context); context = (StandardContext) tomcat.addContext("", docBase.getAbsolutePath()); servlet = Tomcat.addServlet(context, "myservlet", new FailingStartupServlet()); servlet.setLoadOnStartup(1); tomcat.start(); assertFalse("flag true should fail deployment", context.getState().isAvailable()); }
@Test public void testWebappListenerConfigureFail() throws Exception { // Test that if LifecycleListener on webapp fails during // configure_start event and if the cause of the failure is gone, // the context can be started without a need to redeploy it. // Set up a container Tomcat tomcat = getTomcatInstance(); tomcat.start(); // To not start Context automatically, as we have to configure it first ((ContainerBase) tomcat.getHost()).setStartChildren(false); FailingLifecycleListener listener = new FailingLifecycleListener(); File root = new File("test/webapp-3.0"); Context context = tomcat.addWebapp("", root.getAbsolutePath()); context.addLifecycleListener(listener); try { context.start(); fail(); } catch (LifecycleException ex) { // As expected } assertEquals(LifecycleState.FAILED, context.getState()); // The second attempt listener.setFail(false); context.start(); assertEquals(LifecycleState.STARTED, context.getState()); // Using a test from testBug49922() to check that the webapp is running ByteChunk result = getUrl("http://localhost:" + getPort() + "/bug49922/target"); assertEquals("Target", result.toString()); }
private static void initRp() { try { rpServer = new Tomcat(); rpServer.setPort(0); String currentDir = new File(".").getCanonicalPath(); rpServer.setBaseDir(currentDir + File.separator + "target"); rpServer.getHost().setAppBase("tomcat/rp/webapps"); rpServer.getHost().setAutoDeploy(true); rpServer.getHost().setDeployOnStartup(true); Connector httpsConnector = new Connector(); httpsConnector.setPort(Integer.parseInt(rpHttpsPort)); httpsConnector.setSecure(true); httpsConnector.setScheme("https"); // httpsConnector.setAttribute("keyAlias", keyAlias); httpsConnector.setAttribute("keystorePass", "tompass"); httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("truststorePass", "tompass"); httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks"); // httpsConnector.setAttribute("clientAuth", "false"); httpsConnector.setAttribute("clientAuth", "want"); httpsConnector.setAttribute("sslProtocol", "TLS"); httpsConnector.setAttribute("SSLEnabled", true); rpServer.getService().addConnector(httpsConnector); // Context ctx = Context cxt = rpServer.addWebapp("/fedizhelloworld", "simpleWebapp"); FederationAuthenticator fa = new FederationAuthenticator(); fa.setConfigFile( currentDir + File.separator + "target" + File.separator + "test-classes" + File.separator + "fediz_config.xml"); cxt.getPipeline().addValve(fa); rpServer.start(); } catch (Exception e) { e.printStackTrace(); } }
@Override public EmbeddedServletContainer getEmbeddedServletContainer( ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null ? this.baseDirectory : createTempDir("tomcat")); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); tomcat.getEngine().setBackgroundProcessorDelay(-1); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); return getTomcatEmbeddedServletContainer(tomcat); }
@Test public void testBug46243() throws Exception { // This tests that if a Filter init() fails then the web application // is not put into service. (BZ 46243) // This also tests that if the cause of the failure is gone, // the context can be started without a need to redeploy it. // Set up a container Tomcat tomcat = getTomcatInstance(); File docBase = new File(tomcat.getHost().getAppBase(), "ROOT"); if (!docBase.mkdirs() && !docBase.isDirectory()) { fail("Unable to create docBase"); } Context root = tomcat.addContext("", "ROOT"); configureTest46243Context(root, true); tomcat.start(); // Configure the client Bug46243Client client = new Bug46243Client(tomcat.getConnector().getLocalPort()); client.setRequest(new String[] {REQUEST}); client.connect(); client.processRequest(); assertTrue(client.isResponse404()); // Context failed to start. This checks that automatic transition // from FAILED to STOPPED state was successful. assertEquals(LifecycleState.STOPPED, root.getState()); // Prepare context for the second attempt // Configuration was cleared on stop() thanks to // StandardContext.resetContext(), so we need to configure it again // from scratch. configureTest46243Context(root, false); root.start(); // The same request is processed successfully client.connect(); client.processRequest(); assertTrue(client.isResponse200()); assertEquals( Bug46243Filter.class.getName() + HelloWorldServlet.RESPONSE_TEXT, client.getResponseBody()); }
@Test public void testBug54240() throws Exception { Tomcat tomcat = getTomcatInstanceTestWebapp(false, true); ServletContext context = ((Context) tomcat.getHost().findChildren()[0]).getServletContext(); TagPluginManager manager = new TagPluginManager(context); Node.Nodes nodes = new Node.Nodes(); Node.CustomTag c = new Node.CustomTag( "test:ATag", "test", "ATag", "http://tomcat.apache.org/jasper", null, null, null, null, null, new TagFileInfo("ATag", "http://tomcat.apache.org/jasper", tagInfo)); c.setTagHandlerClass(TesterTag.class); nodes.add(c); manager.apply(nodes, null, null); Node n = nodes.getNode(0); Assert.assertNotNull(n); Assert.assertTrue(n instanceof Node.CustomTag); Node.CustomTag t = (Node.CustomTag) n; Assert.assertNotNull(t.getAtSTag()); Node.Nodes sTag = c.getAtSTag(); Node scriptlet = sTag.getNode(0); Assert.assertNotNull(scriptlet); Assert.assertTrue(scriptlet instanceof Node.Scriptlet); Node.Scriptlet s = (Node.Scriptlet) scriptlet; Assert.assertEquals("//Just a comment", s.getText()); }
/* * setup two webapps for every test * * note: the super class tearDown method will stop tomcat */ @Override public void setUp() throws Exception { super.setUp(); // create a tomcat server using the default in-memory Realm tomcat = getTomcatInstance(); // associate the SingeSignOn Valve before the Contexts SingleSignOn sso = new SingleSignOn(); tomcat.getHost().getPipeline().addValve(sso); // add the test user and role to the Realm tomcat.addUser(USER, PWD); tomcat.addRole(USER, ROLE); // setup both NonLogin and Login webapps setUpNonLogin(); setUpLogin(); tomcat.start(); }
@Test public void testBug46243() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); File docBase = new File(tomcat.getHost().getAppBase(), "ROOT"); if (!docBase.mkdirs() && !docBase.isDirectory()) { fail("Unable to create docBase"); } Context root = tomcat.addContext("", "ROOT"); // Add test a filter that fails FilterDef filterDef = new FilterDef(); filterDef.setFilterClass(Bug46243Filter.class.getName()); filterDef.setFilterName("Bug46243"); root.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName("Bug46243"); filterMap.addURLPattern("*"); root.addFilterMap(filterMap); // Add a test servlet so there is something to generate a response if // it works (although it shouldn't) Tomcat.addServlet(root, "Bug46243", new HelloWorldServlet()); root.addServletMapping("/", "Bug46243"); tomcat.start(); // Configure the client Bug46243Client client = new Bug46243Client(tomcat.getConnector().getLocalPort()); client.setRequest(new String[] {REQUEST}); client.connect(); client.processRequest(); assertTrue(client.isResponse404()); }
@Override public void start() { embedded = new Tomcat(); AuthConfigFactory.setFactory(new AuthConfigFactoryImpl()); Context context = embedded.addContext(getContextPath(), "/"); context.addParameter("contextConfigLocation", getContextResource()); context.addApplicationListener(ContextLoaderListener.class.getName()); embedded.getHost().setAppBase(""); // Each servlet should get an unique name, otherwise all servers will reuse // one and the same servlet instance. Note that name clashes with servlets // created somewhere else are still possible. String servletName = getServletName() == null ? "ipf-servlet-" + SERVLET_COUNTER.getAndIncrement() : getServletName(); wrapper = context.createWrapper(); wrapper.setName(servletName); wrapper.setServletClass(getServlet().getClass().getName()); for (Map.Entry<String, String> parameters : getInitParameters().entrySet()) { wrapper.addInitParameter(parameters.getKey(), parameters.getValue()); } context.addChild(wrapper); context.addServletMapping(getServletPath(), servletName); /* VirtualWebappLoader loader = new VirtualWebappLoader(this.getClass().getClassLoader()); loader.setVirtualClasspath(System.getProperty("java.class.path")); context.setLoader(loader); */ Connector connector = embedded.getConnector(); connector.setPort(getPort()); if (isSecure()) { connector.setSecure(true); connector.setScheme("https"); connector.setProperty("SSLEnabled", "true"); connector.setProperty("sslProtocol", "TLS"); connector.setProperty("keystoreFile", getKeystoreFile()); connector.setProperty("keystorePass", getKeystorePass()); connector.setProperty("truststoreFile", getTruststoreFile()); connector.setProperty("truststorePass", getTruststorePass()); if (getClientAuthType() == ClientAuthType.MUST) { connector.setProperty("clientAuth", "true"); } else if (getClientAuthType() == ClientAuthType.WANT) { connector.setProperty("clientAuth", "want"); } } try { embedded.start(); wrapper.allocate(); log.info("Started embedded Tomcat server"); } catch (Exception e) { throw new AssertionError(e); } }
private void assertTimeout(TomcatEmbeddedServletContainerFactory factory, int expected) { this.container = factory.getEmbeddedServletContainer(); Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat(); Context context = (Context) tomcat.getHost().findChildren()[0]; assertThat(context.getSessionTimeout(), equalTo(expected)); }