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/tests FileResourcesExposerFactory.java
Date Sat, 28 Dec 2002 04:40:56 GMT
craigmcc    2002/12/27 20:40:56

  Modified:    resources/src/java/org/apache/commons/resources
                        Resources.java ResourcesBase.java
                        ResourcesException.java ResourcesFactory.java
               resources/src/java/org/apache/commons/resources/file
                        FileResourcesFactory.java
               resources/src/java/org/apache/commons/resources/file/web
                        WebappFileResourcesFactory.java
               resources/src/java/org/apache/commons/resources/impl
                        ResourceBundleResources.java
                        ResourceBundleResourcesFactory.java
                        ResourcesBase.java ResourcesFactoryBase.java
               resources/src/java/org/apache/commons/resources/message
                        MessageResourcesFactory.java
               resources/src/test/org/apache/commons/resources/impl
                        ResourceBundleResourcesTestCase.java
                        ResourcesBaseTestCase.java TestResources.java
                        TestResourcesFactory.java
               resources/src/test/org/apache/commons/resources/tests
                        FileResourcesExposerFactory.java
  Log:
  Add back support for the 'returnNull' property in the refactored
  Resources APIs, including automatic delegation from the corresponding
  ResourcesFactory.
  
  Revision  Changes    Path
  1.5       +30 -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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Resources.java	28 Dec 2002 00:04:07 -0000	1.4
  +++ Resources.java	28 Dec 2002 04:40:55 -0000	1.5
  @@ -76,6 +76,14 @@
    * Localized versions of the resources (based on Locale and optional
    * TimeZone parameters) can be retrieved in a variety of formats.</p>
    *
  + * <p>When presented with an invalid <code>key</code> value, resource
  + * getter methods will behave differently based on the current value of
  + * the <code>returnNull</code> property for this {@link Resources} intance.
  + * If the property value is <code>true</code>, the getter method will return
  + * <code>null</code> (which may be ambiguous if <code>null</code>
is a valid
  + * resource value).  If the property value is <code>false</code>, a
  + * <code>ResourcesKeyException</code> will be thrown instead.</p>
  + *
    * <p>Different implementations of the {@link Resources} interface support
    * a variety of mechanisms for acquiring the data content represented by
    * the keys.  Examples could include property files, XML files, databases, web
  @@ -130,6 +138,24 @@
        * <p>Return the logical name of this {@link Resources} instance.</p>
        */
       public String getName();
  +
  +
  +    /**
  +     * <p>Return <code>true</code> if resource getter methods will return
  +     * <code>null</code> instead of throwing an exception on invalid
  +     * key values.</p>
  +     */
  +    public boolean isReturnNull();
  +
  +
  +    /**
  +     * <p>Set a flag determining whether resource getter methods should
  +     * return <code>null</code> instead of throwing an exception on
  +     * invalid key values.</p>
  +     *
  +     * @param returnNull The new flag value
  +     */
  +    public void setReturnNull(boolean returnNull);
   
   
       // ---------------------------------------------- Content Retrieval Methods
  
  
  
  1.2       +38 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesBase.java
  
  Index: ResourcesBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourcesBase.java	24 Oct 2001 20:46:03 -0000	1.1
  +++ ResourcesBase.java	28 Dec 2002 04:40:55 -0000	1.2
  @@ -92,6 +92,14 @@
   
   
       /**
  +     * <p>Flag indicating whether resource getter methods should return
  +     * <code>null</code> (instead of throwing an exception) on invalid
  +     * key values.</p>
  +     */
  +    protected boolean returnNull = true;
  +
  +
  +    /**
        * This is called on to initialize the Resources, before any of the
        * get* methods are called.
        */
  @@ -122,6 +130,32 @@
       public void setName(String name) {
           this.name = name;
       }
  +
  +    /**
  +     * <p>Return <code>true</code> if resource getter methods will return
  +     * <code>null</code> instead of throwing an exception on invalid
  +     * key values.</p>
  +     */
  +    public boolean isReturnNull() {
  +
  +        return (this.returnNull);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Set a flag determining whether resource getter methods should
  +     * return <code>null</code> instead of throwing an exception on
  +     * invalid key values.</p>
  +     *
  +     * @param returnNull The new flag value
  +     */
  +    public void setReturnNull(boolean returnNull) {
  +
  +        this.returnNull = returnNull;
  +
  +    }
  +
   
       /**
        * Retrieves content based on the locale and time zone specified.  Note
  
  
  
  1.3       +8 -7      jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesException.java
  
  Index: ResourcesException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesException.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResourcesException.java	28 Dec 2002 00:51:45 -0000	1.2
  +++ ResourcesException.java	28 Dec 2002 04:40:55 -0000	1.3
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -66,8 +66,9 @@
   
   
   /**
  - * This class is a general purpose wrapper exception for problems
  - * pertaining to Resources.
  + * <p>This class is a general purpose wrapper exception for problems
  + * pertaining to Resources.</p>
  + *
    * @author Mike Schachter (mschachter@hp.com)
    * @version $Revision$ $Date$
    */
  
  
  
  1.5       +26 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesFactory.java
  
  Index: ResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesFactory.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ResourcesFactory.java	28 Dec 2002 00:51:45 -0000	1.4
  +++ ResourcesFactory.java	28 Dec 2002 04:40:55 -0000	1.5
  @@ -88,6 +88,28 @@
   public interface ResourcesFactory extends Serializable {
       
   
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * <p>Return the <code>returnNull</code> property value that will
be
  +     * configured on {@link Resources} instances created by this factory.</p>
  +     */
  +    public boolean isReturnNull();
  +
  +
  +    /**
  +     * <p>Set the <code>returnNull</code> property value that will be
  +     * configured on {@link Resources} instances created by this factory.</p>
  +     *
  +     * @param returnNull The new value to delegate
  +     */
  +    public void setReturnNull(boolean returnNull);
  +
  +
  +    // --------------------------------------------------------- Public Methods
  +
  +
       /**
        * <p>Create (if necessary) and return a {@link Resources} instance
        * for the specified logical name, with a default configuration.</p>
  
  
  
  1.4       +17 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResourcesFactory.java
  
  Index: FileResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResourcesFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileResourcesFactory.java	28 Dec 2002 00:04:07 -0000	1.3
  +++ FileResourcesFactory.java	28 Dec 2002 04:40:55 -0000	1.4
  @@ -80,4 +80,17 @@
       }
       public void release() {
       }
  +
  +
  +    protected boolean returnNull = true;
  +
  +    public boolean isReturnNull() {
  +        return (this.returnNull);
  +    }
  +
  +    public void setReturnNull(boolean returnNull) {
  +        this.returnNull = returnNull;
  +    }
  +
  +
   }
  
  
  
  1.4       +16 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/web/WebappFileResourcesFactory.java
  
  Index: WebappFileResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/web/WebappFileResourcesFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WebappFileResourcesFactory.java	28 Dec 2002 00:04:07 -0000	1.3
  +++ WebappFileResourcesFactory.java	28 Dec 2002 04:40:55 -0000	1.4
  @@ -82,4 +82,16 @@
       public void release() {
       }
   
  +
  +    protected boolean returnNull = true;
  +
  +    public boolean isReturnNull() {
  +        return (this.returnNull);
  +    }
  +
  +    public void setReturnNull(boolean returnNull) {
  +        this.returnNull = returnNull;
  +    }
  +
  +
   }
  
  
  
  1.3       +15 -6     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResourceBundleResources.java	28 Dec 2002 03:56:29 -0000	1.2
  +++ ResourceBundleResources.java	28 Dec 2002 04:40:55 -0000	1.3
  @@ -76,6 +76,7 @@
   import java.util.TimeZone;
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
  +import org.apache.commons.resources.ResourcesKeyException;
   
   
   /**
  @@ -239,7 +240,11 @@
               ResourceBundle bundle = getBundle(locale, timeZone);
               return (bundle.getObject(key));
           } catch (MissingResourceException e) {
  -            throw new ResourcesException(e);
  +            if (isReturnNull()) {
  +                return (null);
  +            } else {
  +                throw new ResourcesKeyException(key);
  +            }
           }
   
       }
  @@ -290,7 +295,11 @@
           } catch (ClassCastException e) {
               throw new ResourcesException(e);
           } catch (MissingResourceException e) {
  -            throw new ResourcesException(e);
  +            if (isReturnNull()) {
  +                return (null);
  +            } else {
  +                throw new ResourcesKeyException(key);
  +            }
           }
   
       }
  
  
  
  1.3       +6 -5      jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.java
  
  Index: ResourceBundleResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResourceBundleResourcesFactory.java	28 Dec 2002 03:56:29 -0000	1.2
  +++ ResourceBundleResourcesFactory.java	28 Dec 2002 04:40:55 -0000	1.3
  @@ -115,7 +115,7 @@
       /**
        * <p>Create and return a new {@link Resources} instance with the
        * specified logical name, after calling its <code>init()</code>
  -     * method.</p>
  +     * method and delegating the relevant properties.</p>
        *
        * @param name Logical name of the {@link Resources} instance to create
        * @param config Configuration string for this resource (if any)
  @@ -127,6 +127,7 @@
           throws ResourcesException {
   
           Resources resources = new ResourceBundleResources(name, config);
  +        resources.setReturnNull(isReturnNull());
           resources.init();
           return (resources);
   
  
  
  
  1.2       +37 -3     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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourcesBase.java	28 Dec 2002 00:56:24 -0000	1.1
  +++ ResourcesBase.java	28 Dec 2002 04:40:55 -0000	1.2
  @@ -119,6 +119,14 @@
       protected String name = null;
   
   
  +    /**
  +     * <p>Flag indicating whether resource getter methods should return
  +     * <code>null</code> (instead of throwing an exception) on invalid
  +     * key values.</p>
  +     */
  +    protected boolean returnNull = true;
  +
  +
       // ------------------------------------------------------ Lifecycle Methods
   
   
  @@ -160,6 +168,32 @@
       public String getName() {
   
           return (this.name);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Return <code>true</code> if resource getter methods will return
  +     * <code>null</code> instead of throwing an exception on invalid
  +     * key values.</p>
  +     */
  +    public boolean isReturnNull() {
  +
  +        return (this.returnNull);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Set a flag determining whether resource getter methods should
  +     * return <code>null</code> instead of throwing an exception on
  +     * invalid key values.</p>
  +     *
  +     * @param returnNull The new flag value
  +     */
  +    public void setReturnNull(boolean returnNull) {
  +
  +        this.returnNull = returnNull;
   
       }
   
  
  
  
  1.2       +39 -5     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesFactoryBase.java
  
  Index: ResourcesFactoryBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesFactoryBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourcesFactoryBase.java	28 Dec 2002 00:56:24 -0000	1.1
  +++ ResourcesFactoryBase.java	28 Dec 2002 04:40:55 -0000	1.2
  @@ -94,6 +94,40 @@
       protected Map resources = new HashMap();
   
   
  +    /**
  +     * <p>The <code>returnNull</code> property value that will be configured
  +     * on {@link Resources} instances created by this factory.</p>
  +     */
  +    protected boolean returnNull = true;
  +
  +
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * <p>Return the <code>returnNull</code> property value that will
be
  +     * configured on {@link Resources} instances created by this factory.</p>
  +     */
  +    public boolean isReturnNull() {
  +
  +        return (this.returnNull);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Set the <code>returnNull</code> property value that will be
  +     * configured on {@link Resources} instances created by this factory.</p>
  +     *
  +     * @param returnNull The new value to delegate
  +     */
  +    public void setReturnNull(boolean returnNull) {
  +
  +        this.returnNull = returnNull;
  +
  +    }
  +
  +
       // --------------------------------------------------------- Public Methods
   
   
  @@ -173,8 +207,8 @@
       /**
        * <p>Create and return a new {@link Resources} instance with the
        * specified logical name, after calling its <code>init()</code>
  -     * method.  Concrete subclasses <strong>MUST</strong>
  -     * implement this method.</p>
  +     * method and delegating the relevant properties.  Concrete
  +     * subclasses <strong>MUST</strong> implement this method.</p>
        *
        * @param name Logical name of the {@link Resources} instance to create
        * @param config Configuration string for this resource (if any)
  
  
  
  1.6       +8 -4      jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/MessageResourcesFactory.java
  
  Index: MessageResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/MessageResourcesFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MessageResourcesFactory.java	28 Dec 2002 00:04:07 -0000	1.5
  +++ MessageResourcesFactory.java	28 Dec 2002 04:40:55 -0000	1.6
  @@ -102,6 +102,10 @@
           return (this.returnNull);
       }
   
  +    public boolean isReturnNull() {
  +        return (this.returnNull);
  +    }
  +
       public void setReturnNull(boolean returnNull) {
           this.returnNull = returnNull;
       }
  
  
  
  1.2       +13 -4     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourceBundleResourcesTestCase.java
  
  Index: ResourceBundleResourcesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourceBundleResourcesTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourceBundleResourcesTestCase.java	28 Dec 2002 03:56:29 -0000	1.1
  +++ ResourceBundleResourcesTestCase.java	28 Dec 2002 04:40:56 -0000	1.2
  @@ -70,6 +70,7 @@
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
   import org.apache.commons.resources.ResourcesFactory;
  +import org.apache.commons.resources.ResourcesKeyException;
   import org.apache.commons.resources.impl.ResourceBundleResources;
   
   
  @@ -148,10 +149,18 @@
   
           Locale locale = new Locale("en");
           try {
  +            resources.setReturnNull(false);
               String value = resources.getString("test.missing", locale, null);
               fail("Should have thrown ResourcesException");
  -        } catch (ResourcesException e) {
  +        } catch (ResourcesKeyException e) {
               ; // Expected result
  +        }
  +        try {
  +            resources.setReturnNull(true);
  +            String value = resources.getString("test.missing", locale, null);
  +            assertTrue("Should have returned null", value == null);
  +        } catch (ResourcesException e) {
  +            fail("Should have returned null");
           }
   
       }
  
  
  
  1.2       +13 -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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourcesBaseTestCase.java	28 Dec 2002 03:56:29 -0000	1.1
  +++ ResourcesBaseTestCase.java	28 Dec 2002 04:40:56 -0000	1.2
  @@ -70,6 +70,7 @@
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
   import org.apache.commons.resources.ResourcesFactory;
  +import org.apache.commons.resources.ResourcesKeyException;
   import org.apache.commons.resources.impl.ResourcesBase;
   
   
  @@ -147,10 +148,18 @@
   
           Locale locale = new Locale("en");
           try {
  +            resources.setReturnNull(false);
               String value = resources.getString("test.missing", locale, null);
               fail("Should have thrown ResourcesException");
  -        } catch (ResourcesException e) {
  +        } catch (ResourcesKeyException e) {
               ; // Expected result
  +        }
  +        try {
  +            resources.setReturnNull(true);
  +            String value = resources.getString("test.missing", locale, null);
  +            assertTrue("Should have returned null", value == null);
  +        } catch (ResourcesException e) {
  +            fail("Should have returned null");
           }
   
       }
  
  
  
  1.2       +9 -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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestResources.java	28 Dec 2002 03:56:29 -0000	1.1
  +++ TestResources.java	28 Dec 2002 04:40:56 -0000	1.2
  @@ -76,6 +76,7 @@
   import java.util.TimeZone;
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
  +import org.apache.commons.resources.ResourcesKeyException;
   
   
   /**
  @@ -252,7 +253,11 @@
   
           String value = (String) map.get(key);
           if (value == null) {
  -            throw new ResourcesException("Missing key '" + key + "'");
  +            if (isReturnNull()) {
  +                return (null);
  +            } else {
  +                throw new ResourcesKeyException(key);
  +            }
           }
           return (value);
   
  
  
  
  1.2       +4 -3      jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResourcesFactory.java
  
  Index: TestResourcesFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResourcesFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestResourcesFactory.java	28 Dec 2002 03:56:29 -0000	1.1
  +++ TestResourcesFactory.java	28 Dec 2002 04:40:56 -0000	1.2
  @@ -102,6 +102,7 @@
   
           Resources resources = new TestResources(name);
           resources.init();
  +        resources.setReturnNull(isReturnNull());
           return (resources);
   
       }
  
  
  
  1.4       +11 -1     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/FileResourcesExposerFactory.java
  
  Index: FileResourcesExposerFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/FileResourcesExposerFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileResourcesExposerFactory.java	28 Dec 2002 03:56:29 -0000	1.3
  +++ FileResourcesExposerFactory.java	28 Dec 2002 04:40:56 -0000	1.4
  @@ -21,5 +21,15 @@
       public void release() {
       }
   
  -    
  +    protected boolean returnNull = true;
  +
  +    public boolean isReturnNull() {
  +        return (this.returnNull);
  +    }
  +
  +    public void setReturnNull(boolean returnNull) {
  +        this.returnNull = returnNull;
  +    }
  +
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message