1 | /* |
---|
2 | * Cloud9: A MapReduce Library for Hadoop |
---|
3 | * |
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
---|
5 | * may not use this file except in compliance with the License. You may |
---|
6 | * obtain a copy of the License at |
---|
7 | * |
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
9 | * |
---|
10 | * Unless required by applicable law or agreed to in writing, software |
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
---|
13 | * implied. See the License for the specific language governing |
---|
14 | * permissions and limitations under the License. |
---|
15 | */ |
---|
16 | |
---|
17 | package tw.org.nchc.tuple; |
---|
18 | |
---|
19 | import static org.junit.Assert.assertEquals; |
---|
20 | |
---|
21 | import java.util.HashMap; |
---|
22 | |
---|
23 | import junit.framework.JUnit4TestAdapter; |
---|
24 | |
---|
25 | import org.apache.hadoop.io.IntWritable; |
---|
26 | import org.apache.hadoop.io.Text; |
---|
27 | import org.apache.hadoop.io.Writable; |
---|
28 | import org.junit.Test; |
---|
29 | |
---|
30 | public class SchemaTest { |
---|
31 | |
---|
32 | public static final Schema SCHEMA1 = new Schema(); |
---|
33 | static { |
---|
34 | SCHEMA1.addField("field1", String.class, "default"); |
---|
35 | SCHEMA1.addField("field2", Integer.class, new Integer(1)); |
---|
36 | } |
---|
37 | |
---|
38 | @Test |
---|
39 | public void test1() { |
---|
40 | Tuple tuple = SCHEMA1.instantiate(); |
---|
41 | |
---|
42 | assertEquals(tuple.get(0), "default"); |
---|
43 | assertEquals(tuple.get(1), new Integer(1)); |
---|
44 | |
---|
45 | assertEquals(tuple.get("field1"), "default"); |
---|
46 | assertEquals(tuple.get("field2"), new Integer(1)); |
---|
47 | } |
---|
48 | |
---|
49 | @Test |
---|
50 | public void test2() { |
---|
51 | Tuple tuple = SCHEMA1.instantiate("Hello world!", new Integer(5)); |
---|
52 | assertEquals(tuple.get(0), "Hello world!"); |
---|
53 | assertEquals(tuple.get(1), new Integer(5)); |
---|
54 | } |
---|
55 | |
---|
56 | @Test(expected = SchemaException.class) |
---|
57 | public void testIllegalFieldsException1() { |
---|
58 | Schema schema = new Schema(); |
---|
59 | schema.addField("field0", Integer.class, 0); |
---|
60 | schema.addField("field1", HashMap.class, null); |
---|
61 | } |
---|
62 | |
---|
63 | @Test(expected = SchemaException.class) |
---|
64 | public void testIllegalFieldsException2() { |
---|
65 | Schema schema = new Schema(); |
---|
66 | schema.addField("field0", Integer.class, 0); |
---|
67 | // throws exception because Writable isn't a concrete class |
---|
68 | schema.addField("field1", Writable.class, null); |
---|
69 | } |
---|
70 | |
---|
71 | @Test |
---|
72 | public void testWritableFields() { |
---|
73 | Schema schema = new Schema(); |
---|
74 | schema.addField("field0", Integer.class, 0); |
---|
75 | schema.addField("field1", IntWritable.class, new IntWritable(0)); |
---|
76 | schema.addField("field2", Text.class, new Text("default")); |
---|
77 | |
---|
78 | Tuple t = schema.instantiate(); |
---|
79 | assertEquals(t.get(0), 0); |
---|
80 | assertEquals(t.get(1), new IntWritable(0)); |
---|
81 | assertEquals(t.get(2), new Text("default")); |
---|
82 | } |
---|
83 | |
---|
84 | public static junit.framework.Test suite() { |
---|
85 | return new JUnit4TestAdapter(SchemaTest.class); |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|