Author: fmeschbe
Date: Tue Apr  5 07:36:09 2011
New Revision: 1088913

URL: http://svn.apache.org/viewvc?rev=1088913&view=rev
Log:
SLING-2047 First commit: Testcases and pseudo root ResourceBundle

Added:
    
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
   (with props)
    
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
   (with props)
Modified:
    
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java

Added: 
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java?rev=1088913&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
 (added)
+++ 
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
 Tue Apr  5 07:36:09 2011
@@ -0,0 +1,80 @@
+/*
+ * 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.sling.i18n.impl;
+
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.NoSuchElementException;
+import java.util.ResourceBundle;
+
+/**
+ * The <code>RootResourceBundle</code> is an extremely simple resource bundle
+ * which is used as the root resource bundle for the resource bundle 
hierarchies
+ * provided by the {@link JcrResourceBundleProvider}. It has the following
+ * functionality:
+ * <ul>
+ * <li>The {@link #getLocale()} returns a pseudo locale with empty values for
+ * all fields (language, country, and variant)</li>
+ * <li>The {@link #handleGetObject(String)} always returns the provided
+ * <code>key</code> as the value</li>
+ * <li>The {@link #getKeys()} method always returns an empty enumeration</li>
+ * </ul>
+ */
+public class RootResourceBundle extends ResourceBundle {
+
+    // The empty enumeration returned fomr getKeys()
+    private final Enumeration<String> EMPTY = new Enumeration<String>() {
+
+        public boolean hasMoreElements() {
+            return false;
+        }
+
+        public String nextElement() {
+            throw new NoSuchElementException();
+        }
+    };
+
+    // The pseudo Locale returned by getLocale()
+    private final Locale locale = new Locale("");
+
+    /**
+     * Returns a <code>Locale</code> with empty language, country, and variant.
+     */
+    @Override
+    public Locale getLocale() {
+        return locale;
+    }
+
+    /**
+     * Always returns the <code>key</code> parameter as its value.
+     */
+    @Override
+    protected Object handleGetObject(String key) {
+        return key;
+    }
+
+    /**
+     * Always returns an empty enumeration.
+     */
+    @Override
+    public Enumeration<String> getKeys() {
+        return EMPTY;
+    }
+
+}

Propchange: 
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/RootResourceBundle.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Modified: 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java?rev=1088913&r1=1088912&r2=1088913&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
 Tue Apr  5 07:36:09 2011
@@ -265,7 +265,7 @@ public class JcrResourceBundleTest exten
         add(MESSAGES_DE, new Message("s/p/o", "spoon", "L�ffel", true));
 
         // 5. not present in DE
-        add(MESSAGES_DE, PARENT_MSG);
+        add(MESSAGES_EN, PARENT_MSG);
 
         // 6. same as 1.-4., but different translations for overwriting into 
apps
         for (Message msg : MESSAGES_DE.values()) {
@@ -319,6 +319,7 @@ public class JcrResourceBundleTest exten
     public void test_handle_missing_key() {
         // test if key is returned if no entry found in repo
         JcrResourceBundle bundle = new JcrResourceBundle(new Locale("de"), 
null, resolver);
+        bundle.setParent(new RootResourceBundle());
         assertEquals("missing", bundle.getString("missing"));
     }
 
@@ -339,8 +340,10 @@ public class JcrResourceBundleTest exten
         JcrResourceBundle bundle = new JcrResourceBundle(new Locale("de"), 
null, resolver);
         JcrResourceBundle parentBundle = new JcrResourceBundle(new 
Locale("en"), null, resolver);
         bundle.setParent(parentBundle);
+        parentBundle.setParent(new RootResourceBundle());
 
         assertEquals(PARENT_MSG.message, bundle.getObject(PARENT_MSG.key));
+        assertEquals("missing", bundle.getString("missing"));
     }
 
     public void test_search_path() throws Exception {

Added: 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java?rev=1088913&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
 (added)
+++ 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
 Tue Apr  5 07:36:09 2011
@@ -0,0 +1,70 @@
+/*
+ * 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.sling.i18n.impl;
+
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.NoSuchElementException;
+import java.util.ResourceBundle;
+
+import junit.framework.TestCase;
+
+/**
+ * The <code>RootResourceBundleTest</code> tests the assertions of the
+ * <code>RootResourceBundle</code>: <code>getObject</code> and
+ * <code>getString</code> return the key as the value and 
<code>getKeys()</code>
+ * returns an empty <code>Enumeration</code>.
+ */
+public class RootResourceBundleTest extends TestCase {
+
+    public void test_Locale() {
+        Locale rrl = new RootResourceBundle().getLocale();
+        assertEquals("Expecting empty language", "", rrl.getLanguage());
+        assertEquals("Expecting empty country", "", rrl.getCountry());
+        assertEquals("Expecting empty variant", "", rrl.getVariant());
+    }
+
+    public void test_getKeys() {
+        Enumeration<String> keys = new RootResourceBundle().getKeys();
+        assertNotNull("Expecting a keys enumeration", keys);
+        assertFalse("Expecting empty keys enumeration", 
keys.hasMoreElements());
+
+        try {
+            keys.nextElement();
+            fail("Expecting NoŜuchElementException on nextElement");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
+    }
+
+    public void test_get_methods() {
+        ResourceBundle b = new RootResourceBundle();
+        String key = "testKey";
+
+        assertSame("Expecting key as object value", key, b.getObject(key));
+        assertSame("Expecting key as string value", key, b.getString(key));
+
+        try {
+            b.getStringArray(key);
+            fail("Expecting ClassCastException on getStringArray");
+        } catch (ClassCastException cce) {
+            // expected
+        }
+    }
+}

Propchange: 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/RootResourceBundleTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url


Reply via email to