PageRenderTime 46ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/test/org/apache/hcatalog/data/TestDefaultHCatRecord.java

#
Java | 154 lines | 112 code | 25 blank | 17 comment | 2 complexity | 31b6b3c2818737608b9b32c12c6c95a7 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.hcatalog.data;
 19
 20import java.io.DataInput;
 21import java.io.DataInputStream;
 22import java.io.DataOutput;
 23import java.io.DataOutputStream;
 24import java.io.File;
 25import java.io.FileInputStream;
 26import java.io.FileOutputStream;
 27import java.io.IOException;
 28import java.io.InputStream;
 29import java.io.OutputStream;
 30import java.util.ArrayList;
 31import java.util.HashMap;
 32import java.util.List;
 33import java.util.Map;
 34
 35import org.apache.hcatalog.data.DefaultHCatRecord;
 36import org.apache.hcatalog.data.HCatRecord;
 37
 38import junit.framework.Assert;
 39import junit.framework.TestCase;
 40
 41public class TestDefaultHCatRecord extends TestCase{
 42
 43  public void testRYW() throws IOException{
 44
 45    File f = new File("binary.dat");
 46    f.delete();
 47    f.createNewFile();
 48    f.deleteOnExit();
 49
 50    OutputStream fileOutStream = new FileOutputStream(f);
 51    DataOutput outStream = new DataOutputStream(fileOutStream);
 52
 53    HCatRecord[]  recs = getHCatRecords();
 54    for(int i =0; i < recs.length; i++){
 55      recs[i].write(outStream);
 56    }
 57    fileOutStream.flush();
 58    fileOutStream.close();
 59
 60    InputStream fInStream = new FileInputStream(f);
 61    DataInput inpStream = new DataInputStream(fInStream);
 62
 63    for(int i =0; i < recs.length; i++){
 64      HCatRecord rec = new DefaultHCatRecord();
 65      rec.readFields(inpStream);
 66      Assert.assertEquals(recs[i],rec);
 67    }
 68
 69    Assert.assertEquals(fInStream.available(), 0);
 70    fInStream.close();
 71
 72  }
 73
 74  public void testCompareTo() {
 75    HCatRecord[] recs = getHCatRecords();
 76    Assert.assertEquals(recs[0].compareTo(recs[1]),0);
 77  }
 78
 79  public void testEqualsObject() {
 80
 81    HCatRecord[] recs = getHCatRecords();
 82    Assert.assertTrue(recs[0].equals(recs[1]));
 83  }
 84
 85  private HCatRecord[] getHCatRecords(){
 86
 87    List<Object> rec_1 = new ArrayList<Object>(8);
 88    rec_1.add(new Byte("123"));
 89    rec_1.add(new Short("456"));
 90    rec_1.add( new Integer(789));
 91    rec_1.add( new Long(1000L));
 92    rec_1.add( new Double(5.3D));
 93    rec_1.add( new String("hcat and hadoop"));
 94    rec_1.add( null);
 95    rec_1.add( "null");
 96
 97    HCatRecord tup_1 = new DefaultHCatRecord(rec_1);
 98
 99    List<Object> rec_2 = new ArrayList<Object>(8);
100    rec_2.add( new Byte("123"));
101    rec_2.add( new Short("456"));
102    rec_2.add( new Integer(789));
103    rec_2.add( new Long(1000L));
104    rec_2.add( new Double(5.3D));
105    rec_2.add( new String("hcat and hadoop"));
106    rec_2.add( null);
107    rec_2.add( "null");
108    HCatRecord tup_2 = new DefaultHCatRecord(rec_2);
109
110    List<Object> rec_3 = new ArrayList<Object>(10);
111    rec_3.add(new Byte("123"));
112    rec_3.add(new Short("456"));
113    rec_3.add( new Integer(789));
114    rec_3.add( new Long(1000L));
115    rec_3.add( new Double(5.3D));
116    rec_3.add( new String("hcat and hadoop"));
117    rec_3.add( null);
118    List<Integer> innerList = new ArrayList<Integer>();
119    innerList.add(314);
120    innerList.add(007);
121    rec_3.add( innerList);
122    Map<Short, String> map = new HashMap<Short, String>(3);
123    map.put(new Short("2"), "hcat is cool");
124    map.put(new Short("3"), "is it?");
125    map.put(new Short("4"), "or is it not?");
126    rec_3.add(map);
127
128    HCatRecord tup_3 = new DefaultHCatRecord(rec_3);
129
130    List<Object> rec_4 = new ArrayList<Object>(8);
131    rec_4.add( new Byte("123"));
132    rec_4.add( new Short("456"));
133    rec_4.add( new Integer(789));
134    rec_4.add( new Long(1000L));
135    rec_4.add( new Double(5.3D));
136    rec_4.add( new String("hcat and hadoop"));
137    rec_4.add( null);
138    rec_4.add( "null");
139
140    Map<Short, String> map2 = new HashMap<Short, String>(3);
141    map2.put(new Short("2"), "hcat is cool");
142    map2.put(new Short("3"), "is it?");
143    map2.put(new Short("4"), "or is it not?");
144    rec_4.add(map2);
145    List<Integer> innerList2 = new ArrayList<Integer>();
146    innerList2.add(314);
147    innerList2.add(007);
148    rec_4.add( innerList2);
149    HCatRecord tup_4 = new DefaultHCatRecord(rec_4);
150
151    return  new HCatRecord[]{tup_1,tup_2,tup_3,tup_4};
152
153  }
154}