prakapenka closed pull request #733: Add AutoClosable for MiniAccumuloCluster
URL: https://github.com/apache/accumulo/pull/733
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
index 5660f70b50..1dd7f9aa06 100644
---
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
+++
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
@@ -40,7 +40,7 @@
*
* @since 1.5.0
*/
-public class MiniAccumuloCluster {
+public class MiniAccumuloCluster implements AutoCloseable {
private MiniAccumuloClusterImpl impl;
@@ -167,4 +167,9 @@ public static ClientInfo getClientInfo(File directory) {
Preconditions.checkArgument(clientProps.exists());
return
Accumulo.newClient().usingProperties(clientProps.getAbsolutePath()).info();
}
+
+ @Override
+ public void close() throws Exception {
+ this.stop();
+ }
}
diff --git
a/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
b/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
index a9d5197eb3..dfbcdd415e 100644
---
a/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
+++
b/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
@@ -21,22 +21,20 @@
import static org.junit.Assert.assertTrue;
import java.io.File;
-import java.io.IOException;
import java.util.Map;
+import org.apache.accumulo.core.client.AccumuloClient;
import org.apache.accumulo.core.client.impl.ClientContext;
+import org.apache.accumulo.core.client.security.tokens.PasswordToken;
import org.apache.commons.io.FileUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.TestingServer;
-import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -46,68 +44,44 @@
private static final String SECRET = "superSecret";
- private static final Logger log = LoggerFactory
- .getLogger(MiniAccumuloClusterExistingZooKeepersTest.class);
- private TestingServer zooKeeper;
- private MiniAccumuloCluster accumulo;
+ private MiniAccumuloConfig config;
@Rule
public TestName testName = new TestName();
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "intput
determined by test")
@Before
- public void setupTestCluster() throws Exception {
+ public void setupTestCluster() {
assertTrue(BASE_DIR.mkdirs() || BASE_DIR.isDirectory());
File testDir = new File(BASE_DIR, testName.getMethodName());
FileUtils.deleteQuietly(testDir);
assertTrue(testDir.mkdir());
-
- zooKeeper = new TestingServer();
-
- MiniAccumuloConfig config = new MiniAccumuloConfig(testDir, SECRET);
- config.getImpl().setExistingZooKeepers(zooKeeper.getConnectString());
- accumulo = new MiniAccumuloCluster(config);
- accumulo.start();
- }
-
- @After
- public void teardownTestCluster() {
- if (accumulo != null) {
- try {
- accumulo.stop();
- } catch (IOException | InterruptedException e) {
- log.warn("Failure during tear down", e);
- }
- }
-
- if (zooKeeper != null) {
- try {
- zooKeeper.close();
- } catch (IOException e) {
- log.warn("Failure stopping test ZooKeeper server");
- }
- }
+ config = new MiniAccumuloConfig(testDir, SECRET);
}
- @SuppressWarnings("deprecation")
@Test
public void canConnectViaExistingZooKeeper() throws Exception {
- org.apache.accumulo.core.client.Connector conn =
accumulo.getConnector("root", SECRET);
- ClientContext context = new ClientContext(accumulo.getClientInfo());
- assertEquals(zooKeeper.getConnectString(), context.getZooKeepers());
-
- String tableName = "foo";
- conn.tableOperations().create(tableName);
- Map<String,String> tableIds = conn.tableOperations().tableIdMap();
- assertTrue(tableIds.containsKey(tableName));
-
- String zkTablePath = String.format("/accumulo/%s/tables/%s/name",
context.getInstanceID(),
- tableIds.get(tableName));
- try (CuratorFramework client =
CuratorFrameworkFactory.newClient(zooKeeper.getConnectString(),
- new RetryOneTime(1))) {
- client.start();
- assertNotNull(client.checkExists().forPath(zkTablePath));
- assertEquals(tableName, new
String(client.getData().forPath(zkTablePath)));
+ try(TestingServer zooKeeper = new TestingServer();
+ MiniAccumuloCluster accumulo = new
MiniAccumuloCluster(config.setZooKeeperPort(zooKeeper.getPort()))
+ ) {
+ accumulo.start();
+
+ AccumuloClient client = accumulo.getAccumuloClient("root", new
PasswordToken(SECRET));
+ ClientContext context = new ClientContext(accumulo.getClientInfo());
+
+ String tableName = "foo";
+ client.tableOperations().create(tableName);
+ Map<String, String> tableIds = client.tableOperations().tableIdMap();
+ assertTrue(tableIds.containsKey(tableName));
+
+ String zkTablePath = String.format("/accumulo/%s/tables/%s/name",
context.getInstanceID(),
+ tableIds.get(tableName));
+ try (CuratorFramework zkClient =
CuratorFrameworkFactory.newClient(zooKeeper.getConnectString(),
+ new RetryOneTime(1))) {
+ zkClient.start();
+ assertNotNull(zkClient.checkExists().forPath(zkTablePath));
+ assertEquals(tableName, new
String(zkClient.getData().forPath(zkTablePath)));
+ }
}
}
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services