|
| 1 | +/* |
| 2 | + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//io/src/test/org/apache/commons/io/HexDumpTest.java,v 1.1 2002/02/22 06:13:44 bayard Exp $ |
| 3 | + * $Revision: 1.1 $ |
| 4 | + * $Date: 2002/02/22 06:13:44 $ |
| 5 | + * |
| 6 | + * ==================================================================== |
| 7 | + * |
| 8 | + * The Apache Software License, Version 1.1 |
| 9 | + * |
| 10 | + * Copyright (c) 1999-2002 The Apache Software Foundation. All rights |
| 11 | + * reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, are permitted provided that the following conditions |
| 15 | + * are met: |
| 16 | + * |
| 17 | + * 1. Redistributions of source code must retain the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer. |
| 19 | + * |
| 20 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | + * notice, this list of conditions and the following disclaimer in |
| 22 | + * the documentation and/or other materials provided with the |
| 23 | + * distribution. |
| 24 | + * |
| 25 | + * 3. The end-user documentation included with the redistribution, if |
| 26 | + * any, must include the following acknowlegement: |
| 27 | + * "This product includes software developed by the |
| 28 | + * Apache Software Foundation (http://www.apache.org/)." |
| 29 | + * Alternately, this acknowlegement may appear in the software itself, |
| 30 | + * if and wherever such third-party acknowlegements normally appear. |
| 31 | + * |
| 32 | + * 4. The names "The Jakarta Project", "Commons", and "Apache Software |
| 33 | + * Foundation" must not be used to endorse or promote products derived |
| 34 | + * from this software without prior written permission. For written |
| 35 | + * permission, please contact apache@apache.org. |
| 36 | + * |
| 37 | + * 5. Products derived from this software may not be called "Apache" |
| 38 | + * nor may "Apache" appear in their names without prior written |
| 39 | + * permission of the Apache Group. |
| 40 | + * |
| 41 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 42 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 43 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 44 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 45 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 46 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 47 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 48 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 49 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 50 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 51 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 52 | + * SUCH DAMAGE. |
| 53 | + * ==================================================================== |
| 54 | + * |
| 55 | + * This software consists of voluntary contributions made by many |
| 56 | + * individuals on behalf of the Apache Software Foundation. For more |
| 57 | + * information on the Apache Software Foundation, please see |
| 58 | + * <http://www.apache.org/>. |
| 59 | + * |
| 60 | + */ |
| 61 | + |
| 62 | + |
| 63 | +package org.apache.commons.io; |
| 64 | + |
| 65 | + |
| 66 | +import java.io.ByteArrayOutputStream; |
| 67 | +import java.io.IOException; |
| 68 | + |
| 69 | +import junit.framework.TestCase; |
| 70 | + |
| 71 | + |
| 72 | +/** |
| 73 | + * @author Scott Sanders (sanders at apache dot org) |
| 74 | + * @author Marc Johnson (mjohnson at apache dot org) |
| 75 | + * @version $Revision: 1.1 $ $Date: 2002/02/22 06:13:44 $ |
| 76 | + */ |
| 77 | + |
| 78 | +public class HexDumpTest extends TestCase { |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates new HexDumpTest |
| 82 | + * |
| 83 | + * @param name |
| 84 | + */ |
| 85 | + |
| 86 | + public HexDumpTest(String name) { |
| 87 | + super(name); |
| 88 | + } |
| 89 | + |
| 90 | + private char toHex(final int n) { |
| 91 | + char[] hexChars = |
| 92 | + { |
| 93 | + '0', '1', '2', '3', '4', '5', '6', '7', |
| 94 | + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 95 | + }; |
| 96 | + |
| 97 | + return hexChars[n % 16]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * test dump method |
| 102 | + * |
| 103 | + * @exception IOException |
| 104 | + */ |
| 105 | + |
| 106 | + public void testDump() |
| 107 | + throws IOException { |
| 108 | + byte[] testArray = new byte[256]; |
| 109 | + |
| 110 | + for (int j = 0; j < 256; j++) { |
| 111 | + testArray[j] = (byte) j; |
| 112 | + } |
| 113 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 114 | + |
| 115 | + HexDump.dump(testArray, 0, stream, 0); |
| 116 | + byte[] outputArray = new byte[16 * (73 + HexDump.EOL.length())]; |
| 117 | + |
| 118 | + for (int j = 0; j < 16; j++) { |
| 119 | + int offset = (73 + HexDump.EOL.length()) * j; |
| 120 | + |
| 121 | + outputArray[offset++] = (byte) '0'; |
| 122 | + outputArray[offset++] = (byte) '0'; |
| 123 | + outputArray[offset++] = (byte) '0'; |
| 124 | + outputArray[offset++] = (byte) '0'; |
| 125 | + outputArray[offset++] = (byte) '0'; |
| 126 | + outputArray[offset++] = (byte) '0'; |
| 127 | + outputArray[offset++] = (byte) toHex(j); |
| 128 | + outputArray[offset++] = (byte) '0'; |
| 129 | + outputArray[offset++] = (byte) ' '; |
| 130 | + for (int k = 0; k < 16; k++) { |
| 131 | + outputArray[offset++] = (byte) toHex(j); |
| 132 | + outputArray[offset++] = (byte) toHex(k); |
| 133 | + outputArray[offset++] = (byte) ' '; |
| 134 | + } |
| 135 | + for (int k = 0; k < 16; k++) { |
| 136 | + outputArray[offset++] = (byte) toAscii((j * 16) + k); |
| 137 | + } |
| 138 | + System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, |
| 139 | + HexDump.EOL.getBytes().length); |
| 140 | + } |
| 141 | + byte[] actualOutput = stream.toByteArray(); |
| 142 | + |
| 143 | + assertEquals("array size mismatch", outputArray.length, |
| 144 | + actualOutput.length); |
| 145 | + for (int j = 0; j < outputArray.length; j++) { |
| 146 | + assertEquals("array[ " + j + "] mismatch", outputArray[j], |
| 147 | + actualOutput[j]); |
| 148 | + } |
| 149 | + |
| 150 | + // verify proper behavior with non-zero offset |
| 151 | + stream = new ByteArrayOutputStream(); |
| 152 | + HexDump.dump(testArray, 0x10000000, stream, 0); |
| 153 | + outputArray = new byte[16 * (73 + HexDump.EOL.length())]; |
| 154 | + for (int j = 0; j < 16; j++) { |
| 155 | + int offset = (73 + HexDump.EOL.length()) * j; |
| 156 | + |
| 157 | + outputArray[offset++] = (byte) '1'; |
| 158 | + outputArray[offset++] = (byte) '0'; |
| 159 | + outputArray[offset++] = (byte) '0'; |
| 160 | + outputArray[offset++] = (byte) '0'; |
| 161 | + outputArray[offset++] = (byte) '0'; |
| 162 | + outputArray[offset++] = (byte) '0'; |
| 163 | + outputArray[offset++] = (byte) toHex(j); |
| 164 | + outputArray[offset++] = (byte) '0'; |
| 165 | + outputArray[offset++] = (byte) ' '; |
| 166 | + for (int k = 0; k < 16; k++) { |
| 167 | + outputArray[offset++] = (byte) toHex(j); |
| 168 | + outputArray[offset++] = (byte) toHex(k); |
| 169 | + outputArray[offset++] = (byte) ' '; |
| 170 | + } |
| 171 | + for (int k = 0; k < 16; k++) { |
| 172 | + outputArray[offset++] = (byte) toAscii((j * 16) + k); |
| 173 | + } |
| 174 | + System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, |
| 175 | + HexDump.EOL.getBytes().length); |
| 176 | + } |
| 177 | + actualOutput = stream.toByteArray(); |
| 178 | + assertEquals("array size mismatch", outputArray.length, |
| 179 | + actualOutput.length); |
| 180 | + for (int j = 0; j < outputArray.length; j++) { |
| 181 | + assertEquals("array[ " + j + "] mismatch", outputArray[j], |
| 182 | + actualOutput[j]); |
| 183 | + } |
| 184 | + |
| 185 | + // verify proper behavior with negative offset |
| 186 | + stream = new ByteArrayOutputStream(); |
| 187 | + HexDump.dump(testArray, 0xFF000000, stream, 0); |
| 188 | + outputArray = new byte[16 * (73 + HexDump.EOL.length())]; |
| 189 | + for (int j = 0; j < 16; j++) { |
| 190 | + int offset = (73 + HexDump.EOL.length()) * j; |
| 191 | + |
| 192 | + outputArray[offset++] = (byte) 'F'; |
| 193 | + outputArray[offset++] = (byte) 'F'; |
| 194 | + outputArray[offset++] = (byte) '0'; |
| 195 | + outputArray[offset++] = (byte) '0'; |
| 196 | + outputArray[offset++] = (byte) '0'; |
| 197 | + outputArray[offset++] = (byte) '0'; |
| 198 | + outputArray[offset++] = (byte) toHex(j); |
| 199 | + outputArray[offset++] = (byte) '0'; |
| 200 | + outputArray[offset++] = (byte) ' '; |
| 201 | + for (int k = 0; k < 16; k++) { |
| 202 | + outputArray[offset++] = (byte) toHex(j); |
| 203 | + outputArray[offset++] = (byte) toHex(k); |
| 204 | + outputArray[offset++] = (byte) ' '; |
| 205 | + } |
| 206 | + for (int k = 0; k < 16; k++) { |
| 207 | + outputArray[offset++] = (byte) toAscii((j * 16) + k); |
| 208 | + } |
| 209 | + System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, |
| 210 | + HexDump.EOL.getBytes().length); |
| 211 | + } |
| 212 | + actualOutput = stream.toByteArray(); |
| 213 | + assertEquals("array size mismatch", outputArray.length, |
| 214 | + actualOutput.length); |
| 215 | + for (int j = 0; j < outputArray.length; j++) { |
| 216 | + assertEquals("array[ " + j + "] mismatch", outputArray[j], |
| 217 | + actualOutput[j]); |
| 218 | + } |
| 219 | + |
| 220 | + // verify proper behavior with non-zero index |
| 221 | + stream = new ByteArrayOutputStream(); |
| 222 | + HexDump.dump(testArray, 0x10000000, stream, 0x81); |
| 223 | + outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1]; |
| 224 | + for (int j = 0; j < 8; j++) { |
| 225 | + int offset = (73 + HexDump.EOL.length()) * j; |
| 226 | + |
| 227 | + outputArray[offset++] = (byte) '1'; |
| 228 | + outputArray[offset++] = (byte) '0'; |
| 229 | + outputArray[offset++] = (byte) '0'; |
| 230 | + outputArray[offset++] = (byte) '0'; |
| 231 | + outputArray[offset++] = (byte) '0'; |
| 232 | + outputArray[offset++] = (byte) '0'; |
| 233 | + outputArray[offset++] = (byte) toHex(j + 8); |
| 234 | + outputArray[offset++] = (byte) '1'; |
| 235 | + outputArray[offset++] = (byte) ' '; |
| 236 | + for (int k = 0; k < 16; k++) { |
| 237 | + int index = 0x81 + (j * 16) + k; |
| 238 | + |
| 239 | + if (index < 0x100) { |
| 240 | + outputArray[offset++] = (byte) toHex(index / 16); |
| 241 | + outputArray[offset++] = (byte) toHex(index); |
| 242 | + } else { |
| 243 | + outputArray[offset++] = (byte) ' '; |
| 244 | + outputArray[offset++] = (byte) ' '; |
| 245 | + } |
| 246 | + outputArray[offset++] = (byte) ' '; |
| 247 | + } |
| 248 | + for (int k = 0; k < 16; k++) { |
| 249 | + int index = 0x81 + (j * 16) + k; |
| 250 | + |
| 251 | + if (index < 0x100) { |
| 252 | + outputArray[offset++] = (byte) toAscii(index); |
| 253 | + } |
| 254 | + } |
| 255 | + System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, |
| 256 | + HexDump.EOL.getBytes().length); |
| 257 | + } |
| 258 | + actualOutput = stream.toByteArray(); |
| 259 | + assertEquals("array size mismatch", outputArray.length, |
| 260 | + actualOutput.length); |
| 261 | + for (int j = 0; j < outputArray.length; j++) { |
| 262 | + assertEquals("array[ " + j + "] mismatch", outputArray[j], |
| 263 | + actualOutput[j]); |
| 264 | + } |
| 265 | + |
| 266 | + // verify proper behavior with negative index |
| 267 | + try { |
| 268 | + HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), |
| 269 | + -1); |
| 270 | + fail("should have caught ArrayIndexOutOfBoundsException on negative index"); |
| 271 | + } catch (ArrayIndexOutOfBoundsException ignored_exception) { |
| 272 | + |
| 273 | + // as expected |
| 274 | + } |
| 275 | + |
| 276 | + // verify proper behavior with index that is too large |
| 277 | + try { |
| 278 | + HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), |
| 279 | + testArray.length); |
| 280 | + fail("should have caught ArrayIndexOutOfBoundsException on large index"); |
| 281 | + } catch (ArrayIndexOutOfBoundsException ignored_exception) { |
| 282 | + |
| 283 | + // as expected |
| 284 | + } |
| 285 | + |
| 286 | + // verify proper behavior with null stream |
| 287 | + try { |
| 288 | + HexDump.dump(testArray, 0x10000000, null, 0); |
| 289 | + fail("should have caught IllegalArgumentException on negative index"); |
| 290 | + } catch (IllegalArgumentException ignored_exception) { |
| 291 | + |
| 292 | + // as expected |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + private char toAscii(final int c) { |
| 297 | + char rval = '.'; |
| 298 | + |
| 299 | + if ((c >= 32) && (c <= 126)) { |
| 300 | + rval = (char) c; |
| 301 | + } |
| 302 | + return rval; |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * main method to run the unit tests |
| 307 | + * |
| 308 | + * @param ignored_args |
| 309 | + */ |
| 310 | + |
| 311 | + public static void main(String[] ignored_args) { |
| 312 | + System.out.println("Testing io.HexDump functionality"); |
| 313 | + junit.textui.TestRunner.run(HexDumpTest.class); |
| 314 | + } |
| 315 | +} |
0 commit comments