1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.util;
39  
40  import java.util.Arrays;
41  import java.util.List;
42  import junit.framework.TestCase;
43  
44  /***
45   *  Tests for StringUtil
46   *
47   * @version  $Revision: 1.6 $
48   * @author  <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49   */
50  public class StringUtilTest extends TestCase {
51  
52      /***
53       *  Create a new test.
54       *
55       * @param  name The name of the test.
56       */
57      public StringUtilTest( final String name ) {
58          super( name );
59      }
60  
61  
62      /***
63       *  Test joining ints together
64       */
65      public void testJoinInt() {
66          assertEquals( "1,2,3", StringUtil.join( new int[]{1, 2, 3}, "," ) );
67      }
68  
69  
70      /*** */
71      public void testJoinInt_EmptyArray() {
72          assertEquals( "", StringUtil.join( new int[]{}, "," ) );
73      }
74  
75      /*** */
76      public void testJoinInt_OneElement() {
77          assertEquals( "1", StringUtil.join( new int[]{1}, "," ) );
78      }
79  
80      /*** */
81      public void testJoinInt_NullArray() {
82          try {
83              assertEquals( "1", StringUtil.join( (int[])null, "," ) );
84          }
85          catch( final NullPointerException e ) {
86              // expected path
87          }
88      }
89  
90      /*** */
91      public void testJoinInt_NullSeparator() {
92          try {
93              assertEquals( "1", StringUtil.join( new int[]{2, 3}, null ) );
94          }
95          catch( final NullPointerException e ) {
96              // Expected path
97          }
98      }
99  
100     /*** */
101     public void testJoinCollection() {
102         final List list = Arrays.asList( new String[]{"1", "2", "3"} );
103         assertEquals( "1,2,3", StringUtil.join( list, "," ) );
104     }
105 
106 
107     /***
108      *  Test splitAtFirst()
109      */
110     public void testSplitAtFirst() {
111         final String data[][] = {
112                 {"foo=bar", "=", "foo", "bar"},
113                 {"foobar", "=", "foobar", ""},
114                 {"foo=", "=", "foo", ""},
115                 {"=bar", "=", "", "bar"}
116                 };
117 
118         String result[];
119 
120         int i;
121         for( i = 0; i < data.length; i++ ) {
122             result = StringUtil.splitAtFirst( data[i][0], data[i][1] );
123             assertEquals( "first", data[i][2], result[0] );
124             assertEquals( "second", data[i][3], result[1] );
125         }
126     }
127 
128 
129     /***
130      *  Test split()
131      */
132     public void testSplit() {
133         final Object data[][] = {
134                 {"foo=bar", "=", new String[]{"foo", "bar"}},
135                 {"foo!=bar", "!=", new String[]{"foo", "bar"}},
136                 {"foo,bar,cat,dog", ",", new String[]{"foo", "bar", "cat", "dog"}},
137                 {",foo,,cat,dog", ",", new String[]{"", "foo", "", "cat", "dog"}},
138                 {"", "=", new String[0]},
139                 {"foo", "=", new String[]{"foo"}},
140                 {"==", "==", new String[]{"", ""}},
141                 };
142 
143         String result[];
144         String expectedResult[];
145         String line;
146         String separator;
147 
148         int i;
149         int j;
150         for( i = 0; i < data.length; i++ ) {
151             line = ( String )data[i][0];
152             separator = ( String )data[i][1];
153             expectedResult = ( String[] )data[i][2];
154 
155             result = StringUtil.split( line, separator );
156 
157             assertEquals( "Number of strings for input[" + line + "]",
158                     expectedResult.length,
159                     result.length );
160 
161             for( j = 0; j < result.length; j++ ) {
162                 assertEquals( "element[" + j + "] for input[" + line + "]",
163                         expectedResult[j],
164                         result[j] );
165             }
166         }
167     }
168 
169 
170     /***
171      *  Test split() with a null line
172      */
173     public void testSplit_NullLine() {
174         try {
175             StringUtil.split( null, "," );
176             fail( "Expected exception" );
177         }
178         catch( final DetailedNullPointerException e ) {
179             assertEquals("line", e.getArgumentName());
180         }
181     }
182 
183 
184     /***
185      *  Test split() with a null separator
186      */
187     public void testSplit_NullSeparator() {
188         try {
189             StringUtil.split( "foo", null );
190             fail( "Expected exception" );
191         }
192         catch( final DetailedNullPointerException e ) {
193             assertEquals( "separator", e.getArgumentName() );
194             // Expected path
195         }
196     }
197 
198 
199     /***
200      *  Test split() with an empty separator
201      */
202     public void testSplit_EmptySeparator() {
203         try {
204             StringUtil.split( "foo", "" );
205             fail( "Expected exception" );
206         }
207         catch( final IllegalArgumentException e ) {
208             // Expected path
209         }
210     }
211 
212 
213     /***
214      *  Test replace() with a variety of inputs
215      */
216     public void testReplace() {
217         final String[][] data = {
218                 {"", "", "", ""},
219                 {"foo", "bar", "b", "foo"},
220                 {"foo bar", "foo", "rhino", "rhino bar"},
221                 {"foo bar", "foo", "foo", "foo bar"},
222                 {"afoob", "foo", "bar", "abarb"},
223                 {"afoobfooc", "foo", "", "abc"},
224                 };
225 
226         for( int i = 0; i < data.length; i++ ) {
227             final String source = data[i][0];
228             final String oldText = data[i][1];
229             final String newText = data[i][2];
230             final String expectedResult = data[i][3];
231 
232             final String description = "StringUtil.replace(\"" + source + "\", \""
233                 + oldText + "\", \"" + newText + "\")";
234             assertEquals( description, expectedResult, StringUtil.replace( source, oldText, newText ) );
235         }
236     }
237 }
238