1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import org.apache.hadoop.hbase.*;
23  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.junit.experimental.categories.Category;
26  
27  /**
28   * Test of simple partitioner.
29   */
30  @Category(SmallTests.class)
31  public class TestSimpleTotalOrderPartitioner extends HBaseTestCase {
32    public void testSplit() throws Exception {
33      String start = "a";
34      String end = "{";
35      SimpleTotalOrderPartitioner<byte []> p =
36        new SimpleTotalOrderPartitioner<byte []>();
37      this.conf.set(SimpleTotalOrderPartitioner.START, start);
38      this.conf.set(SimpleTotalOrderPartitioner.END, end);
39      p.setConf(this.conf);
40      ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c"));
41      // If one reduce, partition should be 0.
42      int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1);
43      assertEquals(0, partition);
44      // If two reduces, partition should be 0.
45      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2);
46      assertEquals(0, partition);
47      // Divide in 3.
48      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3);
49      assertEquals(0, partition);
50      ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q"));
51      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2);
52      assertEquals(1, partition);
53      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3);
54      assertEquals(2, partition);
55      // What about end and start keys.
56      ImmutableBytesWritable startBytes =
57        new ImmutableBytesWritable(Bytes.toBytes(start));
58      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
59      assertEquals(0, partition);
60      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
61      assertEquals(0, partition);
62      ImmutableBytesWritable endBytes =
63        new ImmutableBytesWritable(Bytes.toBytes("z"));
64      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
65      assertEquals(1, partition);
66      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
67      assertEquals(2, partition);
68    }
69  
70    @org.junit.Rule
71    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
72      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
73  }
74