PageRenderTime 29ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.1-rc2/hive/external/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/TestUnionStructObjectInspector.java

#
Java | 121 lines | 84 code | 13 blank | 24 comment | 5 complexity | ca0950cc32111a840eb3300235c802dd MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hadoop.hive.serde2.objectinspector;
 19
 20import java.util.ArrayList;
 21import java.util.Arrays;
 22import java.util.List;
 23
 24import junit.framework.TestCase;
 25
 26import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 27import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 28
 29/**
 30 * TestUnionStructObjectInspector.
 31 *
 32 */
 33public class TestUnionStructObjectInspector extends TestCase {
 34
 35  public void testUnionStructObjectInspector() throws Throwable {
 36    try {
 37      ArrayList<String> fieldNames1 = new ArrayList<String>();
 38      fieldNames1.add("firstInteger");
 39      fieldNames1.add("secondString");
 40      fieldNames1.add("thirdBoolean");
 41      ArrayList<ObjectInspector> fieldObjectInspectors1 = new ArrayList<ObjectInspector>();
 42      fieldObjectInspectors1
 43          .add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
 44      fieldObjectInspectors1
 45          .add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
 46      fieldObjectInspectors1
 47          .add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
 48      StandardStructObjectInspector soi1 = ObjectInspectorFactory
 49          .getStandardStructObjectInspector(fieldNames1, fieldObjectInspectors1);
 50
 51      ArrayList<String> fieldNames2 = new ArrayList<String>();
 52      fieldNames2.add("fourthDouble");
 53      fieldNames2.add("fifthLong");
 54      ArrayList<ObjectInspector> fieldObjectInspectors2 = new ArrayList<ObjectInspector>();
 55      fieldObjectInspectors2
 56          .add(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector);
 57      fieldObjectInspectors2
 58          .add(PrimitiveObjectInspectorFactory.javaLongObjectInspector);
 59      StandardStructObjectInspector soi2 = ObjectInspectorFactory
 60          .getStandardStructObjectInspector(fieldNames2, fieldObjectInspectors2);
 61
 62      UnionStructObjectInspector usoi1 = ObjectInspectorFactory
 63          .getUnionStructObjectInspector(Arrays
 64          .asList(new StructObjectInspector[] {soi1, soi2}));
 65      UnionStructObjectInspector usoi2 = ObjectInspectorFactory
 66          .getUnionStructObjectInspector(Arrays
 67          .asList(new StructObjectInspector[] {soi1, soi2}));
 68
 69      assertEquals(usoi1, usoi2);
 70
 71      // metadata
 72      assertEquals(Category.STRUCT, usoi1.getCategory());
 73      List<? extends StructField> fields = usoi1.getAllStructFieldRefs();
 74      assertEquals(5, fields.size());
 75      for (int i = 0; i < 5; i++) {
 76        if (i <= 2) {
 77          assertEquals(fieldNames1.get(i).toLowerCase(), fields.get(i)
 78              .getFieldName());
 79          assertEquals(fieldObjectInspectors1.get(i), fields.get(i)
 80              .getFieldObjectInspector());
 81        } else {
 82          assertEquals(fieldNames2.get(i - 3).toLowerCase(), fields.get(i)
 83              .getFieldName());
 84          assertEquals(fieldObjectInspectors2.get(i - 3), fields.get(i)
 85              .getFieldObjectInspector());
 86        }
 87      }
 88      assertEquals(fields.get(1), usoi1.getStructFieldRef("secondString"));
 89      assertEquals(fields.get(4), usoi1.getStructFieldRef("fifthLong"));
 90
 91      // null
 92      for (int i = 0; i < 5; i++) {
 93        assertNull(usoi1.getStructFieldData(null, fields.get(i)));
 94      }
 95
 96      // real struct
 97      ArrayList<Object> struct1 = new ArrayList<Object>(3);
 98      struct1.add(1);
 99      struct1.add("two");
100      struct1.add(true);
101      ArrayList<Object> struct2 = new ArrayList<Object>(2);
102      struct2.add(1.0);
103      struct2.add(new Long(111));
104      ArrayList<Object> struct = new ArrayList<Object>(2);
105      struct.add(struct1);
106      struct.add(struct2);
107      ArrayList<Object> all = new ArrayList<Object>(5);
108      all.addAll(struct1);
109      all.addAll(struct2);
110
111      for (int i = 0; i < 5; i++) {
112        assertEquals(all.get(i), usoi1
113            .getStructFieldData(struct, fields.get(i)));
114      }
115    } catch (Throwable e) {
116      e.printStackTrace();
117      throw e;
118    }
119  }
120
121}