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.mob;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotSame;
023import static org.junit.Assert.assertTrue;
024
025import java.util.Date;
026import java.util.concurrent.ThreadLocalRandom;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.hbase.util.MD5Hash;
032import org.junit.Before;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category(SmallTests.class)
038public class TestMobFileName {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestMobFileName.class);
043
044  private String uuid;
045  private Date date;
046  private String dateStr;
047  private byte[] startKey;
048  private String regionName = "region";
049
050  @Before
051  public void setUp() {
052    uuid = HBaseTestingUtil.getRandomUUID().toString().replaceAll("-", "");
053    date = new Date();
054    dateStr = MobUtils.formatDate(date);
055    startKey = Bytes.toBytes(ThreadLocalRandom.current().nextInt());
056  }
057
058  @Test
059  public void testHashCode() {
060    assertEquals(MobFileName.create(startKey, dateStr, uuid, regionName).hashCode(),
061      MobFileName.create(startKey, dateStr, uuid, regionName).hashCode());
062    assertNotSame(MobFileName.create(startKey, dateStr, uuid, regionName),
063      MobFileName.create(startKey, dateStr, uuid, regionName));
064  }
065
066  @Test
067  public void testCreate() {
068    MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid, regionName);
069    assertEquals(mobFileName, MobFileName.create(mobFileName.getFileName()));
070  }
071
072  @Test
073  public void testGet() {
074    MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid, regionName);
075    assertEquals(MD5Hash.getMD5AsHex(startKey, 0, startKey.length), mobFileName.getStartKey());
076    assertEquals(dateStr, mobFileName.getDate());
077    assertEquals(mobFileName.getFileName(),
078      MD5Hash.getMD5AsHex(startKey, 0, startKey.length) + dateStr + uuid + "_" + regionName);
079  }
080
081  @Test
082  public void testEquals() {
083    MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid, regionName);
084    assertTrue(mobFileName.equals(mobFileName));
085    assertFalse(mobFileName.equals(this));
086    assertTrue(mobFileName.equals(MobFileName.create(startKey, dateStr, uuid, regionName)));
087  }
088}