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