cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anathan...@apache.org
Subject svn commit: r1853884 - in /cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon: SitemapComponentTestCase.java acting/LocaleActionTestCase.java environment/mock/MockRequest.java
Date Tue, 19 Feb 2019 14:39:01 GMT
Author: anathaniel
Date: Tue Feb 19 14:39:01 2019
New Revision: 1853884

URL: http://svn.apache.org/viewvc?rev=1853884&view=rev
Log:
Fix LocaleActionTestCase broken with Java8.  The locale th_TH_TH becomes a special case.
See https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#special_cases_constructor

Modified:
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/SitemapComponentTestCase.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/acting/LocaleActionTestCase.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/environment/mock/MockRequest.java

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/SitemapComponentTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/SitemapComponentTestCase.java?rev=1853884&r1=1853883&r2=1853884&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/SitemapComponentTestCase.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/SitemapComponentTestCase.java
Tue Feb 19 14:39:01 2019
@@ -5,9 +5,9 @@
  * 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.
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.xml.transform.TransformerException;
@@ -64,21 +65,21 @@ import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 /**
- * Testcase for actions, generators, transformers and serializer components. 
+ * Testcase for actions, generators, transformers and serializer components.
  *
  * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
  * @author <a href="mailto:mark.leicester@energyintellect.com">Mark Leicester</a>
  * @version CVS $Id$
  */
 public abstract class SitemapComponentTestCase extends ContainerTestCase {
-    
+
     public final static Parameters EMPTY_PARAMS = Parameters.EMPTY_PARAMETERS;
 
-    private MockRequest request = new MockRequest();
-    private MockResponse response = new MockResponse();
-    private MockContext context = new MockContext();
-    private MockRedirector redirector = new MockRedirector();
-    private Map objectmodel = new HashMap();
+    private final MockRequest request = new MockRequest();
+    private final MockResponse response = new MockResponse();
+    private final MockContext context = new MockContext();
+    private final MockRedirector redirector = new MockRedirector();
+    private final Map<String, Object> objectmodel = new HashMap<String, Object>();
 
     public final MockRequest getRequest() {
         return request;
@@ -92,14 +93,14 @@ public abstract class SitemapComponentTe
         return context;
     }
 
-    public final MockRedirector getRedirector() { 
+    public final MockRedirector getRedirector() {
         return redirector;
     }
 
-    public final Map getObjectModel() {
+    public final Map<String, Object> getObjectModel() {
         return objectmodel;
     }
-    
+
     protected void addContext(DefaultContext context) {
         context.put(ContextHelper.CONTEXT_REQUEST_OBJECT, request);
         context.put(ContextHelper.CONTEXT_RESPONSE_OBJECT, response);
@@ -125,17 +126,16 @@ public abstract class SitemapComponentTe
     /**
      * Match with a pattern.
      *
-     * @param type Hint of the matcher. 
+     * @param type Hint of the matcher.
      * @param pattern Pattern for the matcher.
      * @param parameters Matcher parameters.
      */
-    public final Map match(String type, String pattern, Parameters parameters) throws PatternException
{
+    public final Map<String, String> match(String type, String pattern, Parameters
parameters) throws PatternException {
 
         ServiceSelector selector = null;
         Matcher matcher = null;
         SourceResolver resolver = null;
 
-        Map result = null;
         try {
             selector = (ServiceSelector) this.lookup(Matcher.ROLE +
                 "Selector");
@@ -148,11 +148,13 @@ public abstract class SitemapComponentTe
             matcher = (Matcher) selector.select(type);
             assertNotNull("Test lookup of matcher", matcher);
 
-            result = matcher.match(pattern, objectmodel, parameters);
-
+            @SuppressWarnings("unchecked")
+            Map<String, String> result = (Map<String, String>) matcher.match(pattern,
objectmodel, parameters);
+            return result;
         } catch (ServiceException ce) {
             getLogger().error("Could not retrieve matcher", ce);
             fail("Could not retrieve matcher: " + ce.toString());
+            return null;
         } finally {
             if (matcher != null) {
                 selector.release(matcher);
@@ -160,13 +162,12 @@ public abstract class SitemapComponentTe
             this.release(selector);
             this.release(resolver);
         }
-        return result;
     }
 
     /**
      * Select with a pattern.
      *
-     * @param type Hint of the matcher. 
+     * @param type Hint of the matcher.
      * @param expression Expression for the selector.
      * @param parameters Matcher parameters.
      */
@@ -188,7 +189,7 @@ public abstract class SitemapComponentTe
             assertNotNull("Test if selector name is not null", type);
             sel = (org.apache.cocoon.selection.Selector) selector.select(type);
             assertNotNull("Test lookup of selector", sel);
-            
+
 
             result = sel.select(expression, objectmodel, parameters);
 
@@ -208,19 +209,18 @@ public abstract class SitemapComponentTe
     /**
      * Perform the action component.
      *
-     * @param type Hint of the action. 
+     * @param type Hint of the action.
      * @param source Source for the action.
      * @param parameters Action parameters.
      */
-    public final Map act(String type, String source, Parameters parameters) throws Exception
{
-        
+    public final Map<String, String> act(String type, String source, Parameters parameters)
throws Exception {
+
         redirector.reset();
 
         ServiceSelector selector = null;
         Action action = null;
         SourceResolver resolver = null;
 
-        Map result = null;
         try {
             selector = (ServiceSelector) this.lookup(Action.ROLE +
                 "Selector");
@@ -233,12 +233,14 @@ public abstract class SitemapComponentTe
             action = (Action) selector.select(type);
             assertNotNull("Test lookup of action", action);
 
-            result = action.act(redirector, new SourceResolverAdapter(resolver),
+            @SuppressWarnings("unchecked")
+            Map<String, String> result = (Map<String, String>) action.act(redirector,
new SourceResolverAdapter(resolver),
                                 objectmodel, source, parameters);
-
+            return result;
         } catch (ServiceException ce) {
             getLogger().error("Could not retrieve action", ce);
             fail("Could not retrieve action: " + ce.toString());
+            return null;
         } finally {
             if (action != null) {
                 selector.release(action);
@@ -246,17 +248,16 @@ public abstract class SitemapComponentTe
             this.release(selector);
             this.release(resolver);
         }
-        return result;
     }
 
     /**
      * Generate the generator output.
      *
-     * @param type Hint of the generator. 
+     * @param type Hint of the generator.
      * @param source Source for the generator.
      * @param parameters Generator parameters.
      */
-    public final Document generate(String type, String source, Parameters parameters) 
+    public final Document generate(String type, String source, Parameters parameters)
         throws IOException, SAXException, ProcessingException {
 
         ServiceSelector selector = null;
@@ -308,15 +309,15 @@ public abstract class SitemapComponentTe
         return document;
     }
 
-    /**     
+    /**
      * Trannsform a document by a transformer
-     *      
-     * @param type Hint of the transformer. 
+     *
+     * @param type Hint of the transformer.
      * @param source Source for the transformer.
      * @param parameters Generator parameters.
      * @param input Input document.
-     */ 
-    public final Document transform(String type, String source, Parameters parameters, Document
input) 
+     */
+    public final Document transform(String type, String source, Parameters parameters, Document
input)
         throws SAXException, ProcessingException, IOException {
 
         ServiceSelector selector = null;
@@ -383,7 +384,7 @@ public abstract class SitemapComponentTe
             }
         }
 
-        return document; 
+        return document;
     }
 
     /**
@@ -449,7 +450,7 @@ public abstract class SitemapComponentTe
 
         return document.toByteArray();
     }
-    
+
     public final byte[] read(String type, Parameters parameters, String source) throws SAXException,
IOException, ProcessingException {
         ServiceSelector selector = null;
         Reader reader = null;
@@ -471,7 +472,7 @@ public abstract class SitemapComponentTe
             assertNotNull("Test if reader name is not null", type);
             reader = (Reader) selector.select(type);
             assertNotNull("Test lookup of reader", reader);
-            
+
             reader.setup(new SourceResolverAdapter(resolver),
                     this.getObjectModel(), source, parameters);
 
@@ -502,11 +503,11 @@ public abstract class SitemapComponentTe
 
         return document.toByteArray();
     }
-    
-    public String callFunction(String type, String source, String function, Map params) throws
Exception {
-        
+
+    public String callFunction(String type, String source, String function, Map<String,
String> params) throws Exception {
+
         redirector.reset();
-        
+
         ServiceSelector selector = null;
         Interpreter interpreter = null;
         SourceResolver resolver = null;
@@ -521,18 +522,17 @@ public abstract class SitemapComponentTe
             assertNotNull("Test if interpreter name is not null", type);
             interpreter = (Interpreter) selector.select(type);
             assertNotNull("Test lookup of interpreter", interpreter);
-            
+
             ((AbstractInterpreter)interpreter).register(source);
-            
-            ArrayList parameters = new ArrayList();
-            for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
-                Map.Entry me = (Map.Entry)i.next();
-                String name = (String)me.getKey();
-                String value = (String)me.getValue();
+
+            List<Interpreter.Argument> parameters = new ArrayList<Interpreter.Argument>();
+            for (Map.Entry<String, String> entry : params.entrySet()) {
+                String name = entry.getKey();
+                String value = entry.getValue();
                 parameters.add(new Interpreter.Argument(name, value));
             }
             interpreter.callFunction(function, parameters, getRedirector());
-            
+
         } catch (ServiceException ce) {
             getLogger().error("Could not retrieve interpeter", ce);
             fail("Could not retrieve interpreter: " + ce.toString());
@@ -545,11 +545,11 @@ public abstract class SitemapComponentTe
         }
         return FlowHelper.getWebContinuation(getObjectModel()).getId();
     }
-    
-    public String callContinuation(String type, String source, String id, Map params) throws
Exception {
-        
+
+    public String callContinuation(String type, String source, String id, Map<String,
String> params) throws Exception {
+
         redirector.reset();
-        
+
         ServiceSelector selector = null;
         Interpreter interpreter = null;
         SourceResolver resolver = null;
@@ -566,12 +566,11 @@ public abstract class SitemapComponentTe
             assertNotNull("Test lookup of interpreter", interpreter);
 
             ((AbstractInterpreter)interpreter).register(source);
-            
-            ArrayList parameters = new ArrayList();
-            for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
-                Map.Entry me = (Map.Entry)i.next();
-                String name = (String)me.getKey();
-                String value = (String)me.getValue();
+
+            List<Interpreter.Argument> parameters = new ArrayList<Interpreter.Argument>();
+            for (Map.Entry<String, String> entry : params.entrySet()) {
+                String name = entry.getKey();
+                String value = entry.getValue();
                 parameters.add(new Interpreter.Argument(name, value));
             }
             interpreter.handleContinuation(id, parameters, getRedirector());
@@ -588,22 +587,20 @@ public abstract class SitemapComponentTe
         }
         return FlowHelper.getWebContinuation(getObjectModel()).getId();
     }
-    
+
     public Object getFlowContextObject() {
         return FlowHelper.getContextObject(getObjectModel());
     }
 
     public final void print(Document document) {
         TransformerFactory factory = TransformerFactory.newInstance();
-        try
-        {
-          javax.xml.transform.Transformer serializer = factory.newTransformer();
-          serializer.transform(new DOMSource(document), new StreamResult(System.out));
-          System.out.println();
-        } 
-        catch (TransformerException te)
-        {
-          te.printStackTrace();
+        try {
+            javax.xml.transform.Transformer serializer = factory.newTransformer();
+            serializer.transform(new DOMSource(document), new StreamResult(System.out));
+            System.out.println();
+        }
+        catch (TransformerException ex) {
+            ex.printStackTrace();
         }
     }
 
@@ -751,7 +748,7 @@ public abstract class SitemapComponentTe
      *
      * @param expected The expected XML document
      * @param actual The actual XML Document
-     */  
+     */
     public final void assertEqual(Document expected, Document actual) {
 
         expected.getDocumentElement().normalize();

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/acting/LocaleActionTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/acting/LocaleActionTestCase.java?rev=1853884&r1=1853883&r2=1853884&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/acting/LocaleActionTestCase.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/acting/LocaleActionTestCase.java
Tue Feb 19 14:39:01 2019
@@ -5,9 +5,9 @@
  * 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.
@@ -28,7 +28,7 @@ import org.apache.cocoon.environment.moc
 
 /**
  * JUnit-based tests for {@link LocaleAction} class.
- * 
+ *
  * @author Andrew Stevens
  */
 public class LocaleActionTestCase extends SitemapComponentTestCase {
@@ -51,12 +51,12 @@ public class LocaleActionTestCase extend
     public void testFindLocale() throws Exception {
         // Test different locations for locale info in reverse order
         Parameters parameters = new Parameters();
-        Map result;
+        Map<String, String> result;
 
         // 0. When no configuration, expect action to fail
         result = act("locale0", null, parameters);
         assertNull("Action should have failed", result);
-        
+
         // 1. When nothing specified, use action's default constants
         result = act("locale1", null, parameters);
         assertNotNull("Action should always succeed", result);
@@ -64,7 +64,7 @@ public class LocaleActionTestCase extend
         assertEquals("Test for language", "en", result.get("language"));
         assertEquals("Test for country", "US", result.get("country"));
         assertEquals("Test for variant", "", result.get("variant"));
-        
+
         // 2. Configuration
         result = act("locale2", null, parameters);
         assertNotNull("Action should always succeed", result);
@@ -75,14 +75,14 @@ public class LocaleActionTestCase extend
 
         // 3. User Agent or server default
         getRequest().setLocale(new java.util.Locale("fr", "FR", "MAC"));  // only if use-locale
== true in configuration
-//        getRequest().setHeader("Accept-Language", "fr-FR,fr;q=0.75,en;q=0.5");
+        // getRequest().setHeader("Accept-Language", "fr-FR,fr;q=0.75,en;q=0.5");
         result = act("locale3", null, parameters);
         assertNotNull("Action should always succeed", result);
         assertEquals("Test for locale", "fr_FR_MAC", result.get("locale"));
         assertEquals("Test for language", "fr", result.get("language"));
         assertEquals("Test for country", "FR", result.get("country"));
         assertEquals("Test for variant", "MAC", result.get("variant"));
-        
+
         // 4. Sitemap parameter
         parameters.setParameter("locale", "zh_CN_WIN");
         result = act("locale3", null, parameters);
@@ -93,11 +93,11 @@ public class LocaleActionTestCase extend
         assertEquals("Test for variant", "WIN", result.get("variant"));
 
         // 5. Cookie
-        Map cookies = getRequest().getCookieMap();
+        Map<String, Cookie> cookies = getRequest().getCookieMap();
         MockCookie mockCookie = new MockCookie();
         mockCookie.setName("locale");
         mockCookie.setValue("no_NO_B");
-        cookies.put("locale", mockCookie );
+        cookies.put("locale", mockCookie);
         result = act("locale3", null, parameters);
         assertNotNull("Action should always succeed", result);
         assertEquals("Test for locale", "no_NO_B", result.get("locale"));
@@ -107,13 +107,13 @@ public class LocaleActionTestCase extend
 
         // 6. Session attribute
         MockSession session = (MockSession) getRequest().getSession();
-        session.setAttribute("locale", "th_TH_TH");
+        session.setAttribute("locale", "th_TH");
         result = act("locale3", null, parameters);
         assertNotNull("Action should always succeed", result);
-        assertEquals("Test for locale", "th_TH_TH", result.get("locale"));
+        assertEquals("Test for locale", "th_TH", result.get("locale"));
         assertEquals("Test for language", "th", result.get("language"));
         assertEquals("Test for country", "TH", result.get("country"));
-        assertEquals("Test for variant", "TH", result.get("variant"));
+        assertEquals("Test for variant", "", result.get("variant"));
 
         // 7. Request parameter
         getRequest().addParameter("locale", "es_MX_POSIX");
@@ -131,7 +131,7 @@ public class LocaleActionTestCase extend
     public void testStoreLocale() throws Exception {
         // Test different locations for storing locale
         Parameters parameters = new Parameters();
-        Map result;
+        Map<String, String> result;
         Session session;
         Cookie cookie;
 
@@ -141,7 +141,7 @@ public class LocaleActionTestCase extend
         assertNull("Test for request attribute", getRequest().getAttribute("locale"));
         assertNull("Test for session", getRequest().getSession(false));
         assertTrue("Test for cookie", getResponse().getCookies().isEmpty());
-        
+
         // 2. Store, but don't create session
         result = act("locale4", null, parameters);
         assertNotNull("Action should always succeed", result);
@@ -172,16 +172,16 @@ public class LocaleActionTestCase extend
         getRequest().clearSession();
         getResponse().reset();
         session = getRequest().getSession(true);
+        assertNotNull("Test for getSession(true)", session);
         result = act("locale4", null, parameters);
         assertNotNull("Action should always succeed", result);
         assertEquals("Test for request attribute", "no_NO_B", getRequest().getAttribute("locale"));
         session = getRequest().getSession(false);
-        assertNotNull("Test for session", session);
+        assertNotNull("Test for getSession(false)", session);
         assertEquals("Test session attribute", "no_NO_B", session.getAttribute("locale"));
         assertEquals("Test for cookie", 1, getResponse().getCookies().size());
         cookie = (Cookie) getResponse().getCookies().toArray()[0];
         assertEquals("Check cookie name", "locale", cookie.getName());
         assertEquals("Check cookie value", "no_NO_B", cookie.getValue());
     }
-
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/environment/mock/MockRequest.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/environment/mock/MockRequest.java?rev=1853884&r1=1853883&r2=1853884&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/environment/mock/MockRequest.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/environment/mock/MockRequest.java
Tue Feb 19 14:39:01 2019
@@ -19,7 +19,6 @@ package org.apache.cocoon.environment.mo
 import java.security.Principal;
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.Hashtable;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
@@ -38,7 +37,11 @@ import org.apache.cocoon.environment.Ses
  */
 public class MockRequest implements Request {
 
-    private Hashtable attributes = new Hashtable();
+    private final Map<String, Object> attributes = new HashMap<String, Object>();
+    private final Map<String, String> parameters = new HashMap<String, String>();
+    private final Map<String, String> headers = new HashMap<String, String>();
+    private final Map<String, Cookie> cookies = new HashMap<String, Cookie>();
+
     private String scheme;
     private String protocol = "HTTP/1.1";
     private String requestURI;
@@ -60,10 +63,6 @@ public class MockRequest implements Requ
     private String serverName;
     private int port = 80;
 
-    private Hashtable parameters = new Hashtable();
-    private Hashtable headers = new Hashtable();
-    private HashMap cookies = new HashMap();
-
     private MockSession session;
 
     public Object get(String name) {
@@ -74,8 +73,8 @@ public class MockRequest implements Requ
         return attributes.get(name);
     }
 
-    public Enumeration getAttributeNames() {
-        return attributes.keys();
+    public Enumeration<String> getAttributeNames() {
+        return Collections.enumeration(attributes.keySet());
     }
 
     public void setAttribute(String name, Object o) {
@@ -97,7 +96,7 @@ public class MockRequest implements Requ
         return charEncoding;
     }
 
-    public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException
{
+    public void setCharacterEncoding(String enc) {
         charEncoding = enc;
     }
 
@@ -110,11 +109,11 @@ public class MockRequest implements Requ
     }
 
     public String getParameter(String name) {
-        return (String)parameters.get(name);
+        return parameters.get(name);
     }
 
-    public Enumeration getParameterNames() {
-        return parameters.keys();
+    public Enumeration<String> getParameterNames() {
+        return Collections.enumeration(parameters.keySet());
     }
 
     public String[] getParameterValues(String name) {
@@ -166,7 +165,7 @@ public class MockRequest implements Requ
         locale = loc;
     }
 
-    public Enumeration getLocales() {
+    public Enumeration<Locale> getLocales() {
         return Collections.enumeration(Collections.singleton(getLocale()));
     }
 
@@ -183,11 +182,11 @@ public class MockRequest implements Requ
             return null;
         else {
             Cookie[] cookieArray = new Cookie[cookies.size()];
-            return (Cookie []) cookies.values().toArray(cookieArray);
+            return cookies.values().toArray(cookieArray);
         }
     }
 
-    public Map getCookieMap() {
+    public Map<String, Cookie> getCookieMap() {
         return cookies;
     }
 
@@ -206,15 +205,15 @@ public class MockRequest implements Requ
     }
 
     public String getHeader(String name) {
-        return (String) headers.get(name);
+        return headers.get(name);
     }
 
-    public Enumeration getHeaders(String name) {
+    public Enumeration<String> getHeaders(String name) {
         throw new AssertionFailedError("Not implemented");
     }
 
-    public Enumeration getHeaderNames() {
-        return headers.keys();
+    public Enumeration<String> getHeaderNames() {
+        return Collections.enumeration(headers.keySet());
     }
 
     public String getMethod() {



Mime
View raw message