Add test cases for:
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 8 Oct 2010 11:57:57 +0000 (11:57 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 8 Oct 2010 11:57:57 +0000 (11:57 +0000)
- https://issues.apache.org/bugzilla/show_bug.cgi?id=49994
- https://issues.apache.org/bugzilla/show_bug.cgi?id=23950
Based on the original test case for bug 23950 provided by Eli Miller

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1005790 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/naming/resources/TestNamingContext.java [new file with mode: 0644]
test/org/apache/naming/resources/TesterFactory.java [new file with mode: 0644]
test/org/apache/naming/resources/TesterObject.java [new file with mode: 0644]

diff --git a/test/org/apache/naming/resources/TestNamingContext.java b/test/org/apache/naming/resources/TestNamingContext.java
new file mode 100644 (file)
index 0000000..33daf04
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * 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.IOException;
+import java.io.PrintWriter;
+
+import javax.naming.Binding;
+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.apache.catalina.core.StandardContext;
+import org.apache.catalina.deploy.ContextResource;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestNamingContext extends TomcatBaseTest {
+
+    public void testLookup() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        tomcat.enableNaming();
+        
+        // Must have a real docBase - just use temp
+        StandardContext ctx = (StandardContext)
+            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
+        
+        // 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
+        Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
+        Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
+        ctx.addServletMapping("/", "bug49994Servlet");
+
+        tomcat.start();
+
+        ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
+        assertEquals("OK", bc.toString());
+
+    }
+
+    public 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("FAIL");
+                } else {
+                    out.print("OK");
+                }
+            } catch (NamingException ne) {
+                ne.printStackTrace(out);
+            }
+        }
+    }
+
+    public void testListBindings() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        tomcat.enableNaming();
+        
+        // Must have a real docBase - just use temp
+        StandardContext ctx = (StandardContext)
+            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
+        
+        // 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.addServletMapping("/", "bug23950Servlet");
+
+        tomcat.start();
+
+        ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
+        assertEquals("org.apache.naming.resources.TesterObject", bc.toString());
+    }
+    
+    public 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);
+            }
+        }
+    }
+}
diff --git a/test/org/apache/naming/resources/TesterFactory.java b/test/org/apache/naming/resources/TesterFactory.java
new file mode 100644 (file)
index 0000000..824abee
--- /dev/null
@@ -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.newInstance();
+            }
+        }
+        return null;
+    }
+}
diff --git a/test/org/apache/naming/resources/TesterObject.java b/test/org/apache/naming/resources/TesterObject.java
new file mode 100644 (file)
index 0000000..4f16aa0
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * 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 {
+
+    @Override
+    public String toString() {
+        return "This is a test object (" + super.toString() + ").";
+    }
+}