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