import java.
io.
ByteArrayInputStream;
import java.
io.
InputStream;
import junit.
framework.
TestCase;
public class FileUtilsTest
extends TestCase
{
public void testGetMD5HashInputStream
() throws Exception
{
byte[] data =
new byte[] { 0x61, 0x62, 0x63, 0x0a
};
InputStream ins =
new ByteArrayInputStream(data
);
assertEquals
("0bee89b07a248e27c83fc3d5951213c1", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins
)));
try {
FileUtils.
getMD5Hash(null);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
}
public void testGetMD5HashInputStreamLongLong
() throws Exception
{
byte[] data =
new byte[] { 0x61, 0x62, 0x63, 0x0a
};
InputStream ins =
new ByteArrayInputStream(data
);
assertEquals
("0bee89b07a248e27c83fc3d5951213c1", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins,
0,
4)));
ins =
new ByteArrayInputStream(data
);
assertEquals
("0bee89b07a248e27c83fc3d5951213c1", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins,
0,
0)));
ins =
new ByteArrayInputStream(data
);
assertEquals
("0bee89b07a248e27c83fc3d5951213c1", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins,
0,
-1)));
ins =
new ByteArrayInputStream(data
);
assertEquals
("b79898bb7907648871745cd5422c79ce", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins,
1,
-1)));
ins =
new ByteArrayInputStream(data
);
assertEquals
("b79898bb7907648871745cd5422c79ce", FileUtils.
toHexString(FileUtils.
getMD5Hash(ins,
1,
3)));
try {
FileUtils.
getMD5Hash(ins,
Integer.
MIN_VALUE,
0);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
try {
FileUtils.
getMD5Hash(null,
0,
0);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
}
public void testToHexString
()
{
byte[] data =
"abc\n".
getBytes();
assertEquals
("6162630a", FileUtils.
toHexString(data,
0,
-1));
assertEquals
("62630a", FileUtils.
toHexString(data,
1,
-1));
assertEquals
("6263", FileUtils.
toHexString(data,
1,
2));
assertEquals
("", FileUtils.
toHexString(data,
0,
0));
assertEquals
("", FileUtils.
toHexString(data,
2,
0));
try {
FileUtils.
toHexString(data,
Integer.
MAX_VALUE,
0);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
try {
FileUtils.
toHexString(data,
Integer.
MIN_VALUE,
0);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
try {
FileUtils.
toHexString(data,
0,
Integer.
MAX_VALUE);
fail
("should have thrown IllegalArgumentException");
}
catch(IllegalArgumentException e
) {
// expected exception
}
}
}