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.Random; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 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 static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 045 046 private String uuid; 047 private Date date; 048 private String dateStr; 049 private byte[] startKey; 050 051 @Before 052 public void setUp() { 053 Random random = new Random(); 054 uuid = TEST_UTIL.getRandomUUID().toString().replaceAll("-", ""); 055 date = new Date(); 056 dateStr = MobUtils.formatDate(date); 057 startKey = Bytes.toBytes(random.nextInt()); 058 } 059 060 @Test 061 public void testHashCode() { 062 assertEquals(MobFileName.create(startKey, dateStr, uuid).hashCode(), 063 MobFileName.create(startKey, dateStr, uuid).hashCode()); 064 assertNotSame(MobFileName.create(startKey, dateStr, uuid), 065 MobFileName.create(startKey, dateStr, uuid)); 066 } 067 068 @Test 069 public void testCreate() { 070 MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid); 071 assertEquals(mobFileName, MobFileName.create(mobFileName.getFileName())); 072 } 073 074 @Test 075 public void testGet() { 076 MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid); 077 assertEquals(MD5Hash.getMD5AsHex(startKey, 0, startKey.length), mobFileName.getStartKey()); 078 assertEquals(dateStr, mobFileName.getDate()); 079 assertEquals(mobFileName.getFileName(), MD5Hash.getMD5AsHex(startKey, 0, startKey.length) 080 + dateStr + uuid); 081 } 082 083 @Test 084 public void testEquals() { 085 MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid); 086 assertTrue(mobFileName.equals(mobFileName)); 087 assertFalse(mobFileName.equals(this)); 088 assertTrue(mobFileName.equals(MobFileName.create(startKey, dateStr, uuid))); 089 } 090}