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)
046    .mapToObj(String::valueOf).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()
075      .map(f).collect(Collectors.toList()));
076    do {
077      Collections.shuffle(rows);
078    } while (needShuffle(rows));
079    Collections.sort(rows, Row.COMPARATOR);
080    assertSort(rows);
081  }
082
083  private static boolean needShuffle(List<? extends Row> rows) {
084    assertFalse(rows.isEmpty());
085    assertEquals(DEFAULT_ROWS.size(), rows.size());
086    for (int i = 0; i != DEFAULT_ROWS.size(); ++i) {
087      if (!Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow())) {
088        return false;
089      }
090    }
091    return true;
092  }
093
094  private static void assertSort(List<? extends Row> rows) {
095    assertFalse(rows.isEmpty());
096    assertEquals(DEFAULT_ROWS.size(), rows.size());
097    for (int i = 0; i != DEFAULT_ROWS.size(); ++i) {
098      assertTrue(Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow()));
099    }
100  }
101}