Repository navigation
Expand file tree
/
Copy pathStringUtilsTest.java
More file actions
113 lines (99 loc) · 3.17 KB
/
Copy pathStringUtilsTest.java
File metadata and controls
113 lines (99 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2014 Metamarkets Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metamx.common;
import junit.framework.Assert;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
/**
*
*/
public class StringUtilsTest
{
@Test
public void fromUtf8ConversionTest() throws UnsupportedEncodingException
{
byte[] bytes = new byte[]{'a', 'b', 'c', 'd'};
Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes));
String abcd = "abcd";
Assert.assertEquals(abcd, StringUtils.fromUtf8(abcd.getBytes(StringUtils.UTF8_STRING)));
}
@Test
public void toUtf8ConversionTest()
{
byte[] bytes = new byte[]{'a', 'b', 'c', 'd'};
byte[] strBytes = StringUtils.toUtf8("abcd");
for (int i = 0; i < bytes.length; ++i) {
Assert.assertEquals(bytes[i], strBytes[i]);
}
}
@Test
public void fromUtf8ByteBufferHeap()
{
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes, 4));
bytes.rewind();
Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes));
}
@Test
public void testMiddleOfByteArrayConversion()
{
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
bytes.position(1).limit(3);
Assert.assertEquals("bc", StringUtils.fromUtf8(bytes, 2));
bytes.position(1);
Assert.assertEquals("bc", StringUtils.fromUtf8(bytes));
}
@Test(expected = java.nio.BufferUnderflowException.class)
public void testOutOfBounds()
{
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
bytes.position(1).limit(3);
StringUtils.fromUtf8(bytes, 3);
}
@Test(expected = java.lang.NullPointerException.class)
public void testNullPointerByteBuffer()
{
StringUtils.fromUtf8((ByteBuffer) null);
}
@Test(expected = java.lang.NullPointerException.class)
public void testNullPointerByteArray()
{
StringUtils.fromUtf8((byte[]) null);
}
@Test
public void fromUtf8ByteBufferDirect()
{
ByteBuffer bytes = ByteBuffer.allocateDirect(4);
bytes.put(new byte[]{'a', 'b', 'c', 'd'});
bytes.rewind();
Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes, 4));
bytes.rewind();
Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes));
}
@Test
public void testCharsetShowsUpAsDeprecated()
{
// Not actually a runnable test, just checking the IDE
Assert.assertNotNull(StringUtils.UTF8_CHARSET);
}
@Test
public void testSafeFormat()
{
Assert.assertEquals("test%d; format", StringUtils.safeFormat("test%d", "format"));
Assert.assertEquals("test%s%s; format", StringUtils.safeFormat("test%s%s", "format"));
}
}