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