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 java.nio.charset.Charset;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * @author Andreas Gudian
28   */
29  public class StringUtilsTest
30      extends TestCase
31  {
32  
33      public void testUnescapeString()
34      {
35          CharSequence inputString = createInputString();
36  
37          StringBuilder escaped = new StringBuilder( inputString.length() * 5 );
38          int initialCapacity = escaped.capacity();
39  
40          StringUtils.escapeToPrintable( escaped, inputString );
41  
42          assertEquals( initialCapacity, escaped.capacity() );
43  
44          StringBuilder unescaped = new StringBuilder( inputString.length() );
45          StringUtils.unescapeString( unescaped, escaped );
46  
47          assertEquals( inputString.length(), unescaped.length() );
48  
49          for ( int i = 0; i < inputString.length(); i++ )
50          {
51              if ( inputString.charAt( i ) != unescaped.charAt( i ) )
52              {
53                  fail( "Input and Unescaped String are not equal at position " + i );
54              }
55          }
56      }
57  
58      private CharSequence createInputString()
59      {
60          StringBuilder sb = new StringBuilder();
61          for ( int i = 0; i < Character.MAX_CODE_POINT; i++ )
62          {
63              sb.appendCodePoint( i );
64          }
65  
66          return sb;
67      }
68  
69      public void testUnescapeBytes()
70      {
71          byte[] input = new byte[256];
72  
73          for ( int i = 0; i <= 0xFF; i++ )
74          {
75              byte b = (byte) ( 0xFF & i );
76              input[i] = b;
77          }
78  
79          byte[] escaped = new byte[input.length * 3];
80  
81          int escapedBytes = StringUtils.escapeBytesToPrintable( escaped, 0, input, 0, input.length );
82  
83          String escapedString = new String( escaped, 0, escapedBytes );
84  
85          assertEquals( escapedBytes, escapedString.length() );
86  
87          java.nio.ByteBuffer unescaped = StringUtils.unescapeBytes( escapedString, Charset.defaultCharset().name() );
88  
89          assertEquals( input.length, unescaped.remaining() - unescaped.position() );
90  
91          for ( int i = 0; i < input.length; i++ )
92          {
93              assertEquals( "At position " + i, input[i], unescaped.get() );
94          }
95      }
96  }