Author: sebb
Date: Sun Oct 23 08:53:29 2005
New Revision: 327803

URL: http://svn.apache.org/viewcvs?rev=327803&view=rev
Log:
Add new String split(input,delims,default) method to replace the one in 
JMeterUtils

Update test suite accordingly

Modified:
    
jakarta/jmeter/branches/rel-2-1/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
    
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jorphan/util/TestJorphanUtils.java

Modified: 
jakarta/jmeter/branches/rel-2-1/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java?rev=327803&r1=327802&r2=327803&view=diff
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-1/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
 (original)
+++ 
jakarta/jmeter/branches/rel-2-1/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
 Sun Oct 23 08:53:29 2005
@@ -1,6 +1,5 @@
-// $Header$
 /*
- * Copyright 2002-2004 The Apache Software Foundation.
+ * Copyright 2002-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.
@@ -22,6 +21,9 @@
 import java.lang.reflect.Method;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
 import java.util.Vector;
 
 import org.apache.log.Logger;
@@ -47,7 +49,8 @@
         * This is _almost_ equivalent to the String.split method in JDK 1.4. 
It is
         * here to enable us to support earlier JDKs.
         * 
-        * Note that unlike JDK1.4 spilt(), it ignores leading split Characters.
+        * Note that unlike JDK1.4 split(), it ignores leading split Characters,
+     * and the splitChar parameter is not a Regular expression
         * 
         * <P>
         * This piece of code used to be part of JMeterUtils, but was moved here
@@ -57,7 +60,13 @@
         *            String to be split
         * @param splitChar
         *            Character to split the string on
+     * @param truncate
+     *            Should adjacent and leading splitChars be removed?
+     *            
         * @return Array of all the tokens.
+     * 
+     * @see #split(String, String, String)
+     * 
         */
        public static String[] split(String splittee, String splitChar,boolean 
truncate) {
                if (splittee == null || splitChar == null) {
@@ -98,7 +107,55 @@
         return split(splittee,splitChar,true);
     }
 
-       private static final String SPACES = "                                 
";
+    /**
+     * Takes a String and a tokenizer character string, and returns a new 
array of
+     * strings of the string split by the tokenizer character(s).
+     * 
+     * Trailing delimiters are significant (unless the default = null)
+     *  
+     * @param splittee
+     *            String to be split.
+     * @param delims
+     *            Delimiter character(s) to split the string on
+     * @param def
+     *            Default value to place between two split chars that have
+     *            nothing between them. If null, then ignore omitted elements.
+     *
+     * @return Array of all the tokens.
+     * 
+     * @throws NullPointerException if splittee or delims are null
+     * 
+     * @see #split(String, String, boolean)
+     * @see #split(String, String)
+     * 
+     * This is a rewritten version of JMeterUtils.split()
+     */
+    public static String[] split(String splittee, String delims, String def) {
+        StringTokenizer tokens = new 
StringTokenizer(splittee,delims,def!=null);
+        boolean lastWasDelim=false;
+        List strList=new ArrayList();
+        while (tokens.hasMoreTokens()) {
+            String tok=tokens.nextToken();
+            if (   tok.length()==1 // we have a single character; could be a 
token 
+                && delims.indexOf(tok)!=-1) // it is a token
+            {
+                if (lastWasDelim) {// we saw a delimiter last time
+                    strList.add(def);// so add the default
+                }
+                lastWasDelim=true;
+            } else {
+                lastWasDelim=false;
+                strList.add(tok);
+            }
+        }
+        if (lastWasDelim) {
+            strList.add(def);
+        }
+        return (String[])strList.toArray(new String[0]);
+    }
+    
+    
+    private static final String SPACES = "                                 ";
 
        private static final int SPACES_LEN = SPACES.length();
 

Modified: 
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jorphan/util/TestJorphanUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jorphan/util/TestJorphanUtils.java?rev=327803&r1=327802&r2=327803&view=diff
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jorphan/util/TestJorphanUtils.java
 (original)
+++ 
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jorphan/util/TestJorphanUtils.java
 Sun Oct 23 08:53:29 2005
@@ -1,3 +1,24 @@
+/*
+ * Copyright 2004-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 to test JOrphanUtils methods 
+ */
+     
 package org.apache.jorphan.util;
 
 import junit.framework.TestCase;
@@ -6,12 +27,10 @@
 
     public TestJorphanUtils() {
         super();
-        // TODO Auto-generated constructor stub
     }
 
     public TestJorphanUtils(String arg0) {
         super(arg0);
-        // TODO Auto-generated constructor stub
     }
     
         public void testReplace1() {
@@ -46,6 +65,7 @@
             assertEquals("img src=xyz ", JOrphanUtils.replaceFirst("img 
src=xyz alt=\"\" ", "alt=\"\" ", ""));
         }
 
+        // Tests for split(String,String,boolean)
         public void testSplit1() {
             String in = "a,bc,,"; // Test ignore trailing split characters
             String out[] = JOrphanUtils.split(in, ",",true);
@@ -72,10 +92,110 @@
         {
             String in = "a,b,,,d,e,,f";
             String[] out = JOrphanUtils.split(in,",",true);
+            assertEquals(5, out.length);
             assertEquals("d",out[2]);
+            assertEquals("f",out[4]);
             out = JOrphanUtils.split(in,",",false);
+            assertEquals(8, out.length);
             assertEquals("",out[2]);
+            assertEquals("f",out[7]);
             
+        }
+
+        // Tests for split(String,String,String)
+        public void testSplitSSS1() {
+            String in = "a,bc,,"; // Test non-empty parameters
+            String out[] = JOrphanUtils.split(in, ",","?");
+            assertEquals(4, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("bc", out[1]);
+            assertEquals("?", out[2]);
+            assertEquals("?", out[3]);
+        }
+
+        public void testSplitSSS2() {
+            String in = "a,bc,,"; // Empty default
+            String out[] = JOrphanUtils.split(in, ",","");
+            assertEquals(4, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("bc", out[1]);
+            assertEquals("", out[2]);
+            assertEquals("", out[3]);
+        }
+
+        public void testSplitSSS3() {
+            String in = "a,bc,,"; // Empty delimiter
+            String out[] = JOrphanUtils.split(in, "","?");
+            assertEquals(1, out.length);
+            assertEquals(in, out[0]);
+        }
+
+        public void testSplitSSS4() {
+            String in = "a,b;c,,"; // Multiple delimiters
+            String out[];
+            out = JOrphanUtils.split(in, ",;","?");
+            assertEquals(5, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("b", out[1]);
+            assertEquals("?", out[4]);
+            out = JOrphanUtils.split(in, ",;","");
+            assertEquals(5, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("b", out[1]);
+            assertEquals("", out[4]);
+        }
+
+        public void testSplitSSS5() {
+            String in = "a,bc,,"; // Delimiter same as splitter
+            String out[] = JOrphanUtils.split(in, ",",",");
+            assertEquals(4, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("bc", out[1]);
+            assertEquals(",", out[2]);
+            assertEquals(",", out[3]);
+        }
+
+        public void testSplitSSSNulls() {
+            String in = "a,bc,,";
+            String out[];
+            try {
+                out = JOrphanUtils.split(null, ",","?");
+                assertEquals(0, out.length);
+                fail("Expecting NullPointerException");
+            } catch (NullPointerException ignored){
+                //Ignored
+            }
+            try{
+                out = JOrphanUtils.split(in, null,"?");
+                assertEquals(0, out.length);
+                assertEquals(0, out.length);
+                fail("Expecting NullPointerException");
+            } catch (NullPointerException ignored){
+                //Ignored
+            }
+        }
+
+        public void testSplitSSSNull() {
+            String out[];
+            out = JOrphanUtils.split("a,bc,,", ",",null);
+            assertEquals(2, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("bc", out[1]);
+
+            out = JOrphanUtils.split("a,;bc,;,", ",;",null);
+            assertEquals(2, out.length);
+            assertEquals("a", out[0]);
+            assertEquals("bc", out[1]);
+        }
+
+        public void testSplitSSSNone() {
+            String out[];
+            out = JOrphanUtils.split("", "," ,"x");
+            assertEquals(0, out.length);
+
+            out = JOrphanUtils.split("a,;bc,;,", "","x");
+            assertEquals(1, out.length);
+            assertEquals("a,;bc,;,", out[0]);
         }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to