001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027import java.util.function.Function;
028import java.util.stream.Collectors;
029import java.util.stream.IntStream;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ ClientTests.class, SmallTests.class })
039public class TestRowComparator {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestRowComparator.class);
044
045  private static final List<byte[]> DEFAULT_ROWS = IntStream.range(1, 9).mapToObj(String::valueOf)
046    .map(Bytes::toBytes).collect(Collectors.toList());
047
048  @Test
049  public void testPut() {
050    test(row -> new Put(row));
051  }
052
053  @Test
054  public void testDelete() {
055    test(row -> new Delete(row));
056  }
057
058  @Test
059  public void testAppend() {
060    test(row -> new Append(row));
061  }
062
063  @Test
064  public void testIncrement() {
065    test(row -> new Increment(row));
066  }
067
068  @Test
069  public void testGet() {
070    test(row -> new Get(row));
071  }
072
073  private static <T extends Row> void test(Function<byte[], T> f) {
074    List<T> rows = new ArrayList<T>(DEFAULT_ROWS.stream().map(f).collect(Collectors.toList()));
075    do {
076      Collections.shuffle(rows);
077    } while (needShuffle(rows));
078    Collections.sort(rows, Row.COMPARATOR);
079    assertSort(rows);
080  }
081
082  private static boolean needShuffle(List<? extends Row> rows) {
083    assertFalse(rows.isEmpty());
084    assertEquals(DEFAULT_ROWS.size(), rows.size());
085    for (int i = 0; i != DEFAULT_ROWS.size(); ++i) {
086      if (!Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow())) {
087        return false;
088      }
089    }
090    return true;
091  }
092
093  private static void assertSort(List<? extends Row> rows) {
094    assertFalse(rows.isEmpty());
095    assertEquals(DEFAULT_ROWS.size(), rows.size());
096    for (int i = 0; i != DEFAULT_ROWS.size(); ++i) {
097      assertTrue(Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow()));
098    }
099  }
100}