View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.util.Date;
22  import java.util.Random;
23  import java.util.UUID;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.MD5Hash;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestMobFileName extends TestCase {
35  
36    private String uuid;
37    private Date date;
38    private String dateStr;
39    private byte[] startKey;
40  
41    public void setUp() {
42      Random random = new Random();
43      uuid = UUID.randomUUID().toString().replaceAll("-", "");
44      date = new Date();
45      dateStr = MobUtils.formatDate(date);
46      startKey = Bytes.toBytes(random.nextInt());
47    }
48  
49    @Test
50    public void testHashCode() {
51      assertEquals(MobFileName.create(startKey, dateStr, uuid).hashCode(),
52          MobFileName.create(startKey, dateStr, uuid).hashCode());
53      assertNotSame(MobFileName.create(startKey, dateStr, uuid),
54          MobFileName.create(startKey, dateStr, uuid));
55    }
56  
57    @Test
58    public void testCreate() {
59      MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid);
60      assertEquals(mobFileName, MobFileName.create(mobFileName.getFileName()));
61    }
62  
63    @Test
64    public void testGet() {
65      MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid);
66      assertEquals(MD5Hash.getMD5AsHex(startKey, 0, startKey.length), mobFileName.getStartKey());
67      assertEquals(dateStr, mobFileName.getDate());
68      assertEquals(mobFileName.getFileName(), MD5Hash.getMD5AsHex(startKey, 0, startKey.length)
69          + dateStr + uuid);
70    }
71  
72    @Test
73    public void testEquals() {
74      MobFileName mobFileName = MobFileName.create(startKey, dateStr, uuid);
75      assertTrue(mobFileName.equals(mobFileName));
76      assertFalse(mobFileName.equals(this));
77      assertTrue(mobFileName.equals(MobFileName.create(startKey, dateStr, uuid)));
78    }
79  }