PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 119 lines | 77 code | 13 blank | 29 comment | 0 complexity | def7ad5a6c09153f14be07155830b759 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 junit.framework.TestCase;
  20. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  21. import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  22. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
  24. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  25. import org.apache.hadoop.io.BooleanWritable;
  26. import org.apache.hadoop.io.FloatWritable;
  27. import org.apache.hadoop.io.IntWritable;
  28. import org.apache.hadoop.io.LongWritable;
  29. import org.apache.hadoop.io.Text;
  30. /**
  31. * TestObjectInspectorConverters.
  32. *
  33. */
  34. public class TestObjectInspectorConverters extends TestCase {
  35. public void testObjectInspectorConverters() throws Throwable {
  36. try {
  37. // Boolean
  38. Converter booleanConverter = ObjectInspectorConverters.getConverter(
  39. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  40. PrimitiveObjectInspectorFactory.writableBooleanObjectInspector);
  41. assertEquals("BooleanConverter", new BooleanWritable(false),
  42. booleanConverter.convert(Integer.valueOf(0)));
  43. assertEquals("BooleanConverter", new BooleanWritable(true),
  44. booleanConverter.convert(Integer.valueOf(1)));
  45. // Byte
  46. Converter byteConverter = ObjectInspectorConverters.getConverter(
  47. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  48. PrimitiveObjectInspectorFactory.writableByteObjectInspector);
  49. assertEquals("ByteConverter", new ByteWritable((byte) 0), byteConverter
  50. .convert(Integer.valueOf(0)));
  51. assertEquals("ByteConverter", new ByteWritable((byte) 1), byteConverter
  52. .convert(Integer.valueOf(1)));
  53. // Short
  54. Converter shortConverter = ObjectInspectorConverters.getConverter(
  55. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  56. PrimitiveObjectInspectorFactory.writableShortObjectInspector);
  57. assertEquals("ShortConverter", new ShortWritable((short) 0),
  58. shortConverter.convert(Integer.valueOf(0)));
  59. assertEquals("ShortConverter", new ShortWritable((short) 1),
  60. shortConverter.convert(Integer.valueOf(1)));
  61. // Int
  62. Converter intConverter = ObjectInspectorConverters.getConverter(
  63. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  64. PrimitiveObjectInspectorFactory.writableIntObjectInspector);
  65. assertEquals("IntConverter", new IntWritable(0), intConverter
  66. .convert(Integer.valueOf(0)));
  67. assertEquals("IntConverter", new IntWritable(1), intConverter
  68. .convert(Integer.valueOf(1)));
  69. // Long
  70. Converter longConverter = ObjectInspectorConverters.getConverter(
  71. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  72. PrimitiveObjectInspectorFactory.writableLongObjectInspector);
  73. assertEquals("LongConverter", new LongWritable(0), longConverter
  74. .convert(Integer.valueOf(0)));
  75. assertEquals("LongConverter", new LongWritable(1), longConverter
  76. .convert(Integer.valueOf(1)));
  77. // Float
  78. Converter floatConverter = ObjectInspectorConverters.getConverter(
  79. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  80. PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
  81. assertEquals("LongConverter", new FloatWritable(0), floatConverter
  82. .convert(Integer.valueOf(0)));
  83. assertEquals("LongConverter", new FloatWritable(1), floatConverter
  84. .convert(Integer.valueOf(1)));
  85. // Double
  86. Converter doubleConverter = ObjectInspectorConverters.getConverter(
  87. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  88. PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
  89. assertEquals("DoubleConverter", new DoubleWritable(0), doubleConverter
  90. .convert(Integer.valueOf(0)));
  91. assertEquals("DoubleConverter", new DoubleWritable(1), doubleConverter
  92. .convert(Integer.valueOf(1)));
  93. // Text
  94. Converter textConverter = ObjectInspectorConverters.getConverter(
  95. PrimitiveObjectInspectorFactory.javaIntObjectInspector,
  96. PrimitiveObjectInspectorFactory.writableStringObjectInspector);
  97. assertEquals("TextConverter", new Text("0"), textConverter
  98. .convert(Integer.valueOf(0)));
  99. assertEquals("TextConverter", new Text("1"), textConverter
  100. .convert(Integer.valueOf(1)));
  101. } catch (Throwable e) {
  102. e.printStackTrace();
  103. throw e;
  104. }
  105. }
  106. }