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