mschachter    01/07/06 12:51:12

  Modified:    resources/src/java/org/apache/commons/resources
                        AbstractResource.java ResourceManager.java
               resources/src/java/org/apache/commons/resources/file
                        FileResource.java LocalStrings.properties
               resources/src/java/org/apache/commons/resources/file/web
                        WebappFileResource.java
               resources/src/java/org/apache/commons/resources/message
                        MessageResourcesFactory.java
               resources/src/java/org/apache/commons/resources/web
                        WebappResourceManager.java
               resources/src/test/org/apache/commons/resources/tests
                        AllTests.java ConfigurationReaderTest.java
                        config-test.xml
  Added:       resources/src/test/org/apache/commons/resources/tests
                        FileResourceExposer.java
                        FileResourceExposerFactory.java
                        FileResourceTest.java
  Log:
   - alot of things.  added tests for FileResource and some general updates to
     all parties involved
  
  Revision  Changes    Path
  1.4       +1 -2      
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/AbstractResource.java
  
  Index: AbstractResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/AbstractResource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractResource.java     2001/06/27 22:07:12     1.3
  +++ AbstractResource.java     2001/07/06 19:50:53     1.4
  @@ -34,8 +34,7 @@
       /**
        * This is called on to initialize the Resource, before any of the
        * getData methods are called, and after the setConfigurationURL() method
  -     * is called.  The default implementation wipes the hard drive of the
  -     * unsuspecting developer...
  +     * is called.
        */
       public void init() throws ResourceException {
           ;
  
  
  
  1.4       +8 -5      
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceManager.java
  
  Index: ResourceManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ResourceManager.java      2001/06/27 22:07:16     1.3
  +++ ResourceManager.java      2001/07/06 19:50:54     1.4
  @@ -38,20 +38,23 @@
        * @param configReader The ConfigurationReader implementation
        *                     to use for the resource information
        */
  -    public ResourceManager(URL configURL) throws ResourceException {
  -        
  +    public ResourceManager(URL configURL) throws ResourceException, IOException {
  +        this(configURL.openStream());
  +    }
  +   
  +    public ResourceManager(InputStream inputStream) throws ResourceException {
           ConfigurationReader configReader = new ConfigurationReader();
  -        InputStream inputStream = null;
           try {
  -            inputStream = configURL.openStream();
               configReader.read(inputStream);
           }
           catch (IOException ioe) {
               throw new ResourceException(
  -                messageResources.getMessage("resources.manager.ioexception", ioe));
  +                messageResources.getMessage("resources.manager.ioexception",
  +                    ioe.getMessage()),ioe);
           }
           resources = configReader.getResourceMap();
           initResources();
  +        
       }
   
       /**
  
  
  
  1.2       +40 -9     
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResource.java
  
  Index: FileResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileResource.java 2001/06/27 22:07:20     1.1
  +++ FileResource.java 2001/07/06 19:50:57     1.2
  @@ -11,6 +11,7 @@
   import java.io.IOException;
   import java.io.FileInputStream;
   import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
   import java.io.FileNotFoundException;
   
   import org.apache.commons.resources.AbstractResource;
  @@ -90,7 +91,7 @@
       protected long maxCacheSize = 100 * 1024;
       
       /** 
  -     * The size in bytes to read from the file system at a time
  +     * The size in bytes to read from the file system at a time [20K]
        */
       protected int bufferSize = 20 * 1024;
       
  @@ -122,8 +123,23 @@
        *                 the default time zone
        */
       public byte[] getData(String key, Locale locale, TimeZone timeZone)
  -        throws ResourceException {        
  -            return null;
  +        throws ResourceException {
  +            try {
  +                
  +                InputStream stream = getStream(key, locale, timeZone);              
  
  +                byte[] buffer = new byte[bufferSize];
  +                ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +                int read = 0;
  +                while ((read = stream.read(buffer, 0, buffer.length)) != -1) {
  +                    baos.write(buffer, 0, read);                
  +                }
  +                return baos.toByteArray();
  +            }
  +            catch (IOException ioe) {
  +                throw new ResourceException(
  +                    messageResources.getMessage("resources.file.data.ioexception"),
  +                    ioe);
  +            }
       }
               
       
  @@ -212,7 +228,7 @@
       
       /**
        * Converts a String to an InputStream.  This should not look in the cache
  -     * for a steam, it should create a new stream.  The purpose of this is to
  +     * for a stream, it should create a new stream.  The purpose of this is to
        * localize the way in which this type of resource turns a String into a 
        * stream so that subclasses (i.e. WebappFileResource) can inherit a large
        * portion of the functionality.
  @@ -236,12 +252,24 @@
           
           StringBuffer keyString = new StringBuffer();
           keyString.append(key);
  -        keyString.append(ID_SEPARATOR);
  -        keyString.append(locale.getLanguage());
           keyString.append(ID_SEPARATOR);
  -        keyString.append(locale.getCountry());
  +        if (locale != null) {
  +            keyString.append(locale.getLanguage());
  +            keyString.append(ID_SEPARATOR);
  +            keyString.append(locale.getCountry());
  +        }
  +        else {
  +            keyString.append("null");
  +            keyString.append(ID_SEPARATOR);
  +            keyString.append("null");
  +        }
           keyString.append(ID_SEPARATOR);
  -        keyString.append(timeZone.getID());
  +        if (timeZone != null) {
  +            keyString.append(timeZone.getID());
  +        }
  +        else {
  +            keyString.append("null");
  +        }
           
           return keyString.toString();        
       }
  @@ -269,10 +297,13 @@
           }
           int eIndex = key.lastIndexOf(".");
           if (eIndex != -1) {
  +            //grab extension
               extension = key.substring(eIndex+1, key.length());
  +            //strip extension from key
  +            key = key.substring(0, eIndex);
           }
           String[] paths = getFileNames(key, language, countryCode, zoneID,
  -                                      extension, File.pathSeparatorChar);
  +                                      extension, File.separatorChar);
           
           for (int i = 0; i < paths.length; i++) {
               
  
  
  
  1.2       +3 -1      
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/LocalStrings.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LocalStrings.properties   2001/06/27 22:07:20     1.1
  +++ LocalStrings.properties   2001/07/06 19:50:58     1.2
  @@ -1,4 +1,6 @@
   resources.file.ioexception=[FileResource] problem opening file {0}
   resources.file.filenotfound=[FileResource] file {0} not found
   resources.file.invalidkey=[FileResource] no file found for key {0}, locale {1}, and 
time zone {2}
  -resources.file.cache.ioexception=[FileResource] IO
  +resources.file.data.ioexception=[FileResource] IOException reading file data
  +resources.file.cache.ioexception=[FileResource] IOException while reading key {0} 
from cache
  +resources.file.tostream.ioexception=[FileResource] IOException while turning path 
{0} to stream
  
  
  
  1.2       +81 -4     
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/web/WebappFileResource.java
  
  Index: WebappFileResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/web/WebappFileResource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebappFileResource.java   2001/06/27 22:07:22     1.1
  +++ WebappFileResource.java   2001/07/06 19:51:01     1.2
  @@ -1,15 +1,27 @@
   package org.apache.commons.resources.file.web;
   
  +import java.io.File;
  +import java.io.InputStream;
  +import java.io.IOException;
  +
  +import java.net.URL;
  +import java.net.MalformedURLException;
  +
  +import java.util.Locale;
  +import java.util.TimeZone;
  +
   import javax.servlet.ServletContext;
   
  -import org.apache.commons.resources.web.WebappResource;
  +import org.apache.commons.resources.ResourceException;
   import org.apache.commons.resources.file.FileResource;
  +import org.apache.commons.resources.web.WebappResource;
   
   /**
    * This class can only access files within the context of it's own web application,
    * as opposed to FileResoure, which has access to the entire file system.  The
    * advantage of using this over FileResource in a web application is that you
  - * can specify paths relative to your app.
  + * can specify a base path relative to your webapp instead of a file system 
  + * base path.
    */
   public class WebappFileResource extends FileResource implements WebappResource {
       
  @@ -18,6 +30,71 @@
       
       public void setServletContext(ServletContext servletContext) {
           this.servletContext = servletContext;
  -    }   
  +    }       
  +    
  +    /**
  +     * Converts a String to an InputStream using 
ServletContext.getResourceAsStream()
  +     */
  +    protected InputStream toStream(String streamPath) throws ResourceException {
  +       return servletContext.getResourceAsStream(streamPath);                       
                                        
  +    }
  +    
  +    /**
  +     * Attempts to find an existing file for the key, locale, and time zone
  +     * given, according to the mechanism specified in 
  +     * {@link #getFileNames(String,String,String,String,String,char) getFileNames}
  +     * @returns A String that can be initialized to a stream using
  +     *          {@link #toStream(String) toStream}
  +     */
  +    protected String findValidPath(String key, Locale locale, TimeZone timeZone) {
  +        
  +        String language = null;
  +        String countryCode = null;
  +        String zoneID = null;
  +        String extension = null;
  +        
  +        if (locale != null) {
  +            language = locale.getLanguage();
  +            countryCode = locale.getCountry();
  +        }
  +        if (timeZone != null) {
  +            zoneID = timeZone.getID();
  +        }
  +        int eIndex = key.lastIndexOf(".");
  +        if (eIndex != -1) {
  +            //grab extension
  +            extension = key.substring(eIndex+1, key.length());
  +            //strip extension from key
  +            key = key.substring(0, eIndex);
  +        }
  +        String[] paths = getFileNames(key, language, countryCode, zoneID,
  +                                      extension, File.separatorChar);
  +        try {
  +            for (int i = 0; i < paths.length; i++) {            
  +                URL resource = servletContext.getResource(paths[i]);
  +                if (resource != null) {
  +                    return paths[i];
  +                }            
  +            }
  +        }
  +        catch (MalformedURLException mue) {;}
  +        return null;
  +    }
       
  -}
  +    /**
  +     * Get the size in bytes of the file represented by the argument given.  This
  +     * method is here to abstract the way to get the file size for subclasses
  +     * @param filePath The "path" member variable plus the key (file name)
  +     * @return -1 If the file size cannot be found
  +     */
  +    protected long getFileSize(String filePath) {        
  +        String realPath = servletContext.getRealPath(filePath);
  +        if (realPath != null) {
  +            File file = new File(realPath);
  +            if (file.exists()) {
  +                return file.length();
  +            }
  +        }
  +        return -1;        
  +    }    
  +}
  \ No newline at end of file
  
  
  
  1.2       +7 -14     
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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MessageResourcesFactory.java      2001/06/27 22:07:24     1.1
  +++ MessageResourcesFactory.java      2001/07/06 19:51:02     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/MessageResourcesFactory.java,v
 1.1 2001/06/27 22:07:24 mschachter Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/06/27 22:07:24 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/MessageResourcesFactory.java,v
 1.2 2001/07/06 19:51:02 mschachter Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/07/06 19:51:02 $
    *
    * ====================================================================
    * 
  @@ -82,7 +82,7 @@
    * </ul>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2001/06/27 22:07:24 $
  + * @version $Revision: 1.2 $ $Date: 2001/07/06 19:51:02 $
    */
   
   public abstract class MessageResourcesFactory
  @@ -92,13 +92,6 @@
   
       // ---------------------------------------------------- Instance Properties
           
  -    /**
  -     * The message resources for this class
  -     */
  -    protected static MessageResources messageResources = 
  -                MessageResourcesFactory.createFactory().createResources(
  -                                  "org.apache.commons.resources.LocalStrings");
  -
   
       /**
        * The "return null" property value to which newly created
  @@ -149,7 +142,7 @@
        * <code>MessageResourcesFactory</code> instances.
        */
       protected static String factoryClass =
  -        "org.apache.commons.resources.PropertyMessageResourcesFactory";
  +        "org.apache.commons.resources.message.PropertyMessageResourcesFactory";
   
       public static String getFactoryClass() {
           return (MessageResourcesFactory.factoryClass);
  @@ -180,8 +173,8 @@
                   (MessageResourcesFactory) clazz.newInstance();
               return (factory);
           } catch (Throwable t) {
  -            messageResources.getMessage("resources.message.create",
  -                    t.getMessage());
  +                System.out.println("[MessageResourcesFactory] error of type '" +
  +                    t.getClass().getName() + "': " + t.getMessage());
               return (null);
           }
   
  
  
  
  1.2       +3 -1      
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/web/WebappResourceManager.java
  
  Index: WebappResourceManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/web/WebappResourceManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebappResourceManager.java        2001/06/27 22:09:47     1.1
  +++ WebappResourceManager.java        2001/07/06 19:51:05     1.2
  @@ -1,5 +1,7 @@
   package org.apache.commons.resources.web;
   
  +import java.io.IOException;
  +
   import java.net.URL;
   
   import java.util.Iterator;
  @@ -23,7 +25,7 @@
       protected ServletContext context;
       
       public WebappResourceManager(URL config, ServletContext context)
  -        throws ResourceException {
  +        throws ResourceException, IOException {
               
               super(config);
               this.context = context;
  
  
  
  1.2       +1 -0      
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/AllTests.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AllTests.java     2001/06/22 19:04:16     1.1
  +++ AllTests.java     2001/07/06 19:51:08     1.2
  @@ -12,6 +12,7 @@
       public static Test suite() {
           TestSuite suite = new TestSuite("Resource Test Package");
           suite.addTest(ConfigurationReaderTest.suite());
  +        suite.addTest(FileResourceTest.suite());
           
           return suite;
       }
  
  
  
  1.3       +75 -40    
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/ConfigurationReaderTest.java
  
  Index: ConfigurationReaderTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/ConfigurationReaderTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConfigurationReaderTest.java      2001/06/27 22:07:27     1.2
  +++ ConfigurationReaderTest.java      2001/07/06 19:51:09     1.3
  @@ -10,6 +10,8 @@
   import org.xml.sax.SAXException;
   
   import org.apache.commons.resources.*;
  +import org.apache.commons.resources.file.*;
  +import org.apache.commons.resources.message.*;
   
   import junit.framework.*;
   
  @@ -17,7 +19,9 @@
    
       public static String RESOURCE_TEST_FILE = 
                                   
"/org/apache/commons/resources/tests/config-test.xml";
  +    public static int TOTAL_RESOURCE_NUMBER = 3;
       
  +    
       protected ConfigurationReader configReader;
       protected InputStream inputStream;
       
  @@ -39,8 +43,18 @@
       /**
        * Test the parsing of the config reader
        */
  -    public void testParse() throws ResourceException, IOException {
  -       configReader.read(inputStream);
  +    public void testParse() throws ResourceException, IOException, Exception {
  +        try {
  +            configReader.read(inputStream);
  +        }
  +        catch (ResourceException re) {
  +            if (re.getRootCause() != null) {
  +                throw re.getRootCause();
  +            }
  +            else {
  +                throw re;
  +            }
  +        }
       }
       
       /**
  @@ -48,49 +62,70 @@
        * consistent with the resources delcared in
        * /org/apache/commons/resources/test/config-test.xml.
        */
  -    public void testGetResourceMap() throws ResourceException, IOException {
  -        configReader.read(inputStream);        
  -        Map resourceMap = configReader.getResourceMap();
  +    public void testGetResourceMap() throws ResourceException, IOException, 
Exception {
           
  -        Resource fileResource = (Resource) resourceMap.get("file");
  -        assertNotNull(fileResource);
  -        assertEquals("org.apache.commons.resources.FileResource",
  -                        fileResource.getClass().getName());
  -        assertEquals("c:\\temp", ((FileResource) fileResource).getPath());
  -        
  -        Resource messageResource = (Resource) resourceMap.get("message");
  -        assertNotNull(messageResource);
  -        assertEquals("org.apache.commons.resources.PropertyMessageResources",
  -                     messageResource.getClass().getName());
  -        String config = ((MessageResources) messageResource).getConfig();
  -        assertEquals("org.apache.commons.resources.tests.ExampleStrings", config);
  -        assertEquals(true, ((MessageResources) messageResource).getReturnNull());
  +        try {
  +            configReader.read(inputStream);        
  +            Map resourceMap = configReader.getResourceMap();
  +
  +            Resource fileResource = (Resource) resourceMap.get("file");
  +            assertNotNull(fileResource);
  +            assertEquals("org.apache.commons.resources.file.FileResource",
  +                            fileResource.getClass().getName());
  +            assertEquals("c:\\temp", ((FileResource) fileResource).getPath());
  +
  +            Resource messageResource = (Resource) resourceMap.get("message");
  +            assertNotNull(messageResource);
  +            
assertEquals("org.apache.commons.resources.message.PropertyMessageResources",
  +                         messageResource.getClass().getName());
  +            String config = ((MessageResources) messageResource).getConfig();
  +            assertEquals("org.apache.commons.resources.tests.ExampleStrings", 
config);
  +            assertEquals(true, ((MessageResources) 
messageResource).getReturnNull());
  +        }
  +        catch (ResourceException re) {
  +            if (re.getRootCause() != null) {
  +                throw re.getRootCause();
  +            }
  +            else {
  +                throw re;
  +            }
  +        }
       }   
       /**
        * Test the getResources() method
        */
  -    public void testGetResources() throws ResourceException, IOException {
  -        boolean foundMessage = false;
  -        boolean foundFile = false;
  -        int count = 0;
  -        
  -        configReader.read(inputStream);         
  -        Collection values = configReader.getResources();
  -        Iterator iterator = values.iterator();
  -        while (iterator.hasNext()) {
  -            Object object = iterator.next();
  -            assertEquals(true, (object instanceof Resource));
  -            Resource resource = (Resource) object;
  -            if (resource.getName().equals("message")) {
  -                foundMessage = true;
  -            }
  -            if (resource.getName().equals("file")) {
  -                foundFile = true;
  -            }
  -            count++;
  -        }        
  -        assertEquals(true, (foundMessage && foundFile));
  -        assertEquals(2, count);        
  +    public void testGetResources() throws ResourceException, IOException, Exception 
{
  +        try {
  +            boolean foundMessage = false;
  +            boolean foundFile = false;
  +            int count = 0;
  +
  +            configReader.read(inputStream);         
  +            Collection values = configReader.getResources();
  +            Iterator iterator = values.iterator();
  +            while (iterator.hasNext()) {
  +                Object object = iterator.next();
  +                assertEquals(true, (object instanceof Resource));
  +                Resource resource = (Resource) object;
  +                if (resource.getName().equals("message")) {
  +                    foundMessage = true;
  +                }
  +                if (resource.getName().equals("file")) {
  +                    foundFile = true;
  +                }
  +                count++;
  +            }        
  +            assertEquals(true, (foundMessage && foundFile));
  +            assertEquals(TOTAL_RESOURCE_NUMBER, count);        
  +        }
  +        catch (ResourceException re) {
  +            if (re.getRootCause() != null) {
  +                throw re.getRootCause();
  +            }
  +            else {
  +                throw re;
  +            }
  +        }
       }
       
       public static Test suite() {
  
  
  
  1.2       +11 -2     
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/config-test.xml
  
  Index: config-test.xml
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/config-test.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- config-test.xml   2001/06/27 22:09:48     1.1
  +++ config-test.xml   2001/07/06 19:51:09     1.2
  @@ -5,7 +5,7 @@
       
           <!-- Configuration for a resource that is based on Java ResourceBundles -->
           <resource name="message"
  -                  
factory="org.apache.commons.resources.PropertyMessageResourcesFactory"
  +                  
factory="org.apache.commons.resources.message.PropertyMessageResourcesFactory"
                     config="org.apache.commons.resources.tests.ExampleStrings">
                     
               <set-property property="returnNull" value="true" />        
  @@ -15,7 +15,16 @@
       
           <!-- Configuration for a resource that pulls its data from flat files -->
           <resource name="file"
  -                  factory="org.apache.commons.resources.FileResourceFactory"
  +                  factory="org.apache.commons.resources.file.FileResourceFactory"
  +                  config="">
  +                  
  +            <set-property property="path" value="c:\temp" />
  +            
  +        </resource> 
  +
  +        <!-- Configuration for a resource that pulls its data from flat files -->
  +        <resource name="file-test"
  +                  
factory="org.apache.commons.resources.tests.FileResourceExposerFactory"
                     config="">
                     
               <set-property property="path" value="c:\temp" />
  
  
  
  1.1                  
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/FileResourceExposer.java
  
  Index: FileResourceExposer.java
  ===================================================================
  package org.apache.commons.resources.tests;
  
  import java.util.Locale;
  import java.util.HashMap;
  import java.util.TimeZone;
  import java.util.Iterator;
  import java.util.Collection;
  
  import java.io.File;
  import java.io.InputStream;
  import java.io.IOException;
  
  import org.apache.commons.resources.*;
  import org.apache.commons.resources.file.*;
  
  /**
   * This class exposes the protected methods of FileResource for testing
   * purposes
   */
  public class FileResourceExposer extends FileResource {
   
      
      public String exposeFindValidPath(String key, Locale locale, TimeZone timeZone) 
{        
          return findValidPath(key, locale, timeZone);       
      }
      
      public String[] exposeGetFileNames(String fileName, String language,
                                         String countryCode, String timeZone,
                                         String extension, char separator) {  
               
                 return getFileNames(fileName, language, countryCode, timeZone,
                                     extension, separator);                            
              
      }
      
      public long exposeGetFileSize(String file) {
          return getFileSize(file);
      }
      
      public InputStream exposeToStream(String file) throws ResourceException {
          return toStream(file);
      }
      
      public String exposeToStringKey(String key, Locale locale, TimeZone timeZone) {
          return toStringKey(key, locale, timeZone);
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/FileResourceExposerFactory.java
  
  Index: FileResourceExposerFactory.java
  ===================================================================
  package org.apache.commons.resources.tests;
  
  import org.apache.commons.resources.Resource;
  import org.apache.commons.resources.ResourceFactory;
  
  /**
   * This class creates FileResourceExposer resources, which expose
   * the protected methods of the FileResource class for testing
   */
  public class FileResourceExposerFactory extends ResourceFactory {
      
   
      public Resource createResource(String config) {
          return new FileResourceExposer();
      }
      
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/FileResourceTest.java
  
  Index: FileResourceTest.java
  ===================================================================
  package org.apache.commons.resources.tests;
  
  import java.io.*;
  import java.util.*;
  
  import org.xml.sax.SAXException;
  
  import org.apache.commons.resources.*;
  import org.apache.commons.resources.file.*;
  import org.apache.commons.resources.message.*;
  
  import junit.framework.*;
  
  public class FileResourceTest extends TestCase {
      
      public static String RESOURCE_TEST_FILE = 
                                  
"/org/apache/commons/resources/tests/config-test.xml";
      
      public static String RESOURCE_TEST_NAME = "file-test";
      
      public static String RESOURCE_TEST_CLASS = 
"org.apache.commons.resources.tests.FileResourceExposer";
      
      public static String RESOURCE_PATH = "c:\\temp";
      
      public static String[] FILES_EN_US_EST = new String[] {
                                          RESOURCE_PATH + File.separator + 
"test/test1_en_US_EST.txt",
                                          RESOURCE_PATH + File.separator + 
"test/test1_en_US.txt",
                                          RESOURCE_PATH + File.separator + 
"test/test1_en.txt",
                                          RESOURCE_PATH + File.separator + 
"test/test1.txt"};
                                          
      public static String FILE_TEST1_KEY = "test/test1.txt";
      
      public static String FILE_TEST1_VALID_PATH = RESOURCE_PATH + File.separator +
                                                   "test/test1_en_US_EST.txt";
      
      public static String FILE_TEST1_DATA = "test1_en_US_EST";
      
      public static String TEST1_STRING_KEY = FILE_TEST1_KEY + "_en_US_EST";
                                          
      protected InputStream inputStream;
      
      protected FileResourceExposer fileResource;
      
      protected ResourceManager manager;
      
      protected Locale locale_en_US = new Locale("en", "US");
      
      protected TimeZone timeZone_EST = TimeZone.getTimeZone("EST");
   
      
      
      public FileResourceTest(String test) {
          super(test);
      }
      
      public static void main(String args[]) {
          junit.textui.TestRunner.run(ConfigurationReaderTest.class);
      }
      
      protected void setUp() throws ResourceException, Exception {
          try {
              inputStream = null;
              manager = null;
              fileResource = null;
              
              inputStream = this.getClass().getResourceAsStream(RESOURCE_TEST_FILE);
              manager = new ResourceManager(inputStream);
  
              Resource resource = manager.getResource(RESOURCE_TEST_NAME);
              assertNotNull(resource);
              assertEquals(RESOURCE_TEST_CLASS, resource.getClass().getName());
              fileResource = (FileResourceExposer) resource;
              assertEquals(RESOURCE_PATH, fileResource.getPath());            
          }
          catch (ResourceException re) {
              if (re.getRootCause() != null) {
                  throw re.getRootCause();
              }
              else {
                  throw re;
              }
          }
      }
      
      public void testFindValidPath() {
          
          String validPath = fileResource.exposeFindValidPath(FILE_TEST1_KEY,
                                                              locale_en_US,
                                                              timeZone_EST);        
          assertEquals(FILE_TEST1_VALID_PATH, validPath);
      }
      
      public void testGetData() throws Exception {
          
          try {
          
              byte[] data = fileResource.getData(FILE_TEST1_KEY, locale_en_US, 
timeZone_EST);
              String stringData = new String(data, 0, data.length);
              assertEquals(FILE_TEST1_DATA, stringData);
          }
          catch (ResourceException re) {
              if (re.getRootCause() != null) {
                  throw re.getRootCause();
              }
              else {
                  throw re;
              }
          }
      }
      
      public void testGetFileNames() {
          String extension = null;
          String key = FILE_TEST1_KEY;
          int extensionIndex = key.lastIndexOf(".");
          if (extensionIndex != -1) {
              extension = key.substring(extensionIndex+1, key.length());
              key = key.substring(0, extensionIndex);            
          }
          String language = locale_en_US.getLanguage();
          String countryCode = locale_en_US.getCountry();
          String timeZone = timeZone_EST.getID();
          char separator = File.separatorChar;
          
          String[] files = fileResource.exposeGetFileNames(key, language, countryCode,
                                                           timeZone, extension,
                                                           separator);
          assertNotNull(files);
          for (int i = 0; i < files.length; i++) {
              assertEquals(FILES_EN_US_EST[i], files[i]);
          }        
      }
      
      public void testToStringKey() {
          String result = fileResource.exposeToStringKey(FILE_TEST1_KEY,
                                                         locale_en_US,
                                                         timeZone_EST);
          assertEquals(TEST1_STRING_KEY, result);
      }  
      
      public static Test suite() {
          return new TestSuite(FileResourceTest.class);       
      }
  }
  
  
  

Reply via email to