1 2 17 18 19 package org.apache.poi.util; 20 21 import junit.framework.*; 22 23 import java.io.*; 24 25 30 31 public class TestShortField 32 extends TestCase 33 { 34 35 40 41 public TestShortField(String 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 55 56 public void testConstructors() 57 { 58 try 59 { 60 new ShortField(-1); 61 fail("Should have caught ArrayIndexOutOfBoundsException"); 62 } 63 catch (ArrayIndexOutOfBoundsException ignored_e) 64 { 65 66 } 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 ignored_e) 77 { 78 79 } 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 ignored_e) 91 { 92 93 } 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 ignored_e) 106 { 107 108 } 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 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 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 ignored_e) 157 { 158 159 } 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 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 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 223 224 public static void main(String [] args) 225 { 226 System.out.println("Testing util.ShortField functionality"); 227 junit.textui.TestRunner.run(TestShortField.class); 228 } 229 } 230 | Popular Tags |