1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.nio.ByteBuffer;
23 import java.nio.charset.Charset;
24
25 import junit.framework.TestCase;
26 import org.apache.maven.surefire.util.internal.StringUtils.EncodedArray;
27
28 import static org.junit.Assert.assertArrayEquals;
29
30
31
32
33 public class StringUtilsTest
34 extends TestCase
35 {
36
37 public void testUnescapeString()
38 {
39 CharSequence inputString = createInputString();
40
41 StringBuilder escaped = new StringBuilder( inputString.length() * 5 );
42 int initialCapacity = escaped.capacity();
43
44 StringUtils.escapeToPrintable( escaped, inputString );
45
46 assertEquals( initialCapacity, escaped.capacity() );
47
48 StringBuilder unescaped = new StringBuilder( inputString.length() );
49 StringUtils.unescapeString( unescaped, escaped );
50
51 assertEquals( inputString.length(), unescaped.length() );
52
53 for ( int i = 0; i < inputString.length(); i++ )
54 {
55 if ( inputString.charAt( i ) != unescaped.charAt( i ) )
56 {
57 fail( "Input and Unescaped String are not equal at position " + i );
58 }
59 }
60 }
61
62 private CharSequence createInputString()
63 {
64 StringBuilder sb = new StringBuilder();
65 for ( int i = 0; i < Character.MAX_CODE_POINT; i++ )
66 {
67 sb.appendCodePoint( i );
68 }
69
70 return sb;
71 }
72
73 public void testUnescapeBytes()
74 {
75 byte[] input = new byte[256];
76
77 for ( int i = 0; i <= 0xFF; i++ )
78 {
79 byte b = (byte) ( 0xFF & i );
80 input[i] = b;
81 }
82
83 EncodedArray encodedArray = StringUtils.escapeBytesToPrintable( new byte[0], input, 0, input.length );
84
85 String escapedString = new String( encodedArray.getArray(), 0, encodedArray.getSize() );
86
87 assertEquals( encodedArray.getSize(), escapedString.length() );
88
89 ByteBuffer unescaped = StringUtils.unescapeBytes( escapedString, Charset.defaultCharset().name() );
90
91 assertEquals( input.length + 1, unescaped.remaining() - unescaped.position() );
92
93 for ( int i = 0; i < input.length; i++ )
94 {
95 assertEquals( "At position " + i, input[i], unescaped.get() );
96 }
97 }
98
99 public void testEscapeWithHeader()
100 {
101 byte[] header = { (byte) 'a' };
102 byte[] input = { (byte) '1' };
103
104 EncodedArray encodedArray = StringUtils.escapeBytesToPrintable( header, input, 0, input.length );
105 assertEquals( 3, encodedArray.getSize() );
106
107 byte[] expectedResult = new byte[] { (byte) 'a', (byte) '1', (byte) '\n' };
108 byte[] actualResult = new byte[encodedArray.getSize()];
109 System.arraycopy( encodedArray.getArray(), 0, actualResult, 0, encodedArray.getSize() );
110
111 assertArrayEquals( expectedResult, actualResult );
112 }
113
114 public void testEmptyByteArray()
115 {
116 byte[] header = { (byte) 'a' };
117 byte[] input = {};
118 EncodedArray encodedArray = StringUtils.escapeBytesToPrintable( header, input, 0, input.length );
119 assertEquals( 0, encodedArray.getSize() );
120 assertEquals( 0, encodedArray.getArray().length );
121 }
122
123 public void testSubstringSmall()
124 {
125 byte[] header = { (byte) 'a' };
126 byte[] input = "PleaseLookAfterThisBear".getBytes();
127 EncodedArray encodedArray = StringUtils.escapeBytesToPrintable( header, input,
128 "Please".length(), "Look".length() );
129 assertEquals( "Look",
130 new String( encodedArray.getArray(), 1, encodedArray.getArray().length-1).trim() );
131 }
132
133 public void testSubstringLarge()
134 {
135 byte[] header = { (byte) 'a' };
136 byte[] input = "TheQuickBrownFoxJumpsOverTheLazyDog".getBytes();
137 EncodedArray encodedArray = StringUtils.escapeBytesToPrintable( header, input,
138 "The".length(), "QuickBrownFoxJumpsOverTheLazy".length() );
139 assertEquals( "QuickBrownFoxJumpsOverTheLazy",
140 new String( encodedArray.getArray(), 1, encodedArray.getArray().length-1).trim() );
141 }
142 }