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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.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.testclassification.ClientTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035 036@Tag(ClientTests.TAG) 037@Tag(SmallTests.TAG) 038public class TestRowComparator { 039 040 private static final List<byte[]> DEFAULT_ROWS = IntStream.range(1, 9).mapToObj(String::valueOf) 041 .map(Bytes::toBytes).collect(Collectors.toList()); 042 043 @Test 044 public void testPut() { 045 test(row -> new Put(row)); 046 } 047 048 @Test 049 public void testDelete() { 050 test(row -> new Delete(row)); 051 } 052 053 @Test 054 public void testAppend() { 055 test(row -> new Append(row)); 056 } 057 058 @Test 059 public void testIncrement() { 060 test(row -> new Increment(row)); 061 } 062 063 @Test 064 public void testGet() { 065 test(row -> new Get(row)); 066 } 067 068 private static <T extends Row> void test(Function<byte[], T> f) { 069 List<T> rows = new ArrayList<T>(DEFAULT_ROWS.stream().map(f).collect(Collectors.toList())); 070 do { 071 Collections.shuffle(rows); 072 } while (needShuffle(rows)); 073 Collections.sort(rows, Row.COMPARATOR); 074 assertSort(rows); 075 } 076 077 private static boolean needShuffle(List<? extends Row> rows) { 078 assertFalse(rows.isEmpty()); 079 assertEquals(DEFAULT_ROWS.size(), rows.size()); 080 for (int i = 0; i != DEFAULT_ROWS.size(); ++i) { 081 if (!Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow())) { 082 return false; 083 } 084 } 085 return true; 086 } 087 088 private static void assertSort(List<? extends Row> rows) { 089 assertFalse(rows.isEmpty()); 090 assertEquals(DEFAULT_ROWS.size(), rows.size()); 091 for (int i = 0; i != DEFAULT_ROWS.size(); ++i) { 092 assertTrue(Bytes.equals(DEFAULT_ROWS.get(i), rows.get(i).getRow())); 093 } 094 } 095}