PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. */
  18. package org.apache.hadoop.hive.serde2.objectinspector;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import junit.framework.TestCase;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  24. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  25. import org.apache.hadoop.hive.serde2.thrift.test.Complex;
  26. import org.apache.hadoop.hive.serde2.thrift.test.IntString;
  27. /**
  28. * TestObjectInspectorUtils.
  29. *
  30. */
  31. public class TestObjectInspectorUtils extends TestCase {
  32. public void testObjectInspectorUtils() throws Throwable {
  33. try {
  34. ObjectInspector oi1 = ObjectInspectorFactory
  35. .getReflectionObjectInspector(Complex.class,
  36. ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
  37. // metadata
  38. assertEquals(Category.STRUCT, oi1.getCategory());
  39. // standard ObjectInspector
  40. StructObjectInspector soi = (StructObjectInspector) ObjectInspectorUtils
  41. .getStandardObjectInspector(oi1);
  42. List<? extends StructField> fields = soi.getAllStructFieldRefs();
  43. assertEquals(6, fields.size());
  44. assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
  45. // null
  46. for (int i = 0; i < fields.size(); i++) {
  47. assertNull(soi.getStructFieldData(null, fields.get(i)));
  48. }
  49. // real object
  50. Complex cc = new Complex();
  51. cc.setAint(1);
  52. cc.setAString("test");
  53. List<Integer> c2 = Arrays.asList(new Integer[] {1, 2, 3});
  54. cc.setLint(c2);
  55. List<String> c3 = Arrays.asList(new String[] {"one", "two"});
  56. cc.setLString(c3);
  57. List<IntString> c4 = new ArrayList<IntString>();
  58. cc.setLintString(c4);
  59. cc.setMStringString(null);
  60. // standard object
  61. Object c = ObjectInspectorUtils.copyToStandardObject(cc, oi1);
  62. assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
  63. assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
  64. assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
  65. assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
  66. assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
  67. assertNull(soi.getStructFieldData(c, fields.get(5)));
  68. ArrayList<Object> cfields = new ArrayList<Object>();
  69. for (int i = 0; i < 6; i++) {
  70. cfields.add(soi.getStructFieldData(c, fields.get(i)));
  71. }
  72. assertEquals(cfields, soi.getStructFieldsDataAsList(c));
  73. // sub fields
  74. assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  75. fields.get(0).getFieldObjectInspector());
  76. assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector,
  77. fields.get(1).getFieldObjectInspector());
  78. assertEquals(
  79. ObjectInspectorFactory
  80. .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector),
  81. fields.get(2).getFieldObjectInspector());
  82. assertEquals(
  83. ObjectInspectorFactory
  84. .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector),
  85. fields.get(3).getFieldObjectInspector());
  86. assertEquals(ObjectInspectorUtils
  87. .getStandardObjectInspector(ObjectInspectorFactory
  88. .getStandardListObjectInspector(ObjectInspectorFactory
  89. .getReflectionObjectInspector(IntString.class,
  90. ObjectInspectorFactory.ObjectInspectorOptions.THRIFT))),
  91. fields.get(4).getFieldObjectInspector());
  92. assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(
  93. PrimitiveObjectInspectorFactory.javaStringObjectInspector,
  94. PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields
  95. .get(5).getFieldObjectInspector());
  96. } catch (Throwable e) {
  97. e.printStackTrace();
  98. throw e;
  99. }
  100. }
  101. }