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.poi.util;
019:
020: import junit.framework.*;
021:
022: import java.text.NumberFormat;
023:
024: /**
025: * Unit test for StringUtil
026: *
027: * @author Marc Johnson (mjohnson at apache dot org
028: * @author Glen Stampoultzis (glens at apache.org)
029: * @author Sergei Kozello (sergeikozello at mail.ru)
030: */
031: public class TestStringUtil extends TestCase {
032: /**
033: * Creates new TestStringUtil
034: *
035: * @param name
036: */
037: public TestStringUtil(String name) {
038: super (name);
039: }
040:
041: /**
042: * test simple form of getFromUnicode
043: */
044: public void testSimpleGetFromUnicode() {
045: byte[] test_data = new byte[32];
046: int index = 0;
047:
048: for (int k = 0; k < 16; k++) {
049: test_data[index++] = (byte) 0;
050: test_data[index++] = (byte) ('a' + k);
051: }
052:
053: assertEquals("abcdefghijklmnop", StringUtil
054: .getFromUnicodeBE(test_data));
055: }
056:
057: /**
058: * test simple form of getFromUnicode with symbols with code below and more 127
059: */
060: public void testGetFromUnicodeSymbolsWithCodesMoreThan127() {
061: byte[] test_data = new byte[] { 0x04, 0x22, 0x04, 0x35, 0x04,
062: 0x41, 0x04, 0x42, 0x00, 0x20, 0x00, 0x74, 0x00, 0x65,
063: 0x00, 0x73, 0x00, 0x74, };
064:
065: assertEquals("\u0422\u0435\u0441\u0442 test", StringUtil
066: .getFromUnicodeBE(test_data));
067: }
068:
069: /**
070: * test getFromUnicodeHigh for symbols with code below and more 127
071: */
072: public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127() {
073: byte[] test_data = new byte[] { 0x22, 0x04, 0x35, 0x04, 0x41,
074: 0x04, 0x42, 0x04, 0x20, 0x00, 0x74, 0x00, 0x65, 0x00,
075: 0x73, 0x00, 0x74, 0x00, };
076:
077: assertEquals("\u0422\u0435\u0441\u0442 test", StringUtil
078: .getFromUnicodeLE(test_data));
079: }
080:
081: /**
082: * Test more complex form of getFromUnicode
083: */
084: public void testComplexGetFromUnicode() {
085: byte[] test_data = new byte[32];
086: int index = 0;
087: for (int k = 0; k < 16; k++) {
088: test_data[index++] = (byte) 0;
089: test_data[index++] = (byte) ('a' + k);
090: }
091: assertEquals("abcdefghijklmno", StringUtil.getFromUnicodeBE(
092: test_data, 0, 15));
093: assertEquals("bcdefghijklmnop", StringUtil.getFromUnicodeBE(
094: test_data, 2, 15));
095: try {
096: StringUtil.getFromUnicodeBE(test_data, -1, 16);
097: fail("Should have caught ArrayIndexOutOfBoundsException");
098: } catch (ArrayIndexOutOfBoundsException ignored) {
099: // as expected
100: }
101:
102: try {
103: StringUtil.getFromUnicodeBE(test_data, 32, 16);
104: fail("Should have caught ArrayIndexOutOfBoundsException");
105: } catch (ArrayIndexOutOfBoundsException ignored) {
106: // as expected
107: }
108:
109: try {
110: StringUtil.getFromUnicodeBE(test_data, 1, 16);
111: fail("Should have caught IllegalArgumentException");
112: } catch (IllegalArgumentException ignored) {
113: // as expected
114: }
115:
116: try {
117: StringUtil.getFromUnicodeBE(test_data, 1, -1);
118: fail("Should have caught IllegalArgumentException");
119: } catch (IllegalArgumentException ignored) {
120: // as expected
121: }
122: }
123:
124: /**
125: * Test putCompressedUnicode
126: */
127: public void testPutCompressedUnicode() throws Exception {
128: byte[] output = new byte[100];
129: byte[] expected_output = { (byte) 'H', (byte) 'e', (byte) 'l',
130: (byte) 'l', (byte) 'o', (byte) ' ', (byte) 'W',
131: (byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd',
132: (byte) 0xAE };
133: String input = new String(expected_output, StringUtil
134: .getPreferredEncoding());
135:
136: StringUtil.putCompressedUnicode(input, output, 0);
137: for (int j = 0; j < expected_output.length; j++) {
138: assertEquals("testing offset " + j, expected_output[j],
139: output[j]);
140: }
141: StringUtil.putCompressedUnicode(input, output,
142: 100 - expected_output.length);
143: for (int j = 0; j < expected_output.length; j++) {
144: assertEquals("testing offset " + j, expected_output[j],
145: output[100 + j - expected_output.length]);
146: }
147: try {
148: StringUtil.putCompressedUnicode(input, output,
149: 101 - expected_output.length);
150: fail("Should have caught ArrayIndexOutOfBoundsException");
151: } catch (ArrayIndexOutOfBoundsException ignored) {
152: // as expected
153: }
154: }
155:
156: /**
157: * Test putUncompressedUnicode
158: */
159: public void testPutUncompressedUnicode() {
160: byte[] output = new byte[100];
161: String input = "Hello World";
162: byte[] expected_output = { (byte) 'H', (byte) 0, (byte) 'e',
163: (byte) 0, (byte) 'l', (byte) 0, (byte) 'l', (byte) 0,
164: (byte) 'o', (byte) 0, (byte) ' ', (byte) 0, (byte) 'W',
165: (byte) 0, (byte) 'o', (byte) 0, (byte) 'r', (byte) 0,
166: (byte) 'l', (byte) 0, (byte) 'd', (byte) 0 };
167:
168: StringUtil.putUnicodeLE(input, output, 0);
169: for (int j = 0; j < expected_output.length; j++) {
170: assertEquals("testing offset " + j, expected_output[j],
171: output[j]);
172: }
173: StringUtil.putUnicodeLE(input, output,
174: 100 - expected_output.length);
175: for (int j = 0; j < expected_output.length; j++) {
176: assertEquals("testing offset " + j, expected_output[j],
177: output[100 + j - expected_output.length]);
178: }
179: try {
180: StringUtil.putUnicodeLE(input, output,
181: 101 - expected_output.length);
182: fail("Should have caught ArrayIndexOutOfBoundsException");
183: } catch (ArrayIndexOutOfBoundsException ignored) {
184: // as expected
185: }
186: }
187:
188: public void testFormat() throws Exception {
189: assertEquals("This is a test " + fmt(1.2345, 2, 2), StringUtil
190: .format("This is a test %2.2",
191: new Object[] { new Double(1.2345) }));
192: assertEquals("This is a test " + fmt(1.2345, -1, 3), StringUtil
193: .format("This is a test %.3",
194: new Object[] { new Double(1.2345) }));
195: assertEquals("This is a great test " + fmt(1.2345, -1, 3),
196: StringUtil.format("This is a % test %.3", new Object[] {
197: "great", new Double(1.2345) }));
198: assertEquals("This is a test 1", StringUtil.format(
199: "This is a test %", new Object[] { new Integer(1) }));
200: assertEquals("This is a test 1", StringUtil.format(
201: "This is a test %", new Object[] { new Integer(1),
202: new Integer(1) }));
203: assertEquals("This is a test 1.x", StringUtil.format(
204: "This is a test %1.x", new Object[] { new Integer(1) }));
205: assertEquals("This is a test ?missing data?1.x", StringUtil
206: .format("This is a test %1.x", new Object[] {}));
207: assertEquals("This is a test %1.x", StringUtil.format(
208: "This is a test \\%1.x", new Object[] {}));
209: }
210:
211: private String fmt(double num, int minIntDigits, int maxFracDigitis) {
212: NumberFormat nf = NumberFormat.getInstance();
213:
214: if (minIntDigits != -1) {
215: nf.setMinimumIntegerDigits(minIntDigits);
216: }
217: if (maxFracDigitis != -1) {
218: nf.setMaximumFractionDigits(maxFracDigitis);
219: }
220:
221: return nf.format(num);
222: }
223:
224: /**
225: * main
226: *
227: * @param ignored_args
228: */
229: public static void main(String[] ignored_args) {
230: System.out.println("Testing util.StringUtil functionality");
231: junit.textui.TestRunner.run(TestStringUtil.class);
232: }
233:
234: /**
235: * @see junit.framework.TestCase#setUp()
236: */
237: protected void setUp() throws Exception {
238: super .setUp();
239:
240: // System.setProperty()
241: }
242:
243: }
|