瀏覽代碼

java: etl tests and example implementation

Use Guava in the tests to make it easier to build up
maps needed for testing the API.
Emil Sit 12 年之前
父節點
當前提交
e53508827a

+ 2
- 0
assignments/java/etl/build.gradle 查看文件

8
 
8
 
9
 dependencies {
9
 dependencies {
10
   testCompile "junit:junit:4.10"
10
   testCompile "junit:junit:4.10"
11
+  testCompile "org.easytesting:fest-assert-core:2.0M10"
12
+  testCompile "com.google.guava:guava:16+"
11
 }
13
 }

+ 15
- 0
assignments/java/etl/example.java 查看文件

1
+import java.util.HashMap;
2
+import java.util.List;
3
+import java.util.Map;
4
+
5
+public class Etl {
6
+    public Map<String, Integer> transform(Map<Integer, List<String>> old) {
7
+        final Map<String, Integer> result = new HashMap<String, Integer>();
8
+        for (Map.Entry<Integer, List<String>> e : old.entrySet()) {
9
+            for (String s : e.getValue()) {
10
+                result.put(s.toLowerCase(), e.getKey());
11
+            }
12
+        }
13
+        return result;
14
+    }
15
+}

+ 74
- 0
assignments/java/etl/src/test/java/EtlTest.java 查看文件

1
+import com.google.common.collect.ImmutableMap;
2
+import org.junit.Test;
3
+
4
+import java.util.Arrays;
5
+import java.util.List;
6
+import java.util.Map;
7
+
8
+import static org.fest.assertions.api.Assertions.assertThat;
9
+
10
+public class EtlTest {
11
+    private final Etl etl = new Etl();
12
+
13
+    @Test
14
+    public void testTransformOneValue() {
15
+        Map<Integer, List<String>> old = ImmutableMap.of(1, Arrays.asList("A"));
16
+        Map<String, Integer> expected = ImmutableMap.of("a", 1);
17
+
18
+        assertThat(etl.transform(old)).isEqualTo(expected);
19
+    }
20
+
21
+    @Test
22
+    public void testTransformMoreValues() {
23
+        Map<Integer, List<String>> old = ImmutableMap.of(
24
+                1, Arrays.asList("A", "E", "I", "O", "U")
25
+        );
26
+        Map<String, Integer> expected = ImmutableMap.of(
27
+                "a", 1,
28
+                "e", 1,
29
+                "i", 1,
30
+                "o", 1,
31
+                "u", 1
32
+        );
33
+
34
+        assertThat(etl.transform(old)).isEqualTo(expected);
35
+    }
36
+
37
+    @Test
38
+    public void testMoreKeys() {
39
+        Map<Integer, List<String>> old = ImmutableMap.of(
40
+                1, Arrays.asList("A", "E"),
41
+                2, Arrays.asList("D", "G")
42
+        );
43
+        Map<String, Integer> expected = ImmutableMap.of(
44
+                "a", 1,
45
+                "e", 1,
46
+                "d", 2,
47
+                "g", 2
48
+        );
49
+
50
+        assertThat(etl.transform(old)).isEqualTo(expected);
51
+    }
52
+
53
+    @Test
54
+    public void testFullDataset() {
55
+        Map<Integer, List<String>> old = ImmutableMap.<Integer, List<String>>builder().
56
+                put(1, Arrays.asList("A", "E", "I", "O", "U", "L", "N", "R", "S", "T")).
57
+                put(2, Arrays.asList("D", "G")).
58
+                put(3, Arrays.asList("B", "C", "M", "P")).
59
+                put(4, Arrays.asList("F", "H", "V", "W", "Y")).
60
+                put(5, Arrays.asList("K")).
61
+                put(8, Arrays.asList("J", "X")).
62
+                put(10, Arrays.asList("Q", "Z")).
63
+                build();
64
+        Map<String, Integer> expected = ImmutableMap.<String, Integer>builder().
65
+                put("a", 1).put("b", 3).put("c", 3).put("d", 2).put("e", 1).
66
+                put("f", 4).put("g", 2).put("h", 4).put("i", 1).put("j", 8).
67
+                put("k", 5).put("l", 1).put("m", 3).put("n", 1).put("o", 1).
68
+                put("p", 3).put("q", 10).put("r", 1).put("s", 1).put("t", 1).
69
+                put("u", 1).put("v", 4).put("w", 4).put("x", 8).put("y", 4).
70
+                put("z", 10).build();
71
+
72
+        assertThat(etl.transform(old)).isEqualTo(expected);
73
+    }
74
+}