001: package org.archive.crawler.util;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.Collection;
006: import java.util.HashSet;
007: import java.util.List;
008: import java.util.Random;
009: import java.util.Set;
010:
011: import junit.framework.TestCase;
012:
013: /**
014: * Tests the {@link dex.misc.Transform} class.
015: */
016: public class TransformTest extends TestCase {
017:
018: // Convert integers to strings, eliminating negative numbers
019: private static class PositiveToString implements
020: Transformer<Integer, String> {
021: public String transform(Integer i) {
022: if (i < 0) {
023: return null;
024: }
025: return i.toString();
026: }
027: }
028:
029: /**
030: * Tests using a simple Transformer. The Transformer changes
031: * positive integers into strings. The test sets up a
032: * list of random integers, remembering which ones are
033: * positive. The Transform is created, and the Transform's
034: * contents are compared against the list of remembered positive
035: * integers.
036: */
037: public void testTransform() {
038: Transformer<Integer, String> transformer = new PositiveToString();
039:
040: // Transform of an empty collection should be empty.
041: List<Integer> empty = new ArrayList<Integer>();
042: assertTrue(new Transform<Integer, String>(empty, transformer)
043: .isEmpty());
044:
045: // Some simple test data.
046: Integer[] testData = new Integer[] { -5, 3, 2, -11, 0, 111,
047: -161 };
048: String[] expected = new String[] { "3", "2", "0", "111" };
049: List<Integer> list = Arrays.asList(testData);
050: Transform<Integer, String> c = new Transform<Integer, String>(
051: list, transformer);
052: List<String> expectedList = Arrays.asList(expected);
053: assertEquals(new ArrayList<String>(c), expectedList);
054:
055: // Same test as above, with random data
056: for (int i = 0; i < 100; i++) {
057: randomTest();
058: }
059: }
060:
061: private void randomTest() {
062: Transformer<Integer, String> transformer = new PositiveToString();
063: Random random = new Random();
064: int max = random.nextInt(1024) + 10;
065: List<Integer> testData = new ArrayList<Integer>(max);
066: List<String> expected = new ArrayList<String>(max);
067: for (int i = 0; i < max; i++) {
068: int e = random.nextInt();
069: testData.add(e);
070: if (e >= 0) {
071: expected.add(Integer.toString(e));
072: }
073: }
074:
075: Transform<Integer, String> c = new Transform<Integer, String>(
076: testData, transformer);
077: List<String> results = new ArrayList<String>(c);
078: assertEquals(expected, results);
079: }
080:
081: /**
082: * Tests the static subclasses method. The test sets up a list of
083: * Number instances that may contain random Double, Float, Integer
084: * or Long values. The Long values are remembered. The subclasses
085: * method is used to create a Transform containing only the Long
086: * values. The Transform is compared against the list of remembered
087: * Long values.
088: */
089: public void testSubclasses() {
090: Random random = new Random();
091: for (int i = 0; i < 100; i++) {
092: int max = random.nextInt(1024) + 10;
093: List<Number> testData = new ArrayList<Number>(max);
094: List<Long> expected = new ArrayList<Long>(max);
095: for (int j = 0; j < max; j++) {
096: int v = random.nextInt(4);
097: switch (v) {
098: case 0:
099: long l = random.nextLong();
100: testData.add(l);
101: expected.add(l);
102: break;
103: case 1:
104: testData.add(random.nextInt());
105: break;
106: case 2:
107: testData.add(random.nextDouble());
108: break;
109: case 3:
110: testData.add(random.nextFloat());
111: break;
112: }
113: }
114: Collection<Long> c = Transform.subclasses(testData,
115: Long.class);
116: List<Long> results = new ArrayList<Long>(c);
117: assertEquals(expected, results);
118: }
119: }
120:
121: public void testSingleton() {
122: Set<Number> set = new HashSet<Number>();
123: set.add(3);
124: Collection<Integer> c = Transform
125: .subclasses(set, Integer.class);
126: for (Integer i : c) {
127: System.out.println(i);
128: }
129: }
130: }
|