mirror of https://github.com/apache/poi.git
Fix formatting of trailing comma in HexDumps, cover class fully with tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1612496 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ea8e44678b
commit
5e74bf9133
|
@ -70,7 +70,7 @@ public class HexDump {
|
||||||
{
|
{
|
||||||
if (data.length == 0)
|
if (data.length == 0)
|
||||||
{
|
{
|
||||||
stream.write( ("No Data" + System.getProperty( "line.separator")).getBytes() );
|
stream.write( ("No Data" + EOL).getBytes() );
|
||||||
stream.flush();
|
stream.flush();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -276,8 +276,10 @@ public class HexDump {
|
||||||
retVal.append('[');
|
retVal.append('[');
|
||||||
for(int x = 0; x < value.length; x++)
|
for(int x = 0; x < value.length; x++)
|
||||||
{
|
{
|
||||||
|
if (x>0) {
|
||||||
|
retVal.append(", ");
|
||||||
|
}
|
||||||
retVal.append(toHex(value[x]));
|
retVal.append(toHex(value[x]));
|
||||||
retVal.append(", ");
|
|
||||||
}
|
}
|
||||||
retVal.append(']');
|
retVal.append(']');
|
||||||
return retVal.toString();
|
return retVal.toString();
|
||||||
|
@ -311,9 +313,10 @@ public class HexDump {
|
||||||
retVal.append('\n');
|
retVal.append('\n');
|
||||||
retVal.append(format.format(x));
|
retVal.append(format.format(x));
|
||||||
i = 0;
|
i = 0;
|
||||||
|
} else if (x>0) {
|
||||||
|
retVal.append(", ");
|
||||||
}
|
}
|
||||||
retVal.append(toHex(value[x]));
|
retVal.append(toHex(value[x]));
|
||||||
retVal.append(", ");
|
|
||||||
}
|
}
|
||||||
return retVal.toString();
|
return retVal.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,14 @@
|
||||||
|
|
||||||
package org.apache.poi.util;
|
package org.apache.poi.util;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
@ -259,11 +265,28 @@ public final class TestHexDump extends TestCase {
|
||||||
|
|
||||||
public void testToHex() {
|
public void testToHex() {
|
||||||
assertEquals("000A", HexDump.toHex((short)0xA));
|
assertEquals("000A", HexDump.toHex((short)0xA));
|
||||||
|
|
||||||
|
assertEquals("[]", HexDump.toHex(new short[] { }));
|
||||||
|
assertEquals("[000A]", HexDump.toHex(new short[] { 0xA }));
|
||||||
|
assertEquals("[000A, 000B]", HexDump.toHex(new short[] { 0xA, 0xB }));
|
||||||
|
|
||||||
assertEquals("0A", HexDump.toHex((byte)0xA));
|
assertEquals("0A", HexDump.toHex((byte)0xA));
|
||||||
assertEquals("0000000A", HexDump.toHex(0xA));
|
assertEquals("0000000A", HexDump.toHex(0xA));
|
||||||
|
|
||||||
|
assertEquals("[]", HexDump.toHex(new byte[] { }));
|
||||||
|
assertEquals("[0A]", HexDump.toHex(new byte[] { 0xA }));
|
||||||
|
assertEquals("[0A, 0B]", HexDump.toHex(new byte[] { 0xA, 0xB }));
|
||||||
|
|
||||||
|
assertEquals(": 0", HexDump.toHex(new byte[] { }, 10));
|
||||||
|
assertEquals("0: 0A", HexDump.toHex(new byte[] { 0xA }, 10));
|
||||||
|
assertEquals("0: 0A, 0B", HexDump.toHex(new byte[] { 0xA, 0xB }, 10));
|
||||||
|
assertEquals("0: 0A, 0B\n2: 0C, 0D", HexDump.toHex(new byte[] { 0xA, 0xB, 0xC, 0xD }, 2));
|
||||||
|
assertEquals("0: 0A, 0B\n2: 0C, 0D\n4: 0E, 0F", HexDump.toHex(new byte[] { 0xA, 0xB, 0xC, 0xD, 0xE, 0xF }, 2));
|
||||||
|
|
||||||
assertEquals("FFFF", HexDump.toHex((short)0xFFFF));
|
assertEquals("FFFF", HexDump.toHex((short)0xFFFF));
|
||||||
|
|
||||||
|
assertEquals("00000000000004D2", HexDump.toHex(1234l));
|
||||||
|
|
||||||
confirmStr("0xFE", HexDump.byteToHex(-2));
|
confirmStr("0xFE", HexDump.byteToHex(-2));
|
||||||
confirmStr("0x25", HexDump.byteToHex(37));
|
confirmStr("0x25", HexDump.byteToHex(37));
|
||||||
confirmStr("0xFFFE", HexDump.shortToHex(-2));
|
confirmStr("0xFFFE", HexDump.shortToHex(-2));
|
||||||
|
@ -286,4 +309,149 @@ public final class TestHexDump extends TestCase {
|
||||||
}
|
}
|
||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDumpToString() throws Exception {
|
||||||
|
byte[] testArray = new byte[ 256 ];
|
||||||
|
|
||||||
|
for (int j = 0; j < 256; j++)
|
||||||
|
{
|
||||||
|
testArray[ j ] = ( byte ) j;
|
||||||
|
}
|
||||||
|
String dump = HexDump.dump(testArray, 0, 0);
|
||||||
|
//System.out.println("Hex: \n" + dump);
|
||||||
|
assertTrue("Had: \n" + dump,
|
||||||
|
dump.contains("0123456789:;<=>?"));
|
||||||
|
|
||||||
|
dump = HexDump.dump(testArray, 2, 1);
|
||||||
|
//System.out.println("Hex: \n" + dump);
|
||||||
|
assertTrue("Had: \n" + dump,
|
||||||
|
dump.contains("123456789:;<=>?@"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDumpToStringOutOfIndex() throws Exception {
|
||||||
|
byte[] testArray = new byte[ 0 ];
|
||||||
|
|
||||||
|
try {
|
||||||
|
HexDump.dump(testArray, 0, -1);
|
||||||
|
fail("Should throw an exception with invalid input");
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
HexDump.dump(testArray, 0, 1);
|
||||||
|
fail("Should throw an exception with invalid input");
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDumpToPrintStream() throws IOException {
|
||||||
|
byte[] testArray = new byte[ 256 ];
|
||||||
|
|
||||||
|
for (int j = 0; j < 256; j++)
|
||||||
|
{
|
||||||
|
testArray[ j ] = ( byte ) j;
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream in = new ByteArrayInputStream(testArray);
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
|
PrintStream out = new PrintStream(byteOut);
|
||||||
|
try {
|
||||||
|
HexDump.dump(in, out, 0, 256);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = new String(byteOut.toByteArray());
|
||||||
|
assertTrue("Had: \n" + str,
|
||||||
|
str.contains("0123456789:;<=>?"));
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
in = new ByteArrayInputStream(testArray);
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
|
PrintStream out = new PrintStream(byteOut);
|
||||||
|
try {
|
||||||
|
// test with more than we have
|
||||||
|
HexDump.dump(in, out, 0, 1000);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = new String(byteOut.toByteArray());
|
||||||
|
assertTrue("Had: \n" + str,
|
||||||
|
str.contains("0123456789:;<=>?"));
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
in = new ByteArrayInputStream(testArray);
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
|
PrintStream out = new PrintStream(byteOut);
|
||||||
|
try {
|
||||||
|
// test with -1
|
||||||
|
HexDump.dump(in, out, 0, -1);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = new String(byteOut.toByteArray());
|
||||||
|
assertTrue("Had: \n" + str,
|
||||||
|
str.contains("0123456789:;<=>?"));
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
in = new ByteArrayInputStream(testArray);
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
|
PrintStream out = new PrintStream(byteOut);
|
||||||
|
try {
|
||||||
|
HexDump.dump(in, out, 1, 235);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
String str = new String(byteOut.toByteArray());
|
||||||
|
assertTrue("Line contents should be moved by one now, but Had: \n" + str,
|
||||||
|
str.contains("123456789:;<=>?@"));
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConstruct() throws Exception {
|
||||||
|
// to cover private constructor
|
||||||
|
// get the default constructor
|
||||||
|
final Constructor<HexDump> c = HexDump.class.getDeclaredConstructor(new Class[] {});
|
||||||
|
|
||||||
|
// make it callable from the outside
|
||||||
|
c.setAccessible(true);
|
||||||
|
|
||||||
|
// call it
|
||||||
|
assertNotNull(c.newInstance((Object[]) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMain() throws Exception {
|
||||||
|
File file = TempFile.createTempFile("HexDump", ".dat");
|
||||||
|
try {
|
||||||
|
FileOutputStream out = new FileOutputStream(file);
|
||||||
|
try {
|
||||||
|
IOUtils.copy(new ByteArrayInputStream("teststring".getBytes()), out);
|
||||||
|
} finally {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
assertTrue(file.exists());
|
||||||
|
assertTrue(file.length() > 0);
|
||||||
|
|
||||||
|
HexDump.main(new String[] { file.getAbsolutePath() });
|
||||||
|
} finally {
|
||||||
|
assertTrue(file.exists() && file.delete());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue