KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Test IntegerField code
27  *
28  * @author Marc Johnson (mjohnson at apache dot org)
29  */

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

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

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

122
123     public void testSet()
124     {
125         IntegerField field = new IntegerField(0);
126         byte[] array = new byte[ 4 ];
127
128         for (int j = 0; j < _test_array.length; j++)
129         {
130             field.set(_test_array[ j ]);
131             assertEquals("testing _1 " + j, _test_array[ j ], field.get());
132             field = new IntegerField(0);
133             field.set(_test_array[ j ], array);
134             assertEquals("testing _2 ", _test_array[ j ], field.get());
135             assertEquals("testing _3.0 " + _test_array[ j ],
136                          ( byte ) (_test_array[ j ] % 256), array[ 0 ]);
137             assertEquals("testing _3.1 " + _test_array[ j ],
138                          ( byte ) ((_test_array[ j ] >> 8) % 256),
139                          array[ 1 ]);
140             assertEquals("testing _3.2 " + _test_array[ j ],
141                          ( byte ) ((_test_array[ j ] >> 16) % 256),
142                          array[ 2 ]);
143             assertEquals("testing _3.3 " + _test_array[ j ],
144                          ( byte ) ((_test_array[ j ] >> 24) % 256),
145                          array[ 3 ]);
146         }
147     }
148
149     /**
150      * Test readFromBytes
151      */

152
153     public void testReadFromBytes()
154     {
155         IntegerField field = new IntegerField(1);
156         byte[] array = new byte[ 4 ];
157
158         try
159         {
160             field.readFromBytes(array);
161             fail("should have caught ArrayIndexOutOfBoundsException");
162         }
163         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
164         {
165
166             // as expected
167
}
168         field = new IntegerField(0);
169         for (int j = 0; j < _test_array.length; j++)
170         {
171             array[ 0 ] = ( byte ) (_test_array[ j ] % 256);
172             array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
173             array[ 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256);
174             array[ 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256);
175             field.readFromBytes(array);
176             assertEquals("testing " + j, _test_array[ j ], field.get());
177         }
178     }
179
180     /**
181      * Test readFromStream
182      *
183      * @exception IOException
184      */

185
186     public void testReadFromStream()
187         throws IOException
188     {
189         IntegerField field = new IntegerField(0);
190         byte[] buffer = new byte[ _test_array.length * 4 ];
191
192         for (int j = 0; j < _test_array.length; j++)
193         {
194             buffer[ (j * 4) + 0 ] = ( byte ) (_test_array[ j ] % 256);
195             buffer[ (j * 4) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
196             buffer[ (j * 4) + 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256);
197             buffer[ (j * 4) + 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256);
198         }
199         ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
200
201         for (int j = 0; j < buffer.length / 4; j++)
202         {
203             field.readFromStream(stream);
204             assertEquals("Testing " + j, _test_array[ j ], field.get());
205         }
206     }
207
208     /**
209      * test writeToBytes
210      */

211
212     public void testWriteToBytes()
213     {
214         IntegerField field = new IntegerField(0);
215         byte[] array = new byte[ 4 ];
216
217         for (int j = 0; j < _test_array.length; j++)
218         {
219             field.set(_test_array[ j ]);
220             field.writeToBytes(array);
221             int val = array[ 3 ] << 24;
222
223             val &= 0xFF000000;
224             val += (array[ 2 ] << 16) & 0x00FF0000;
225             val += (array[ 1 ] << 8) & 0x0000FF00;
226             val += (array[ 0 ] & 0x000000FF);
227             assertEquals("testing ", _test_array[ j ], val);
228         }
229     }
230
231     /**
232      * Main
233      *
234      * @param args
235      */

236
237     public static void main(String JavaDoc [] args)
238     {
239         System.out.println("Testing util.IntegerField functionality");
240         junit.textui.TestRunner.run(TestIntegerField.class);
241     }
242 }
243
Popular Tags