/home/wpollock1/public_html/AJava/NatOrderComparator/NatOrderComparatorTestSuite.java
package com.wpollock.natordercomp.tests;
import static org.junit.Assert.*;
import com.wpollock.natordercomp.NatOrderComparator;
import java.util.Arrays;
import org.junit.Test;
/**
* Unit tests for the Natural Order Comparator (for Strings).
* These tests use a data-driven approach when multiple tests of the
* same type (but with different data) is desired. This approach minimizes
* the size of each test and reduces the chance of logic errors in the
* test suite.
*
* Each method tests several related sets of arguments. Note that the first
* failed test aborts the method, thus skipping the remaining checks. This
* was a trade-off I felt was worth while, as long as all the checks in a
* method checked the same thing, and the exception thrown and the data that
* caused it were included in the output.
*
* @author Wayne Pollock, Tampa Florida USA
*
*/
public class NatOrderComparatorTestSuite {
private final NatOrderComparator noc = NatOrderComparator.getInstance();
@Test
public final void testCompareTextOnly() {
String[][][] dataSet = { // pairs of Strings to compare
// ACTUAL EXPECTED
{ {"A", "B"}, {"A", "B"} },
{ {"BB", "AA"}, {"AA", "BB"} },
{ {"C", "B", "A"}, {"A", "B", "C"} },
};
for ( String[][] data : dataSet ) {
try {
final String[] actual = data[0].clone();
final String[] expected = data[1];
Arrays.sort(actual, noc);
assertArrayEquals("Data " + Arrays.toString(data[0]),
expected, actual);
} catch (Exception e ) {
System.out.println( "Data: " + Arrays.deepToString(data)
+ " - Exception Thrown: " + e );
}
}
}
/**
* Include tests for leading zeros (4 cases), "00", "0", and numbers too
* large for an int.
*/
@Test
public final void testCompareNumericStringsOnly() {
String[][][] dataSet = { // pairs of Strings to compare
// ACTUAL EXPECTED
{ {"0", "0"}, {"0", "0"} },
{ {"00", "00"}, {"00", "00"} },
{ {"0", "00"}, {"0", "00"} },
{ {"00", "0"}, {"0", "00"} },
{ {"012", "12"}, {"12", "012"} },
{ {"34", "034"}, {"34", "034"} },
{ {"12", "12"}, {"12", "12"} },
{ {"12", "23", "123"}, {"12", "23", "123"} },
{ {"456", "23", "1"}, {"1", "23", "456"} },
{ { "7000000000", "6000000000"}, { "6000000000", "7000000000"} }
};
for ( String[][] data : dataSet ) {
try {
final String[] actual = data[0].clone();
final String[] expected = data[1];
Arrays.sort(actual, noc);
assertArrayEquals("Data " + Arrays.toString(data[0]),
expected, actual);
} catch (Exception e ) {
System.out.println( "Data: " + Arrays.deepToString(data)
+ " - Exception Thrown: " + e );
}
}
}
/**
* Compare alpha to numeric (2 cases), and alphanum to any (6 cases),
* and non-ASCII alphanums:
*/
@Test
public final void testComparMixedStringsOnly() {
String[][][] dataSet = { // pairs of Strings to compare
{ /*actual*/ { "image1.png", "image2.png", "image01.png",
"image10.png", "image5.png", "image0.png", "image3.png" },
/*expected*/ { "image0.png", "image1.png",
"image01.png", "image2.png", "image3.png",
"image5.png", "image10.png" }
},
{ {/* actual */ "foo12", "foo123", "foo", "foobar" },
{/* expected*/ "foo", "foo12", "foo123", "foobar" }
},
{ {/* actual */ "123foo", "12foo", "foobar", "foo" },
{/* expected*/ "12foo", "123foo", "foo", "foobar" }
},
{ {/* actual */ "4foo123", "4foo12", "foo", "4foo", "4foobar" },
{/* expected*/ "4foo", "4foo12", "4foo123", "4foobar", "foo" }
}
};
for ( String[][] data : dataSet ) {
try {
final String[] actual = data[0].clone();
final String[] expected = data[1];
Arrays.sort(actual, noc);
assertArrayEquals("Data " + Arrays.toString(data[0]),
expected, actual);
} catch (Exception e ) {
System.out.println( "Data: " + Arrays.deepToString(data)
+ " - Exception Thrown: " + e );
}
}
}
@Test
public final void testCompareNullArray() {
String[][][] dataSet = { // pairs of Strings to compare
/*actual, expected (NullPointerException)*/
{ null, null },
{ {null, "foo" }, null },
{ {"foo", null}, null }
};
for ( String[][] data : dataSet ) {
String[] actual = data[0]==null ? null : data[0].clone();
String[] expected = data[1]==null ? null : data[1];
try {
Arrays.sort(actual, noc);
// It's an error if we get to here:
fail("Data: actual=" + Arrays.toString(data[0])
+ ", expected=" + Arrays.toString(expected)
+ " should throw NullPointerException, "
+ "but resulted in: " + Arrays.deepToString(data));
} catch (NullPointerException e) { } // Expected result.
catch (Exception e) {
fail("Data: actual=" + Arrays.toString(data[0])
+ ", expected=" + Arrays.toString(expected)
+ " should throw NullPointerException, "
+ " but threw \"" + e + "\".");
}
}
}
/** Boundary value tests: sort zero-length array, 1-element array:
*/
@Test
public final void testBoundaryValuesComparisons() {
String[][][] dataSet = { // pairs of Strings to compare
// ACTUAL EXPECTED
{ new String[0], new String[0] }, // array of zero Strings
{ {""}, {""} }, // Array of 1 zero-length String
{ {"foo", ""}, {"", "foo" } },
{ {"foo", "", "bar"}, {"", "bar", "foo" } },
{ {"foo"}, {"foo"} }
};
for ( String[][] data : dataSet ) {
try {
final String[] actual = data[0].clone();
final String[] expected = data[1];
Arrays.sort(actual, noc);
assertArrayEquals("Data " + Arrays.toString(data[0]),
expected, actual);
} catch (Exception e ) {
System.out.println( "Data: " + Arrays.deepToString(data)
+ " - Exception Thrown: " + e );
}
}
}
}