commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mart...@apache.org
Subject svn commit: r156615 - in jakarta/commons/proper/chain/trunk/src: java/org/apache/commons/chain/web/ChainResources.java test/org/apache/commons/chain/web/ChainResourcesTestCase.java
Date Wed, 09 Mar 2005 04:38:52 GMT
Author: martinc
Date: Tue Mar  8 20:38:51 2005
New Revision: 156615

URL: http://svn.apache.org/viewcvs?view=rev&rev=156615
Log:
Factor out the comma-delimited parsing into a separate method, fix the whitespace-skipping
bugs in it, and add unit tests to verify that.

Added:
    jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/ChainResourcesTestCase.java
Modified:
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/ChainResources.java

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/ChainResources.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/ChainResources.java?view=diff&r1=156614&r2=156615
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/ChainResources.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/ChainResources.java
Tue Mar  8 20:38:51 2005
@@ -17,6 +17,8 @@
 
 
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 import javax.servlet.ServletContext;
 import org.apache.commons.chain.Catalog;
 import org.apache.commons.chain.config.ConfigParser;
@@ -65,20 +67,11 @@
         if (loader == null) {
             loader = ChainResources.class.getClassLoader();
         }
+        String[] paths = getResourcePaths(resources);
         String path = null;
         try {
-            while (true) {
-                int comma = resources.indexOf(",");
-                if (comma < 0) {
-                    path = resources.trim();
-                    resources = "";
-                } else {
-                    path = resources.substring(0, comma);
-                    resources = resources.substring(comma + 1);
-                }
-                if (path.length() < 1) {
-                    break;
-                }
+            for (int i = 0; i < paths.length; i++) {
+                path = paths[i];
                 URL url = loader.getResource(path);
                 if (url == null) {
                     throw new IllegalStateException
@@ -119,20 +112,11 @@
         if (loader == null) {
             loader = ChainResources.class.getClassLoader();
         }
+        String[] paths = getResourcePaths(resources);
         String path = null;
         try {
-            while (true) {
-                int comma = resources.indexOf(",");
-                if (comma < 0) {
-                    path = resources.trim();
-                    resources = "";
-                } else {
-                    path = resources.substring(0, comma);
-                    resources = resources.substring(comma + 1);
-                }
-                if (path.length() < 1) {
-                    break;
-                }
+            for (int i = 0; i < paths.length; i++) {
+                path = paths[i];
                 URL url = loader.getResource(path);
                 if (url == null) {
                     throw new IllegalStateException
@@ -166,20 +150,11 @@
         if (resources == null) {
             return;
         }
+        String[] paths = getResourcePaths(resources);
         String path = null;
         try {
-            while (true) {
-                int comma = resources.indexOf(",");
-                if (comma < 0) {
-                    path = resources.trim();
-                    resources = "";
-                } else {
-                    path = resources.substring(0, comma);
-                    resources = resources.substring(comma + 1);
-                }
-                if (path.length() < 1) {
-                    break;
-                }
+            for (int i = 0; i < paths.length; i++) {
+                path = paths[i];
                 URL url = context.getResource(path);
                 if (url == null) {
                     throw new IllegalStateException
@@ -217,20 +192,11 @@
         if (resources == null) {
             return;
         }
+        String[] paths = getResourcePaths(resources);
         String path = null;
         try {
-            while (true) {
-                int comma = resources.indexOf(",");
-                if (comma < 0) {
-                    path = resources.trim();
-                    resources = "";
-                } else {
-                    path = resources.substring(0, comma);
-                    resources = resources.substring(comma + 1);
-                }
-                if (path.length() < 1) {
-                    break;
-                }
+            for (int i = 0; i < paths.length; i++) {
+                path = paths[i];
                 URL url = context.getResource(path);
                 if (url == null) {
                     throw new IllegalStateException
@@ -248,6 +214,39 @@
         }
 
     }
+
+
+    /**
+     * <p> Parse the resource string into an array of paths. Empty entries will
+     * be skipped. (That is, all entries in the array are non-empty paths.)</p>
+     *
+     * @param resources A comma-delimited list of resource paths (or
+     *                  <code>null</code>).
+     *
+     * @return An array of non-empty paths. The array itself may be empty.
+     */
+    static String[] getResourcePaths(String resources) {
+        List paths = new ArrayList();
+
+        if (resources != null) {
+            String path;
+            int comma;
+
+            while ((comma = resources.indexOf(',')) >= 0) {
+                path = resources.substring(0, comma).trim();
+                if (path.length() > 0) {
+                    paths.add(path);
+                }
+                resources = resources.substring(comma + 1);
+            }
+            resources = resources.trim();
+            if (resources.length() > 0) {
+                paths.add(resources);
+            }
+        }
+
+        return (String[]) paths.toArray(new String[0]);
+    }        
 
 
 }

Added: jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/ChainResourcesTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/ChainResourcesTestCase.java?view=auto&rev=156615
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/ChainResourcesTestCase.java
(added)
+++ jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/ChainResourcesTestCase.java
Tue Mar  8 20:38:51 2005
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.commons.chain.web;
+
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+// Test case for org.apache.commons.chain.web.ChainResources
+
+public class ChainResourcesTestCase extends TestCase {
+
+
+    // ---------------------------------------------------------- Constructors
+
+    /**
+     * Construct a new instance of this test case.
+     *
+     * @param name Name of the test case
+     */
+    public ChainResourcesTestCase(String name) {
+        super(name);
+    }
+
+
+    // ----------------------------------------------------- Instance Variables
+
+
+    protected TestData[] data = new TestData[]
+        {
+            new TestData("a,b,c",            new String[] {"a", "b", "c"}),
+            new TestData("a , b , c ",       new String[] {"a", "b", "c"}),
+            new TestData("a,\tb,\tc ",       new String[] {"a", "b", "c"}),
+            new TestData("\na,\nb,\nc\n",    new String[] {"a", "b", "c"}),
+            new TestData("a,,b,c ",          new String[] {"a", "b", "c"}),
+            new TestData(",a,b,,c,,",        new String[] {"a", "b", "c"}),
+            new TestData(null,               new String[] {}),
+            new TestData("",                 new String[] {}),
+            new TestData(",",                new String[] {}),
+            new TestData(",,",               new String[] {})
+        };
+
+
+    // ------------------------------------------------ Individual Test Methods
+
+
+    public void testGetPaths() throws Exception {
+        for (int i = 0; i < data.length; i++) {
+            TestData datum = data[i];
+            String[] expected = datum.getExpected();
+            String[] actual = ChainResources.getResourcePaths(datum.getInput());
+
+            assertNotNull(actual);
+            assertEquals(expected.length, actual.length);
+            for (int j = 0; j < actual.length; j++) {
+                assertEquals(expected[j], actual[j]);
+            }
+        }
+    }
+
+
+    // ---------------------------------------------------------- Inner classes
+
+
+    // Container for test data for one test
+    public static final class TestData {
+        private String input;
+        private String[] expected;
+        public TestData(String input, String[] expected) {
+            this.input = input;
+            this.expected = expected;
+        }
+        public String getInput() {
+            return input;
+        }
+        public String[] getExpected() {
+            return expected;
+        }
+    }
+
+}



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


Mime
View raw message