cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: cocoon-2.2/src/test/org/apache/cocoon/util/test NetUtilsTestCase.java
Date Fri, 31 Oct 2003 21:41:10 GMT
vgritsenko    2003/10/31 13:41:10

  Modified:    src/java/org/apache/cocoon/bean Target.java
               src/java/org/apache/cocoon/util NetUtils.java
               src/test/org/apache/cocoon/util/test NetUtilsTestCase.java
  Log:
  NetUtils.absolutize: support null or empty resource path
  
  Revision  Changes    Path
  1.8       +7 -7      cocoon-2.2/src/java/org/apache/cocoon/bean/Target.java
  
  Index: Target.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/bean/Target.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Target.java	19 Sep 2003 09:09:43 -0000	1.7
  +++ Target.java	31 Oct 2003 21:41:10 -0000	1.8
  @@ -93,14 +93,14 @@
       private transient int _hashCode;
       private transient String _toString;
   
  -    public Target(
  -        String type,
  -        String root,
  -        String sourceURI,
  -        String destURI)
  -        throws IllegalArgumentException {
  +    public Target(String type,
  +                  String root,
  +                  String sourceURI,
  +                  String destURI)
  +    throws IllegalArgumentException {
           this.type = type;
           this.root = root;
  +        
           if (destURI == null || destURI.length() == 0) {
               throw new IllegalArgumentException("You must specify a destination directory
when defining a target");
           }
  
  
  
  1.6       +66 -38    cocoon-2.2/src/java/org/apache/cocoon/util/NetUtils.java
  
  Index: NetUtils.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/util/NetUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- NetUtils.java	24 Sep 2003 21:41:11 -0000	1.5
  +++ NetUtils.java	31 Oct 2003 21:41:10 -0000	1.6
  @@ -242,19 +242,20 @@
        */
       public static String getPath(String uri) {
           int i = uri.lastIndexOf('/');
  -        if(i > -1)
  +        if (i > -1) {
               return uri.substring(0, i);
  +        }
           i = uri.indexOf(':');
  -        return (i > -1) ? uri.substring(i+1,uri.length()) : "";
  +        return (i > -1) ? uri.substring(i + 1, uri.length()) : "";
       }
   
  -   /**
  -    * Remove path and file information from a filename returning only its
  -    * extension  component
  -    *
  -    * @param uri The filename
  -    * @return The filename extension (with starting dot!)
  -    */
  +    /**
  +     * Remove path and file information from a filename returning only its
  +     * extension  component
  +     *
  +     * @param uri The filename
  +     * @return The filename extension (with starting dot!)
  +     */
       public static String getExtension(String uri) {
           int dot = uri.lastIndexOf('.');
           if (dot > -1) {
  @@ -283,26 +284,38 @@
       }
   
       /**
  -     * Absolutize a relative resource on the given absolute path.
  +     * Absolutize a relative resource path on the given absolute base path.
        *
  -     * @param path The absolute path
  -     * @param relativeResource The relative resource
  -     * @return the absolutized resource
  -     */
  -    public static String absolutize(String path, String relativeResource) {
  -        if (("".equals(path)) || (path == null)) return relativeResource;
  -        if (relativeResource.charAt(0) != '/') {
  -            int length = path.length() - 1;
  -            boolean slashPresent = (path.charAt(length) == '/');
  -            StringBuffer b = new StringBuffer();
  -            b.append(path);
  -            if (!slashPresent) b.append('/');
  -            b.append(relativeResource);
  -            return b.toString();
  -        } else {
  -            // resource is already absolute
  -            return relativeResource;
  -        }
  +     * @param path The absolute base path
  +     * @param resource The relative resource path
  +     * @return The absolutized resource path
  +     */
  +    public static String absolutize(String path, String resource) {
  +        if (path == null || path.length() == 0) {
  +            // Base path is empty
  +            return resource;
  +        }
  +        
  +        if (resource == null || resource.length() == 0) {
  +            // Resource path is empty
  +            return path;
  +        }
  +
  +        if (resource.charAt(0) == '/') {
  +            // Resource path is already absolute
  +            return resource;
  +        }
  +        
  +        int length = path.length() - 1;
  +        boolean slash = (path.charAt(length) == '/');
  +        
  +        StringBuffer b = new StringBuffer();
  +        b.append(path);
  +        if (!slash) {
  +            b.append('/');
  +        } 
  +        b.append(resource);
  +        return b.toString();
       }
   
       /**
  @@ -313,8 +326,14 @@
        * @return the resource relative to the given path
        */
       public static String relativize(String path, String absoluteResource) {
  -        if (("".equals(path)) || (path == null)) return absoluteResource;
  -        if (path.charAt(path.length() - 1) != '/') path += "/";
  +        if (path == null || "".equals(path)) {
  +            return absoluteResource;
  +        }
  +        
  +        if (path.charAt(path.length() - 1) != '/') {
  +            path += "/";
  +        }
  +        
           if (absoluteResource.startsWith(path)) {
               // resource is direct descentant
               return absoluteResource.substring(path.length());
  @@ -395,12 +414,17 @@
        */
       public static String deparameterize(String uri, Map parameters) {
           int i = uri.lastIndexOf('?');
  -        if (i == -1) return uri;
  -        String[] params = StringUtils.split(uri.substring(i+1), "&");
  +        if (i == -1) {
  +            return uri;
  +        }
  +        
  +        String[] params = StringUtils.split(uri.substring(i + 1), "&");
           for (int j = 0; j < params.length; j++) {
               String p = params[j];
               int k = p.indexOf('=');
  -            if (k == -1) break;
  +            if (k == -1) {
  +                break;
  +            }
               String name = p.substring(0, k);
               String value = p.substring(k+1);
               parameters.put(name, value);
  @@ -412,6 +436,7 @@
           if (parameters.size() == 0) {
               return uri;
           }
  +        
           StringBuffer buffer = new StringBuffer(uri);
           buffer.append('?');
           for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
  @@ -432,18 +457,20 @@
        */
       public static SourceParameters createParameters(Request request) {
           final SourceParameters pars = new SourceParameters();
  -        if ( null != request ) {
  +
  +        if (null != request) {
               final Enumeration names = request.getParameterNames();
  -            while ( names.hasMoreElements() ) {
  +            while (names.hasMoreElements()) {
                   final String current = (String)names.nextElement();
  -                final String[] values = request.getParameterValues( current );
  -                if ( null != values ) {
  +                final String[] values = request.getParameterValues(current);
  +                if (null != values) {
                       for(int i=0; i < values.length; i++) {
  -                        pars.setParameter( current, values[i]);
  +                        pars.setParameter(current, values[i]);
                       }
                   }
               }
           }
  +
           return pars;
       }
   
  @@ -454,6 +481,7 @@
           if (uri.indexOf("@")!=-1 && (uri.startsWith("ftp://") || uri.startsWith("http://")))
{
               return uri.substring(0, uri.indexOf(":")+2)+uri.substring(uri.indexOf("@")+1);
           } 
  +
           return uri;
       }
   }
  
  
  
  1.4       +22 -14    cocoon-2.2/src/test/org/apache/cocoon/util/test/NetUtilsTestCase.java
  
  Index: NetUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/test/org/apache/cocoon/util/test/NetUtilsTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- NetUtilsTestCase.java	16 Mar 2003 18:03:56 -0000	1.3
  +++ NetUtilsTestCase.java	31 Oct 2003 21:41:10 -0000	1.4
  @@ -153,10 +153,17 @@
       public void testAbsolutize() throws Exception {
   
           Object[] test_values = {
  -                new String[]{"http://xml.apache.org", "foo.bar", "http://xml.apache.org/foo.bar"},
  -                new String[]{"http://xml.apache.org/", "foo.bar", "http://xml.apache.org/foo.bar"},
  -                new String[]{"http://xml.apache.org", "/foo.bar", "/foo.bar"},
  -                };
  +            new String[]{"/base/path",  "foo.bar",  "/base/path/foo.bar"},
  +            new String[]{"/base/path/", "foo.bar",  "/base/path/foo.bar"},
  +            new String[]{"/base/path",  "/foo.bar", "/foo.bar"},
  +            
  +            new String[]{"/base/path", "",   "/base/path"},
  +            new String[]{"/base/path", null, "/base/path"},
  +            
  +            new String[]{"",   "foo.bar", "foo.bar"},
  +            new String[]{null, "foo.bar", "foo.bar"},
  +        };
  +        
           for (int i = 0; i < test_values.length; i++) {
               String tests[] = (String[]) test_values[i];
               String test_path = tests[0];
  @@ -259,10 +266,12 @@
        */
       public void testDeparameterize() throws Exception {
           Map parameters = new HashMap();
  +
           Object[] test_values = {
  -                new String[]{"/foo/bar", "/foo/bar"},
  -                new String[]{"bar?a=b&c=d", "bar"},
  -                };
  +            new String[]{"/foo/bar", "/foo/bar"},
  +            new String[]{"bar?a=b&c=d", "bar"},
  +        };
  +
           for (int i = 0; i < test_values.length; i++) {
               String tests[] = (String[]) test_values[i];
               String test = tests[0];
  @@ -289,8 +298,9 @@
           Map parameters1 = new HashMap();
   
           Object[] test_values = {
  -                new Object[]{"/foo/bar", parameters1, "/foo/bar"},
  -                };
  +            new Object[]{"/foo/bar", parameters1, "/foo/bar"},
  +        };
  +
           for (int i = 0; i < test_values.length; i++) {
               Object tests[] = (Object[]) test_values[i];
               String test = (String) tests[0];
  @@ -314,12 +324,10 @@
                       
           String result = NetUtils.parameterize(test, parameters2);        
   
  -        if(expected1.equals(result)){
  +        if (expected1.equals(result)) {
             assertEquals(message, expected1, result);  
  -        }
  -        else{
  +        } else {
             assertEquals(message, expected2, result);  
           }
  -                              
       }
   }
  
  
  

Mime
View raw message