001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mob;
020
021import java.io.IOException;
022import java.util.List;
023import java.util.Random;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.client.Result;
029import org.apache.hadoop.hbase.client.ResultScanner;
030import org.apache.hadoop.hbase.client.Scan;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.regionserver.StoreFileWriter;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.Assert;
035
036public class MobTestUtil {
037  protected static final char FIRST_CHAR = 'a';
038  protected static final char LAST_CHAR = 'z';
039
040  protected static String generateRandomString(int demoLength) {
041    String base = "abcdefghijklmnopqrstuvwxyz";
042    Random random = new Random();
043    StringBuilder sb = new StringBuilder();
044    for (int i = 0; i < demoLength; i++) {
045      int number = random.nextInt(base.length());
046      sb.append(base.charAt(number));
047    }
048    return sb.toString();
049  }
050  protected static void writeStoreFile(final StoreFileWriter writer, String caseName)
051      throws IOException {
052    writeStoreFile(writer, Bytes.toBytes(caseName), Bytes.toBytes(caseName));
053  }
054
055  /*
056   * Writes HStoreKey and ImmutableBytes data to passed writer and then closes
057   * it.
058   *
059   * @param writer
060   *
061   * @throws IOException
062   */
063  private static void writeStoreFile(final StoreFileWriter writer, byte[] fam,
064      byte[] qualifier) throws IOException {
065    long now = System.currentTimeMillis();
066    try {
067      for (char d = FIRST_CHAR; d <= LAST_CHAR; d++) {
068        for (char e = FIRST_CHAR; e <= LAST_CHAR; e++) {
069          byte[] b = new byte[] { (byte) d, (byte) e };
070          writer.append(new KeyValue(b, fam, qualifier, now, b));
071        }
072      }
073    } finally {
074      writer.close();
075    }
076  }
077
078  /**
079   * Compare two Cells only for their row family qualifier value
080   */
081  public static void assertCellEquals(Cell firstKeyValue, Cell secondKeyValue) {
082    Assert.assertArrayEquals(CellUtil.cloneRow(firstKeyValue),
083        CellUtil.cloneRow(secondKeyValue));
084    Assert.assertArrayEquals(CellUtil.cloneFamily(firstKeyValue),
085        CellUtil.cloneFamily(secondKeyValue));
086    Assert.assertArrayEquals(CellUtil.cloneQualifier(firstKeyValue),
087        CellUtil.cloneQualifier(secondKeyValue));
088    Assert.assertArrayEquals(CellUtil.cloneValue(firstKeyValue),
089        CellUtil.cloneValue(secondKeyValue));
090  }
091
092  public static void assertCellsValue(Table table, Scan scan,
093      byte[] expectedValue, int expectedCount) throws IOException {
094    ResultScanner results = table.getScanner(scan);
095    int count = 0;
096    for (Result res : results) {
097      List<Cell> cells = res.listCells();
098      for(Cell cell : cells) {
099        // Verify the value
100        Assert.assertArrayEquals(expectedValue, CellUtil.cloneValue(cell));
101        count++;
102      }
103    }
104    results.close();
105    Assert.assertEquals(expectedCount, count);
106  }
107}