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 java.util.concurrent.ExecutionException;
023import java.util.function.Supplier;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.client.AsyncConnection;
026import org.apache.hadoop.hbase.client.Connection;
027import org.apache.hadoop.hbase.client.ConnectionFactory;
028import org.junit.ClassRule;
029import org.junit.Rule;
030import org.junit.rules.ExternalResource;
031import org.junit.rules.TestRule;
032
033/**
034 * A {@link TestRule} that manages an instance of the {@link SingleProcessHBaseCluster}. Can be used
035 * in either the {@link Rule} or {@link ClassRule} positions. Built on top of an instance of
036 * {@link HBaseTestingUtil}, so be weary of intermixing direct use of that class with this Rule.
037 * </p>
038 * Use in combination with {@link ConnectionRule}, for example:
039 *
040 * <pre>
041 * {
042 *   &#64;code
043 *   public class TestMyClass {
044 *     &#64;ClassRule
045 *     public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
046 *
047 *     &#64;Rule
048 *     public final ConnectionRule connectionRule =
049 *       ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
050 *   }
051 * }
052 * </pre>
053 *
054 * @deprecated Use {@link MiniClusterExtension} instead, Once we finish the migration of JUnit5,
055 *             which means we do not need {@link MiniClusterRule} any more, we can remove these
056 *             dependencies, see
057 *             <a href="https://issues.apache.org/jira/browse/HBASE-23671">HBASE-23671</a> for more
058 *             details.
059 */
060@Deprecated
061public final class MiniClusterRule extends ExternalResource {
062
063  /**
064   * A builder for fluent composition of a new {@link MiniClusterRule}.
065   */
066  public static class Builder {
067
068    private StartTestingClusterOption miniClusterOption;
069    private Configuration conf;
070
071    /**
072     * Use the provided {@link StartTestingClusterOption} to construct the
073     * {@link SingleProcessHBaseCluster}.
074     */
075    public Builder setMiniClusterOption(final StartTestingClusterOption miniClusterOption) {
076      this.miniClusterOption = miniClusterOption;
077      return this;
078    }
079
080    /**
081     * Seed the underlying {@link HBaseTestingUtil} with the provided {@link Configuration}.
082     */
083    public Builder setConfiguration(final Configuration conf) {
084      this.conf = conf;
085      return this;
086    }
087
088    public Builder setConfiguration(Supplier<Configuration> supplier) {
089      return setConfiguration(supplier.get());
090    }
091
092    public MiniClusterRule build() {
093      return new MiniClusterRule(conf,
094        miniClusterOption != null
095          ? miniClusterOption
096          : StartTestingClusterOption.builder().build());
097    }
098  }
099
100  private final HBaseTestingUtil testingUtility;
101  private final StartTestingClusterOption miniClusterOptions;
102
103  private SingleProcessHBaseCluster miniCluster;
104
105  private MiniClusterRule(final Configuration conf,
106    final StartTestingClusterOption miniClusterOptions) {
107    this.testingUtility = new HBaseTestingUtil(conf);
108    this.miniClusterOptions = miniClusterOptions;
109  }
110
111  public static Builder newBuilder() {
112    return new Builder();
113  }
114
115  /**
116   * Returns the underlying instance of {@link HBaseTestingUtil}
117   */
118  public HBaseTestingUtil getTestingUtility() {
119    return testingUtility;
120  }
121
122  /**
123   * Create a {@link Connection} to the managed {@link SingleProcessHBaseCluster}. It's up to the
124   * caller to {@link Connection#close() close()} the connection when finished.
125   */
126  public Connection createConnection() {
127    try {
128      return createAsyncConnection().get().toConnection();
129    } catch (InterruptedException | ExecutionException e) {
130      throw new RuntimeException(e);
131    }
132  }
133
134  /**
135   * Create a {@link AsyncConnection} to the managed {@link SingleProcessHBaseCluster}. It's up to
136   * the caller to {@link AsyncConnection#close() close()} the connection when finished.
137   */
138  public CompletableFuture<AsyncConnection> createAsyncConnection() {
139    if (miniCluster == null) {
140      throw new IllegalStateException("test cluster not initialized");
141    }
142    return ConnectionFactory.createAsyncConnection(miniCluster.getConf());
143  }
144
145  @Override
146  protected void before() throws Throwable {
147    miniCluster = testingUtility.startMiniCluster(miniClusterOptions);
148  }
149
150  @Override
151  protected void after() {
152    try {
153      testingUtility.shutdownMiniCluster();
154    } catch (IOException e) {
155      throw new RuntimeException(e);
156    }
157  }
158}