View Javadoc

    1   package org.archive.crawler.util;
    2
    3   import java.util.ArrayList;
    4   import java.util.Arrays;
    5   import java.util.Collection;
    6   import java.util.HashSet;
    7   import java.util.List;
    8   import java.util.Random;
    9   import java.util.Set;
    10
    11  import junit.framework.TestCase;
    12
    13  /***
    14   * Tests the {@link dex.misc.Transform} class.
    15   */
    16  public class TransformTest extends TestCase {
    17
    18      // Convert integers to strings, eliminating negative numbers
    19      private static class PositiveToString
    20      implements Transformer<Integer, String> {
    21          public String transform(Integer i) {
    22              if (i < 0) {
    23                  return null;
    24              }
    25              return i.toString();
    26          }
    27      }
    28
    29      /***
    30       * Tests using a simple Transformer.  The Transformer changes
    31       * positive integers into strings.  The test sets up a
    32       * list of random integers, remembering which ones are
    33       * positive.  The Transform is created, and the Transform's
    34       * contents are compared against the list of remembered positive
    35       * integers.
    36       */
    37      public void testTransform() {
    38          Transformer<Integer,String> transformer = new PositiveToString();
    39
    40          // Transform of an empty collection should be empty.
    41          List<Integer> empty = new ArrayList<Integer>();
    42          assertTrue(new Transform<Integer,String>(empty, transformer).isEmpty());
    43
    44          // Some simple test data.
    45          Integer[] testData = new Integer[] { -5, 3, 2, -11, 0, 111, -161 };
    46          String[] expected = new String[] { "3", "2", "0", "111" };
    47          List<Integer> list = Arrays.asList(testData);
    48          Transform<Integer,String> c = new Transform<Integer,String>(list,
    49                  transformer);
    50          List<String> expectedList = Arrays.asList(expected);
    51          assertEquals(new ArrayList<String>(c), expectedList);
    52
    53          // Same test as above, with random data
    54          for (int i = 0; i < 100; i++) {
    55              randomTest();
    56          }
    57      }
    58
    59      private void randomTest() {
    60          Transformer<Integer, String> transformer = new PositiveToString();
    61          Random random = new Random();
    62          int max = random.nextInt(1024) + 10;
    63          List<Integer> testData = new ArrayList<Integer>(max);
    64          List<String> expected = new ArrayList<String>(max);
    65          for (int i = 0; i < max; i++) {
    66              int e = random.nextInt();
    67              testData.add(e);
    68              if (e >= 0) {
    69                  expected.add(Integer.toString(e));
    70              }
    71          }
    72
    73          Transform<Integer,String> c = new Transform<Integer,String>(testData,
    74                  transformer);
    75          List<String> results = new ArrayList<String>(c);
    76          assertEquals(expected, results);
    77      }
    78
    79      /***
    80       * Tests the static subclasses method.  The test sets up a list of
    81       * Number instances that may contain random Double, Float, Integer
    82       * or Long values.  The Long values are remembered.  The subclasses
    83       * method is used to create a Transform containing only the Long
    84       * values.  The Transform is compared against the list of remembered
    85       * Long values.
    86       */
    87      public void testSubclasses() {
    88          Random random = new Random();
    89          for (int i = 0; i < 100; i++) {
    90              int max = random.nextInt(1024) + 10;
    91              List<Number> testData = new ArrayList<Number>(max);
    92              List<Long> expected = new ArrayList<Long>(max);
    93              for (int j = 0; j < max; j++) {
    94                  int v = random.nextInt(4);
    95                  switch (v) {
    96                  case 0:
    97                      long l = random.nextLong();
    98                      testData.add(l);
    99                      expected.add(l);
    100                     break;
    101                 case 1:
    102                     testData.add(random.nextInt());
    103                     break;
    104                 case 2:
    105                     testData.add(random.nextDouble());
    106                     break;
    107                 case 3:
    108                     testData.add(random.nextFloat());
    109                     break;
    110                 }
    111             }
    112             Collection<Long> c = Transform.subclasses(testData, Long.class);
    113             List<Long> results = new ArrayList<Long>(c);
    114             assertEquals(expected, results);
    115         }
    116     }
    117
    118     public void testSingleton() {
    119         Set<Number> set = new HashSet<Number>();
    120         set.add(3);
    121         Collection<Integer> c = Transform.subclasses(set, Integer.class);
    122         for (Integer i : c) {
    123             System.out.println(i);
    124         }
    125     }
    126 }