View Javadoc

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.testclassification.SmallTests;
27  import org.apache.hadoop.hbase.util.test.LoadTestKVGenerator;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestLoadTestKVGenerator {
33  
34    private static final int MIN_LEN = 10;
35    private static final int MAX_LEN = 20;
36  
37    private Random rand = new Random(28937293L);
38    private LoadTestKVGenerator gen = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
39  
40    @Test
41    public void testValueLength() {
42      for (int i = 0; i < 1000; ++i) {
43        byte[] v = gen.generateRandomSizeValue(Integer.toString(i).getBytes(),
44            String.valueOf(rand.nextInt()).getBytes());
45        assertTrue(MIN_LEN <= v.length);
46        assertTrue(v.length <= MAX_LEN);
47      }
48    }
49  
50    @Test
51    public void testVerification() {
52      for (int i = 0; i < 1000; ++i) {
53        for (int qualIndex = 0; qualIndex < 20; ++qualIndex) {
54          byte[] qual = String.valueOf(qualIndex).getBytes();
55          byte[] rowKey = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
56          byte[] v = gen.generateRandomSizeValue(rowKey, qual);
57          assertTrue(LoadTestKVGenerator.verify(v, rowKey, qual));
58          v[0]++;
59          assertFalse(LoadTestKVGenerator.verify(v, rowKey, qual));
60        }
61      }
62    }
63  
64    @Test
65    public void testCorrectAndUniqueKeys() {
66      Set<String> keys = new HashSet<String>();
67      for (int i = 0; i < 1000; ++i) {
68        String k = LoadTestKVGenerator.md5PrefixedKey(i);
69        assertFalse(keys.contains(k));
70        assertTrue(k.endsWith("-" + i));
71        keys.add(k);
72      }
73    }
74  
75  }