You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by dh...@apache.org on 2019/07/22 21:27:51 UTC

[geode] 01/01: Extract reusable rules from GatewayReceiverMetricsTest

This is an automated email from the ASF dual-hosted git repository.

dhemery pushed a commit to branch extract-jar-rules
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 1f14fc0577eb404d7ddab1ef297803ad6e42aee5
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Tue Jul 16 12:58:59 2019 -0700

    Extract reusable rules from GatewayReceiverMetricsTest
---
 .../geode/metrics/GatewayReceiverMetricsTest.java  | 67 ++++------------
 .../rules/MetricsPublishingServiceJarRule.java     | 92 ++++++++++++++++++++++
 .../geode/metrics/rules/SingleFunctionJarRule.java | 65 +++++++++++++++
 3 files changed, 171 insertions(+), 53 deletions(-)

diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/GatewayReceiverMetricsTest.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/GatewayReceiverMetricsTest.java
index 3e2931a..2973eb9 100644
--- a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/GatewayReceiverMetricsTest.java
+++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/GatewayReceiverMetricsTest.java
@@ -18,17 +18,11 @@ package org.apache.geode.metrics;
 import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.jar.JarEntry;
-import java.util.jar.JarOutputStream;
 
 import io.micrometer.core.instrument.Counter;
-import org.apache.commons.io.IOUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -40,7 +34,8 @@ import org.apache.geode.cache.RegionShortcut;
 import org.apache.geode.cache.execute.Function;
 import org.apache.geode.cache.execute.FunctionContext;
 import org.apache.geode.internal.AvailablePortHelper;
-import org.apache.geode.test.compiler.ClassBuilder;
+import org.apache.geode.metrics.rules.MetricsPublishingServiceJarRule;
+import org.apache.geode.metrics.rules.SingleFunctionJarRule;
 import org.apache.geode.test.junit.categories.MetricsTest;
 import org.apache.geode.test.junit.rules.gfsh.GfshRule;
 
@@ -53,12 +48,22 @@ public class GatewayReceiverMetricsTest {
   @Rule
   public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
+  @Rule
+  public MetricsPublishingServiceJarRule metricsPublishingServiceJarRule =
+      new MetricsPublishingServiceJarRule("metrics-publishing-service.jar",
+          SimpleMetricsPublishingService.class);
+
+  @Rule
+  public SingleFunctionJarRule functionJarRule =
+      new SingleFunctionJarRule("function.jar", GetEventsReceivedCountFunction.class);
+
   private static final String SENDER_LOCATOR_NAME = "sender-locator";
   private static final String RECEIVER_LOCATOR_NAME = "receiver-locator";
   private static final String SENDER_SERVER_NAME = "sender-server";
   private static final String RECEIVER_SERVER_NAME = "receiver-server";
   private static final String REGION_NAME = "region";
   private static final String GFSH_COMMAND_SEPARATOR = " ";
+
   private String senderLocatorFolder;
   private String receiverLocatorFolder;
   private String senderServerFolder;
@@ -119,17 +124,13 @@ public class GatewayReceiverMetricsTest {
         "--server-port=" + senderServerPort,
         "--J=-Dgemfire.distributed-system-id=" + senderSystemId);
 
-    String metricsPublishingServiceJarPath =
-        newJarForMetricsPublishingServiceClass(SimpleMetricsPublishingService.class,
-            "metrics-publishing-service.jar");
-
     String startReceiverServerCommand = String.join(GFSH_COMMAND_SEPARATOR,
         "start server",
         "--name=" + RECEIVER_SERVER_NAME,
         "--dir=" + receiverServerFolder,
         "--locators=localhost[" + receiverLocatorPort + "]",
         "--server-port=" + receiverServerPort,
-        "--classpath=" + metricsPublishingServiceJarPath,
+        "--classpath=" + metricsPublishingServiceJarRule.absolutePath(),
         "--J=-Dgemfire.distributed-system-id=" + receiverSystemId);
 
     gfshRule.execute(startSenderLocatorCommand, startReceiverLocatorCommand,
@@ -166,9 +167,7 @@ public class GatewayReceiverMetricsTest {
         createReceiverRegionCommand);
 
     // Deploy function to members
-    String functionJarPath =
-        newJarForFunctionClass(GetEventsReceivedCountFunction.class, "function.jar");
-    String deployCommand = "deploy --jar=" + functionJarPath;
+    String deployCommand = functionJarRule.deployCommand();
     String listFunctionsCommand = "list functions";
 
     gfshRule.execute(connectToReceiverLocatorCommand, deployCommand, listFunctionsCommand);
@@ -229,44 +228,6 @@ public class GatewayReceiverMetricsTest {
     return temporaryFolder.newFolder(folderName).getAbsolutePath();
   }
 
-  private String newJarForFunctionClass(Class clazz, String jarName) throws IOException {
-    File jar = temporaryFolder.newFile(jarName);
-    new ClassBuilder().writeJarFromClass(clazz, jar);
-    return jar.getAbsolutePath();
-  }
-
-  private String newJarForMetricsPublishingServiceClass(Class clazz, String jarName)
-      throws IOException {
-    File jar = temporaryFolder.newFile(jarName);
-
-    String className = clazz.getName();
-    String classAsPath = className.replace('.', '/') + ".class";
-    InputStream stream = clazz.getClassLoader().getResourceAsStream(classAsPath);
-    byte[] bytes = IOUtils.toByteArray(stream);
-    try (FileOutputStream out = new FileOutputStream(jar)) {
-      JarOutputStream jarOutputStream = new JarOutputStream(out);
-
-      // Add the class file to the JAR file
-      JarEntry classEntry = new JarEntry(classAsPath);
-      classEntry.setTime(System.currentTimeMillis());
-      jarOutputStream.putNextEntry(classEntry);
-      jarOutputStream.write(bytes);
-      jarOutputStream.closeEntry();
-
-      String metaInfPath = "META-INF/services/org.apache.geode.metrics.MetricsPublishingService";
-
-      JarEntry metaInfEntry = new JarEntry(metaInfPath);
-      metaInfEntry.setTime(System.currentTimeMillis());
-      jarOutputStream.putNextEntry(metaInfEntry);
-      jarOutputStream.write(className.getBytes());
-      jarOutputStream.closeEntry();
-
-      jarOutputStream.close();
-    }
-
-    return jar.getAbsolutePath();
-  }
-
   public static class GetEventsReceivedCountFunction implements Function<Void> {
     static final String ID = "GetEventsReceivedCountFunction";
 
diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/MetricsPublishingServiceJarRule.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/MetricsPublishingServiceJarRule.java
new file mode 100644
index 0000000..52948a5
--- /dev/null
+++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/MetricsPublishingServiceJarRule.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.geode.metrics.rules;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.rules.ExternalResource;
+
+import org.apache.geode.metrics.MetricsPublishingService;
+import org.apache.geode.test.junit.rules.accessible.AccessibleTemporaryFolder;
+
+public class MetricsPublishingServiceJarRule extends ExternalResource {
+
+  private final AccessibleTemporaryFolder temporaryFolder;
+  private final String jarName;
+  private final Class<? extends MetricsPublishingService> serviceClass;
+
+  private Path jarPath;
+
+  public MetricsPublishingServiceJarRule(String jarName,
+      Class<? extends MetricsPublishingService> serviceClass) {
+    temporaryFolder = new AccessibleTemporaryFolder();
+    this.jarName = jarName;
+    this.serviceClass = serviceClass;
+  }
+
+  @Override
+  protected void before() throws Throwable {
+    temporaryFolder.before();
+    jarPath = newJar(jarName, serviceClass);
+  }
+
+  @Override
+  protected void after() {
+    temporaryFolder.after();
+  }
+
+  public String absolutePath() {
+    return jarPath.toAbsolutePath().toString();
+  }
+
+  private Path newJar(String jarName, Class<? extends MetricsPublishingService> serviceClass)
+      throws IOException {
+    File jar = temporaryFolder.newFile(jarName);
+
+    String className = serviceClass.getName();
+    String classAsPath = className.replace('.', '/') + ".class";
+    InputStream stream = serviceClass.getClassLoader().getResourceAsStream(classAsPath);
+    byte[] bytes = IOUtils.toByteArray(stream);
+    try (FileOutputStream out = new FileOutputStream(jar)) {
+      JarOutputStream jarOutputStream = new JarOutputStream(out);
+
+      // Add the class file to the JAR file
+      JarEntry classEntry = new JarEntry(classAsPath);
+      classEntry.setTime(System.currentTimeMillis());
+      jarOutputStream.putNextEntry(classEntry);
+      jarOutputStream.write(bytes);
+      jarOutputStream.closeEntry();
+
+      String metaInfPath = "META-INF/services/org.apache.geode.metrics.MetricsPublishingService";
+
+      JarEntry metaInfEntry = new JarEntry(metaInfPath);
+      metaInfEntry.setTime(System.currentTimeMillis());
+      jarOutputStream.putNextEntry(metaInfEntry);
+      jarOutputStream.write(className.getBytes());
+      jarOutputStream.closeEntry();
+
+      jarOutputStream.close();
+    }
+
+    return jar.toPath();
+  }
+}
diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/SingleFunctionJarRule.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/SingleFunctionJarRule.java
new file mode 100644
index 0000000..0d4b205
--- /dev/null
+++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/rules/SingleFunctionJarRule.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.geode.metrics.rules;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+
+import org.junit.rules.ExternalResource;
+
+import org.apache.geode.cache.execute.Function;
+import org.apache.geode.test.compiler.ClassBuilder;
+import org.apache.geode.test.junit.rules.accessible.AccessibleTemporaryFolder;
+
+public class SingleFunctionJarRule extends ExternalResource {
+
+  private final AccessibleTemporaryFolder temporaryFolder;
+  private final String jarName;
+  private final Class<? extends Function> functionClass;
+
+  private Path jarPath;
+
+  public SingleFunctionJarRule(String jarName, Class<? extends Function> functionClass) {
+    temporaryFolder = new AccessibleTemporaryFolder();
+    this.functionClass = functionClass;
+    this.jarName = jarName;
+  }
+
+  @Override
+  protected void before() throws Throwable {
+    temporaryFolder.before();
+    jarPath = newJar(jarName, functionClass);
+  }
+
+  @Override
+  protected void after() {
+    temporaryFolder.after();
+  }
+
+  public String absolutePath() {
+    return jarPath.toAbsolutePath().toString();
+  }
+
+  public String deployCommand() {
+    return "deploy --jar=" + absolutePath();
+  }
+
+  private Path newJar(String jarName, Class<? extends Function> functionClass) throws IOException {
+    File jar = temporaryFolder.newFile(jarName);
+    new ClassBuilder().writeJarFromClass(functionClass, jar);
+    return jar.toPath();
+  }
+}