PageRenderTime 44ms CodeModel.GetById 18ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 112 lines | 74 code | 11 blank | 27 comment | 2 complexity | ee61d7da144dbbbabc19989aa8c9d004 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;
 28import org.apache.hadoop.hive.serde2.thrift.test.Complex;
 29import org.apache.hadoop.hive.serde2.thrift.test.IntString;
 30
 31/**
 32 * TestObjectInspectorUtils.
 33 *
 34 */
 35public class TestObjectInspectorUtils extends TestCase {
 36
 37  public void testObjectInspectorUtils() throws Throwable {
 38    try {
 39      ObjectInspector oi1 = ObjectInspectorFactory
 40          .getReflectionObjectInspector(Complex.class,
 41          ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
 42
 43      // metadata
 44      assertEquals(Category.STRUCT, oi1.getCategory());
 45      // standard ObjectInspector
 46      StructObjectInspector soi = (StructObjectInspector) ObjectInspectorUtils
 47          .getStandardObjectInspector(oi1);
 48      List<? extends StructField> fields = soi.getAllStructFieldRefs();
 49      assertEquals(6, fields.size());
 50      assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
 51
 52      // null
 53      for (int i = 0; i < fields.size(); i++) {
 54        assertNull(soi.getStructFieldData(null, fields.get(i)));
 55      }
 56
 57      // real object
 58      Complex cc = new Complex();
 59      cc.setAint(1);
 60      cc.setAString("test");
 61      List<Integer> c2 = Arrays.asList(new Integer[] {1, 2, 3});
 62      cc.setLint(c2);
 63      List<String> c3 = Arrays.asList(new String[] {"one", "two"});
 64      cc.setLString(c3);
 65      List<IntString> c4 = new ArrayList<IntString>();
 66      cc.setLintString(c4);
 67      cc.setMStringString(null);
 68      // standard object
 69      Object c = ObjectInspectorUtils.copyToStandardObject(cc, oi1);
 70
 71      assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
 72      assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
 73      assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
 74      assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
 75      assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
 76      assertNull(soi.getStructFieldData(c, fields.get(5)));
 77      ArrayList<Object> cfields = new ArrayList<Object>();
 78      for (int i = 0; i < 6; i++) {
 79        cfields.add(soi.getStructFieldData(c, fields.get(i)));
 80      }
 81      assertEquals(cfields, soi.getStructFieldsDataAsList(c));
 82
 83      // sub fields
 84      assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector,
 85          fields.get(0).getFieldObjectInspector());
 86      assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector,
 87          fields.get(1).getFieldObjectInspector());
 88      assertEquals(
 89          ObjectInspectorFactory
 90          .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector),
 91          fields.get(2).getFieldObjectInspector());
 92      assertEquals(
 93          ObjectInspectorFactory
 94              .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector),
 95          fields.get(3).getFieldObjectInspector());
 96      assertEquals(ObjectInspectorUtils
 97          .getStandardObjectInspector(ObjectInspectorFactory
 98          .getStandardListObjectInspector(ObjectInspectorFactory
 99          .getReflectionObjectInspector(IntString.class,
100          ObjectInspectorFactory.ObjectInspectorOptions.THRIFT))),
101          fields.get(4).getFieldObjectInspector());
102      assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(
103          PrimitiveObjectInspectorFactory.javaStringObjectInspector,
104          PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields
105          .get(5).getFieldObjectInspector());
106    } catch (Throwable e) {
107      e.printStackTrace();
108      throw e;
109    }
110
111  }
112}