View Javadoc
1   package org.apache.maven.surefire.util.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * @author Andreas Gudian
26   */
27  public class StringUtilsTest
28      extends TestCase
29  {
30  
31      public void testUnescapeString()
32      {
33          CharSequence inputString = createInputString();
34  
35          StringBuilder escaped = new StringBuilder( inputString.length() * 5 );
36          int initialCapacity = escaped.capacity();
37  
38          StringUtils.escapeToPrintable( escaped, inputString );
39  
40          assertEquals( initialCapacity, escaped.capacity() );
41  
42          StringBuilder unescaped = new StringBuilder( inputString.length() );
43          StringUtils.unescapeString( unescaped, escaped );
44  
45          assertEquals( inputString.length(), unescaped.length() );
46  
47          for ( int i = 0; i < inputString.length(); i++ )
48          {
49              if ( inputString.charAt( i ) != unescaped.charAt( i ) )
50              {
51                  fail( "Input and Unescaped String are not equal at position " + i );
52              }
53          }
54      }
55  
56      private CharSequence createInputString()
57      {
58          StringBuilder sb = new StringBuilder();
59          for ( int i = 0; i < Character.MAX_CODE_POINT; i++ )
60          {
61              sb.appendCodePoint( i );
62          }
63  
64          return sb;
65      }
66  
67      public void testUnescapeBytes()
68      {
69          byte[] input = new byte[256];
70  
71          for ( int i = 0; i <= 0xFF; i++ )
72          {
73              byte b = (byte) ( 0xFF & i );
74              input[i] = b;
75          }
76  
77          byte[] escaped = new byte[input.length * 3];
78  
79          int escapedBytes = StringUtils.escapeBytesToPrintable( escaped, 0, input, 0, input.length );
80  
81          String escapedString = new String( escaped, 0, escapedBytes );
82  
83          assertEquals( escapedBytes, escapedString.length() );
84  
85          byte[] unescaped = new byte[input.length];
86          int unescapeBytes = StringUtils.unescapeBytes( unescaped, escapedString );
87  
88          assertEquals( input.length, unescapeBytes );
89  
90          for ( int i = 0; i < input.length; i++ )
91          {
92              assertEquals( "At position " + i, input[i], unescaped[i] );
93          }
94      }
95  }