Add a test case for https://issues.apache.org/bugzilla/show_bug.cgi?id=50351
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 29 Nov 2010 15:40:11 +0000 (15:40 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 29 Nov 2010 15:40:11 +0000 (15:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1040158 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/naming/resources/TestNamingContext.java
test/org/apache/naming/resources/TesterObject.java

index d374f51..2e7b84e 100644 (file)
@@ -156,4 +156,55 @@ public class TestNamingContext extends TomcatBaseTest {
             }
         }
     }
+    
+    public void testBeanFactory() 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("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.addServletMapping("/", "bug50351Servlet");
+
+        tomcat.start();
+
+        ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
+        assertEquals("value", bc.toString());
+    }
+
+    public 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);
+            }
+        }
+    }
+    
+
 }
index 4f16aa0..1f09489 100644 (file)
@@ -18,8 +18,18 @@ 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;
+    }
 }