commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl ResourcesBaseTestCase.java TestResources.java
Date Sun, 17 Aug 2003 03:34:14 GMT
craigmcc    2003/08/16 20:34:14

  Modified:    resources/src/java/org/apache/commons/resources
                        Resources.java
               resources/src/java/org/apache/commons/resources/impl
                        CollectionResourcesBase.java
                        ResourceBundleResources.java ResourcesBase.java
               resources/src/test/org/apache/commons/resources/impl
                        ResourcesBaseTestCase.java TestResources.java
  Log:
  Add a getKeys() method to Resources, and all of the associated implementations,
  to support iterating over all of the defined keys that are known.
  
  Revision  Changes    Path
  1.10      +12 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resources.java
  
  Index: Resources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resources.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Resources.java	28 Feb 2003 02:50:34 -0000	1.9
  +++ Resources.java	17 Aug 2003 03:34:14 -0000	1.10
  @@ -65,6 +65,7 @@
   import java.io.InputStream;
   import java.io.Reader;
   import java.io.Serializable;
  +import java.util.Iterator;
   import java.util.Locale;
   import java.util.TimeZone;
   
  @@ -132,6 +133,13 @@
   
   
       // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * <p>Return an <code>Iterator</code> over the defined keys in this
  +     * {@link Resources} instance.</p>
  +     */
  +    public Iterator getKeys();
   
   
       /**
  
  
  
  1.6       +31 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/CollectionResourcesBase.java
  
  Index: CollectionResourcesBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/CollectionResourcesBase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CollectionResourcesBase.java	21 Jul 2003 04:57:21 -0000	1.5
  +++ CollectionResourcesBase.java	17 Aug 2003 03:34:14 -0000	1.6
  @@ -65,9 +65,12 @@
   
   import java.util.ArrayList;
   import java.util.HashMap;
  +import java.util.HashSet;
  +import java.util.Iterator;
   import java.util.List;
   import java.util.Locale;
   import java.util.Map;
  +import java.util.Set;
   import java.util.TimeZone;
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
  @@ -140,6 +143,30 @@
        * </p>
        */
       private Map maps = new HashMap();
  +
  +
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    public Iterator getKeys() {
  +
  +        synchronized (maps) {
  +
  +            Set results = new HashSet();
  +            Iterator locales = maps.keySet().iterator();
  +            while (locales.hasNext()) {
  +                Locale locale = (Locale) locales.next();
  +                Map map = (Map) maps.get(locale);
  +                results.addAll(map.keySet());
  +            }
  +            return (results.iterator());
  +
  +        }
  +
  +
  +
  +
  +    }
   
   
       // ---------------------------------------------- Content Retrieval Methods
  
  
  
  1.8       +52 -9     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
  
  Index: ResourceBundleResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ResourceBundleResources.java	21 Jul 2003 04:57:21 -0000	1.7
  +++ ResourceBundleResources.java	17 Aug 2003 03:34:14 -0000	1.8
  @@ -65,12 +65,16 @@
   
   import java.io.InputStream;
   import java.io.Reader;
  +import java.util.Collections;
  +import java.util.Enumeration;
   import java.util.HashMap;
  +import java.util.HashSet;
   import java.util.Iterator;
   import java.util.Locale;
   import java.util.Map;
   import java.util.MissingResourceException;
   import java.util.ResourceBundle;
  +import java.util.Set;
   import java.util.TimeZone;
   
   import org.apache.commons.logging.Log;
  @@ -198,6 +202,33 @@
       }
   
   
  +    public Iterator getKeys() {
  +
  +        synchronized (bundles) {
  +
  +            ClassLoader loader = getClassLoader();
  +            Map loaderMap = (Map) bundles.get(loader);
  +            if (loaderMap == null) {
  +                return (Collections.EMPTY_SET.iterator());
  +            }
  +            Set results = new HashSet();
  +            Iterator locales = loaderMap.keySet().iterator();
  +            while (locales.hasNext()) {
  +                Locale locale = (Locale) locales.next();
  +                ResourceBundle bundle = (ResourceBundle)
  +                    loaderMap.get(locale);
  +                Enumeration keys = bundle.getKeys();
  +                while (keys.hasMoreElements()) {
  +                    results.add(keys.nextElement());
  +                }
  +            }
  +            return (results.iterator());
  +
  +        }
  +
  +    }
  +
  +
       // ---------------------------------------------- Content Retrieval Methods
   
   
  @@ -386,11 +417,7 @@
           synchronized (bundles) {
   
               // Locate or create the Map for the appropriate ClassLoader
  -            ClassLoader loader =
  -                Thread.currentThread().getContextClassLoader();
  -            if (loader == null) {
  -                loader = this.getClass().getClassLoader();
  -            }
  +            ClassLoader loader = getClassLoader();
               Map loaderMap = (Map) bundles.get(loader);
               if (loaderMap == null) {
                   loaderMap = new HashMap();
  @@ -407,6 +434,22 @@
               return (bundle);
   
           }
  +
  +    }
  +
  +
  +    /**
  +     * <p>Return the <code>ClassLoader</code> for which we are mapping
  +     * <code>ResourceBundle</code> instances.</p>
  +     */
  +    protected ClassLoader getClassLoader() {
  +
  +        ClassLoader loader =
  +            Thread.currentThread().getContextClassLoader();
  +        if (loader == null) {
  +            loader = this.getClass().getClassLoader();
  +        }
  +        return (loader);
   
       }
   
  
  
  
  1.6       +8 -4      jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesBase.java
  
  Index: ResourcesBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesBase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ResourcesBase.java	19 Apr 2003 18:30:45 -0000	1.5
  +++ ResourcesBase.java	17 Aug 2003 03:34:14 -0000	1.6
  @@ -67,6 +67,7 @@
   import java.io.InputStream;
   import java.io.Reader;
   import java.io.StringReader;
  +import java.util.Iterator;
   import java.util.Locale;
   import java.util.TimeZone;
   
  @@ -172,6 +173,9 @@
   
   
       // ------------------------------------------------------------- Properties
  +
  +
  +    public abstract Iterator getKeys();
   
   
       /**
  
  
  
  1.6       +34 -4     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourcesBaseTestCase.java
  
  Index: ResourcesBaseTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourcesBaseTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ResourcesBaseTestCase.java	28 Feb 2003 02:54:50 -0000	1.5
  +++ ResourcesBaseTestCase.java	17 Aug 2003 03:34:14 -0000	1.6
  @@ -63,6 +63,9 @@
   package org.apache.commons.resources.impl;
   
   
  +import java.util.ArrayList;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.Locale;
   
   import junit.framework.Test;
  @@ -138,6 +141,33 @@
           assertEquals("fr_FR inherit value",
                        "[test] INHERIT",
                        resources.getString("test.inherit", locale, null));
  +
  +    }
  +
  +
  +    // Test retrieving all of the defined keys
  +    public void testKeys() throws Exception {
  +
  +        // Pre-seed all of the keys we expect to find
  +        Locale locale = new Locale("en", "US");
  +        resources.getString("test.base", locale, null);
  +        resources.getString("test.specific", locale, null);
  +        resources.getString("test.inherit", locale, null);
  +        resources.getString("test.message", locale, null);
  +
  +        // Accumulate all of the keys that are actually returned
  +        List results = new ArrayList();
  +        Iterator keys = resources.getKeys();
  +        while (keys.hasNext()) {
  +            results.add(keys.next());
  +        }
  +
  +        // Validate the keys that were actually returned
  +        assertEquals(4, results.size());
  +        assertTrue(results.contains("test.base"));
  +        assertTrue(results.contains("test.specific"));
  +        assertTrue(results.contains("test.inherit"));
  +        assertTrue(results.contains("test.message"));
   
       }
   
  
  
  
  1.5       +20 -4     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.java
  
  Index: TestResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestResources.java	28 Feb 2003 02:54:50 -0000	1.4
  +++ TestResources.java	17 Aug 2003 03:34:14 -0000	1.5
  @@ -63,7 +63,10 @@
   package org.apache.commons.resources.impl;
   
   
  +import java.util.ArrayList;
   import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.Locale;
   import java.util.Map;
   import java.util.TimeZone;
  @@ -127,6 +130,19 @@
   
   
       // ------------------------------------------------------------- Properties
  +
  +
  +    // Return the keys for all defined Resources
  +    public Iterator getKeys() {
  +
  +        List results = new ArrayList();
  +        results.add("test.base");
  +        results.add("test.specific");
  +        results.add("test.inherit");
  +        results.add("test.message");
  +        return (results.iterator());
  +
  +    }
   
   
       // Has this instance been initialized?
  
  
  

Mime
View raw message