001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.codec.binary;
019
020 import java.io.UnsupportedEncodingException;
021 import java.util.Arrays;
022
023 import org.junit.Assert;
024
025 import org.junit.Test;
026
027 /**
028 * Tests {@link StringUtils}
029 *
030 * @version $Id: StringUtilsTest.html 889935 2013-12-11 05:05:13Z ggregory $
031 */
032 public class StringUtilsTest {
033
034 private static final byte[] BYTES_FIXTURE = {'a','b','c'};
035
036 // This is valid input for UTF-16BE
037 private static final byte[] BYTES_FIXTURE_16BE = {0, 'a', 0, 'b', 0, 'c'};
038
039 // This is valid for UTF-16LE
040 private static final byte[] BYTES_FIXTURE_16LE = {'a', 0, 'b', 0, 'c', 0};
041
042 private static final String STRING_FIXTURE = "ABC";
043
044 /**
045 * We could make the constructor private but there does not seem to be a point to jumping through extra code hoops
046 * to restrict instantiation right now.
047 */
048 @Test
049 public void testConstructor() {
050 new StringUtils();
051 }
052
053 @Test
054 public void testGetBytesIso8859_1() throws UnsupportedEncodingException {
055 final String charsetName = "ISO-8859-1";
056 testGetBytesUnchecked(charsetName);
057 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
058 final byte[] actual = StringUtils.getBytesIso8859_1(STRING_FIXTURE);
059 Assert.assertTrue(Arrays.equals(expected, actual));
060 }
061
062 private void testGetBytesUnchecked(final String charsetName) throws UnsupportedEncodingException {
063 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
064 final byte[] actual = StringUtils.getBytesUnchecked(STRING_FIXTURE, charsetName);
065 Assert.assertTrue(Arrays.equals(expected, actual));
066 }
067
068 @Test
069 public void testGetBytesUsAscii() throws UnsupportedEncodingException {
070 final String charsetName = "US-ASCII";
071 testGetBytesUnchecked(charsetName);
072 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
073 final byte[] actual = StringUtils.getBytesUsAscii(STRING_FIXTURE);
074 Assert.assertTrue(Arrays.equals(expected, actual));
075 }
076
077 @Test
078 public void testGetBytesUtf16() throws UnsupportedEncodingException {
079 final String charsetName = "UTF-16";
080 testGetBytesUnchecked(charsetName);
081 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
082 final byte[] actual = StringUtils.getBytesUtf16(STRING_FIXTURE);
083 Assert.assertTrue(Arrays.equals(expected, actual));
084 }
085
086 @Test
087 public void testGetBytesUtf16Be() throws UnsupportedEncodingException {
088 final String charsetName = "UTF-16BE";
089 testGetBytesUnchecked(charsetName);
090 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
091 final byte[] actual = StringUtils.getBytesUtf16Be(STRING_FIXTURE);
092 Assert.assertTrue(Arrays.equals(expected, actual));
093 }
094
095 @Test
096 public void testGetBytesUtf16Le() throws UnsupportedEncodingException {
097 final String charsetName = "UTF-16LE";
098 testGetBytesUnchecked(charsetName);
099 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
100 final byte[] actual = StringUtils.getBytesUtf16Le(STRING_FIXTURE);
101 Assert.assertTrue(Arrays.equals(expected, actual));
102 }
103
104 @Test
105 public void testGetBytesUtf8() throws UnsupportedEncodingException {
106 final String charsetName = "UTF-8";
107 testGetBytesUnchecked(charsetName);
108 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
109 final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE);
110 Assert.assertTrue(Arrays.equals(expected, actual));
111 }
112
113 @Test
114 public void testGetBytesUncheckedBadName() {
115 try {
116 StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN");
117 Assert.fail("Expected " + IllegalStateException.class.getName());
118 } catch (final IllegalStateException e) {
119 // Expected
120 }
121 }
122
123 @Test
124 public void testGetBytesUncheckedNullInput() {
125 Assert.assertNull(StringUtils.getBytesUnchecked(null, "UNKNOWN"));
126 }
127
128 private void testNewString(final String charsetName) throws UnsupportedEncodingException {
129 final String expected = new String(BYTES_FIXTURE, charsetName);
130 final String actual = StringUtils.newString(BYTES_FIXTURE, charsetName);
131 Assert.assertEquals(expected, actual);
132 }
133
134 @Test
135 public void testNewStringBadEnc() {
136 try {
137 StringUtils.newString(BYTES_FIXTURE, "UNKNOWN");
138 Assert.fail("Expected " + IllegalStateException.class.getName());
139 } catch (final IllegalStateException e) {
140 // Expected
141 }
142 }
143
144 @Test
145 public void testNewStringNullInput() {
146 Assert.assertNull(StringUtils.newString(null, "UNKNOWN"));
147 }
148
149 @Test
150 public void testNewStringIso8859_1() throws UnsupportedEncodingException {
151 final String charsetName = "ISO-8859-1";
152 testNewString(charsetName);
153 final String expected = new String(BYTES_FIXTURE, charsetName);
154 final String actual = StringUtils.newStringIso8859_1(BYTES_FIXTURE);
155 Assert.assertEquals(expected, actual);
156 }
157
158 @Test
159 public void testNewStringUsAscii() throws UnsupportedEncodingException {
160 final String charsetName = "US-ASCII";
161 testNewString(charsetName);
162 final String expected = new String(BYTES_FIXTURE, charsetName);
163 final String actual = StringUtils.newStringUsAscii(BYTES_FIXTURE);
164 Assert.assertEquals(expected, actual);
165 }
166
167 @Test
168 public void testNewStringUtf16() throws UnsupportedEncodingException {
169 final String charsetName = "UTF-16";
170 testNewString(charsetName);
171 final String expected = new String(BYTES_FIXTURE, charsetName);
172 final String actual = StringUtils.newStringUtf16(BYTES_FIXTURE);
173 Assert.assertEquals(expected, actual);
174 }
175
176 @Test
177 public void testNewStringUtf16Be() throws UnsupportedEncodingException {
178 final String charsetName = "UTF-16BE";
179 testNewString(charsetName);
180 final String expected = new String(BYTES_FIXTURE_16BE, charsetName);
181 final String actual = StringUtils.newStringUtf16Be(BYTES_FIXTURE_16BE);
182 Assert.assertEquals(expected, actual);
183 }
184
185 @Test
186 public void testNewStringUtf16Le() throws UnsupportedEncodingException {
187 final String charsetName = "UTF-16LE";
188 testNewString(charsetName);
189 final String expected = new String(BYTES_FIXTURE_16LE, charsetName);
190 final String actual = StringUtils.newStringUtf16Le(BYTES_FIXTURE_16LE);
191 Assert.assertEquals(expected, actual);
192 }
193
194 @Test
195 public void testNewStringUtf8() throws UnsupportedEncodingException {
196 final String charsetName = "UTF-8";
197 testNewString(charsetName);
198 final String expected = new String(BYTES_FIXTURE, charsetName);
199 final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);
200 Assert.assertEquals(expected, actual);
201 }
202 }