Skip to content

Commit 33c9cfd

Browse files
author
Nathan Marz
committed
Merge remote branch 'samus/Fields'
2 parents 7a9ff26 + 66275c1 commit 33c9cfd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/jvm/backtype/storm/tuple/Fields.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ public Fields(String... fields) {
1717
}
1818

1919
public Fields(List<String> fields) {
20-
_fields = new ArrayList<String>(fields);
20+
_fields = new ArrayList<String>(fields.size());
21+
for (String field : fields) {
22+
if (_fields.contains(field))
23+
throw new IllegalArgumentException(
24+
String.format("duplicate field '%s'", field)
25+
);
26+
_fields.add(field);
27+
}
2128
index();
2229
}
2330

@@ -58,4 +65,4 @@ private void index() {
5865
_index.put(_fields.get(i), i);
5966
}
6067
}
61-
}
68+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(ns backtype.storm.fields-test
2+
(:use [clojure test])
3+
(:import [backtype.storm.tuple Fields])
4+
(:import [java.util List])
5+
(:import [java.util Iterator]))
6+
7+
(deftest test-fields-constructor
8+
(testing "constructor"
9+
(testing "with (String... fields)"
10+
(is (instance? Fields (Fields. (into-array String '("foo" "bar")))))
11+
(is (thrown? IllegalArgumentException (Fields. (into-array String '("foo" "bar" "foo"))))))
12+
(testing "with (List<String> fields)"
13+
(is (instance? Fields (Fields. '("foo" "bar"))))
14+
(is (thrown? IllegalArgumentException (Fields. '("foo" "bar" "foo")))))))
15+
16+
(deftest test-fields-methods
17+
(let [fields (Fields. '("foo" "bar"))]
18+
(testing "method"
19+
(testing ".size"
20+
(is (= (.size fields) 2)))
21+
(testing ".get"
22+
(is (= (.get fields 0) "foo"))
23+
(is (= (.get fields 1) "bar"))
24+
(is (thrown? IndexOutOfBoundsException (.get fields 2))))
25+
(testing ".fieldIndex"
26+
(is (= (.fieldIndex fields "foo") 0))
27+
(is (= (.fieldIndex fields "bar") 1))
28+
(is (thrown? IllegalArgumentException (.fieldIndex fields "baz"))))
29+
(testing ".toList"
30+
(is (instance? List (.toList fields)))
31+
(is (= (count (.toList fields)) 2))
32+
(is (not-any? false? (map = (.toList fields) '("foo" "bar")))))
33+
(testing ".iterator"
34+
(is (instance? Iterator (.iterator fields)))
35+
(is (= (count (iterator-seq (.iterator fields))) 2))
36+
(is (not-any? false? (map = (iterator-seq (.iterator fields)) '("foo" "bar")))))
37+
(testing ".select"
38+
(is (instance? List (.select fields (Fields. '("bar")) '("a" "b" "c"))))
39+
(is (= (.select fields (Fields. '("bar")) '("a" "b" "c")) '("b")))))))
40+

0 commit comments

Comments
 (0)