NatOrderComparatorTestSuite.java

Download NatOrderComparatorTestSuite.java

  1: package com.wpollock.natordercomp.tests;
  2: 
  3: import static org.junit.Assert.*;
  4: 
  5: import com.wpollock.natordercomp.NatOrderComparator;
  6: 
  7: import java.util.Arrays;
  8: import org.junit.Test;
  9: 
 10: /**
 11:  * Unit tests for the Natural Order Comparator (for Strings).
 12:  * These tests use a data-driven approach when multiple tests of the
 13:  * same type (but with different data) is desired.  This approach minimizes
 14:  * the size of each test and reduces the chance of logic errors in the
 15:  * test suite.
 16:  *
 17:  * Each method tests several related sets of arguments.  Note that the first
 18:  * failed test aborts the method, thus skipping the remaining checks.  This
 19:  * was a trade-off I felt was worth while, as long as all the checks in a
 20:  * method checked the same thing, and the exception thrown and the data that
 21:  * caused it were included in the output.
 22:  *
 23:  * @author Wayne Pollock, Tampa Florida USA
 24:  *
 25:  */
 26: public class NatOrderComparatorTestSuite {
 27:     private final NatOrderComparator noc = NatOrderComparator.getInstance();
 28: 
 29:     @Test
 30:     public final void testCompareTextOnly() {
 31:         String[][][] dataSet = { // pairs of Strings to compare
 32:                 //  ACTUAL      EXPECTED
 33:                 { {"A", "B"}, {"A", "B"} },
 34:                 { {"BB", "AA"}, {"AA", "BB"} },
 35:                 { {"C", "B", "A"}, {"A", "B", "C"} },
 36:         };
 37:         for ( String[][] data : dataSet ) {
 38:             try {
 39:                 final String[] actual = data[0].clone();
 40:                 final String[] expected = data[1];
 41:                 Arrays.sort(actual, noc);
 42:                 assertArrayEquals("Data " + Arrays.toString(data[0]),
 43:                         expected, actual);
 44:             } catch (Exception e ) {
 45:                 System.out.println( "Data: " + Arrays.deepToString(data)
 46:                 + " - Exception Thrown: " + e );
 47:             }
 48:         }
 49:     }
 50: 
 51:     /**
 52:      *  Include tests for leading zeros (4 cases), "00", "0", and numbers too
 53:      *  large for an int.
 54:      */
 55:     @Test
 56:     public final void testCompareNumericStringsOnly() {
 57:         String[][][] dataSet = { // pairs of Strings to compare
 58:                 //  ACTUAL      EXPECTED
 59:                 { {"0", "0"}, {"0", "0"} },
 60:                 { {"00", "00"}, {"00", "00"} },
 61:                 { {"0", "00"}, {"0", "00"} },
 62:                 { {"00", "0"}, {"0", "00"} },
 63:                 { {"012", "12"}, {"12", "012"} },
 64:                 { {"34", "034"}, {"34", "034"} },
 65:                 { {"12", "12"}, {"12", "12"} },
 66:                 { {"12", "23", "123"}, {"12", "23", "123"} },
 67:                 { {"456", "23", "1"}, {"1", "23", "456"} },
 68:                 { { "7000000000", "6000000000"}, { "6000000000", "7000000000"} }
 69:         };
 70:         for ( String[][] data : dataSet ) {
 71:             try {
 72:                 final String[] actual = data[0].clone();
 73:                 final String[] expected = data[1];
 74:                 Arrays.sort(actual, noc);
 75:                 assertArrayEquals("Data " + Arrays.toString(data[0]),
 76:                         expected, actual);
 77:             } catch (Exception e ) {
 78:                 System.out.println( "Data: " + Arrays.deepToString(data)
 79:                 + " - Exception Thrown: " + e );
 80:             }
 81:         }
 82:     }
 83: 
 84:     /**
 85:      * Compare alpha to numeric (2 cases), and alphanum to any (6 cases),
 86:      * and non-ASCII alphanums:
 87:      */
 88:     @Test
 89:     public final void testComparMixedStringsOnly() {
 90:         String[][][] dataSet = { // pairs of Strings to compare
 91:                 { /*actual*/     { "image1.png", "image2.png", "image01.png",
 92:                     "image10.png", "image5.png", "image0.png", "image3.png" },
 93:                     /*expected*/   { "image0.png", "image1.png",
 94:                         "image01.png", "image2.png", "image3.png",
 95:                         "image5.png", "image10.png" }
 96:                 },
 97:                 { {/* actual  */ "foo12", "foo123", "foo", "foobar" },
 98:                     {/* expected*/ "foo", "foo12", "foo123", "foobar" }
 99:                 },
100:                 { {/* actual  */ "123foo", "12foo", "foobar", "foo" },
101:                     {/* expected*/ "12foo", "123foo", "foo", "foobar" }
102:                 },
103:                 { {/* actual  */ "4foo123", "4foo12", "foo", "4foo", "4foobar" },
104:                     {/* expected*/ "4foo", "4foo12", "4foo123", "4foobar", "foo" }
105:                 }
106: 
107:         };
108:         for ( String[][] data : dataSet ) {
109:             try {
110:                 final String[] actual = data[0].clone();
111:                 final String[] expected = data[1];
112:                 Arrays.sort(actual, noc);
113:                 assertArrayEquals("Data " + Arrays.toString(data[0]),
114:                         expected, actual);
115:             } catch (Exception e ) {
116:                 System.out.println( "Data: " + Arrays.deepToString(data)
117:                 + " - Exception Thrown: " + e );
118:             }
119:         }
120:     }
121: 
122:     @Test
123:     public final void testCompareNullArray() {
124:         String[][][] dataSet = { // pairs of Strings to compare
125:                 /*actual, expected (NullPointerException)*/
126:                 { null, null },
127:                 { {null, "foo" }, null },
128:                 { {"foo", null}, null }
129:         };
130:         for ( String[][] data : dataSet ) {
131:             String[] actual = data[0]==null ? null : data[0].clone();
132:             String[] expected = data[1]==null ? null : data[1];
133:             try {
134:                 Arrays.sort(actual, noc);
135:                 // It's an error if we get to here:
136:                 fail("Data: actual=" + Arrays.toString(data[0])
137:                 + ", expected=" + Arrays.toString(expected)
138:                 + " should throw NullPointerException, "
139:                 + "but resulted in: " + Arrays.deepToString(data));
140:             } catch (NullPointerException e) { }  // Expected result.
141:             catch (Exception e) {
142:                 fail("Data: actual=" + Arrays.toString(data[0])
143:                 + ", expected=" + Arrays.toString(expected)
144:                 + " should throw NullPointerException, "
145:                 + " but threw \"" + e + "\".");
146:             }
147:         }
148:     }
149: 
150:     /** Boundary value tests: sort zero-length array, 1-element array:
151:      */
152:     @Test
153:     public final void testBoundaryValuesComparisons() {
154:         String[][][] dataSet = { // pairs of Strings to compare
155:                 //  ACTUAL      EXPECTED
156:                 { new String[0], new String[0] }, // array of zero Strings
157:                 { {""}, {""} },  // Array of 1 zero-length String
158:                 { {"foo", ""}, {"", "foo" } },
159:                 { {"foo", "", "bar"}, {"", "bar", "foo" } },
160:                 { {"foo"}, {"foo"} }
161:         };
162:         for ( String[][] data : dataSet ) {
163:             try {
164:                 final String[] actual = data[0].clone();
165:                 final String[] expected = data[1];
166:                 Arrays.sort(actual, noc);
167:                 assertArrayEquals("Data " + Arrays.toString(data[0]),
168:                         expected, actual);
169:             } catch (Exception e ) {
170:                 System.out.println( "Data: " + Arrays.deepToString(data)
171:                 + " - Exception Thrown: " + e );
172:             }
173:         }
174:     }
175: }