1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.util;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.util.HashSet;
23  import java.util.Random;
24  import java.util.Set;
25  
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestLoadTestKVGenerator {
32  
33    private static final int MIN_LEN = 10;
34    private static final int MAX_LEN = 20;
35  
36    private Random rand = new Random(28937293L);
37    private LoadTestKVGenerator gen = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
38  
39    @Test
40    public void testValueLength() {
41      for (int i = 0; i < 1000; ++i) {
42        byte[] v = gen.generateRandomSizeValue(Integer.toString(i).getBytes(),
43          String.valueOf(rand.nextInt()).getBytes());
44        assertTrue(MIN_LEN <= v.length);
45        assertTrue(v.length <= MAX_LEN);
46      }
47    }
48  
49    @Test
50    public void testVerification() {
51      for (int i = 0; i < 1000; ++i) {
52        for (int qualIndex = 0; qualIndex < 20; ++qualIndex) {
53          byte[] qual = String.valueOf(qualIndex).getBytes();
54          byte[] rowKey = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
55          byte[] v = gen.generateRandomSizeValue(rowKey, qual);
56          assertTrue(LoadTestKVGenerator.verify(v, rowKey, qual));
57          v[0]++;
58          assertFalse(LoadTestKVGenerator.verify(v, rowKey, qual));
59        }
60      }
61    }
62  
63    @Test
64    public void testCorrectAndUniqueKeys() {
65      Set<String> keys = new HashSet<String>();
66      for (int i = 0; i < 1000; ++i) {
67        String k = gen.md5PrefixedKey(i);
68        assertFalse(keys.contains(k));
69        assertTrue(k.endsWith("-" + i));
70        keys.add(k);
71      }
72    }
73  
74  }