You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2007/03/22 16:18:30 UTC

svn commit: r521307 - in /tiles/framework/trunk/tiles-core/src: main/java/org/apache/tiles/servlet/ServletContextAdapter.java test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java

Author: apetrelli
Date: Thu Mar 22 08:18:29 2007
New Revision: 521307

URL: http://svn.apache.org/viewvc?view=rev&rev=521307
Log:
TILES-142
Now the init parameters are put into a Hashtable that is populated with init parameters of the root context at creation.
Added a test case.

Added:
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java   (with props)
Modified:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/ServletContextAdapter.java

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/ServletContextAdapter.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/ServletContextAdapter.java?view=diff&rev=521307&r1=521306&r2=521307
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/ServletContextAdapter.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/ServletContextAdapter.java Thu Mar 22 08:18:29 2007
@@ -22,6 +22,8 @@
 package org.apache.tiles.servlet;
 
 import javax.servlet.*;
+
+import java.util.Hashtable;
 import java.util.Set;
 import java.util.Enumeration;
 import java.net.URL;
@@ -43,9 +45,10 @@
     private ServletContext rootContext;
 
     /**
-     * The configuration object to use.
+     * The union of init parameters of {@link ServletConfig} and
+     * {@link ServletContext}.
      */
-    private ServletConfig config;
+    private Hashtable<String, String> initParameters;
 
 
     /**
@@ -53,9 +56,22 @@
      *
      * @param config The servlet configuration object.
      */
+    @SuppressWarnings("unchecked")
     public ServletContextAdapter(ServletConfig config) {
         this.rootContext = config.getServletContext();
-        this.config = config;
+        initParameters = new Hashtable<String, String>();
+        Enumeration<String> enumeration = rootContext
+                .getInitParameterNames();
+        while (enumeration.hasMoreElements()) {
+            String paramName = enumeration.nextElement();
+            initParameters.put(paramName, rootContext
+                    .getInitParameter(paramName));
+        }
+        enumeration = config.getInitParameterNames();
+        while (enumeration.hasMoreElements()) {
+            String paramName = enumeration.nextElement();
+            initParameters.put(paramName, config.getInitParameter(paramName));
+        }
     }
 
     /** {@inheritDoc} */
@@ -150,18 +166,13 @@
 
     /** {@inheritDoc} */
     public String getInitParameter(String string) {
-        String parm = config.getInitParameter(string);
-        if(parm == null) {
-            parm = rootContext.getInitParameter(string);
-        }
-        return parm;
+        return initParameters.get(string);
     }
 
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     public Enumeration getInitParameterNames() {
-        return new CompositeEnumeration(config.getInitParameterNames(),
-            rootContext.getInitParameterNames());
+        return initParameters.keys();
     }
 
     /** {@inheritDoc} */

Added: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java?view=auto&rev=521307
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java (added)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java Thu Mar 22 08:18:29 2007
@@ -0,0 +1,80 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.servlet;
+
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.shale.test.mock.MockServletConfig;
+import org.apache.shale.test.mock.MockServletContext;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link ServletContextAdapter}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ServletContextAdapterTest extends TestCase {
+
+    /**
+     * The context to test.
+     */
+    ServletContextAdapter context;
+
+    /** {@inheritDoc} */
+    @Override
+    protected void setUp() throws Exception {
+        MockServletContext rootContext = new MockServletContext();
+        rootContext.addInitParameter("initParameter1", "parameterValue1");
+        rootContext.addInitParameter("initParameter2", "parameterValue2");
+        MockServletConfig config = new MockServletConfig(rootContext);
+        config.addInitParameter("initParameter1", "newParameterValue1");
+        config.addInitParameter("newInitParameter", "newParameterValue2");
+        context = new ServletContextAdapter(config);
+    }
+
+    /**
+     * Test init parameters.
+     */
+    @SuppressWarnings("unchecked")
+    public void testGetInitParameters() {
+        assertEquals(context.getInitParameter("initParameter1"), "newParameterValue1");
+        assertEquals(context.getInitParameter("initParameter2"), "parameterValue2");
+        assertEquals(context.getInitParameter("newInitParameter"), "newParameterValue2");
+        
+        Set<String> paramSet = new HashSet<String>();
+        paramSet.add("initParameter1");
+        paramSet.add("initParameter2");
+        paramSet.add("newInitParameter");
+        Enumeration<String> names = context.getInitParameterNames();
+        while (names.hasMoreElements()) {
+            String name = names.nextElement();
+            assertTrue(paramSet.contains(name));
+            paramSet.remove(name);
+        }
+        
+        assertTrue(paramSet.isEmpty());
+    }
+
+}

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/servlet/ServletContextAdapterTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL