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