TestStaticUtils.java revision 8e174a3b5ad5ba4f3267bffceb0d952eea891b5f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2009 Sun Microsystems, Inc.
* Portions Copyright 2013-2015 ForgeRock AS
*/
/**
* This class defines a set of tests for the
* {@link org.opends.server.util.StaticUtils} class.
*/
public final class TestStaticUtils extends UtilTestCase {
/** Lower case hex digit lookup table. */
'2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/** Upper case hex digit lookup table. */
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/** Lookup table for 8-bit strings. */
static {
for (int i = 0; i < 256; i++) {
}
}
/**
* Once-only initialization.
*
* @throws Exception
* If an unexpected error occurred.
*/
}
/**
* Create test strings for the {@link StaticUtils#getBytes(String)}.
*
* @return Returns an array of test strings.
*/
public Object[][] createGetBytesTestData() {
// Some simple strings.
// A string containing just UTF-8 1 byte sequences.
for (char c = '\u0000'; c < '\u0080'; c++) {
}
// A string containing UTF-8 1 and 2 byte sequences.
builder = new StringBuilder();
for (char c = '\u0000'; c < '\u0100'; c++) {
}
// A string containing UTF-8 1 and 6 byte sequences.
builder = new StringBuilder();
for (char c = '\u0000'; c < '\u0080'; c++) {
}
for (char c = '\uff00'; c != '\u0000'; c++) {
}
// Construct the array.
}
return data;
}
/**
* Tests the {@link StaticUtils#decodeUTF8(byte[])} method.
*
* @param inputString
* The input string.
* @throws Exception
* If the test failed unexpectedly.
*/
{
}
/**
* Tests the {@link StaticUtils#getBytes(String)} method.
*
* @param inputString
* The input string.
* @throws Exception
* If the test failed unexpectedly.
*/
.getBytes("UTF-8"));
}
/**
* Tests the {@link StaticUtils#getBytes(char[])} method.
*
* @param inputString
* The input string.
* @throws Exception
* If the test failed unexpectedly.
*/
}
/**
* Create test strings for the {@link StaticUtils#byteToHex(byte)}.
*
* @return Returns an array of test strings.
*/
public Object[][] createByteToHexTestData() {
for (int i = 0; i < 256; i++) {
}
return data;
}
/**
* Tests the {@link StaticUtils#byteToHex(byte)} method.
*
* @param b
* The input byte.
* @throws Exception
* If the test failed unexpectedly.
*/
public void testByteToHex(byte b) throws Exception {
HEX_DIGITS_UPPER[b & 0x0f] };
}
/**
* Tests the {@link StaticUtils#byteToLowerHex(byte)} method.
*
* @param b
* The input byte.
* @throws Exception
* If the test failed unexpectedly.
*/
public void testByteToLowerHex(byte b) throws Exception {
HEX_DIGITS_LOWER[b & 0x0f] };
}
/**
* Tests the {@link StaticUtils#byteToASCII(byte)} method.
*
* @param b
* The input byte.
* @throws Exception
* If the test failed unexpectedly.
*/
public void testByteToASCII(byte b) throws Exception {
if (b < 32 || b > 126) {
} else {
}
}
/**
* Create test strings for the {@link StaticUtils#bytesToHex(byte[])}.
*
* @return Returns an array of test data.
*/
public Object[][] createBytesToHexTestData() {
return new Object[][] {
{ null, "" },
{ new byte[0], "" },
{ new byte[] { 0x00 }, "00" },
{ new byte[] { 0x00, 0x7f, (byte) 0x80, (byte) 0xff },
"00 7F 80 FF" } };
}
/**
* Tests the {@link StaticUtils#bytesToHex(byte[])} method.
*
* @param bytes
* The input byte array.
* @param expected
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the {@link StaticUtils#bytesToHex(java.nio.ByteBuffer)}
* method.
*
* @param bytes
* The input byte array.
* @param expected
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the {@link StaticUtils#byteToBinary(byte)} method.
*
* @param b
* The input byte.
* @throws Exception
* If the test failed unexpectedly.
*/
public void testByteToBinary(byte b) throws Exception {
}
/**
* Create test data for {@link StaticUtils#compare(byte[], byte[])}.
*
* @return Returns an array of test data.
*/
public Object[][] createCompareBytesTestData() {
return new Object[][] {
{ new byte[0], new byte[0], 0 },
{ new byte[] { 0x00 }, new byte[] { 0x00 }, 0 },
{ new byte[] { 0x01 }, new byte[] { 0x00 }, 1 },
{ new byte[] { 0x7f }, new byte[] { 0x00 }, 1 },
{ new byte[] { (byte) 0x80 }, new byte[] { 0x00 }, 1 },
{ new byte[] { (byte) 0xff }, new byte[] { 0x00 }, 1 },
{ new byte[] { 0x00 }, new byte[] { 0x01 }, -1 },
{ new byte[] { 0x00 }, new byte[] { 0x7f }, -1 },
{ new byte[] { 0x00 }, new byte[] { (byte) 0x80 }, -1 },
{ new byte[] { 0x00 }, new byte[] { (byte) 0xff }, -1 },
{ new byte[] { 0x00, 0x01, 0x02 },
new byte[] { 0x00, 0x01, 0x02 }, 0 },
{ new byte[] { 0x00, 0x01 }, new byte[] { 0x00, 0x01, 0x02 },
-1 },
{ new byte[] { 0x00, 0x01, 0x02 }, new byte[] { 0x00, 0x01 },
1 }, };
}
/**
* Tests the {@link StaticUtils#compare(byte[], byte[])} method.
*
* @param a
* The first byte array.
* @param a2
* The second byte array.
* @param expected
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
}
}
}
/**
* Create test strings for the {@link StaticUtils#isDigit(char)}.
*
* @return Returns an array of test data.
*/
public Object[][] createIsDigitTestData() {
for (char c = '0'; c <= '9'; c++) {
}
}
/**
* Tests the {@link StaticUtils#isDigit(char)} method.
*
* @param c
* The test char.
* @param result
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
}
/**
* Create test strings for the {@link StaticUtils#isAlpha(char)}.
*
* @return Returns an array of test data.
*/
public Object[][] createIsAlphaTestData() {
for (char c = 'a'; c <= 'z'; c++) {
}
for (char c = 'A'; c <= 'Z'; c++) {
}
for (char c = '0'; c <= '9'; c++) {
}
}
/**
* Tests the {@link StaticUtils#isAlpha(char)} method.
*
* @param c
* The test char.
* @param result
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
}
/**
* Create test strings for the {@link StaticUtils#isHexDigit(char)}.
*
* @return Returns an array of test data.
*/
public Object[][] createIsHexDigitTestData() {
for (char c = 'a'; c <= 'f'; c++) {
}
for (char c = 'A'; c <= 'F'; c++) {
}
for (char c = '0'; c <= '9'; c++) {
}
}
/**
* Tests the {@link StaticUtils#isHexDigit(char)} method.
*
* @param c
* The test char.
* @param result
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
}
/**
* Create invalid test strings for the
* {@link StaticUtils#hexStringToByteArray(String)}.
*
* @return Returns an array of test data.
*/
public Object[][] createHexStringToByteArrayInvalidTestData() {
{ "0@" }, { "0G" }, { "0`" }, { "0g" } };
}
/**
* Tests the {@link StaticUtils#hexStringToByteArray(String)} method.
*
* @param hexString
* The test string.
* @throws Exception
* If the test failed unexpectedly.
*/
@Test(expectedExceptions = ParseException.class, dataProvider = "hexStringToByteArrayInvalidTestData")
throws Exception {
}
/**
* Create test strings for the
* {@link StaticUtils#hexStringToByteArray(String)}.
*
* @return Returns an array of test data.
*/
public Object[][] createHexStringToByteArrayTestData() {
{ "00010f107f80ff", new byte[] { 0, 1, 15, 16, 127, -128, -1 } } };
}
/**
* Tests the {@link StaticUtils#hexStringToByteArray(String)} method.
*
* @param hexString
* The test string.
* @param bytes
* The expected byte array.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Create test strings for the
* {@link StaticUtils#needsBase64Encoding(String)}.
*
* @return Returns an array of test data.
*/
public Object[][] createNeedsBase64EncodingTestData() {
// Check SAFE-INIT-CHAR.
for (char c = '\u0000'; c < '\u0100'; c++) {
boolean result = false;
switch (c) {
case '\u0000':
case '\r':
case '\n':
case ' ':
case ':':
case '<':
result = true;
break;
default:
if (c >= '\u0080') {
result = true;
}
break;
}
}
// Check SAFE-CHAR.
for (char c = '\u0000'; c < '\u0100'; c++) {
boolean result = false;
switch (c) {
case '\u0000':
case '\r':
case '\n':
result = true;
break;
default:
if (c >= '\u0080') {
result = true;
}
break;
}
}
}
/**
* Tests the {@link StaticUtils#needsBase64Encoding(String)} method.
*
* @param s
* The test string.
* @param result
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the {@link StaticUtils#needsBase64Encoding(ByteSequence)} method.
*
* @param s
* The test string.
* @param result
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Create test strings for the
* {@link StaticUtils#isRelativePath(String)}.
*
* @return Returns an array of test data.
*/
public Object[][] createIsRelativePathTestData() {
{ ".", true }, { "..", true },
}
/**
* Tests the {@link StaticUtils#isRelativePath(String)} method.
*
* @param path
* The test string.
* @param result
* Expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Create test lists for the {@link StaticUtils#listToArray(List)}.
*
* @return Returns an array of test data.
*/
public Object[][] createListToArrayTestData() {
{ new String[] { "aaa" } },
}
/**
* Tests the {@link StaticUtils#listToArray(List)} method.
*
* @param strings
* The test string list.
* @throws Exception
* If the test failed unexpectedly.
*/
}
} else {
}
}
/**
* Tests the {@link StaticUtils#moveFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
public void testMoveFileNonExistentSrc() throws Exception {
try {
} finally {
}
}
/**
* Tests the {@link StaticUtils#moveFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
public void testMoveFileNonExistentDst() throws Exception {
try {
} finally {
}
}
/**
* Tests the {@link StaticUtils#moveFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
public void testMoveFileSrcNotFile() throws Exception {
try {
} finally {
}
}
/**
* Tests the {@link StaticUtils#moveFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
public void testMoveFileDstNotDirectory() throws Exception {
try {
} finally {
}
}
/**
* Create test content for {@link StaticUtils#moveFile(File, File)}.
*
* @return Returns an array of test data.
*/
public Object[][] createMoveFileTestData() {
}
/**
* Tests the {@link StaticUtils#moveFile(java.io.File, java.io.File)}
* method.
*
* @param lines
* The test file contents.
* @throws Exception
* If the test failed unexpectedly.
*/
try {
// Generate contents.
new FileWriter(src)));
}
// Move the file.
// Post conditions.
}
} finally {
}
}
/**
* Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception If the test failed unexpectedly.
*/
@Test
public void testRenameFileNonExistentTarget() throws Exception {
try {
}
} finally {
}
}
/**
* Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
* method.
*
* @throws Exception If the test failed unexpectedly.
*/
@Test
public void testRenameFileExistentTarget() throws Exception {
try {
} finally {
}
}
/**
* Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
* method. Renaming locked files is a problem on Windows but not so
* much on other platforms.
*
* @throws Exception If the test failed unexpectedly.
*/
public void testRenameFileLockedTarget() throws Exception {
try {
} finally {
}
}
/**
* Tests the {@link StaticUtils#recursiveDelete(File)} method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
@Test
public void testRecursiveDeleteNonExistent() throws Exception {
}
/**
* Tests the {@link StaticUtils#recursiveDelete(File)} method.
*
* @throws Exception
* If the test failed unexpectedly.
*/
@Test
public void testRecursiveDelete() throws Exception {
f1.createNewFile();
f2.createNewFile();
f3.createNewFile();
}
}
/**
* Create test strings for the {@link StaticUtils#toLowerCase(String)}
* related methods.
*
* @return Returns an array of test data.
*/
public Object[][] createStringCaseConversionTestData() {
return new Object[][] {
{ "", "", "" },
{ " ", " ", " " },
{ " a B c ", " a b c ", " A B C " },
{
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}|[]:;'<>?,./!@#$%^&*()_+",
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789{}|[]:;'<>?,./!@#$%^&*()_+",
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}|[]:;'<>?,./!@#$%^&*()_+" },
{ "some non-ascii \u00c0\u00e0\u00c6\u00e6\u00dd\u00fd",
"some non-ascii \u00e0\u00e0\u00e6\u00e6\u00fd\u00fd",
"SOME NON-ASCII \u00c0\u00c0\u00c6\u00c6\u00dd\u00dd" } };
}
/**
* Tests the {@link StaticUtils#toLowerCase(String)} method.
*
* @param input
* The test string.
* @param lower
* The test string in lower case.
* @param upper
* The test string in upper case.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the
* {@link StaticUtils#toLowerCase(ByteSequence, StringBuilder, boolean)}
* method.
*
* @param input
* The test string.
* @param lower
* The test string in lower case.
* @param upper
* The test string in upper case.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the {@link StaticUtils#toUpperCase(String)} method.
*
* @param input
* The test string.
* @param lower
* The test string in lower case.
* @param upper
* The test string in upper case.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Tests the
* {@link StaticUtils#toUpperCase(byte[], StringBuilder, boolean)}
* method.
*
* @param input
* The test string.
* @param lower
* The test string in lower case.
* @param upper
* The test string in upper case.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Create test strings for the
* {@link StaticUtils#toRFC3641StringValue(StringBuilder, String)}
* method.
*
* @return Returns an array of test data.
*/
public Object[][] createToRFC3641StringValueTestData() {
{ " a B c ", "\" a B c \"" },
{ " \"hello world\" ", "\" \"\"hello world\"\" \"" },
{ "\"\"\"", "\"\"\"\"\"\"\"\"" }, };
}
/**
* Tests the
* {@link StaticUtils#toRFC3641StringValue(StringBuilder, String)}
* method.
*
* @param input
* The test string.
* @param expected
* The expected result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
/**
* Create test lists for the
* {@link StaticUtils#listsAreEqual(List, List)} method.
*
* @return Returns an array of test data.
*/
public Object[][] createListsAreEqualTestData() {
return new Object[][] {
// Check null behaviour.
// Check empty-list behaviour.
// Check single-element behaviour.
// Check multi-element random access behaviour.
// ...With duplicates.
// Check multi-element sequential behaviour.
// ...With duplicates.
}
/**
* Tests the {@link StaticUtils#listsAreEqual(List, List)} method.
*
* @param list1
* The first list.
* @param list2
* The second list.
* @param result
* The expected equality result.
* @throws Exception
* If the test failed unexpectedly.
*/
throws Exception {
}
@Test
public void testStackTraceHasCause() throws Exception
{
boolean hasCause = StaticUtils.stackTraceContainsCause(new RuntimeException(new ArithmeticException()), ArithmeticException.class);
hasCause = StaticUtils.stackTraceContainsCause(new RuntimeException(new RuntimeException()), ArithmeticException.class);
Assert.assertFalse(hasCause, "Second case : ArithmeticException should not be detected as a cause");
hasCause = StaticUtils.stackTraceContainsCause(new RuntimeException(new IllegalThreadStateException()), IllegalArgumentException.class);
Assert.assertTrue(hasCause, "Third case : IllegalThreadStateException should be detected as a cause");
}
}