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.HBaseTestingUtil;
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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 = new byte[][] {Bytes.toBytes("cf1"), Bytes.toBytes("cf2"),
079      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),
094        Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(),
095          f, 0, f.length));
096    }
097    // Check getRowOrBefore
098    byte[] f = fs[0];
099    Get get = new Get(row);
100    get.addFamily(f);
101    r = ht.get(get);
102    assertTrue(r.toString(), r.containsColumn(f, f));
103    // Check scan.
104    ResultScanner scanner = ht.getScanner(new Scan());
105    int count = 0;
106    while ((r = scanner.next()) != null) {
107      assertTrue(r.listCells().size() == 3);
108      count++;
109    }
110    assertTrue(count == 1);
111  }
112
113  @Test
114  public void testNoCodec() {
115    Configuration c = new Configuration();
116    c.set("hbase.client.default.rpc.codec", "");
117    String codec = AbstractRpcClient.getDefaultCodec(c);
118    assertTrue(codec == null || codec.length() == 0);
119  }
120}