Hi,

I realized that the tests don't pass for some locales. In my case, the DefaultLocalePicker returns "de_DE" instead of "en" even if I explicitly add "en" to the MockRequest. To solve this and being able to write locale-specific tests, I created a very simple, but working MockLocalePicker and added it to the StripesTestFixture. This way, the unit tests run with the system's default locale, but can be overridden in the test by adding a specific locale. The "broken" tests now are fixed to Locale.ENGLISH so they will run fine.

A patch file based on the 1.5.x branch is attached to this email. Feel free to change/criticize it :-)

Best regards,
Marcus
Index: tests/src/net/sourceforge/stripes/controller/GenericsBindingTests.java
===================================================================
--- tests/src/net/sourceforge/stripes/controller/GenericsBindingTests.java      
(revision 1267)
+++ tests/src/net/sourceforge/stripes/controller/GenericsBindingTests.java      
(working copy)
@@ -13,6 +13,7 @@
 
 import java.util.Calendar;
 import java.util.Date;
+import java.util.Locale;
 
 /**
  *
@@ -40,6 +41,7 @@
     @Test(groups="fast")
     public void testSimpleTypeVariable() throws Exception {
         MockRoundtrip trip = getRoundtrip();
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("number", "123.4");
         trip.execute();
 
@@ -51,6 +53,7 @@
     @Test(groups="fast")
     public void testGenericBean() throws Exception {
         MockRoundtrip trip = getRoundtrip();
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("genericBean.genericA", "123.4");
         trip.addParameter("genericBean.genericB", "true");
         trip.execute();
Index: tests/src/net/sourceforge/stripes/controller/GenericsBindingTests2.java
===================================================================
--- tests/src/net/sourceforge/stripes/controller/GenericsBindingTests2.java     
(revision 1267)
+++ tests/src/net/sourceforge/stripes/controller/GenericsBindingTests2.java     
(working copy)
@@ -2,6 +2,7 @@
 
 import java.util.Calendar;
 import java.util.Date;
+import java.util.Locale;
 import net.sourceforge.stripes.StripesTestFixture;
 import net.sourceforge.stripes.action.ActionBean;
 import net.sourceforge.stripes.action.ActionBeanContext;
@@ -51,6 +52,7 @@
     @Test(groups="fast")
     public void testSimpleTypeVariable() throws Exception {
         MockRoundtrip trip = getRoundtrip();
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("number", "123.4");
         trip.execute();
 
@@ -62,6 +64,7 @@
     @Test(groups="fast")
     public void testGenericBean() throws Exception {
         MockRoundtrip trip = getRoundtrip();
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("genericBean.genericA", "123.4");
         trip.addParameter("genericBean.genericB", "true");
         trip.execute();
Index: tests/src/net/sourceforge/stripes/controller/MapBindingTests.java
===================================================================
--- tests/src/net/sourceforge/stripes/controller/MapBindingTests.java   
(revision 1267)
+++ tests/src/net/sourceforge/stripes/controller/MapBindingTests.java   
(working copy)
@@ -15,6 +15,7 @@
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 /**
@@ -230,6 +231,7 @@
     @Test(groups="fast")
     public void bindDateKeysInMap() throws Exception {
         MockRoundtrip trip = getRoundtrip();
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("mapDateDate['31-Dec-1999']", "01/01/2000");
         trip.execute();
 
Index: tests/src/net/sourceforge/stripes/localization/MockLocalePicker.java
===================================================================
--- tests/src/net/sourceforge/stripes/localization/MockLocalePicker.java        
(revision 0)
+++ tests/src/net/sourceforge/stripes/localization/MockLocalePicker.java        
(revision 0)
@@ -0,0 +1,41 @@
+/* Copyright 2010 Marcus Krassmann
+ *
+ * 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 net.sourceforge.stripes.localization;
+
+import java.util.Locale;
+import javax.servlet.http.HttpServletRequest;
+import net.sourceforge.stripes.config.Configuration;
+
+/**
+ * Simple locale picker that just uses the locale of the passed
+ * HttpServletRequest. This should be used for locale dependent test cases.
+ * 
+ * @author Marcus Krassmann
+ */
+public class MockLocalePicker implements LocalePicker {
+
+    public Locale pickLocale(HttpServletRequest request) {
+        return request.getLocale() == null ? Locale.getDefault() : 
request.getLocale();
+    }
+
+    public String pickCharacterEncoding(HttpServletRequest request, Locale 
locale) {
+        return "UTF-8";
+    }
+
+    public void init(Configuration configuration) throws Exception {
+        // nothing todo
+    }
+
+}
Index: tests/src/net/sourceforge/stripes/StripesTestFixture.java
===================================================================
--- tests/src/net/sourceforge/stripes/StripesTestFixture.java   (revision 1267)
+++ tests/src/net/sourceforge/stripes/StripesTestFixture.java   (working copy)
@@ -29,6 +29,7 @@
             // Add the Stripes Filter
             Map<String,String> filterParams = new HashMap<String,String>();
             filterParams.put("ActionResolver.Packages", 
"net.sourceforge.stripes");
+            filterParams.put("LocalePicker.Class", 
"net.sourceforge.stripes.localization.MockLocalePicker");
             context.addFilter(StripesFilter.class, "StripesFilter", 
filterParams);
 
             // Add the Stripes Dispatcher
Index: 
tests/src/net/sourceforge/stripes/validation/OneToManyTypeConverterTest.java
===================================================================
--- 
tests/src/net/sourceforge/stripes/validation/OneToManyTypeConverterTest.java    
    (revision 1267)
+++ 
tests/src/net/sourceforge/stripes/validation/OneToManyTypeConverterTest.java    
    (working copy)
@@ -16,6 +16,7 @@
 import java.util.Collection;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Locale;
 
 /**
  * Unit tests that ensure that the OneToManyTypeConverter works in the context 
of the
@@ -42,6 +43,7 @@
     @Test(groups="fast")
     public void testListOfDate() throws Exception {
         MockRoundtrip trip = new 
MockRoundtrip(StripesTestFixture.getServletContext(), getClass());
+        trip.getRequest().addLocale(Locale.ENGLISH);
         trip.addParameter("dates", "12/31/2005, 1/1/2006, 6/15/2008, 
7/7/2007");
         trip.execute();
         OneToManyTypeConverterTest bean = trip.getActionBean(getClass());
------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to