From: markt Date: Fri, 23 Apr 2010 22:19:12 +0000 (+0000) Subject: Add test case for bug 49132. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=ae32c994ccdfab4840f869b8f7b348ddf44daf54;p=tomcat7.0 Add test case for bug 49132. Check JNDI is available when ServletContextListeners start git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@937541 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/org/apache/catalina/core/TestNamingContextListener.java b/test/org/apache/catalina/core/TestNamingContextListener.java new file mode 100644 index 000000000..5be97831a --- /dev/null +++ b/test/org/apache/catalina/core/TestNamingContextListener.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.catalina.core; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.catalina.LifecycleState; +import org.apache.catalina.deploy.ContextEnvironment; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.Tomcat; + +public class TestNamingContextListener extends TomcatBaseTest { + + private static final String JNDI_NAME = "TestName"; + private static final String JNDI_VALUE= "Test Value"; + + /** + * Test JNDI is available to ServletContextListeners. + */ + public void testBug49132() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + org.apache.catalina.Context ctx = + tomcat.addContext("/", System.getProperty("java.io.tmpdir")); + + // Enable JNDI - it is disabled by default + tomcat.enableNaming(); + + ContextEnvironment environment = new ContextEnvironment(); + environment.setType(JNDI_VALUE.getClass().getName()); + environment.setName(JNDI_NAME); + environment.setValue(JNDI_VALUE); + ctx.getNamingResources().addEnvironment(environment); + + ctx.addApplicationListener(Bug49132Listener.class.getName()); + + tomcat.start(); + + assertEquals(LifecycleState.STARTED, ctx.getState()); + } + + public static final class Bug49132Listener implements ServletContextListener { + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // NOOP + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + javax.naming.Context initCtx; + try { + initCtx = new InitialContext(); + javax.naming.Context envCtx = + (javax.naming.Context) initCtx.lookup("java:comp/env"); + String value = (String) envCtx.lookup(JNDI_NAME); + if (!JNDI_VALUE.equals(value)) { + throw new RuntimeException(); + } + } catch (NamingException e) { + throw new RuntimeException(e); + } + } + } + +}