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.filter; 019 020import static org.junit.Assert.assertNull; 021import static org.junit.Assert.assertTrue; 022 023import java.math.BigDecimal; 024import java.util.regex.Pattern; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.testclassification.FilterTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.junit.ClassRule; 030import org.junit.Test; 031import org.junit.experimental.categories.Category; 032 033import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 034 035@Category({FilterTests.class, SmallTests.class}) 036public class TestComparatorSerialization { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestComparatorSerialization.class); 041 042 @Test 043 public void testBinaryComparator() throws Exception { 044 BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes("binaryComparator")); 045 assertTrue(binaryComparator.areSerializedFieldsEqual( 046 ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryComparator)))); 047 } 048 049 @Test 050 public void testBinaryPrefixComparator() throws Exception { 051 BinaryPrefixComparator binaryPrefixComparator = 052 new BinaryPrefixComparator(Bytes.toBytes("binaryPrefixComparator")); 053 assertTrue(binaryPrefixComparator.areSerializedFieldsEqual( 054 ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryPrefixComparator)))); 055 } 056 057 @Test 058 public void testBitComparator() throws Exception { 059 BitComparator bitComparator = 060 new BitComparator(Bytes.toBytes("bitComparator"), BitComparator.BitwiseOp.XOR); 061 assertTrue(bitComparator.areSerializedFieldsEqual( 062 ProtobufUtil.toComparator(ProtobufUtil.toComparator(bitComparator)))); 063 } 064 065 @Test 066 public void testNullComparator() throws Exception { 067 NullComparator nullComparator = new NullComparator(); 068 assertTrue(nullComparator.areSerializedFieldsEqual( 069 ProtobufUtil.toComparator(ProtobufUtil.toComparator(nullComparator)))); 070 } 071 072 @Test 073 public void testRegexStringComparator() throws Exception { 074 // test without specifying flags 075 RegexStringComparator regexStringComparator = new RegexStringComparator(".+-2"); 076 assertTrue(regexStringComparator.areSerializedFieldsEqual( 077 ProtobufUtil.toComparator(ProtobufUtil.toComparator(regexStringComparator)))); 078 079 // test with specifying flags 080 try { 081 new RegexStringComparator("regex", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); 082 } catch (Throwable t) { 083 assertNull("Exception occurred while created the RegexStringComparator object", t); 084 } 085 } 086 087 @Test 088 public void testSubstringComparator() throws Exception { 089 SubstringComparator substringComparator = new SubstringComparator("substr"); 090 assertTrue(substringComparator.areSerializedFieldsEqual( 091 ProtobufUtil.toComparator(ProtobufUtil.toComparator(substringComparator)))); 092 } 093 094 @Test 095 public void testBigDecimalComparator() throws Exception { 096 BigDecimal bigDecimal = new BigDecimal(Double.MIN_VALUE); 097 BigDecimalComparator bigDecimalComparator = new BigDecimalComparator(bigDecimal); 098 assertTrue(bigDecimalComparator.areSerializedFieldsEqual(ProtobufUtil.toComparator(ProtobufUtil 099 .toComparator(bigDecimalComparator)))); 100 } 101 102}