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
;
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.
35 * Use in combination with {@link ConnectionRule}, for example:
40 * public class TestMyClass {
42 * public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
45 * public final ConnectionRule connectionRule =
46 * new ConnectionRule(miniClusterRule::createConnection);
51 public final class MiniClusterRule
extends ExternalResource
{
54 * A builder for fluent composition of a new {@link MiniClusterRule}.
56 public static class Builder
{
58 private StartTestingClusterOption miniClusterOption
;
59 private Configuration conf
;
62 * Use the provided {@link StartTestingClusterOption} to construct the
63 * {@link SingleProcessHBaseCluster}.
65 public Builder
setMiniClusterOption(final StartTestingClusterOption miniClusterOption
) {
66 this.miniClusterOption
= miniClusterOption
;
71 * Seed the underlying {@link HBaseTestingUtil} with the provided {@link Configuration}.
73 public Builder
setConfiguration(final Configuration conf
) {
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() {
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());
118 protected void before() throws Throwable
{
119 miniCluster
= testingUtility
.startMiniCluster(miniClusterOptions
);
123 protected void after() {
125 testingUtility
.shutdownMiniCluster();
126 } catch (IOException e
) {
127 throw new RuntimeException(e
);