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.thrift2;
019
020import java.net.InetAddress;
021import java.util.ArrayList;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.ClientTests;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.thrift.ImplType;
027import org.apache.hadoop.hbase.thrift.TestThriftServerCmdLine;
028import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
029import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
030import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
031import org.apache.hadoop.hbase.thrift2.generated.TTableName;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.thrift.protocol.TBinaryProtocol;
034import org.apache.thrift.protocol.TCompactProtocol;
035import org.apache.thrift.protocol.TProtocol;
036import org.apache.thrift.transport.TFramedTransport;
037import org.apache.thrift.transport.TSocket;
038import org.apache.thrift.transport.TTransport;
039import org.junit.Assert;
040import org.junit.ClassRule;
041import org.junit.experimental.categories.Category;
042
043@Category({ ClientTests.class, MediumTests.class})
044public class TestThrift2ServerCmdLine extends TestThriftServerCmdLine {
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestThrift2ServerCmdLine.class);
048
049  private static final String TABLENAME = "TestThrift2ServerCmdLineTable";
050
051
052  @Override
053  protected ThriftServer createThriftServer() {
054    return new ThriftServer(TEST_UTIL.getConfiguration());
055  }
056
057  public TestThrift2ServerCmdLine(ImplType implType, boolean specifyFramed,
058      boolean specifyBindIP, boolean specifyCompact) {
059    super(implType, specifyFramed, specifyBindIP, specifyCompact);
060  }
061
062  @Override
063  protected void talkToThriftServer() throws Exception {
064    TSocket sock = new TSocket(InetAddress.getLocalHost().getHostName(),
065        port);
066    TTransport transport = sock;
067    if (specifyFramed || implType.isAlwaysFramed()) {
068      transport = new TFramedTransport(transport);
069    }
070
071    sock.open();
072    try {
073      TProtocol tProtocol;
074      if (specifyCompact) {
075        tProtocol = new TCompactProtocol(transport);
076      } else {
077        tProtocol = new TBinaryProtocol(transport);
078      }
079      THBaseService.Client client = new THBaseService.Client(tProtocol);
080      TTableName tTableName = new TTableName();
081      tTableName.setNs(Bytes.toBytes(""));
082      tTableName.setQualifier(Bytes.toBytes(TABLENAME));
083      if (!tableCreated){
084        Assert.assertTrue(!client.tableExists(tTableName));
085        TTableDescriptor tTableDescriptor = new TTableDescriptor();
086        tTableDescriptor.setTableName(tTableName);
087        TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
088        columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
089        tTableDescriptor.addToColumns(columnFamilyDescriptor);
090        client.createTable(tTableDescriptor, new ArrayList<>());
091        tableCreated = true;
092      }
093      Assert.assertTrue(client.tableExists(tTableName));
094    } finally {
095      sock.close();
096    }
097  }
098
099}