You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by jb...@apache.org on 2021/11/03 03:36:19 UTC

[geode] 01/01: getDetails as Map.

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

jbarrett pushed a commit to branch pull/7071
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 4f61486fce8d85467824eb94dade1fd0bae9c27f
Author: Jacob Barrett <jb...@pivotal.io>
AuthorDate: Tue Nov 2 20:34:41 2021 -0700

    getDetails as Map<String, String>.
---
 .../geode/internal/version/ComponentVersion.java   |  4 +-
 .../internal/version/DistributionVersion.java      |  4 +-
 .../internal/util/ProductVersionUtilTest.java      | 87 ++++++++++++++++++++++
 ....apache.geode.internal.version.ComponentVersion | 16 ++++
 ...ache.geode.internal.version.DistributionVersion | 16 ++++
 .../org/apache/geode/internal/GemFireVersion.java  |  7 ++
 .../org/apache/geode/internal/GeodeVersion.java    |  6 +-
 .../apache/geode/internal/VersionDescription.java  |  8 ++
 .../geode/internal/util/ProductVersionUtil.java    | 48 +++++++++---
 ....apache.geode.internal.version.ComponentVersion | 15 ++++
 .../internal/util/ProductVersionUtilTest.java      | 53 +++++++++++++
 .../geode/management/internal/cli/Launcher.java    |  6 +-
 .../geode/management/internal/cli/shell/Gfsh.java  |  6 +-
 .../internal/web/http/support/HttpRequester.java   |  6 +-
 14 files changed, 259 insertions(+), 23 deletions(-)

diff --git a/geode-common/src/main/java/org/apache/geode/internal/version/ComponentVersion.java b/geode-common/src/main/java/org/apache/geode/internal/version/ComponentVersion.java
index 9d2b109..dafbe10 100644
--- a/geode-common/src/main/java/org/apache/geode/internal/version/ComponentVersion.java
+++ b/geode-common/src/main/java/org/apache/geode/internal/version/ComponentVersion.java
@@ -14,6 +14,8 @@
  */
 package org.apache.geode.internal.version;
 
+import java.util.Map;
+
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -63,5 +65,5 @@ public interface ComponentVersion {
    * @return Detailed component version information.
    */
   @NotNull
-  String getDetails();
+  Map<@NotNull String, @NotNull String> getDetails();
 }
diff --git a/geode-common/src/main/java/org/apache/geode/internal/version/DistributionVersion.java b/geode-common/src/main/java/org/apache/geode/internal/version/DistributionVersion.java
index 3cdf0c9..ea70ddd 100644
--- a/geode-common/src/main/java/org/apache/geode/internal/version/DistributionVersion.java
+++ b/geode-common/src/main/java/org/apache/geode/internal/version/DistributionVersion.java
@@ -14,6 +14,8 @@
  */
 package org.apache.geode.internal.version;
 
+import java.util.Map;
+
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -67,5 +69,5 @@ public interface DistributionVersion {
    * @return Detailed distribution version information.
    */
   @NotNull
-  String getDetails();
+  Map<@NotNull String, @NotNull String> getDetails();
 }
diff --git a/geode-core/src/integrationTest/java/org/apache/geode/internal/util/ProductVersionUtilTest.java b/geode-core/src/integrationTest/java/org/apache/geode/internal/util/ProductVersionUtilTest.java
new file mode 100644
index 0000000..35d4ca5
--- /dev/null
+++ b/geode-core/src/integrationTest/java/org/apache/geode/internal/util/ProductVersionUtilTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.internal.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jetbrains.annotations.NotNull;
+import org.junit.Test;
+
+import org.apache.geode.internal.GeodeVersion;
+import org.apache.geode.internal.version.ComponentVersion;
+import org.apache.geode.internal.version.DistributionVersion;
+
+public class ProductVersionUtilTest {
+  @Test
+  public void getDistributionVersionReturnFakeVersion() {
+    assertThat(ProductVersionUtil.getDistributionVersion()).isInstanceOf(FakeVersion.class);
+  }
+
+  @Test
+  public void getComponentVersionsReturnsGeodeVersionOnly() {
+    assertThat(ProductVersionUtil.getComponentVersions())
+        .hasSize(2)
+        .hasAtLeastOneElementOfType(GeodeVersion.class)
+        .hasAtLeastOneElementOfType(FakeVersion.class);
+  }
+
+  @Test
+  public void appendFullVersionAppendsGeodeVersion() throws IOException {
+    assertThat(ProductVersionUtil.appendFullVersion(new StringBuilder()))
+        .contains("Apache Geode")
+        .contains("Source-Revision")
+        .contains("Build-Id")
+        .contains("Fake Distribution")
+        .contains("Fake-Attribute");
+  }
+
+  @Test
+  public void getFullVersionContainsGeodeVersion() {
+    assertThat(ProductVersionUtil.getFullVersion())
+        .contains("Apache Geode")
+        .contains("Source-Revision")
+        .contains("Build-Id")
+        .contains("Fake Distribution")
+        .contains("Fake-Attribute");
+  }
+
+  public static class FakeVersion implements DistributionVersion, ComponentVersion {
+
+    @Override
+    public @NotNull String getName() {
+      return "Fake Distribution";
+    }
+
+    @Override
+    public @NotNull String getVersion() {
+      return "1.2.3";
+    }
+
+    @Override
+    public @NotNull Map<@NotNull String, @NotNull String> getDetails() {
+      return new HashMap<String, String>() {
+        {
+          put("Version", getVersion());
+          put("Fake-Attribute", "42");
+        }
+      };
+    }
+  }
+}
diff --git a/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion b/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion
new file mode 100644
index 0000000..c0460a3
--- /dev/null
+++ b/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion
@@ -0,0 +1,16 @@
+#
+# 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.
+#
+
+org.apache.geode.internal.util.ProductVersionUtilTest$FakeVersion
diff --git a/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.DistributionVersion b/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.DistributionVersion
new file mode 100644
index 0000000..c0460a3
--- /dev/null
+++ b/geode-core/src/integrationTest/resources/META-INF/services/org.apache.geode.internal.version.DistributionVersion
@@ -0,0 +1,16 @@
+#
+# 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.
+#
+
+org.apache.geode.internal.util.ProductVersionUtilTest$FakeVersion
diff --git a/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java b/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
index 0bb236d..b1c637c 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
@@ -31,8 +31,11 @@ import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.URL;
+import java.util.Map;
 import java.util.StringTokenizer;
 
+import org.jetbrains.annotations.NotNull;
+
 import org.apache.geode.annotations.Immutable;
 
 /**
@@ -145,4 +148,8 @@ public class GemFireVersion {
     }
     return csLoc;
   }
+
+  public static @NotNull Map<String, String> asMap() {
+    return description.asMap();
+  }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/GeodeVersion.java b/geode-core/src/main/java/org/apache/geode/internal/GeodeVersion.java
index 7857283..a957eb7 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/GeodeVersion.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/GeodeVersion.java
@@ -14,6 +14,8 @@
  */
 package org.apache.geode.internal;
 
+import java.util.Map;
+
 import org.jetbrains.annotations.NotNull;
 
 import org.apache.geode.internal.version.ComponentVersion;
@@ -32,7 +34,7 @@ public class GeodeVersion implements ComponentVersion, DistributionVersion {
   }
 
   @Override
-  public @NotNull String getDetails() {
-    return GemFireVersion.asString();
+  public @NotNull Map<@NotNull String, @NotNull String> getDetails() {
+    return GemFireVersion.asMap();
   }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/VersionDescription.java b/geode-core/src/main/java/org/apache/geode/internal/VersionDescription.java
index 092a5ff..a6e55dd 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/VersionDescription.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/VersionDescription.java
@@ -21,11 +21,14 @@ import static org.apache.geode.internal.lang.SystemUtils.getOsVersion;
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.net.UnknownHostException;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.TreeMap;
 
+import org.jetbrains.annotations.NotNull;
+
 import org.apache.geode.internal.classloader.ClassPathLoader;
 import org.apache.geode.internal.inet.LocalHostUtil;
 
@@ -114,6 +117,11 @@ public class VersionDescription {
     return error.orElseGet(() -> description.getProperty(key));
   }
 
+  @SuppressWarnings("unchecked")
+  public @NotNull Map<String, String> asMap() {
+    return (Map<String, String>) (Map<?, ?>) description;
+  }
+
   void print(PrintWriter pw) {
     if (error.isPresent()) {
       pw.println(error.get());
diff --git a/geode-core/src/main/java/org/apache/geode/internal/util/ProductVersionUtil.java b/geode-core/src/main/java/org/apache/geode/internal/util/ProductVersionUtil.java
index 221a03d..0edbe3b 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/util/ProductVersionUtil.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/util/ProductVersionUtil.java
@@ -12,17 +12,27 @@
  * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  */
+
 package org.apache.geode.internal.util;
 
+import static java.lang.System.lineSeparator;
+
+import java.io.IOException;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.ServiceLoader;
 
+import org.jetbrains.annotations.NotNull;
+
 import org.apache.geode.internal.GeodeVersion;
 import org.apache.geode.internal.version.ComponentVersion;
 import org.apache.geode.internal.version.DistributionVersion;
 
 public class ProductVersionUtil {
-  public static DistributionVersion getProductVersion() {
+
+  public static final String line = "----------------------------------------";
+
+  public static @NotNull DistributionVersion getDistributionVersion() {
     final ServiceLoader<DistributionVersion> loader = ServiceLoader.load(DistributionVersion.class);
     final Iterator<DistributionVersion> loaderIter = loader.iterator();
     if (loaderIter.hasNext()) {
@@ -31,14 +41,32 @@ public class ProductVersionUtil {
     return new GeodeVersion();
   }
 
-  public static String getFullVersion() {
-    ServiceLoader<ComponentVersion> loader = ServiceLoader.load(ComponentVersion.class);
-    StringBuilder versionString = new StringBuilder();
-    loader.forEach(v -> versionString
-        .append("----------------------------------------\n")
-        .append(v.getName()).append("\n")
-        .append("----------------------------------------\n")
-        .append(v.getDetails()));
-    return versionString.toString();
+  public static @NotNull Iterable<ComponentVersion> getComponentVersions() {
+    return ServiceLoader.load(ComponentVersion.class);
   }
+
+  public static <T extends Appendable> @NotNull T appendFullVersion(final @NotNull T appendable)
+      throws IOException {
+    for (final ComponentVersion version : getComponentVersions()) {
+      appendable
+          .append(line).append(lineSeparator())
+          .append(version.getName()).append(lineSeparator())
+          .append("----------------------------------------").append(lineSeparator());
+      for (final Map.Entry<String, String> entry : version.getDetails().entrySet()) {
+        appendable.append(entry.getKey()).append(": ").append(entry.getValue())
+            .append(lineSeparator());
+      }
+    }
+
+    return appendable;
+  }
+
+  public static @NotNull String getFullVersion() {
+    try {
+      return appendFullVersion(new StringBuilder()).toString();
+    } catch (IOException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
 }
diff --git a/geode-core/src/main/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion b/geode-core/src/main/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion
index f9785c1..3184f5e 100644
--- a/geode-core/src/main/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion
+++ b/geode-core/src/main/resources/META-INF/services/org.apache.geode.internal.version.ComponentVersion
@@ -1 +1,16 @@
+#
+# 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.
+#
+
 org.apache.geode.internal.GeodeVersion
diff --git a/geode-core/src/test/java/org/apache/geode/internal/util/ProductVersionUtilTest.java b/geode-core/src/test/java/org/apache/geode/internal/util/ProductVersionUtilTest.java
new file mode 100644
index 0000000..28ec38e
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/internal/util/ProductVersionUtilTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.internal.util;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import org.apache.geode.internal.GeodeVersion;
+
+public class ProductVersionUtilTest {
+
+  @Test
+  public void getDistributionVersionReturnGeodeVersion() {
+    assertThat(ProductVersionUtil.getDistributionVersion()).isInstanceOf(GeodeVersion.class);
+  }
+
+  @Test
+  public void getComponentVersionsReturnsGeodeVersionOnly() {
+    assertThat(ProductVersionUtil.getComponentVersions()).singleElement()
+        .isInstanceOf(GeodeVersion.class);
+  }
+
+  @Test
+  public void appendFullVersionAppendsGeodeVersion() throws IOException {
+    assertThat(ProductVersionUtil.appendFullVersion(new StringBuilder())).contains("Apache Geode");
+  }
+
+  @Test
+  public void getFullVersionContainsGeodeVersion() {
+    assertThat(ProductVersionUtil.getFullVersion())
+        .contains("Apache Geode")
+        .contains("Source-Revision")
+        .contains("Build-Id");
+  }
+
+}
diff --git a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/Launcher.java b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/Launcher.java
index d61319a..bf89057 100755
--- a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/Launcher.java
+++ b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/Launcher.java
@@ -14,7 +14,7 @@
  */
 package org.apache.geode.management.internal.cli;
 
-import static org.apache.geode.internal.util.ProductVersionUtil.getProductVersion;
+import static org.apache.geode.internal.util.ProductVersionUtil.getDistributionVersion;
 
 import java.io.PrintStream;
 import java.util.HashSet;
@@ -264,8 +264,8 @@ public class Launcher {
 
   private void printUsage(final Gfsh gfsh, final PrintStream stream) {
     int terminalWidth = gfsh.getTerminalWidth();
-    stream.print(getProductVersion().getName() + " v");
-    stream.print(getProductVersion().getVersion());
+    stream.print(getDistributionVersion().getName() + " v");
+    stream.print(getDistributionVersion().getVersion());
     stream.println(" Command Line Shell" + GfshParser.LINE_SEPARATOR);
     stream.println("USAGE");
     stream.println(
diff --git a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
index 929f40a..53da960 100755
--- a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
+++ b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/shell/Gfsh.java
@@ -14,8 +14,8 @@
  */
 package org.apache.geode.management.internal.cli.shell;
 
+import static org.apache.geode.internal.util.ProductVersionUtil.getDistributionVersion;
 import static org.apache.geode.internal.util.ProductVersionUtil.getFullVersion;
-import static org.apache.geode.internal.util.ProductVersionUtil.getProductVersion;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -680,7 +680,7 @@ public class Gfsh extends JLineShell {
   }
 
   private String getShortVersion() {
-    return getProductVersion().getVersion();
+    return getDistributionVersion().getVersion();
   }
 
   public String getGeodeSerializationVersion() {
@@ -688,7 +688,7 @@ public class Gfsh extends JLineShell {
   }
 
   public String getWelcomeMessage() {
-    return ansiHandler.decorateString("Monitor and Manage " + getProductVersion().getName(),
+    return ansiHandler.decorateString("Monitor and Manage " + getDistributionVersion().getName(),
         ANSIStyle.CYAN);
   }
 
diff --git a/geode-gfsh/src/main/java/org/apache/geode/management/internal/web/http/support/HttpRequester.java b/geode-gfsh/src/main/java/org/apache/geode/management/internal/web/http/support/HttpRequester.java
index b3830ff..a6c894e 100644
--- a/geode-gfsh/src/main/java/org/apache/geode/management/internal/web/http/support/HttpRequester.java
+++ b/geode-gfsh/src/main/java/org/apache/geode/management/internal/web/http/support/HttpRequester.java
@@ -15,7 +15,7 @@
 
 package org.apache.geode.management.internal.web.http.support;
 
-import static org.apache.geode.internal.util.ProductVersionUtil.getProductVersion;
+import static org.apache.geode.internal.util.ProductVersionUtil.getDistributionVersion;
 
 import java.io.IOException;
 import java.net.URI;
@@ -62,8 +62,8 @@ public class HttpRequester {
   private Properties securityProperties;
 
   protected static final String USER_AGENT_HTTP_REQUEST_HEADER_VALUE =
-      "gfsh (pronounced " + getProductVersion().getName() + " shell)/v"
-          + getProductVersion().getVersion();
+      "gfsh (pronounced " + getDistributionVersion().getName() + " shell)/v"
+          + getDistributionVersion().getVersion();
 
   // a list of acceptable content/media types supported by Gfsh
   private final List<MediaType> acceptableMediaTypes = Arrays.asList(MediaType.APPLICATION_JSON,