You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by il...@apache.org on 2012/05/04 17:05:50 UTC

svn commit: r1334017 - in /cocoon/cocoon3/trunk/cocoon-sax/src: main/java/org/apache/cocoon/sax/component/ test/java/org/apache/cocoon/sax/component/ test/resources/i18n/

Author: ilgrosso
Date: Fri May  4 15:05:50 2012
New Revision: 1334017

URL: http://svn.apache.org/viewvc?rev=1334017&view=rev
Log:
[COCOON3-64] Adding test for XML fragments in bundles

Added:
    cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml   (with props)
    cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties   (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
    cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/I18NTransformerTest.java

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java?rev=1334017&r1=1334016&r2=1334017&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java Fri May  4 15:05:50 2012
@@ -584,9 +584,14 @@ public class I18nTransformer extends Abs
     public static final String PARAM_PARSE_XML = "parse-xml";
 
     /**
-     * The default namespace for XML elements inside messages. Defaults to http://www.w3.org/1999/xhtml.
+     * The namespace for XML elements inside messages.
      */
     public static final String PARAM_PARSE_NAMESPACE = "parse-namespace";
+    
+    /**
+     * The namespace for XML elements inside messages.
+     */
+    public static final String DEFAULT_NAMESPACE = "http://www.w3.org/1999/xhtml";
 
     /**
      * States of the transformer.
@@ -737,6 +742,7 @@ public class I18nTransformer extends Abs
 
     public void setParseXml(boolean parseXml) {
         this.parseXml = parseXml;
+        this.parseNamespace = DEFAULT_NAMESPACE;
     }
 
     public void setParseNamespace(String parseNamespace) {
@@ -832,18 +838,17 @@ public class I18nTransformer extends Abs
          * If we do not have parameters we will not go on 
          * If we do not have a bundle we cannot proceed
          */
-        if (parameters == null||!parameters.containsKey(PARAM_BUNDLE)) {
+        if (parameters == null || !parameters.containsKey(PARAM_BUNDLE)) {
             return;
-        }
+        }        
         
-        this.parseXml = parameters.containsKey(PARAM_PARSE_XML)
+        this.setParseXml(parameters.containsKey(PARAM_PARSE_XML)
                 ? Boolean.parseBoolean((String) parameters.get(PARAM_PARSE_XML))
-                : false;
-
+                : false);
         if (this.parseXml) {
             this.parseNamespace = parameters.containsKey(PARAM_PARSE_NAMESPACE)
                     ? (String) parameters.get(PARAM_PARSE_NAMESPACE)
-                    : "http://www.w3.org/1999/xhtml";
+                    : DEFAULT_NAMESPACE;
         }
         final String encoding = (String) (parameters.containsKey(PARAM_ENCODING)
                 ? parameters.get(PARAM_ENCODING) : DEFAULT_ENCODING);
@@ -854,6 +859,7 @@ public class I18nTransformer extends Abs
                 encoding);
     }
 
+    @Override
     public CacheKey constructCacheKey() {
         final ParameterCacheKey result = new ParameterCacheKey();
 

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/I18NTransformerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/I18NTransformerTest.java?rev=1334017&r1=1334016&r2=1334017&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/I18NTransformerTest.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/I18NTransformerTest.java Fri May  4 15:05:50 2012
@@ -26,6 +26,7 @@ import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.text.SimpleDateFormat;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -56,15 +57,25 @@ public final class I18NTransformerTest {
         XMLUnit.setNormalizeWhitespace(true);
     }
 
-    private String pipeline(final URL resource,
-            final Locale locale, final String type)
+    private String pipeline(final URL resource, final Locale locale, final String type,
+            final Map<String, Object> parameters)
             throws Exception {
 
         final Pipeline<SAXPipelineComponent> pipeline =
                 new CachingPipeline<SAXPipelineComponent>();
         pipeline.addComponent(new XMLGenerator(resource));
-        pipeline.addComponent(new I18nTransformer(locale, "i18n/" + type,
-                "missingKey"));
+
+        I18nTransformer i18n = new I18nTransformer();
+        Map<String, Object> fullparams = new HashMap<String, Object>();
+        fullparams.put(I18nTransformer.PARAM_LOCALE, locale.toString());
+        fullparams.put(I18nTransformer.PARAM_BUNDLE, "i18n/" + type);
+        fullparams.put(I18nTransformer.PARAM_UNTRANSLATED, "missingKey");
+        if (parameters != null) {
+            fullparams.putAll(parameters);
+        }
+        i18n.setup(fullparams);
+        pipeline.addComponent(i18n);
+
         pipeline.addComponent(new XMLSerializer());
 
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -90,12 +101,10 @@ public final class I18NTransformerTest {
         return expectedDocBuf.toString();
     }
 
-    private String getExpectedForBase(final URL resource,
-            final Locale locale)
+    private String getExpectedForBase(final URL resource, final Locale locale, final String bundleName)
             throws IOException {
 
-        final ResourceBundle bundle =
-                ResourceBundle.getBundle("i18n/base", locale);
+        final ResourceBundle bundle = ResourceBundle.getBundle("i18n/" + bundleName, locale);
 
         String keyText = null;
         try {
@@ -126,29 +135,24 @@ public final class I18NTransformerTest {
     }
 
     @Test
-    public void base()
-            throws Exception {
-
+    public void base() throws Exception {
         final URL resource = this.getClass().getResource("/i18n/base.xml");
 
-        String actualDocument = pipeline(resource, Locale.ENGLISH, "base");
-        String expectedDocument = getExpectedForBase(resource, Locale.ENGLISH);
+        String actualDocument = pipeline(resource, Locale.ENGLISH, "base", null);
+        String expectedDocument = getExpectedForBase(resource, Locale.ENGLISH, "base");
         Diff diff = new Diff(expectedDocument, actualDocument);
-        assertTrue("i18NTransformer did not work as expected: " + diff,
-                diff.identical());
+        assertTrue("i18NTransformer did not work as expected: " + diff, diff.identical());
 
-        actualDocument = pipeline(resource, Locale.ITALIAN, "base");
-        expectedDocument = getExpectedForBase(resource, Locale.ITALIAN);
+        actualDocument = pipeline(resource, Locale.ITALIAN, "base", null);
+        expectedDocument = getExpectedForBase(resource, Locale.ITALIAN, "base");
         diff = new Diff(expectedDocument, actualDocument);
-        assertTrue("i18NTransformer did not work as expected: " + diff,
-                diff.identical());
+        assertTrue("i18NTransformer did not work as expected: " + diff, diff.identical());
 
         Locale.setDefault(Locale.ENGLISH);
-        actualDocument = pipeline(resource, Locale.FRENCH, "base");
-        expectedDocument = getExpectedForBase(resource, Locale.FRENCH);
+        actualDocument = pipeline(resource, Locale.FRENCH, "base", null);
+        expectedDocument = getExpectedForBase(resource, Locale.FRENCH, "base");
         diff = new Diff(expectedDocument, actualDocument);
-        assertTrue("i18NTransformer did not work as expected: " + diff,
-                diff.identical());
+        assertTrue("i18NTransformer did not work as expected: " + diff, diff.identical());
     }
 
     private String getExpectedForTranslate(final URL resource,
@@ -177,14 +181,14 @@ public final class I18NTransformerTest {
 
         final URL resource = this.getClass().getResource("/i18n/translate.xml");
 
-        String actualDocument = pipeline(resource, Locale.ENGLISH, "translate");
+        String actualDocument = pipeline(resource, Locale.ENGLISH, "translate", null);
         String expectedDocument = getExpectedForTranslate(resource,
                 Locale.ENGLISH);
         Diff diff = new Diff(expectedDocument, actualDocument);
         assertTrue("i18NTransformer did not work as expected: " + diff,
                 diff.identical());
 
-        actualDocument = pipeline(resource, Locale.GERMAN, "translate");
+        actualDocument = pipeline(resource, Locale.GERMAN, "translate", null);
         expectedDocument = getExpectedForTranslate(resource,
                 Locale.GERMAN);
         diff = new Diff(expectedDocument, actualDocument);
@@ -229,7 +233,7 @@ public final class I18NTransformerTest {
                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
         final Document doc = loader.parse(resource.openStream());
 
-        final Map<String, Integer> datePatterns = new HashMap<String, Integer>(4*2+1);
+        final Map<String, Integer> datePatterns = new HashMap<String, Integer>(4 * 2 + 1);
         datePatterns.put("SHORT", DateFormat.SHORT);
         datePatterns.put("MEDIUM", DateFormat.MEDIUM);
         datePatterns.put("LONG", DateFormat.LONG);
@@ -295,8 +299,7 @@ public final class I18NTransformerTest {
 
                     decimal = (DecimalFormat) NumberFormat.getCurrencyInstance(
                             dstLocale);
-                    decimal.setMaximumFractionDigits(Integer.valueOf(children.
-                            item(i).getAttributes().getNamedItem(
+                    decimal.setMaximumFractionDigits(Integer.valueOf(children.item(i).getAttributes().getNamedItem(
                             "fraction-digits").getNodeValue()));
                     expected.append(decimal.format(Double.valueOf(
                             children.item(i).getAttributes().getNamedItem(
@@ -353,17 +356,43 @@ public final class I18NTransformerTest {
     }
 
     @Test
-    public void formatting()
-            throws Exception {
-
+    public void formatting() throws Exception {
         final URL resource = this.getClass().getResource("/i18n/formatting.xml");
 
-        final String actualDocument =
-                pipeline(resource, Locale.US, "formatting");
-        final String expectedDocument = getExpectedForFormatting(resource,
-                Locale.US);
+        final String actualDocument = pipeline(resource, Locale.US, "formatting", null);
+        final String expectedDocument = getExpectedForFormatting(resource, Locale.US);
         final Diff diff = new Diff(expectedDocument, actualDocument);
-        assertTrue("i18NTransformer did not work as expected: " + diff,
-                diff.identical());
+        assertTrue("i18NTransformer did not work as expected: " + diff, diff.identical());
+    }
+
+    @Test
+    public void fragment() throws Exception {
+        final URL resource = this.getClass().getResource("/i18n/fragment.xml");
+
+        final String actualDocument = pipeline(resource, Locale.ENGLISH, "fragment",
+                Collections.singletonMap(I18nTransformer.PARAM_PARSE_XML, (Object) "true"));
+
+//        final Pipeline<SAXPipelineComponent> pipeline =
+//                new CachingPipeline<SAXPipelineComponent>();
+//        pipeline.addComponent(new XMLGenerator(resource));
+//
+//        I18nTransformer i18n = new I18nTransformer(Locale.US, "i18n/" + "fragment", "missingKey");
+//        i18n.setParseXml(true);
+//        pipeline.addComponent(i18n);
+//
+//        pipeline.addComponent(new XMLSerializer());
+//
+//        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+//        pipeline.setup(baos);
+//        pipeline.execute();
+//
+//        final String actualDocument = new String(baos.toByteArray(), "UTF-8");
+
+        System.out.println("XXXXXXXXXXXX " + actualDocument);
+
+        String expectedDocument = getExpectedForBase(resource, Locale.ENGLISH, "fragment");
+        System.out.println("XXXXXXXXXXX2 " + expectedDocument);
+        Diff diff = new Diff(expectedDocument, actualDocument);
+        assertTrue("i18NTransformer did not work as expected: " + diff, diff.identical());
     }
 }

Added: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml?rev=1334017&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml Fri May  4 15:05:50 2012
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+      xmlns:i18n="http://apache.org/cocoon/i18n/3.0">
+
+  <i18n:text>This text will be translated.</i18n:text>
+</test>

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties?rev=1334017&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties Fri May  4 15:05:50 2012
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+This\ text\ will\ be\ translated.=Just <h1 xmlns="http://www.w3.org/1999/xhtml">sample</h1> text
\ No newline at end of file

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/resources/i18n/fragment_en.properties
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id