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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.junit.jupiter.api.Assertions.fail;
023
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.jupiter.api.Test;
028
029@org.junit.jupiter.api.Tag(MiscTests.TAG)
030@org.junit.jupiter.api.Tag(SmallTests.TAG)
031public class TestTagBuilder {
032
033  @Test
034  public void testArrayBackedTagBuilder() {
035    byte type = (byte) 50;
036    String value = "Array-Backed-Tag";
037    TagBuilder builder = TagBuilderFactory.create();
038    assertTrue(builder instanceof TagBuilderImpl);
039    builder.setTagType(type);
040    builder.setTagValue(Bytes.toBytes(value));
041    Tag tag = builder.build();
042    assertEquals(value, Tag.getValueAsString(tag));
043    assertEquals(type, tag.getType());
044  }
045
046  @Test
047  public void testErrorMessages() {
048    String arrayValue = "Array-Backed-Tag";
049    TagBuilder builder = TagBuilderFactory.create();
050    builder.setTagValue(Bytes.toBytes(arrayValue));
051    try {
052      // Dont set type for the tag.
053      builder.build();
054      fail("Shouldn't have come here.");
055    } catch (IllegalArgumentException iae) {
056      assertTrue(iae.getMessage().contains(TagBuilderImpl.TAG_TYPE_NOT_SET_EXCEPTION));
057    }
058
059    byte type = (byte) 50;
060    builder = TagBuilderFactory.create();
061    builder.setTagType(type);
062    try {
063      // Need to Call setTagValue(byte[]) to set the value.
064      builder.build();
065      fail("Shouldn't have come here.");
066    } catch (IllegalArgumentException iae) {
067      assertTrue(iae.getMessage().contains(TagBuilderImpl.TAG_VALUE_NULL_EXCEPTION));
068    }
069  }
070}