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.io.*;
023:
024: /**
025: * Test ShortField code
026: *
027: * @author Marc Johnson (mjohnson at apache dot org)
028: */
029:
030: public class TestShortField extends TestCase {
031:
032: /**
033: * Constructor
034: *
035: * @param name
036: */
037:
038: public TestShortField(String name) {
039: super (name);
040: }
041:
042: static private final short[] _test_array = { Short.MIN_VALUE,
043: (short) -1, (short) 0, (short) 1, Short.MAX_VALUE };
044:
045: /**
046: * Test constructors.
047: */
048:
049: public void testConstructors() {
050: try {
051: new ShortField(-1);
052: fail("Should have caught ArrayIndexOutOfBoundsException");
053: } catch (ArrayIndexOutOfBoundsException ignored_e) {
054:
055: // as expected
056: }
057: ShortField field = new ShortField(2);
058:
059: assertEquals(0, field.get());
060: try {
061: new ShortField(-1, (short) 1);
062: fail("Should have caught ArrayIndexOutOfBoundsException");
063: } catch (ArrayIndexOutOfBoundsException ignored_e) {
064:
065: // as expected
066: }
067: field = new ShortField(2, (short) 0x1234);
068: assertEquals(0x1234, field.get());
069: byte[] array = new byte[4];
070:
071: try {
072: new ShortField(-1, (short) 1, array);
073: fail("Should have caught ArrayIndexOutOfBoundsException");
074: } catch (ArrayIndexOutOfBoundsException ignored_e) {
075:
076: // as expected
077: }
078: field = new ShortField(2, (short) 0x1234, array);
079: assertEquals((short) 0x1234, field.get());
080: assertEquals((byte) 0x34, array[2]);
081: assertEquals((byte) 0x12, array[3]);
082: array = new byte[3];
083: try {
084: new ShortField(2, (short) 5, array);
085: fail("should have gotten ArrayIndexOutOfBoundsException");
086: } catch (ArrayIndexOutOfBoundsException ignored_e) {
087:
088: // as expected
089: }
090: for (int j = 0; j < _test_array.length; j++) {
091: array = new byte[2];
092: new ShortField(0, _test_array[j], array);
093: assertEquals(_test_array[j], new ShortField(0, array).get());
094: }
095: }
096:
097: /**
098: * Test set() methods
099: */
100:
101: public void testSet() {
102: ShortField field = new ShortField(0);
103: byte[] array = new byte[2];
104:
105: for (int j = 0; j < _test_array.length; j++) {
106: field.set(_test_array[j]);
107: assertEquals("testing _1 " + j, _test_array[j], field.get());
108: field = new ShortField(0);
109: field.set(_test_array[j], array);
110: assertEquals("testing _2 ", _test_array[j], field.get());
111: assertEquals("testing _3.0 " + _test_array[j],
112: (byte) (_test_array[j] % 256), array[0]);
113: assertEquals("testing _3.1 " + _test_array[j],
114: (byte) ((_test_array[j] >> 8) % 256), array[1]);
115: }
116: }
117:
118: /**
119: * Test readFromBytes
120: */
121:
122: public void testReadFromBytes() {
123: ShortField field = new ShortField(1);
124: byte[] array = new byte[2];
125:
126: try {
127: field.readFromBytes(array);
128: fail("should have caught ArrayIndexOutOfBoundsException");
129: } catch (ArrayIndexOutOfBoundsException ignored_e) {
130:
131: // as expected
132: }
133: field = new ShortField(0);
134: for (int j = 0; j < _test_array.length; j++) {
135: array[0] = (byte) (_test_array[j] % 256);
136: array[1] = (byte) ((_test_array[j] >> 8) % 256);
137: field.readFromBytes(array);
138: assertEquals("testing " + j, _test_array[j], field.get());
139: }
140: }
141:
142: /**
143: * Test readFromStream
144: *
145: * @exception IOException
146: */
147:
148: public void testReadFromStream() throws IOException {
149: ShortField field = new ShortField(0);
150: byte[] buffer = new byte[_test_array.length * 2];
151:
152: for (int j = 0; j < _test_array.length; j++) {
153: buffer[(j * 2) + 0] = (byte) (_test_array[j] % 256);
154: buffer[(j * 2) + 1] = (byte) ((_test_array[j] >> 8) % 256);
155: }
156: ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
157:
158: for (int j = 0; j < buffer.length / 2; j++) {
159: field.readFromStream(stream);
160: assertEquals("Testing " + j, _test_array[j], field.get());
161: }
162: }
163:
164: /**
165: * test writeToBytes
166: */
167:
168: public void testWriteToBytes() {
169: ShortField field = new ShortField(0);
170: byte[] array = new byte[2];
171:
172: for (int j = 0; j < _test_array.length; j++) {
173: field.set(_test_array[j]);
174: field.writeToBytes(array);
175: short val = (short) (array[1] << 8);
176:
177: val &= (short) 0xFF00;
178: val += (short) (array[0] & 0x00FF);
179: assertEquals("testing ", _test_array[j], val);
180: }
181: }
182:
183: /**
184: * Main
185: *
186: * @param args
187: */
188:
189: public static void main(String[] args) {
190: System.out.println("Testing util.ShortField functionality");
191: junit.textui.TestRunner.run(TestShortField.class);
192: }
193: }
|