KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestByteField


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import junit.framework.*;
22
23 import java.io.*;
24
25 /**
26  * Title: Unit test for ByteField class
27  * Description: Unit test for ByteField class
28  * @author Marc Johnson (mjohnson at apache dot org)
29  */

30
31 public class TestByteField
32     extends TestCase
33 {
34
35     /**
36      * Constructor
37      *
38      * @param name
39      */

40
41     public TestByteField(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     static private final byte[] _test_array =
47     {
48         Byte.MIN_VALUE, ( byte ) -1, ( byte ) 0, ( byte ) 1, Byte.MAX_VALUE
49     };
50
51     /**
52      * Test constructors.
53      */

54
55     public void testConstructors()
56     {
57         try
58         {
59             new ByteField(-1);
60             fail("Should have caught ArrayIndexOutOfBoundsException");
61         }
62         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
63         {
64
65             // as expected
66
}
67         ByteField field = new ByteField(2);
68
69         assertEquals(( byte ) 0, field.get());
70         try
71         {
72             new ByteField(-1, ( byte ) 1);
73             fail("Should have caught ArrayIndexOutOfBoundsException");
74         }
75         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
76         {
77
78             // as expected
79
}
80         field = new ByteField(2, ( byte ) 3);
81         assertEquals(( byte ) 3, field.get());
82         byte[] array = new byte[ 3 ];
83
84         try
85         {
86             new ByteField(-1, ( byte ) 1, array);
87             fail("Should have caught ArrayIndexOutOfBoundsException");
88         }
89         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
90         {
91
92             // as expected
93
}
94         field = new ByteField(2, ( byte ) 4, array);
95         assertEquals(( byte ) 4, field.get());
96         assertEquals(( byte ) 4, array[ 2 ]);
97         array = new byte[ 2 ];
98         try
99         {
100             new ByteField(2, ( byte ) 5, array);
101             fail("should have gotten ArrayIndexOutOfBoundsException");
102         }
103         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
104         {
105
106             // as expected
107
}
108         for (int j = 0; j < _test_array.length; j++)
109         {
110             array = new byte[ 1 ];
111             new ByteField(0, _test_array[ j ], array);
112             assertEquals(_test_array[ j ], new ByteField(0, array).get());
113         }
114     }
115
116     /**
117      * Test set() methods
118      */

119
120     public void testSet()
121     {
122         ByteField field = new ByteField(0);
123         byte[] array = new byte[ 1 ];
124
125         for (int j = 0; j < _test_array.length; j++)
126         {
127             field.set(_test_array[ j ]);
128             assertEquals("testing _1 " + j, _test_array[ j ], field.get());
129             field = new ByteField(0);
130             field.set(_test_array[ j ], array);
131             assertEquals("testing _2 ", _test_array[ j ], field.get());
132             assertEquals("testing _3 ", _test_array[ j ], array[ 0 ]);
133         }
134     }
135
136     /**
137      * Test readFromBytes
138      */

139
140     public void testReadFromBytes()
141     {
142         ByteField field = new ByteField(1);
143         byte[] array = new byte[ 1 ];
144
145         try
146         {
147             field.readFromBytes(array);
148             fail("should have caught ArrayIndexOutOfBoundsException");
149         }
150         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
151         {
152
153             // as expected
154
}
155         field = new ByteField(0);
156         for (int j = 0; j < _test_array.length; j++)
157         {
158             array[ 0 ] = _test_array[ j ];
159             field.readFromBytes(array);
160             assertEquals("testing " + j, _test_array[ j ], field.get());
161         }
162     }
163
164     /**
165      * Test readFromStream
166      *
167      * @exception IOException
168      */

169
170     public void testReadFromStream()
171         throws IOException
172     {
173         ByteField field = new ByteField(0);
174         byte[] buffer = new byte[ _test_array.length ];
175
176         System.arraycopy(_test_array, 0, buffer, 0, buffer.length);
177         ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
178
179         for (int j = 0; j < buffer.length; j++)
180         {
181             field.readFromStream(stream);
182             assertEquals("Testing " + j, _test_array[ j ], field.get());
183         }
184     }
185
186     /**
187      * test writeToBytes
188      */

189
190     public void testWriteToBytes()
191     {
192         ByteField field = new ByteField(0);
193         byte[] array = new byte[ 1 ];
194
195         for (int j = 0; j < _test_array.length; j++)
196         {
197             field.set(_test_array[ j ]);
198             field.writeToBytes(array);
199             assertEquals("testing ", _test_array[ j ], array[ 0 ]);
200         }
201     }
202
203     /**
204      * Main
205      *
206      * @param ignored_args
207      */

208
209     public static void main(String JavaDoc [] ignored_args)
210     {
211         System.out.println("Testing util.ByteField functionality");
212         junit.textui.TestRunner.run(TestByteField.class);
213     }
214 }
215
Popular Tags