Author: jdonnerstag
Date: Mon Nov  5 11:39:49 2007
New Revision: 592131

URL: http://svn.apache.org/viewvc?rev=592131&view=rev
Log:
wicket-1128: Make disabling the Localizer cache more obvious

Added:
    
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java
   (with props)
Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java
    
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/LocalizerTest.java

Modified: 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java?rev=592131&r1=592130&r2=592131&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java 
(original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java 
Mon Nov  5 11:39:49 2007
@@ -51,12 +51,12 @@
 {
        private static final Logger logger = 
LoggerFactory.getLogger(Localizer.class);
 
-       /** Cache properties */
-       private Map cache = new ConcurrentHashMap();
-
        /** ConcurrentHashMap does not allow null values */
        private static final String NULL_VALUE = "<null-value>";
 
+       /** Cache properties */
+       private Map cache = newCache();
+
        /**
         * Create the utils instance class backed by the configuration 
information contained within the
         * supplied application object.
@@ -70,7 +70,10 @@
         */
        public final void clearCache()
        {
-               cache = new ConcurrentHashMap();
+               if (cache != null)
+               {
+                       cache = new ConcurrentHashMap();
+               }
        }
 
        /**
@@ -182,12 +185,11 @@
 
                        if (!addedToPage)
                        {
-                               logger
-                                       .warn(
-                                               "Tried to retrieve a localized 
string for a component that has not yet been added to the page. "
-                                                       + "This can sometimes 
lead to an invalid or no localized resource returned. "
-                                                       + "Make sure you are 
not calling Component#getString() inside your Component's constructor. "
-                                                       + "Offending component: 
{}", component);
+                               logger.warn(
+                                       "Tried to retrieve a localized string 
for a component that has not yet been added to the page. "
+                                               + "This can sometimes lead to 
an invalid or no localized resource returned. "
+                                               + "Make sure you are not 
calling Component#getString() inside your Component's constructor. "
+                                               + "Offending component: {}", 
component);
                        }
                }
 
@@ -197,13 +199,13 @@
 
                // If this component is not yet added to page we do not want to 
check
                // cache as we can generate an invalid cache key
-               if (addedToPage)
+               if ((cache != null) && addedToPage)
                {
                        cacheKey = getCacheKey(key, component);
                }
 
                // Value not found are cached as well (value = null)
-               if (cacheKey != null && cache.containsKey(cacheKey))
+               if ((cacheKey != null) && cache.containsKey(cacheKey))
                {
                        string = getFromCache(cacheKey);
                }
@@ -257,8 +259,8 @@
                                message.append(component.getPageRelativePath());
                                message.append(" 
[class=").append(component.getClass().getName()).append("]");
                        }
-                       throw new MissingResourceException(message.toString(), 
(component != null ? component
-                               .getClass().getName() : ""), key);
+                       throw new MissingResourceException(message.toString(), 
(component != null
+                               ? component.getClass().getName() : ""), key);
                }
 
                return "[Warning: String resource for '" + key + "' not found]";
@@ -348,5 +350,32 @@
                        return PropertyVariableInterpolator.interpolate(string, 
model.getObject());
                }
                return string;
+       }
+
+       /**
+        * By default the cache is enabled. Disabling the cache will disable it 
and clear the cache.
+        * 
+        * @param value
+        */
+       public final void setEnableCache(boolean value)
+       {
+               if (value == false)
+               {
+                       cache = null;
+               }
+               else if (cache == null)
+               {
+                       cache = newCache();
+               }
+       }
+
+       /**
+        * Create a new cache
+        * 
+        * @return
+        */
+       private Map newCache()
+       {
+               return new ConcurrentHashMap();
        }
 }

Added: 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java?rev=592131&view=auto
==============================================================================
--- 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java
 (added)
+++ 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java
 Mon Nov  5 11:39:49 2007
@@ -0,0 +1,47 @@
+/*
+ * 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.wicket;
+
+
+/**
+ * Test cases for the <code>Localizer</code> class.
+ * 
+ * @author Chris Turner
+ */
+public class CachingDisabledLocalizerTest extends LocalizerTest
+{
+       /**
+        * Create the test case.
+        * 
+        * @param message
+        *            The test name
+        */
+       public CachingDisabledLocalizerTest(String message)
+       {
+               super(message);
+       }
+
+       /**
+        * 
+        * @throws Exception
+        */
+       protected void setUp() throws Exception
+       {
+               super.setUp();
+               localizer.setEnableCache(false);
+       }
+}

Propchange: 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/CachingDisabledLocalizerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/LocalizerTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/LocalizerTest.java?rev=592131&r1=592130&r2=592131&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/LocalizerTest.java 
(original)
+++ 
wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/LocalizerTest.java 
Mon Nov  5 11:39:49 2007
@@ -69,7 +69,7 @@
 
        private IResourceSettings settings;
 
-       private Localizer localizer;
+       protected Localizer localizer;
 
        /**
         * Create the test case.
@@ -98,8 +98,8 @@
         */
        public void testGetStringValidString()
        {
-               Assert.assertEquals("Expected string should be returned", "This 
is a test", localizer
-                               .getString("test.string", null, null, 
"DEFAULT"));
+               Assert.assertEquals("Expected string should be returned", "This 
is a test",
+                       localizer.getString("test.string", null, null, 
"DEFAULT"));
        }
 
        /**
@@ -109,7 +109,7 @@
        {
                settings.setUseDefaultOnMissingResource(true);
                Assert.assertEquals("Default string should be returned", 
"DEFAULT", localizer.getString(
-                               "unknown.string", null, null, "DEFAULT"));
+                       "unknown.string", null, null, "DEFAULT"));
        }
 
        /**
@@ -121,8 +121,8 @@
                settings.setThrowExceptionOnMissingResource(false);
 
                Assert.assertEquals("Wrapped key should be returned on no 
default",
-                               "[Warning: String resource for 'unknown.string' 
not found]", localizer.getString(
-                                               "unknown.string", null, null, 
null));
+                       "[Warning: String resource for 'unknown.string' not 
found]", localizer.getString(
+                               "unknown.string", null, null, null));
        }
 
        /**
@@ -133,8 +133,8 @@
                settings.setUseDefaultOnMissingResource(false);
                settings.setThrowExceptionOnMissingResource(false);
                Assert.assertEquals("Wrapped key should be returned on not 
using default and no exception",
-                               "[Warning: String resource for 'unknown.string' 
not found]", localizer.getString(
-                                               "unknown.string", null, null, 
"DEFAULT"));
+                       "[Warning: String resource for 'unknown.string' not 
found]", localizer.getString(
+                               "unknown.string", null, null, "DEFAULT"));
        }
 
        /**
@@ -163,8 +163,8 @@
                ValueMap vm = new ValueMap();
                vm.put("user", "John Doe");
                Model model = new Model(vm);
-               Assert.assertEquals("Property substitution should occur", 
"Welcome, John Doe", localizer
-                               .getString("test.substitute", null, model, 
null));
+               Assert.assertEquals("Property substitution should occur", 
"Welcome, John Doe",
+                       localizer.getString("test.substitute", null, model, 
null));
        }
 
        /**
@@ -183,7 +183,7 @@
                Session.get().setLocale(Locale.ENGLISH);
                MyMockPage page = new MyMockPage();
                Application.get().getResourceSettings().addStringResourceLoader(
-                               new ComponentStringResourceLoader());
+                       new ComponentStringResourceLoader());
 
                Localizer localizer = 
Application.get().getResourceSettings().getLocalizer();
                assertEquals("value 1", localizer.getString("null", 
page.drop1));
@@ -203,13 +203,13 @@
                HashMap model = new HashMap();
                model.put("user", "juergen");
 
-               Assert.assertEquals("Expected string should be returned", 
"Welcome, juergen", localizer
-                               .getString("test.substitute", null, new 
PropertyModel(model, null),
-                                               "DEFAULT {user}"));
-
-               Assert.assertEquals("Expected string should be returned", 
"DEFAULT juergen", localizer
-                               .getString("test.substituteDoesNotExist", null, 
new PropertyModel(model, null),
-                                               "DEFAULT ${user}"));
+               Assert.assertEquals("Expected string should be returned", 
"Welcome, juergen",
+                       localizer.getString("test.substitute", null, new 
PropertyModel(model, null),
+                               "DEFAULT {user}"));
+
+               Assert.assertEquals("Expected string should be returned", 
"DEFAULT juergen",
+                       localizer.getString("test.substituteDoesNotExist", null,
+                               new PropertyModel(model, null), "DEFAULT 
${user}"));
        }
 
        /**
@@ -232,8 +232,8 @@
 
                        // should work properly in a component constructor 
(without parent)
                        // as well
-                       Assert.assertEquals("Expected string should be 
returned", "This is a test", localizer
-                                       .getString("test.string", this, 
"DEFAULT"));
+                       Assert.assertEquals("Expected string should be 
returned", "This is a test",
+                               localizer.getString("test.string", this, 
"DEFAULT"));
 
                }
        }


Reply via email to