001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.types;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.testclassification.MiscTests;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.apache.hadoop.hbase.util.Order;
026import org.apache.hadoop.hbase.util.PositionedByteRange;
027import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ MiscTests.class, SmallTests.class })
033public class TestUnion2 {
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestUnion2.class);
036
037  /**
038   * An example <code>Union</code>
039   */
040  private static class SampleUnion1 extends Union2<Integer, String> {
041    private static final byte IS_INTEGER = 0x00;
042    private static final byte IS_STRING = 0x01;
043
044    public SampleUnion1() {
045      super(new RawInteger(), new RawStringTerminated(Order.DESCENDING, "."));
046    }
047
048    @Override
049    public int skip(PositionedByteRange src) {
050      switch (src.get()) {
051        case IS_INTEGER:
052          return 1 + typeA.skip(src);
053        case IS_STRING:
054          return 1 + typeB.skip(src);
055        default:
056          throw new IllegalArgumentException("Unrecognized encoding format.");
057      }
058    }
059
060    @Override
061    public Object decode(PositionedByteRange src) {
062      switch (src.get()) {
063        case IS_INTEGER:
064          return typeA.decode(src);
065        case IS_STRING:
066          return typeB.decode(src);
067        default:
068          throw new IllegalArgumentException("Unrecognized encoding format.");
069      }
070    }
071
072    @Override
073    public int encodedLength(Object val) {
074      Integer i = null;
075      String s = null;
076      if (val instanceof Integer) {
077        i = (Integer) val;
078      }
079      if (val instanceof String) {
080        s = (String) val;
081      }
082      if (null != i) {
083        return 1 + typeA.encodedLength(i);
084      }
085
086      if (null != s) {
087        return 1 + typeB.encodedLength(s);
088      }
089      throw new IllegalArgumentException("val is not a valid member of this union.");
090    }
091
092    @Override
093    public int encode(PositionedByteRange dst, Object val) {
094      Integer i = null;
095      String s = null;
096      if (val instanceof Integer) {
097        i = (Integer) val;
098      }
099      if (val instanceof String) {
100        s = (String) val;
101      }
102      if (null != i) {
103        dst.put(IS_INTEGER);
104        return 1 + typeA.encode(dst, i);
105      } else if (null != s) {
106        dst.put(IS_STRING);
107        return 1 + typeB.encode(dst, s);
108      } else {
109        throw new IllegalArgumentException("val is not of a supported type.");
110      }
111    }
112  }
113
114  @Test
115  public void testEncodeDecode() {
116    Integer intVal = 10;
117    String strVal = "hello";
118    PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
119    SampleUnion1 type = new SampleUnion1();
120
121    type.encode(buff, intVal);
122    buff.setPosition(0);
123    assertEquals(0, intVal.compareTo(type.decodeA(buff)));
124    buff.setPosition(0);
125    type.encode(buff, strVal);
126    buff.setPosition(0);
127    assertEquals(0, strVal.compareTo(type.decodeB(buff)));
128  }
129
130  @Test
131  public void testSkip() {
132    Integer intVal = 10;
133    String strVal = "hello";
134    PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
135    SampleUnion1 type = new SampleUnion1();
136
137    int len = type.encode(buff, intVal);
138    buff.setPosition(0);
139    assertEquals(len, type.skip(buff));
140    buff.setPosition(0);
141    len = type.encode(buff, strVal);
142    buff.setPosition(0);
143    assertEquals(len, type.skip(buff));
144  }
145}