View Javadoc

1   /**
2    * Copyright 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.filter;
21  
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.regex.Pattern;
26  
27  import org.apache.hadoop.hbase.SmallTests;
28  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestComparatorSerialization {
35  
36    @Test
37    public void testBinaryComparator() throws Exception {
38      BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes("binaryComparator"));
39      assertTrue(binaryComparator.areSerializedFieldsEqual(
40        ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryComparator))));
41    }
42  
43    @Test
44    public void testBinaryPrefixComparator() throws Exception {
45      BinaryPrefixComparator binaryPrefixComparator =
46        new BinaryPrefixComparator(Bytes.toBytes("binaryPrefixComparator"));
47      assertTrue(binaryPrefixComparator.areSerializedFieldsEqual(
48        ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryPrefixComparator))));
49    }
50  
51    @Test
52    public void testBitComparator() throws Exception {
53      BitComparator bitComparator =
54        new BitComparator(Bytes.toBytes("bitComparator"), BitComparator.BitwiseOp.XOR);
55      assertTrue(bitComparator.areSerializedFieldsEqual(
56        ProtobufUtil.toComparator(ProtobufUtil.toComparator(bitComparator))));
57    }
58  
59    @Test
60    public void testNullComparator() throws Exception {
61      NullComparator nullComparator = new NullComparator();
62      assertTrue(nullComparator.areSerializedFieldsEqual(
63        ProtobufUtil.toComparator(ProtobufUtil.toComparator(nullComparator))));
64    }
65  
66    @Test
67    public void testRegexStringComparator() throws Exception {
68      // test without specifying flags
69      RegexStringComparator regexStringComparator = new RegexStringComparator(".+-2");
70      assertTrue(regexStringComparator.areSerializedFieldsEqual(
71        ProtobufUtil.toComparator(ProtobufUtil.toComparator(regexStringComparator))));
72  
73      // test with specifying flags
74      try {
75        new RegexStringComparator("regex", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
76      } catch (Throwable t) {
77        assertNull("Exception occured while created the RegexStringComparator object", t);
78      }
79    }
80  
81    @Test
82    public void testSubstringComparator() throws Exception {
83      SubstringComparator substringComparator = new SubstringComparator("substr");
84      assertTrue(substringComparator.areSerializedFieldsEqual(
85        ProtobufUtil.toComparator(ProtobufUtil.toComparator(substringComparator))));
86    }
87  
88  }