View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellScanner;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.ipc.RpcClient;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * Do some ops and prove that client and server can work w/o codecs; that we can pb all the time.
38   * Good for third-party clients or simple scripts that want to talk direct to hbase.
39   */
40  @Category(MediumTests.class)
41  public class TestFromClientSideNoCodec {
42    protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
43    /**
44     * @throws java.lang.Exception
45     */
46    @BeforeClass
47    public static void setUpBeforeClass() throws Exception {
48      // Turn off codec use
49      TEST_UTIL.getConfiguration().set("hbase.client.default.rpc.codec", "");
50      TEST_UTIL.startMiniCluster(1);
51    }
52  
53    /**
54     * @throws java.lang.Exception
55     */
56    @AfterClass
57    public static void tearDownAfterClass() throws Exception {
58      TEST_UTIL.shutdownMiniCluster();
59    }
60  
61    @Test
62    public void testBasics() throws IOException {
63      final byte [] t = Bytes.toBytes("testBasics");
64      final byte [][] fs = new byte[][] {Bytes.toBytes("cf1"), Bytes.toBytes("cf2"),
65        Bytes.toBytes("cf3") };
66      HTable ht = TEST_UTIL.createTable(t, fs);
67      // Check put and get.
68      final byte [] row = Bytes.toBytes("row");
69      Put p = new Put(row);
70      for (byte [] f: fs) p.add(f, f, f);
71      ht.put(p);
72      Result r = ht.get(new Get(row));
73      int i = 0;
74      for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) {
75        Cell cell = cellScanner.current();
76        byte [] f = fs[i++];
77        assertTrue(Bytes.toString(f),
78          Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(),
79            f, 0, f.length));
80      }
81      // Check getRowOrBefore
82      byte [] f = fs[0];
83      r = ht.getRowOrBefore(row, f);
84      assertTrue(r.toString(), r.containsColumn(f, f));
85      // Check scan.
86      ResultScanner scanner = ht.getScanner(new Scan());
87      int count = 0;
88      while ((r = scanner.next()) != null) {
89        assertTrue(r.listCells().size() == 3);
90        count++;
91      }
92      assertTrue(count == 1);
93    }
94  
95    @Test
96    public void testNoCodec() {
97      Configuration c = new Configuration();
98      c.set("hbase.client.default.rpc.codec", "");
99      String codec = RpcClient.getDefaultCodec(c);
100     assertTrue(codec == null || codec.length() == 0);
101   }
102 }