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;
019
020import java.io.IOException;
021import java.util.concurrent.CompletableFuture;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.client.AsyncConnection;
024import org.apache.hadoop.hbase.client.ConnectionFactory;
025import org.junit.ClassRule;
026import org.junit.Rule;
027import org.junit.rules.ExternalResource;
028import org.junit.rules.TestRule;
029
030/**
031 * A {@link TestRule} that manages an instance of the {@link SingleProcessHBaseCluster}. Can be used
032 * in either the {@link Rule} or {@link ClassRule} positions. Built on top of an instance of
033 * {@link HBaseTestingUtil}, so be weary of intermixing direct use of that class with this Rule.
034 * </p>
035 * Use in combination with {@link ConnectionRule}, for example:
036 *
037 * <pre>
038 * {
039 *   &#64;code
040 *   public class TestMyClass {
041 *     &#64;ClassRule
042 *     public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
043 *
044 *     &#64;Rule
045 *     public final ConnectionRule connectionRule =
046 *       new ConnectionRule(miniClusterRule::createConnection);
047 *   }
048 * }
049 * </pre>
050 */
051public final class MiniClusterRule extends ExternalResource {
052
053  /**
054   * A builder for fluent composition of a new {@link MiniClusterRule}.
055   */
056  public static class Builder {
057
058    private StartTestingClusterOption miniClusterOption;
059    private Configuration conf;
060
061    /**
062     * Use the provided {@link StartTestingClusterOption} to construct the
063     * {@link SingleProcessHBaseCluster}.
064     */
065    public Builder setMiniClusterOption(final StartTestingClusterOption miniClusterOption) {
066      this.miniClusterOption = miniClusterOption;
067      return this;
068    }
069
070    /**
071     * Seed the underlying {@link HBaseTestingUtil} with the provided {@link Configuration}.
072     */
073    public Builder setConfiguration(final Configuration conf) {
074      this.conf = conf;
075      return this;
076    }
077
078    public MiniClusterRule build() {
079      return new MiniClusterRule(conf, miniClusterOption != null ? miniClusterOption :
080        StartTestingClusterOption.builder().build());
081    }
082  }
083
084  private final HBaseTestingUtil testingUtility;
085  private final StartTestingClusterOption miniClusterOptions;
086
087  private SingleProcessHBaseCluster miniCluster;
088
089  private MiniClusterRule(final Configuration conf,
090    final StartTestingClusterOption miniClusterOptions) {
091    this.testingUtility = new HBaseTestingUtil(conf);
092    this.miniClusterOptions = miniClusterOptions;
093  }
094
095  public static Builder newBuilder() {
096    return new Builder();
097  }
098
099  /**
100   * Returns the underlying instance of {@link HBaseTestingUtil}
101   */
102  public HBaseTestingUtil getTestingUtility() {
103    return testingUtility;
104  }
105
106  /**
107   * Create a {@link AsyncConnection} to the managed {@link SingleProcessHBaseCluster}. It's up to
108   * the caller to {@link AsyncConnection#close() close()} the connection when finished.
109   */
110  public CompletableFuture<AsyncConnection> createConnection() {
111    if (miniCluster == null) {
112      throw new IllegalStateException("test cluster not initialized");
113    }
114    return ConnectionFactory.createAsyncConnection(miniCluster.getConf());
115  }
116
117  @Override
118  protected void before() throws Throwable {
119    miniCluster = testingUtility.startMiniCluster(miniClusterOptions);
120  }
121
122  @Override
123  protected void after() {
124    try {
125      testingUtility.shutdownMiniCluster();
126    } catch (IOException e) {
127      throw new RuntimeException(e);
128    }
129  }
130}