HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / MiniClusterRule.java
blob44a8caeea3366f7906e9b9dfdbc47a6ff732af4e
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase;
20 import java.io.IOException;
21 import java.util.concurrent.CompletableFuture;
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.client.AsyncConnection;
24 import org.apache.hadoop.hbase.client.ConnectionFactory;
25 import org.junit.ClassRule;
26 import org.junit.Rule;
27 import org.junit.rules.ExternalResource;
28 import org.junit.rules.TestRule;
30 /**
31 * A {@link TestRule} that manages an instance of the {@link SingleProcessHBaseCluster}. Can be used
32 * in either the {@link Rule} or {@link ClassRule} positions. Built on top of an instance of
33 * {@link HBaseTestingUtil}, so be weary of intermixing direct use of that class with this Rule.
34 * </p>
35 * Use in combination with {@link ConnectionRule}, for example:
37 * <pre>
38 * {
39 * &#64;code
40 * public class TestMyClass {
41 * &#64;ClassRule
42 * public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
44 * &#64;Rule
45 * public final ConnectionRule connectionRule =
46 * new ConnectionRule(miniClusterRule::createConnection);
47 * }
48 * }
49 * </pre>
51 public final class MiniClusterRule extends ExternalResource {
53 /**
54 * A builder for fluent composition of a new {@link MiniClusterRule}.
56 public static class Builder {
58 private StartTestingClusterOption miniClusterOption;
59 private Configuration conf;
61 /**
62 * Use the provided {@link StartTestingClusterOption} to construct the
63 * {@link SingleProcessHBaseCluster}.
65 public Builder setMiniClusterOption(final StartTestingClusterOption miniClusterOption) {
66 this.miniClusterOption = miniClusterOption;
67 return this;
70 /**
71 * Seed the underlying {@link HBaseTestingUtil} with the provided {@link Configuration}.
73 public Builder setConfiguration(final Configuration conf) {
74 this.conf = conf;
75 return this;
78 public MiniClusterRule build() {
79 return new MiniClusterRule(conf, miniClusterOption != null ? miniClusterOption :
80 StartTestingClusterOption.builder().build());
84 private final HBaseTestingUtil testingUtility;
85 private final StartTestingClusterOption miniClusterOptions;
87 private SingleProcessHBaseCluster miniCluster;
89 private MiniClusterRule(final Configuration conf,
90 final StartTestingClusterOption miniClusterOptions) {
91 this.testingUtility = new HBaseTestingUtil(conf);
92 this.miniClusterOptions = miniClusterOptions;
95 public static Builder newBuilder() {
96 return new Builder();
99 /**
100 * Returns the underlying instance of {@link HBaseTestingUtil}
102 public HBaseTestingUtil getTestingUtility() {
103 return testingUtility;
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.
110 public CompletableFuture<AsyncConnection> createConnection() {
111 if (miniCluster == null) {
112 throw new IllegalStateException("test cluster not initialized");
114 return ConnectionFactory.createAsyncConnection(miniCluster.getConf());
117 @Override
118 protected void before() throws Throwable {
119 miniCluster = testingUtility.startMiniCluster(miniClusterOptions);
122 @Override
123 protected void after() {
124 try {
125 testingUtility.shutdownMiniCluster();
126 } catch (IOException e) {
127 throw new RuntimeException(e);