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 TestByteField 32 extends TestCase 33 { 34 35 40 41 public TestByteField(String 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 54 55 public void testConstructors() 56 { 57 try 58 { 59 new ByteField(-1); 60 fail("Should have caught ArrayIndexOutOfBoundsException"); 61 } 62 catch (ArrayIndexOutOfBoundsException ignored_e) 63 { 64 65 } 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 ignored_e) 76 { 77 78 } 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 ignored_e) 90 { 91 92 } 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 ignored_e) 104 { 105 106 } 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 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 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 ignored_e) 151 { 152 153 } 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 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 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 208 209 public static void main(String [] ignored_args) 210 { 211 System.out.println("Testing util.ByteField functionality"); 212 junit.textui.TestRunner.run(TestByteField.class); 213 } 214 } 215 | Popular Tags |