View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Arrays;
20  import java.util.Locale;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * Test string utils.
26   *
27   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
28   * @version $Id$
29   */
30  public class StringUtilsTest
31      extends TestCase
32  {
33  
34      public void testIsEmpty()
35      {
36          assertEquals( true, StringUtils.isEmpty( null ) );
37          assertEquals( true, StringUtils.isEmpty( "" ) );
38          assertEquals( true, StringUtils.isEmpty( " " ) );
39          assertEquals( false, StringUtils.isEmpty( "foo" ) );
40          assertEquals( false, StringUtils.isEmpty( "  foo  " ) );
41      }
42  
43      public void testIsNotEmpty()
44      {
45          assertEquals( false, StringUtils.isNotEmpty( null ) );
46          assertEquals( false, StringUtils.isNotEmpty( "" ) );
47          assertEquals( true, StringUtils.isNotEmpty( " " ) );
48          assertEquals( true, StringUtils.isNotEmpty( "foo" ) );
49          assertEquals( true, StringUtils.isNotEmpty( "  foo  " ) );
50      }
51  
52      public void testIsBlank()
53      {
54          assertEquals( true, StringUtils.isBlank( null ) );
55          assertEquals( true, StringUtils.isBlank( "" ) );
56          assertEquals( true, StringUtils.isBlank( " \t\r\n" ) );
57          assertEquals( false, StringUtils.isBlank( "foo" ) );
58          assertEquals( false, StringUtils.isBlank( "  foo  " ) );
59      }
60  
61      public void testIsNotBlank()
62      {
63          assertEquals( false, StringUtils.isNotBlank( null ) );
64          assertEquals( false, StringUtils.isNotBlank( "" ) );
65          assertEquals( false, StringUtils.isNotBlank( " \t\r\n" ) );
66          assertEquals( true, StringUtils.isNotBlank( "foo" ) );
67          assertEquals( true, StringUtils.isNotBlank( "  foo  " ) );
68      }
69  
70      public void testCapitalizeFirstLetter()
71      {
72          assertEquals( "Id", StringUtils.capitalizeFirstLetter( "id" ) );
73          assertEquals( "Id", StringUtils.capitalizeFirstLetter( "Id" ) );
74      }
75  
76      public void testCapitalizeFirstLetterTurkish()
77      {
78          Locale l = Locale.getDefault();
79          Locale.setDefault( new Locale( "tr" ) );
80          assertEquals( "Id", StringUtils.capitalizeFirstLetter( "id" ) );
81          assertEquals( "Id", StringUtils.capitalizeFirstLetter( "Id" ) );
82          Locale.setDefault( l );
83      }
84  
85      public void testLowerCaseFirstLetter()
86      {
87          assertEquals( "id", StringUtils.lowercaseFirstLetter( "id" ) );
88          assertEquals( "id", StringUtils.lowercaseFirstLetter( "Id" ) );
89      }
90  
91      public void testLowerCaseFirstLetterTurkish()
92      {
93          Locale l = Locale.getDefault();
94          Locale.setDefault( new Locale( "tr" ) );
95          assertEquals( "id", StringUtils.lowercaseFirstLetter( "id" ) );
96          assertEquals( "id", StringUtils.lowercaseFirstLetter( "Id" ) );
97          Locale.setDefault( l );
98      }
99  
100     public void testRemoveAndHump()
101     {
102         assertEquals( "Id", StringUtils.removeAndHump( "id", "-" ) );
103         assertEquals( "SomeId", StringUtils.removeAndHump( "some-id", "-" ) );
104     }
105 
106     public void testRemoveAndHumpTurkish()
107     {
108         Locale l = Locale.getDefault();
109         Locale.setDefault( new Locale( "tr" ) );
110         assertEquals( "Id", StringUtils.removeAndHump( "id", "-" ) );
111         assertEquals( "SomeId", StringUtils.removeAndHump( "some-id", "-" ) );
112         Locale.setDefault( l );
113     }
114 
115     public void testQuote_EscapeEmbeddedSingleQuotes()
116     {
117         String src = "This \'is a\' test";
118         String check = "\'This \\\'is a\\\' test\'";
119 
120         char[] escaped = { '\'', '\"' };
121         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', false );
122 
123         assertEquals( check, result );
124     }
125 
126     public void testQuote_EscapeEmbeddedSingleQuotesWithPattern()
127     {
128         String src = "This \'is a\' test";
129         String check = "\'This pre'postis apre'post test\'";
130 
131         char[] escaped = { '\'', '\"' };
132         String result = StringUtils.quoteAndEscape( src, '\'', escaped, new char[] { ' ' }, "pre%spost", false );
133 
134         assertEquals( check, result );
135     }
136 
137     public void testQuote_EscapeEmbeddedDoubleQuotesAndSpaces()
138     {
139         String src = "This \"is a\" test";
140         String check = "\'This\\ \\\"is\\ a\\\"\\ test\'";
141 
142         char[] escaped = { '\'', '\"', ' ' };
143         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', false );
144 
145         assertEquals( check, result );
146     }
147 
148     public void testQuote_DontQuoteIfUnneeded()
149     {
150         String src = "ThisIsATest";
151 
152         char[] escaped = { '\'', '\"' };
153         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', false );
154 
155         assertEquals( src, result );
156     }
157 
158     public void testQuote_WrapWithSingleQuotes()
159     {
160         String src = "This is a test";
161         String check = "\'This is a test\'";
162 
163         char[] escaped = { '\'', '\"' };
164         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', false );
165 
166         assertEquals( check, result );
167     }
168 
169     public void testQuote_PreserveExistingQuotes()
170     {
171         String src = "\'This is a test\'";
172 
173         char[] escaped = { '\'', '\"' };
174         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', false );
175 
176         assertEquals( src, result );
177     }
178 
179     public void testQuote_WrapExistingQuotesWhenForceIsTrue()
180     {
181         String src = "\'This is a test\'";
182         String check = "\'\\\'This is a test\\\'\'";
183 
184         char[] escaped = { '\'', '\"' };
185         String result = StringUtils.quoteAndEscape( src, '\'', escaped, '\\', true );
186 
187         assertEquals( check, result );
188     }
189 
190     public void testQuote_ShortVersion_SingleQuotesPreserved()
191     {
192         String src = "\'This is a test\'";
193 
194         String result = StringUtils.quoteAndEscape( src, '\'' );
195 
196         assertEquals( src, result );
197     }
198 
199     public void testSplit()
200     {
201         String[] tokens;
202 
203         tokens = StringUtils.split( "", ", " );
204         assertNotNull( tokens );
205         assertEquals( Arrays.asList( new String[0] ), Arrays.asList( tokens ) );
206 
207         tokens = StringUtils.split( ", ,,,   ,", ", " );
208         assertNotNull( tokens );
209         assertEquals( Arrays.asList( new String[0] ), Arrays.asList( tokens ) );
210 
211         tokens = StringUtils.split( "this", ", " );
212         assertNotNull( tokens );
213         assertEquals( Arrays.asList( new String[] { "this" } ), Arrays.asList( tokens ) );
214 
215         tokens = StringUtils.split( "this is a test", ", " );
216         assertNotNull( tokens );
217         assertEquals( Arrays.asList( new String[] { "this", "is", "a", "test" } ), Arrays.asList( tokens ) );
218 
219         tokens = StringUtils.split( "   this   is   a   test  ", ", " );
220         assertNotNull( tokens );
221         assertEquals( Arrays.asList( new String[] { "this", "is", "a", "test" } ), Arrays.asList( tokens ) );
222 
223         tokens = StringUtils.split( "this is a test, really", ", " );
224         assertNotNull( tokens );
225         assertEquals( Arrays.asList( new String[] { "this", "is", "a", "test", "really" } ), Arrays.asList( tokens ) );
226     }
227 
228     public void testRemoveDuplicateWhitespace()
229         throws Exception
230     {
231         String s = "this     is     test   ";
232         assertEquals( "this is test ", StringUtils.removeDuplicateWhitespace( s ) );
233         s = "this  \r\n   is \n  \r  test   ";
234         assertEquals( "this is test ", StringUtils.removeDuplicateWhitespace( s ) );
235         s = "     this  \r\n   is \n  \r  test";
236         assertEquals( " this is test", StringUtils.removeDuplicateWhitespace( s ) );
237         s = "this  \r\n   is \n  \r  test   \n ";
238         assertEquals( "this is test ", StringUtils.removeDuplicateWhitespace( s ) );
239 
240     }
241 
242     public void testUnifyLineSeparators()
243         throws Exception
244     {
245         String s = "this\r\nis\na\r\ntest";
246 
247         try
248         {
249             StringUtils.unifyLineSeparators( s, "abs" );
250             assertTrue( "Exception NOT catched", false );
251         }
252         catch ( IllegalArgumentException e )
253         {
254             assertTrue( "Exception catched", true );
255         }
256 
257         assertEquals( "this\nis\na\ntest", StringUtils.unifyLineSeparators( s, "\n" ) );
258         assertEquals( "this\r\nis\r\na\r\ntest", StringUtils.unifyLineSeparators( s, "\r\n" ) );
259     }
260 }