Author: hlship
Date: Thu Feb 26 18:56:29 2009
New Revision: 748272

URL: http://svn.apache.org/viewvc?rev=748272&view=rev
Log:
TAP5-537: PersistentLocale.setLocale() allows the application to set a locale 
that isn't supported, and the subsequent URL may not be interpreted correctly

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/PersistentLocaleImplTest.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java?rev=748272&r1=748271&r2=748272&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PersistentLocaleImpl.java
 Thu Feb 26 18:56:29 2009
@@ -14,30 +14,64 @@
 
 package org.apache.tapestry5.internal.services;
 
-import org.apache.tapestry5.ioc.ScopeConstants;
-import org.apache.tapestry5.ioc.annotations.Scope;
+import org.apache.tapestry5.SymbolConstants;
+import org.apache.tapestry5.internal.TapestryInternalUtils;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.annotations.Symbol;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.internal.util.Defense;
+import org.apache.tapestry5.ioc.services.PerthreadManager;
 import org.apache.tapestry5.services.PersistentLocale;
 
 import java.util.Locale;
+import java.util.Set;
 
-...@scope(ScopeConstants.PERTHREAD)
 public class PersistentLocaleImpl implements PersistentLocale
 {
-    private Locale locale;
+    private final PerthreadManager perThreadManager;
+
+    private final String supportedLocales;
+
+    private final Set<String> localeNames = CollectionFactory.newSet();
+
+    public PersistentLocaleImpl(PerthreadManager perThreadManager,
+
+                                @Inject 
@Symbol(SymbolConstants.SUPPORTED_LOCALES)
+                                String supportedLocales)
+    {
+        this.perThreadManager = perThreadManager;
+        this.supportedLocales = supportedLocales;
+
+        for (String name : 
TapestryInternalUtils.splitAtCommas(supportedLocales))
+        {
+            localeNames.add(name.toLowerCase());
+        }
+    }
 
     public void set(Locale locale)
     {
-        this.locale = Defense.notNull(locale, "locale");
+        Defense.notNull(locale, "locale");
+
+        if (!localeNames.contains(locale.toString().toLowerCase()))
+        {
+            String message = String.format(
+                    "Locale '%s' is not supported by this application. 
Supported locales are '%s'; this is configured via the %s symbol.",
+                    locale, supportedLocales, 
SymbolConstants.SUPPORTED_LOCALES);
+
+            throw new IllegalArgumentException(message);
+        }
+
+
+        perThreadManager.put(this, locale);
     }
 
     public Locale get()
     {
-        return locale;
+        return (Locale) perThreadManager.get(this);
     }
 
     public boolean isSet()
     {
-        return locale != null;
+        return get() != null;
     }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/PersistentLocaleImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/PersistentLocaleImplTest.java?rev=748272&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/PersistentLocaleImplTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/PersistentLocaleImplTest.java
 Thu Feb 26 18:56:29 2009
@@ -0,0 +1,46 @@
+// Copyright 2009 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.internal.services;
+
+import org.apache.tapestry5.ioc.test.TestBase;
+import org.apache.tapestry5.services.PersistentLocale;
+import org.testng.annotations.Test;
+
+import java.util.Locale;
+
+public class PersistentLocaleImplTest extends TestBase
+{
+    /**
+     * TAP5-537
+     */
+    @Test
+    public void set_to_unsupported_locale()
+    {
+        PersistentLocale pl = new PersistentLocaleImpl(null, "en,fr");
+
+        try
+        {
+            pl.set(Locale.CHINESE);
+            unreachable();
+        }
+        catch (IllegalArgumentException ex)
+        {
+            assertEquals(ex.getMessage(),
+                         "Locale 'zh' is not supported by this application. 
Supported locales are 'en,fr'; this is configured via the 
tapestry.supported-locales symbol.");
+        }
+
+    }
+
+}


Reply via email to