init
This commit is contained in:
136
test/org/apache/naming/TestEnvEntry.java
Normal file
136
test/org/apache/naming/TestEnvEntry.java
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.naming;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.startup.TomcatBaseTest;
|
||||
import org.apache.tomcat.util.buf.ByteChunk;
|
||||
|
||||
public class TestEnvEntry extends TomcatBaseTest {
|
||||
|
||||
@Test
|
||||
public void testEnvEntryBasic() throws Exception {
|
||||
doTestJndiLookup("env-entry/basic", "basic-value");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryValid() throws Exception {
|
||||
doTestJndiLookup("env-entry/valid", "valid");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryInvalid() throws Exception {
|
||||
doTestJndiLookup("env-entry/invalid", "Not Found");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryInjectField() throws Exception {
|
||||
doTestJndiInjection("property1", "inject-value-1");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryInjectProperty() throws Exception {
|
||||
doTestJndiInjection("property2", "inject-value-2");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryInjectFieldNoType() throws Exception {
|
||||
doTestJndiInjection("property3", "inject-value-3");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryInjectionNoValue() throws Exception {
|
||||
doTestJndiLookup("env-entry/injectNoValue", "Not Found");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryLookup() throws Exception {
|
||||
doTestJndiLookup("env-entry/lookup", "basic-value");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryLookupCircular() throws Exception {
|
||||
doTestJndiLookup("env-entry/circular1", "Naming Error");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnvEntryLookupInvalid() throws Exception {
|
||||
doTestJndiLookup("env-entry/lookup-invalid", "Naming Error");
|
||||
}
|
||||
|
||||
|
||||
private void doTestJndiLookup(String jndiName, String expected) throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
File appDir = new File("test/webapp-fragments");
|
||||
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
|
||||
|
||||
tomcat.enableNaming();
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk out = new ByteChunk();
|
||||
|
||||
int rc = getUrl("http://localhost:" + getPort() + "/test/jndi.jsp?jndiName=" +
|
||||
jndiName, out, null);
|
||||
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
|
||||
|
||||
// JSP has leading and trailing white-space
|
||||
String result = out.toString().trim();
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
|
||||
private void doTestJndiInjection(String injectionName, String expected) throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
File appDir = new File("test/webapp-fragments");
|
||||
Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
|
||||
|
||||
Tomcat.addServlet(context, "InjectionServlet", "org.apache.naming.TesterInjectionServlet");
|
||||
context.addServletMappingDecoded("/injection", "InjectionServlet");
|
||||
|
||||
tomcat.enableNaming();
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk out = new ByteChunk();
|
||||
|
||||
int rc = getUrl("http://localhost:" + getPort() + "/test/injection?injectionName=" +
|
||||
injectionName, out, null);
|
||||
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
|
||||
|
||||
// JSP has leading and trailing white-space
|
||||
String result = out.toString().trim();
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
104
test/org/apache/naming/TestNamingContext.java
Normal file
104
test/org/apache/naming/TestNamingContext.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.naming;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.startup.TomcatBaseTest;
|
||||
import org.apache.naming.factory.ResourceLinkFactory;
|
||||
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
|
||||
import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
|
||||
|
||||
public class TestNamingContext extends TomcatBaseTest {
|
||||
|
||||
private static final String COMP_ENV = "comp/env";
|
||||
private static final String GLOBAL_NAME = "global";
|
||||
private static final String LOCAL_NAME = "local";
|
||||
private static final String DATA = "Cabbage";
|
||||
|
||||
|
||||
@Test
|
||||
public void testGlobalNaming() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
org.apache.catalina.Context ctx = tomcat.addContext("", null);
|
||||
|
||||
tomcat.start();
|
||||
|
||||
Context webappInitial = ContextBindings.getContext(ctx);
|
||||
|
||||
// Nothing added at the moment so should be null
|
||||
Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
|
||||
Assert.assertNull(obj);
|
||||
|
||||
ContextEnvironment ce = new ContextEnvironment();
|
||||
ce.setName(GLOBAL_NAME);
|
||||
ce.setValue(DATA);
|
||||
ce.setType(DATA.getClass().getName());
|
||||
|
||||
tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);
|
||||
|
||||
// No link so still should be null
|
||||
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
|
||||
Assert.assertNull(obj);
|
||||
|
||||
// Now add a resource link to the context
|
||||
ContextResourceLink crl = new ContextResourceLink();
|
||||
crl.setGlobal(GLOBAL_NAME);
|
||||
crl.setName(LOCAL_NAME);
|
||||
crl.setType(DATA.getClass().getName());
|
||||
ctx.getNamingResources().addResourceLink(crl);
|
||||
|
||||
// Link exists so should be OK now
|
||||
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
|
||||
Assert.assertEquals(DATA, obj);
|
||||
|
||||
// Try shortcut
|
||||
ResourceLinkFactory factory = new ResourceLinkFactory();
|
||||
ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
|
||||
obj = factory.getObjectInstance(rlr, null, null, null);
|
||||
Assert.assertEquals(DATA, obj);
|
||||
|
||||
// Remove the link
|
||||
ctx.getNamingResources().removeResourceLink(LOCAL_NAME);
|
||||
|
||||
// No link so should be null
|
||||
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
|
||||
Assert.assertNull(obj);
|
||||
|
||||
// Shortcut should fail too
|
||||
obj = factory.getObjectInstance(rlr, null, null, null);
|
||||
Assert.assertNull(obj);
|
||||
}
|
||||
|
||||
|
||||
private Object doLookup(Context context, String name) {
|
||||
Object result = null;
|
||||
try {
|
||||
result = context.lookup(name);
|
||||
} catch (NamingException nnfe) {
|
||||
// Ignore
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
33
test/org/apache/naming/TesterEnvEntry.java
Normal file
33
test/org/apache/naming/TesterEnvEntry.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.naming;
|
||||
|
||||
public class TesterEnvEntry {
|
||||
|
||||
private static final String VALID = "valid";
|
||||
|
||||
public TesterEnvEntry(String value) {
|
||||
if (!VALID.equals(value)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return VALID;
|
||||
}
|
||||
}
|
||||
68
test/org/apache/naming/TesterInjectionServlet.java
Normal file
68
test/org/apache/naming/TesterInjectionServlet.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.naming;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.tomcat.util.IntrospectionUtils;
|
||||
|
||||
public class TesterInjectionServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String property1 = null;
|
||||
public String getProperty1() { return property1; }
|
||||
|
||||
// Not used directly.
|
||||
// Here to ensure properties are injected in preference to fields
|
||||
private String property2 = null;
|
||||
public void setProperty2a(String property2) { this.property2 = property2; }
|
||||
public String getProperty2a() { return property2; }
|
||||
|
||||
private String property2a = null;
|
||||
public void setProperty2(String property2) { this.property2a = property2; }
|
||||
public String getProperty2() { return property2a; }
|
||||
|
||||
private String property3 = null;
|
||||
public String getProperty3() { return property3; }
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain");
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
|
||||
String injectionName = req.getParameter("injectionName");
|
||||
|
||||
PrintWriter pw = resp.getWriter();
|
||||
pw.print(IntrospectionUtils.getProperty(this, injectionName));
|
||||
|
||||
// The property should tyake precedence over the field and this should
|
||||
// be null
|
||||
if (getProperty2a() != null) {
|
||||
pw.println();
|
||||
pw.print(getProperty2a());
|
||||
}
|
||||
}
|
||||
}
|
||||
365
test/org/apache/naming/resources/TestNamingContext.java
Normal file
365
test/org/apache/naming/resources/TestNamingContext.java
Normal file
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
* 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.naming.resources;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import javax.naming.Binding;
|
||||
import javax.naming.CompositeName;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.catalina.core.StandardContext;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.startup.TomcatBaseTest;
|
||||
import org.apache.tomcat.util.buf.ByteChunk;
|
||||
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
|
||||
import org.apache.tomcat.util.descriptor.web.ContextResource;
|
||||
|
||||
public class TestNamingContext extends TomcatBaseTest {
|
||||
|
||||
@Test
|
||||
public void testLookupSingletonResource() throws Exception {
|
||||
doTestLookup(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookupNonSingletonResource() throws Exception {
|
||||
doTestLookup(false);
|
||||
}
|
||||
|
||||
public void doTestLookup(boolean useSingletonResource) throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
// No file system docBase required
|
||||
org.apache.catalina.Context ctx = tomcat.addContext("", null);
|
||||
|
||||
// Create the resource
|
||||
ContextResource cr = new ContextResource();
|
||||
cr.setName("list/foo");
|
||||
cr.setType("org.apache.naming.resources.TesterObject");
|
||||
cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
|
||||
cr.setSingleton(useSingletonResource);
|
||||
ctx.getNamingResources().addResource(cr);
|
||||
|
||||
// Map the test Servlet
|
||||
Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
|
||||
Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
|
||||
ctx.addServletMappingDecoded("/", "bug49994Servlet");
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
|
||||
|
||||
String expected;
|
||||
if (useSingletonResource) {
|
||||
expected = "EQUAL";
|
||||
} else {
|
||||
expected = "NOTEQUAL";
|
||||
}
|
||||
Assert.assertEquals(expected, bc.toString());
|
||||
|
||||
}
|
||||
|
||||
public static final class Bug49994Servlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain;UTF-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
Object obj1 = ctx.lookup("java:comp/env/list/foo");
|
||||
Object obj2 = ctx.lookup("java:comp/env/list/foo");
|
||||
if (obj1 == obj2) {
|
||||
out.print("EQUAL");
|
||||
} else {
|
||||
out.print("NOTEQUAL");
|
||||
}
|
||||
} catch (NamingException ne) {
|
||||
ne.printStackTrace(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListBindings() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
// No file system docBase required
|
||||
org.apache.catalina.Context ctx = tomcat.addContext("", null);
|
||||
|
||||
// Create the resource
|
||||
ContextResource cr = new ContextResource();
|
||||
cr.setName("list/foo");
|
||||
cr.setType("org.apache.naming.resources.TesterObject");
|
||||
cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
|
||||
ctx.getNamingResources().addResource(cr);
|
||||
|
||||
// Map the test Servlet
|
||||
Bug23950Servlet bug23950Servlet = new Bug23950Servlet();
|
||||
Tomcat.addServlet(ctx, "bug23950Servlet", bug23950Servlet);
|
||||
ctx.addServletMappingDecoded("/", "bug23950Servlet");
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
|
||||
Assert.assertEquals("org.apache.naming.resources.TesterObject", bc.toString());
|
||||
}
|
||||
|
||||
public static final class Bug23950Servlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain;UTF-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
NamingEnumeration<Binding> enm =
|
||||
ctx.listBindings("java:comp/env/list");
|
||||
while (enm.hasMore()) {
|
||||
Binding b = enm.next();
|
||||
out.print(b.getObject().getClass().getName());
|
||||
}
|
||||
} catch (NamingException ne) {
|
||||
ne.printStackTrace(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanFactory() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
// No file system docBase required
|
||||
org.apache.catalina.Context ctx = tomcat.addContext("", null);
|
||||
|
||||
// Create the resource
|
||||
ContextResource cr = new ContextResource();
|
||||
cr.setName("bug50351");
|
||||
cr.setType("org.apache.naming.resources.TesterObject");
|
||||
cr.setProperty("factory", "org.apache.naming.factory.BeanFactory");
|
||||
cr.setProperty("foo", "value");
|
||||
ctx.getNamingResources().addResource(cr);
|
||||
|
||||
// Map the test Servlet
|
||||
Bug50351Servlet bug50351Servlet = new Bug50351Servlet();
|
||||
Tomcat.addServlet(ctx, "bug50351Servlet", bug50351Servlet);
|
||||
ctx.addServletMappingDecoded("/", "bug50351Servlet");
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
|
||||
Assert.assertEquals("value", bc.toString());
|
||||
}
|
||||
|
||||
public static final class Bug50351Servlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain;UTF-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
Object obj = ctx.lookup("java:comp/env/bug50351");
|
||||
TesterObject to = (TesterObject) obj;
|
||||
out.print(to.getFoo());
|
||||
} catch (NamingException ne) {
|
||||
ne.printStackTrace(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug51744a() throws Exception {
|
||||
doTestBug51744(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug51744b() throws Exception {
|
||||
doTestBug51744(false);
|
||||
}
|
||||
|
||||
private void doTestBug51744(boolean exceptionOnFailedWrite)
|
||||
throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
// No file system docBase required
|
||||
StandardContext ctx = (StandardContext) tomcat.addContext("", null);
|
||||
|
||||
ctx.setJndiExceptionOnFailedWrite(exceptionOnFailedWrite);
|
||||
|
||||
// Map the test Servlet
|
||||
Bug51744Servlet bug51744Servlet = new Bug51744Servlet();
|
||||
Tomcat.addServlet(ctx, "bug51744Servlet", bug51744Servlet);
|
||||
ctx.addServletMappingDecoded("/", "bug51744Servlet");
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
|
||||
Assert.assertEquals(200, rc);
|
||||
Assert.assertTrue(bc.toString().contains(Bug51744Servlet.EXPECTED));
|
||||
if (exceptionOnFailedWrite) {
|
||||
Assert.assertTrue(bc.toString().contains(Bug51744Servlet.ERROR_MESSAGE));
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Bug51744Servlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String EXPECTED = "TestValue";
|
||||
public static final String ERROR_MESSAGE = "Error";
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain;UTF-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
try {
|
||||
Context ctx1 = new InitialContext();
|
||||
Context env1 = (Context) ctx1.lookup("java:comp/env");
|
||||
env1.addToEnvironment("TestName", EXPECTED);
|
||||
|
||||
out.print(env1.getEnvironment().get("TestName"));
|
||||
|
||||
try {
|
||||
env1.close();
|
||||
} catch (NamingException ne) {
|
||||
out.print(ERROR_MESSAGE);
|
||||
}
|
||||
} catch (NamingException ne) {
|
||||
ne.printStackTrace(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug52830() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
// No file system docBase required
|
||||
org.apache.catalina.Context ctx = tomcat.addContext("", null);
|
||||
|
||||
// Create the resource
|
||||
ContextEnvironment env = new ContextEnvironment();
|
||||
env.setName("boolean");
|
||||
env.setType(Boolean.class.getName());
|
||||
env.setValue("true");
|
||||
ctx.getNamingResources().addEnvironment(env);
|
||||
|
||||
// Map the test Servlet
|
||||
Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
|
||||
Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
|
||||
ctx.addServletMappingDecoded("/", "bug52830Servlet");
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
|
||||
Assert.assertEquals(200, rc);
|
||||
Assert.assertTrue(bc.toString().contains("truetrue"));
|
||||
}
|
||||
|
||||
public static final class Bug52830Servlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String JNDI_NAME = "java:comp/env/boolean";
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
resp.setContentType("text/plain;UTF-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
try {
|
||||
Context initCtx = new InitialContext();
|
||||
|
||||
Boolean b1 = (Boolean) initCtx.lookup(JNDI_NAME);
|
||||
Boolean b2 = (Boolean) initCtx.lookup(
|
||||
new CompositeName(JNDI_NAME));
|
||||
|
||||
out.print(b1);
|
||||
out.print(b2);
|
||||
|
||||
} catch (NamingException ne) {
|
||||
throw new ServletException(ne);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug53465() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
tomcat.enableNaming();
|
||||
|
||||
File appDir =
|
||||
new File("test/webapp");
|
||||
// app dir is relative to server home
|
||||
org.apache.catalina.Context ctxt =
|
||||
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
int rc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/bug5nnnn/bug53465.jsp", bc, null);
|
||||
|
||||
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
|
||||
Assert.assertTrue(bc.toString().contains("<p>10</p>"));
|
||||
|
||||
ContextEnvironment ce =
|
||||
ctxt.getNamingResources().findEnvironment("bug53465");
|
||||
Assert.assertEquals("Bug53465MappedName", ce.getProperty("mappedName"));
|
||||
}
|
||||
}
|
||||
130
test/org/apache/naming/resources/TestWarDirContext.java
Normal file
130
test/org/apache/naming/resources/TestWarDirContext.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* 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.naming.resources;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.catalina.core.JreMemoryLeakPreventionListener;
|
||||
import org.apache.catalina.core.StandardContext;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.startup.TomcatBaseTest;
|
||||
import org.apache.catalina.webresources.StandardRoot;
|
||||
import org.apache.tomcat.util.buf.ByteChunk;
|
||||
|
||||
public class TestWarDirContext extends TomcatBaseTest {
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
// The test fails if JreMemoryLeakPreventionListener is not
|
||||
// present. The listener affects the JVM, and thus not only the current,
|
||||
// but also the subsequent tests that are run in the same JVM. So it is
|
||||
// fair to add it in every test.
|
||||
tomcat.getServer().addLifecycleListener(
|
||||
new JreMemoryLeakPreventionListener());
|
||||
}
|
||||
|
||||
/*
|
||||
* Check https://jira.springsource.org/browse/SPR-7350 isn't really an issue
|
||||
*/
|
||||
@Test
|
||||
public void testLookupException() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
File appDir = new File("test/webapp-fragments");
|
||||
// app dir is relative to server home
|
||||
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
|
||||
|
||||
tomcat.start();
|
||||
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/warDirContext.jsp");
|
||||
Assert.assertEquals("<p>java.lang.ClassNotFoundException</p>",
|
||||
bc.toString());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Additional test following on from SPR-7350 above to check files that
|
||||
* contain JNDI reserved characters can be served when caching is enabled.
|
||||
*/
|
||||
@Test
|
||||
public void testReservedJNDIFileNamesWithCache() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
File appDir = new File("test/webapp-fragments");
|
||||
// app dir is relative to server home
|
||||
StandardContext ctxt = (StandardContext) tomcat.addWebapp(
|
||||
null, "/test", appDir.getAbsolutePath());
|
||||
StandardRoot root = new StandardRoot();
|
||||
root.setCachingAllowed(true);
|
||||
ctxt.setResources(root);
|
||||
|
||||
tomcat.start();
|
||||
|
||||
// Should be found in resources.jar
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/'singlequote.jsp");
|
||||
Assert.assertEquals("<p>'singlequote.jsp in resources.jar</p>",
|
||||
bc.toString());
|
||||
|
||||
// Should be found in file system
|
||||
bc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/'singlequote2.jsp");
|
||||
Assert.assertEquals("<p>'singlequote2.jsp in file system</p>",
|
||||
bc.toString());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Additional test following on from SPR-7350 above to check files that
|
||||
* contain JNDI reserved characters can be served when caching is disabled.
|
||||
*/
|
||||
@Test
|
||||
public void testReservedJNDIFileNamesNoCache() throws Exception {
|
||||
Tomcat tomcat = getTomcatInstance();
|
||||
|
||||
File appDir = new File("test/webapp-fragments");
|
||||
// app dir is relative to server home
|
||||
StandardContext ctxt = (StandardContext) tomcat.addWebapp(
|
||||
null, "/test", appDir.getAbsolutePath());
|
||||
StandardRoot root = new StandardRoot();
|
||||
root.setCachingAllowed(true);
|
||||
ctxt.setResources(root);
|
||||
skipTldsForResourceJars(ctxt);
|
||||
|
||||
tomcat.start();
|
||||
|
||||
// Should be found in resources.jar
|
||||
ByteChunk bc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/'singlequote.jsp");
|
||||
Assert.assertEquals("<p>'singlequote.jsp in resources.jar</p>",
|
||||
bc.toString());
|
||||
|
||||
// Should be found in file system
|
||||
bc = getUrl("http://localhost:" + getPort() +
|
||||
"/test/'singlequote2.jsp");
|
||||
Assert.assertEquals("<p>'singlequote2.jsp in file system</p>",
|
||||
bc.toString());
|
||||
}
|
||||
}
|
||||
49
test/org/apache/naming/resources/TesterFactory.java
Normal file
49
test/org/apache/naming/resources/TesterFactory.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.naming.resources;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
public class TesterFactory implements ObjectFactory {
|
||||
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment) throws Exception {
|
||||
|
||||
if (obj instanceof Reference) {
|
||||
Reference ref = (Reference)obj;
|
||||
String className = ref.getClassName();
|
||||
|
||||
if (className == null) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
if (className.equals("org.apache.naming.resources.TesterObject")) {
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
Class<?> clazz =
|
||||
cl.loadClass("org.apache.naming.resources.TesterObject");
|
||||
return clazz.getConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
35
test/org/apache/naming/resources/TesterObject.java
Normal file
35
test/org/apache/naming/resources/TesterObject.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.naming.resources;
|
||||
|
||||
public class TesterObject {
|
||||
|
||||
private String foo;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is a test object (" + super.toString() + ").";
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user