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.assertTrue; 021 022import java.io.IOException; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.Cell; 025import org.apache.hadoop.hbase.CellScanner; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.TableNameTestExtension; 029import org.apache.hadoop.hbase.ipc.AbstractRpcClient; 030import org.apache.hadoop.hbase.testclassification.ClientTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.jupiter.api.AfterAll; 034import org.junit.jupiter.api.BeforeAll; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.Test; 037import org.junit.jupiter.api.extension.RegisterExtension; 038 039/** 040 * Do some ops and prove that client and server can work w/o codecs; that we can pb all the time. 041 * Good for third-party clients or simple scripts that want to talk direct to hbase. 042 */ 043@Tag(MediumTests.TAG) 044@Tag(ClientTests.TAG) 045public class TestFromClientSideNoCodec { 046 047 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 048 049 @RegisterExtension 050 private TableNameTestExtension name = new TableNameTestExtension(); 051 052 @BeforeAll 053 public static void setUpBeforeClass() throws Exception { 054 // Turn off codec use 055 TEST_UTIL.getConfiguration().set("hbase.client.default.rpc.codec", ""); 056 TEST_UTIL.startMiniCluster(1); 057 } 058 059 @AfterAll 060 public static void tearDownAfterClass() throws Exception { 061 TEST_UTIL.shutdownMiniCluster(); 062 } 063 064 @Test 065 public void testBasics() throws IOException { 066 final TableName tableName = name.getTableName(); 067 final byte[][] fs = 068 new byte[][] { Bytes.toBytes("cf1"), Bytes.toBytes("cf2"), Bytes.toBytes("cf3") }; 069 Table ht = TEST_UTIL.createTable(tableName, fs); 070 // Check put and get. 071 final byte[] row = Bytes.toBytes("row"); 072 Put p = new Put(row); 073 for (byte[] f : fs) { 074 p.addColumn(f, f, f); 075 } 076 ht.put(p); 077 Result r = ht.get(new Get(row)); 078 int i = 0; 079 for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) { 080 Cell cell = cellScanner.current(); 081 byte[] f = fs[i++]; 082 assertTrue(Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), f, 083 0, f.length), Bytes.toString(f)); 084 } 085 // Check getRowOrBefore 086 byte[] f = fs[0]; 087 Get get = new Get(row); 088 get.addFamily(f); 089 r = ht.get(get); 090 assertTrue(r.containsColumn(f, f), r.toString()); 091 // Check scan. 092 ResultScanner scanner = ht.getScanner(new Scan()); 093 int count = 0; 094 while ((r = scanner.next()) != null) { 095 assertTrue(r.listCells().size() == 3); 096 count++; 097 } 098 assertTrue(count == 1); 099 } 100 101 @Test 102 public void testNoCodec() { 103 Configuration c = new Configuration(); 104 c.set("hbase.client.default.rpc.codec", ""); 105 String codec = AbstractRpcClient.getDefaultCodec(c); 106 assertTrue(codec == null || codec.length() == 0); 107 } 108}