KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

145
146     public void testReadFromBytes()
147     {
148         ShortField field = new ShortField(1);
149         byte[] array = new byte[ 2 ];
150
151         try
152         {
153             field.readFromBytes(array);
154             fail("should have caught ArrayIndexOutOfBoundsException");
155         }
156         catch (ArrayIndexOutOfBoundsException JavaDoc ignored_e)
157         {
158
159             // as expected
160
}
161         field = new ShortField(0);
162         for (int j = 0; j < _test_array.length; j++)
163         {
164             array[ 0 ] = ( byte ) (_test_array[ j ] % 256);
165             array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
166             field.readFromBytes(array);
167             assertEquals("testing " + j, _test_array[ j ], field.get());
168         }
169     }
170
171     /**
172      * Test readFromStream
173      *
174      * @exception IOException
175      */

176
177     public void testReadFromStream()
178         throws IOException
179     {
180         ShortField field = new ShortField(0);
181         byte[] buffer = new byte[ _test_array.length * 2 ];
182
183         for (int j = 0; j < _test_array.length; j++)
184         {
185             buffer[ (j * 2) + 0 ] = ( byte ) (_test_array[ j ] % 256);
186             buffer[ (j * 2) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
187         }
188         ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
189
190         for (int j = 0; j < buffer.length / 2; j++)
191         {
192             field.readFromStream(stream);
193             assertEquals("Testing " + j, _test_array[ j ], field.get());
194         }
195     }
196
197     /**
198      * test writeToBytes
199      */

200
201     public void testWriteToBytes()
202     {
203         ShortField field = new ShortField(0);
204         byte[] array = new byte[ 2 ];
205
206         for (int j = 0; j < _test_array.length; j++)
207         {
208             field.set(_test_array[ j ]);
209             field.writeToBytes(array);
210             short val = ( short ) (array[ 1 ] << 8);
211
212             val &= ( short ) 0xFF00;
213             val += ( short ) (array[ 0 ] & 0x00FF);
214             assertEquals("testing ", _test_array[ j ], val);
215         }
216     }
217
218     /**
219      * Main
220      *
221      * @param args
222      */

223
224     public static void main(String JavaDoc [] args)
225     {
226         System.out.println("Testing util.ShortField functionality");
227         junit.textui.TestRunner.run(TestShortField.class);
228     }
229 }
230
Popular Tags