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.mob; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import org.apache.hadoop.hbase.TableName; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.apache.hadoop.hbase.util.Bytes; 026import org.junit.jupiter.api.Tag; 027import org.junit.jupiter.api.Test; 028 029import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet; 030import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSetMultimap; 031 032@Tag(SmallTests.TAG) 033public class TestMobUtils { 034 public static final TableName TEST_TABLE_1 = TableName.valueOf("testTable1"); 035 public static final TableName TEST_TABLE_2 = TableName.valueOf("testTable2"); 036 public static final TableName TEST_TABLE_3 = TableName.valueOf("testTable3"); 037 038 @Test 039 public void serializeSingleMobFileRefs() { 040 ImmutableSetMultimap<TableName, String> mobRefSet = 041 ImmutableSetMultimap.<TableName, String> builder().putAll(TEST_TABLE_1, "file1a").build(); 042 byte[] result = MobUtils.serializeMobFileRefs(mobRefSet); 043 assertEquals("testTable1/file1a", Bytes.toString(result)); 044 } 045 046 @Test 047 public void serializeMultipleMobFileRefs() { 048 ImmutableSetMultimap<TableName, String> mobRefSet = 049 ImmutableSetMultimap.<TableName, String> builder().putAll(TEST_TABLE_1, "file1a", "file1b") 050 .putAll(TEST_TABLE_2, "file2a").putAll(TEST_TABLE_3, "file3a", "file3b").build(); 051 byte[] result = MobUtils.serializeMobFileRefs(mobRefSet); 052 assertEquals("testTable1/file1a,file1b//testTable2/file2a//testTable3/file3a,file3b", 053 Bytes.toString(result)); 054 } 055 056 @Test 057 public void deserializeSingleMobFileRefs() { 058 ImmutableSetMultimap<TableName, String> mobRefSet = 059 MobUtils.deserializeMobFileRefs(Bytes.toBytes("testTable1/file1a")).build(); 060 assertEquals(1, mobRefSet.size()); 061 ImmutableSet<String> testTable1Refs = mobRefSet.get(TEST_TABLE_1); 062 assertEquals(1, testTable1Refs.size()); 063 assertTrue(testTable1Refs.contains("file1a")); 064 } 065 066 @Test 067 public void deserializeMultipleMobFileRefs() { 068 ImmutableSetMultimap<TableName, 069 String> mobRefSet = MobUtils 070 .deserializeMobFileRefs( 071 Bytes.toBytes("testTable1/file1a,file1b//testTable2/file2a//testTable3/file3a,file3b")) 072 .build(); 073 assertEquals(5, mobRefSet.size()); 074 ImmutableSet<String> testTable1Refs = mobRefSet.get(TEST_TABLE_1); 075 ImmutableSet<String> testTable2Refs = mobRefSet.get(TEST_TABLE_2); 076 ImmutableSet<String> testTable3Refs = mobRefSet.get(TEST_TABLE_3); 077 assertEquals(2, testTable1Refs.size()); 078 assertEquals(1, testTable2Refs.size()); 079 assertEquals(2, testTable3Refs.size()); 080 assertTrue(testTable1Refs.contains("file1a")); 081 assertTrue(testTable1Refs.contains("file1b")); 082 assertTrue(testTable2Refs.contains("file2a")); 083 assertTrue(testTable3Refs.contains("file3a")); 084 assertTrue(testTable3Refs.contains("file3b")); 085 } 086 087 public static String getTableName(String testMethodName) { 088 return testMethodName.replace("[", "-").replace("]", ""); 089 } 090}