[36] | 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 | import static org.junit.Assert.assertFalse; |
---|
| 21 | import static org.junit.Assert.assertTrue; |
---|
| 22 | |
---|
| 23 | import java.io.ByteArrayInputStream; |
---|
| 24 | import java.io.ByteArrayOutputStream; |
---|
| 25 | import java.io.DataInputStream; |
---|
| 26 | import java.io.DataOutputStream; |
---|
| 27 | import java.io.IOException; |
---|
| 28 | |
---|
| 29 | import junit.framework.JUnit4TestAdapter; |
---|
| 30 | |
---|
| 31 | import org.apache.hadoop.io.IntWritable; |
---|
| 32 | import org.apache.hadoop.io.Text; |
---|
| 33 | import org.junit.Test; |
---|
| 34 | |
---|
| 35 | public class TupleTest { |
---|
| 36 | |
---|
| 37 | public static final Schema SCHEMA1 = new Schema(); |
---|
| 38 | static { |
---|
| 39 | SCHEMA1.addField("field0", String.class, "default"); |
---|
| 40 | SCHEMA1.addField("field1", Boolean.class, true); |
---|
| 41 | SCHEMA1.addField("field2", Integer.class, new Integer(1)); |
---|
| 42 | SCHEMA1.addField("field3", Long.class, new Long(2)); |
---|
| 43 | SCHEMA1.addField("field4", Float.class, new Float(2.5)); |
---|
| 44 | SCHEMA1.addField("field5", Double.class, new Double(3.14)); |
---|
| 45 | SCHEMA1.addField("field6", String.class, "test"); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | // next few cases tests invalid field names: should throw exceptions |
---|
| 49 | @Test(expected = TupleException.class) |
---|
| 50 | public void testAccessNonExistentField1() throws IOException { |
---|
| 51 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 52 | |
---|
| 53 | tuple.get("FIELD"); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | @Test(expected = TupleException.class) |
---|
| 57 | public void testAccessNonExistentField2() throws IOException { |
---|
| 58 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 59 | |
---|
| 60 | tuple.getSymbol("FIELD"); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | @Test(expected = TupleException.class) |
---|
| 64 | public void testAccessNonExistentField3() throws IOException { |
---|
| 65 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 66 | |
---|
| 67 | tuple.set("Field0", "test"); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | @Test(expected = TupleException.class) |
---|
| 71 | public void testAccessNonExistentField4() throws IOException { |
---|
| 72 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 73 | |
---|
| 74 | tuple.setSymbol("Field0", "test"); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | @Test(expected = TupleException.class) |
---|
| 78 | public void testAccessNonExistentField5() throws IOException { |
---|
| 79 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 80 | |
---|
| 81 | tuple.containsSymbol("Field0"); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | @Test(expected = TupleException.class) |
---|
| 85 | public void testAccessNonExistentField6() throws IOException { |
---|
| 86 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 87 | |
---|
| 88 | tuple.getFieldType("Field0"); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // can't set fields to null |
---|
| 92 | @Test(expected = TupleException.class) |
---|
| 93 | public void testSetNull1() throws IOException { |
---|
| 94 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 95 | |
---|
| 96 | tuple.set(0, null); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | // can't set symbol as null |
---|
| 100 | @Test(expected = TupleException.class) |
---|
| 101 | public void testSetNull2() throws IOException { |
---|
| 102 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 103 | |
---|
| 104 | tuple.setSymbol(0, null); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // mismatch in field type |
---|
| 108 | @Test(expected = TupleException.class) |
---|
| 109 | public void testSetWrongType() throws IOException { |
---|
| 110 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 111 | |
---|
| 112 | tuple.set(0, 1); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | @Test |
---|
| 116 | public void testHashCode() { |
---|
| 117 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 118 | |
---|
| 119 | tuple.setSymbol(0, "*"); |
---|
| 120 | int hash1 = tuple.hashCode(); |
---|
| 121 | |
---|
| 122 | tuple.set(0, "sample"); |
---|
| 123 | int hash2 = tuple.hashCode(); |
---|
| 124 | |
---|
| 125 | assertTrue(hash1 != hash2); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | @Test |
---|
| 129 | public void testSorting() { |
---|
| 130 | |
---|
| 131 | Tuple tuple1 = SCHEMA1.instantiate(); |
---|
| 132 | Tuple tuple2 = SCHEMA1.instantiate(); |
---|
| 133 | |
---|
| 134 | assertEquals(tuple1.compareTo(tuple2), 0); |
---|
| 135 | |
---|
| 136 | tuple1.set("field0", "a"); |
---|
| 137 | tuple1.set("field0", "b"); |
---|
| 138 | |
---|
| 139 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 140 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 141 | |
---|
| 142 | tuple1.set("field1", true); |
---|
| 143 | tuple1.set("field1", false); |
---|
| 144 | |
---|
| 145 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 146 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 147 | |
---|
| 148 | tuple1.set("field2", 1); |
---|
| 149 | tuple1.set("field2", 2); |
---|
| 150 | |
---|
| 151 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 152 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 153 | |
---|
| 154 | tuple1.set("field3", 1L); |
---|
| 155 | tuple1.set("field3", 2L); |
---|
| 156 | |
---|
| 157 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 158 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 159 | |
---|
| 160 | tuple1.set("field4", 1.0f); |
---|
| 161 | tuple1.set("field4", 2.0f); |
---|
| 162 | |
---|
| 163 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 164 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 165 | |
---|
| 166 | tuple1.set("field5", 1.0d); |
---|
| 167 | tuple1.set("field5", 2.0d); |
---|
| 168 | |
---|
| 169 | assertTrue(tuple1.compareTo(tuple2) < 0); |
---|
| 170 | assertTrue(tuple2.compareTo(tuple1) > 0); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // tests unpacking of default values |
---|
| 174 | @Test |
---|
| 175 | public void testSerializeDefaultValues() throws IOException { |
---|
| 176 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 177 | |
---|
| 178 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
| 179 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
| 180 | |
---|
| 181 | tuple.write(dataOut); |
---|
| 182 | |
---|
| 183 | Tuple t = Tuple.createFrom(new DataInputStream( |
---|
| 184 | new ByteArrayInputStream(bytesOut.toByteArray()))); |
---|
| 185 | |
---|
| 186 | assertEquals(t.get(0), "default"); |
---|
| 187 | assertEquals(t.get(1), true); |
---|
| 188 | assertEquals(t.get(2), new Integer(1)); |
---|
| 189 | assertEquals(t.get(3), new Long(2)); |
---|
| 190 | assertEquals(t.get(4), new Float(2.5)); |
---|
| 191 | assertEquals(t.get(5), new Double(3.14)); |
---|
| 192 | assertEquals(t.get(6), "test"); |
---|
| 193 | |
---|
| 194 | assertEquals(t.get("field0"), "default"); |
---|
| 195 | assertEquals(t.get("field1"), true); |
---|
| 196 | assertEquals(t.get("field2"), new Integer(1)); |
---|
| 197 | assertEquals(t.get("field3"), new Long(2)); |
---|
| 198 | assertEquals(t.get("field4"), new Float(2.5)); |
---|
| 199 | assertEquals(t.get("field5"), new Double(3.14)); |
---|
| 200 | assertEquals(t.get("field6"), "test"); |
---|
| 201 | |
---|
| 202 | assertEquals(t.getFieldType(0), String.class); |
---|
| 203 | assertEquals(t.getFieldType(1), Boolean.class); |
---|
| 204 | assertEquals(t.getFieldType(2), Integer.class); |
---|
| 205 | assertEquals(t.getFieldType(3), Long.class); |
---|
| 206 | assertEquals(t.getFieldType(4), Float.class); |
---|
| 207 | assertEquals(t.getFieldType(5), Double.class); |
---|
| 208 | assertEquals(t.getFieldType(6), String.class); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | @Test |
---|
| 212 | public void testSerializeInstantiatedValues() throws IOException { |
---|
| 213 | Tuple tuple = SCHEMA1.instantiate("Hello world!", false, |
---|
| 214 | new Integer(5), new Long(3), new Float(1.2), new Double(2.871), |
---|
| 215 | "another string"); |
---|
| 216 | |
---|
| 217 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
| 218 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
| 219 | |
---|
| 220 | tuple.write(dataOut); |
---|
| 221 | |
---|
| 222 | Tuple t = Tuple.createFrom(new DataInputStream( |
---|
| 223 | new ByteArrayInputStream(bytesOut.toByteArray()))); |
---|
| 224 | |
---|
| 225 | assertEquals(t.get(0), "Hello world!"); |
---|
| 226 | assertEquals(t.get(1), false); |
---|
| 227 | assertEquals(t.get(2), new Integer(5)); |
---|
| 228 | assertEquals(t.get(3), new Long(3)); |
---|
| 229 | assertEquals(t.get(4), new Float(1.2)); |
---|
| 230 | assertEquals(t.get(5), new Double(2.871)); |
---|
| 231 | assertEquals(t.get(6), "another string"); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | @Test |
---|
| 235 | public void testSerializeSetValues() throws IOException { |
---|
| 236 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 237 | |
---|
| 238 | tuple.set(0, "Hello world!"); |
---|
| 239 | tuple.set(1, false); |
---|
| 240 | tuple.set(2, new Integer(5)); |
---|
| 241 | tuple.set(3, new Long(3)); |
---|
| 242 | tuple.set(4, new Float(1.2)); |
---|
| 243 | tuple.set(5, new Double(2.871)); |
---|
| 244 | tuple.set(6, "another string"); |
---|
| 245 | |
---|
| 246 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
| 247 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
| 248 | |
---|
| 249 | tuple.write(dataOut); |
---|
| 250 | |
---|
| 251 | Tuple t = Tuple.createFrom(new DataInputStream( |
---|
| 252 | new ByteArrayInputStream(bytesOut.toByteArray()))); |
---|
| 253 | |
---|
| 254 | assertEquals(t.get(0), "Hello world!"); |
---|
| 255 | assertEquals(t.get(1), false); |
---|
| 256 | assertEquals(t.get(2), new Integer(5)); |
---|
| 257 | assertEquals(t.get(3), new Long(3)); |
---|
| 258 | assertEquals(t.get(4), new Float(1.2)); |
---|
| 259 | assertEquals(t.get(5), new Double(2.871)); |
---|
| 260 | assertEquals(t.get(6), "another string"); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | @Test |
---|
| 264 | public void testToString() throws IOException { |
---|
| 265 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 266 | |
---|
| 267 | assertEquals(tuple.toString(), "(default, true, 1, 2, 2.5, 3.14, test)"); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | @Test |
---|
| 271 | public void testSymbols() throws IOException { |
---|
| 272 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 273 | |
---|
| 274 | assertFalse(tuple.containsSymbol(0)); |
---|
| 275 | assertFalse(tuple.containsSymbol(1)); |
---|
| 276 | assertFalse(tuple.containsSymbol(2)); |
---|
| 277 | assertFalse(tuple.containsSymbol(3)); |
---|
| 278 | assertFalse(tuple.containsSymbol(4)); |
---|
| 279 | assertFalse(tuple.containsSymbol(5)); |
---|
| 280 | assertFalse(tuple.containsSymbol(6)); |
---|
| 281 | |
---|
| 282 | assertFalse(tuple.containsSymbol("field0")); |
---|
| 283 | assertFalse(tuple.containsSymbol("field1")); |
---|
| 284 | assertFalse(tuple.containsSymbol("field2")); |
---|
| 285 | assertFalse(tuple.containsSymbol("field3")); |
---|
| 286 | assertFalse(tuple.containsSymbol("field4")); |
---|
| 287 | assertFalse(tuple.containsSymbol("field5")); |
---|
| 288 | assertFalse(tuple.containsSymbol("field6")); |
---|
| 289 | |
---|
| 290 | tuple.setSymbol(0, "*"); |
---|
| 291 | tuple.setSymbol("field1", "*"); |
---|
| 292 | |
---|
| 293 | assertTrue(tuple.containsSymbol(0)); |
---|
| 294 | assertTrue(tuple.containsSymbol(1)); |
---|
| 295 | assertFalse(tuple.containsSymbol(2)); |
---|
| 296 | assertFalse(tuple.containsSymbol(3)); |
---|
| 297 | assertFalse(tuple.containsSymbol(4)); |
---|
| 298 | assertFalse(tuple.containsSymbol(5)); |
---|
| 299 | assertFalse(tuple.containsSymbol(6)); |
---|
| 300 | |
---|
| 301 | assertEquals(tuple.get(0), null); |
---|
| 302 | assertEquals(tuple.getSymbol(0), "*"); |
---|
| 303 | |
---|
| 304 | assertEquals(tuple.get(1), null); |
---|
| 305 | assertEquals(tuple.getSymbol(1), "*"); |
---|
| 306 | |
---|
| 307 | assertEquals(tuple.getFieldType(0), String.class); |
---|
| 308 | assertEquals(tuple.getFieldType(1), Boolean.class); |
---|
| 309 | |
---|
| 310 | assertEquals(tuple.toString(), "(*, *, 1, 2, 2.5, 3.14, test)"); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | @Test |
---|
| 314 | public void testSerializeSymbols() throws IOException { |
---|
| 315 | Tuple tuple = SCHEMA1.instantiate(); |
---|
| 316 | |
---|
| 317 | tuple.setSymbol(0, "*"); |
---|
| 318 | tuple.setSymbol("field1", "*"); |
---|
| 319 | |
---|
| 320 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
| 321 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
| 322 | |
---|
| 323 | tuple.write(dataOut); |
---|
| 324 | |
---|
| 325 | Tuple t = Tuple.createFrom(new DataInputStream( |
---|
| 326 | new ByteArrayInputStream(bytesOut.toByteArray()))); |
---|
| 327 | |
---|
| 328 | assertTrue(t.containsSymbol(0)); |
---|
| 329 | assertTrue(t.containsSymbol(1)); |
---|
| 330 | assertFalse(t.containsSymbol(2)); |
---|
| 331 | assertFalse(t.containsSymbol(3)); |
---|
| 332 | assertFalse(t.containsSymbol(4)); |
---|
| 333 | assertFalse(t.containsSymbol(5)); |
---|
| 334 | assertFalse(t.containsSymbol(6)); |
---|
| 335 | |
---|
| 336 | assertEquals(t.get(0), null); |
---|
| 337 | assertEquals(t.getSymbol(0), "*"); |
---|
| 338 | |
---|
| 339 | assertEquals(t.get(1), null); |
---|
| 340 | assertEquals(t.getSymbol(1), "*"); |
---|
| 341 | |
---|
| 342 | assertEquals(tuple.getFieldType(0), String.class); |
---|
| 343 | assertEquals(tuple.getFieldType(1), Boolean.class); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | public static final Schema SCHEMA2 = new Schema(); |
---|
| 347 | static { |
---|
| 348 | SCHEMA2.addField("field0", Integer.class, 0); |
---|
| 349 | SCHEMA2.addField("field1", IntWritable.class, new IntWritable(0)); |
---|
| 350 | SCHEMA2.addField("field2", Text.class, new Text("default")); |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | @Test |
---|
| 354 | public void testSerializeWritableFields() throws IOException { |
---|
| 355 | Tuple tuple = SCHEMA2.instantiate(); |
---|
| 356 | |
---|
| 357 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
| 358 | DataOutputStream dataOut = new DataOutputStream(bytesOut); |
---|
| 359 | |
---|
| 360 | tuple.write(dataOut); |
---|
| 361 | |
---|
| 362 | Tuple t = Tuple.createFrom(new DataInputStream( |
---|
| 363 | new ByteArrayInputStream(bytesOut.toByteArray()))); |
---|
| 364 | |
---|
| 365 | assertEquals(t.get(0), 0); |
---|
| 366 | assertEquals(t.get(1), new IntWritable(0)); |
---|
| 367 | assertEquals(t.get(2), new Text("default")); |
---|
| 368 | |
---|
| 369 | assertEquals(t.getFieldType(0), Integer.class); |
---|
| 370 | assertEquals(t.getFieldType(1), IntWritable.class); |
---|
| 371 | assertEquals(t.getFieldType(2), Text.class); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | public static junit.framework.Test suite() { |
---|
| 375 | return new JUnit4TestAdapter(TupleTest.class); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | } |
---|