geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [2/2] geode git commit: Extract default properties generation to its own class
Date Wed, 15 Feb 2017 23:24:45 GMT
Extract default properties generation to its own class


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/703fb143
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/703fb143
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/703fb143

Branch: refs/heads/feature/GEODE-2474
Commit: 703fb143d948555760ccf726d0da1e35eeff7c54
Parents: a08379c
Author: Kirk Lund <klund@apache.org>
Authored: Wed Feb 15 15:19:49 2017 -0800
Committer: Kirk Lund <klund@apache.org>
Committed: Wed Feb 15 15:19:49 2017 -0800

----------------------------------------------------------------------
 geode-assembly/build.gradle                     |   2 +-
 .../internal/DefaultPropertiesGenerator.java    |  52 ++++++++
 .../internal/DistributionConfigImpl.java        |  23 ++--
 ...faultPropertiesGeneratorIntegrationTest.java | 128 +++++++++++++++++++
 .../GemFireVersionIntegrationJUnitTest.java     |  50 --------
 5 files changed, 189 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/703fb143/geode-assembly/build.gradle
----------------------------------------------------------------------
diff --git a/geode-assembly/build.gradle b/geode-assembly/build.gradle
index f346880..3452141 100755
--- a/geode-assembly/build.gradle
+++ b/geode-assembly/build.gradle
@@ -101,7 +101,7 @@ tasks.withType(Test){
 
 task defaultDistributionConfig(type: JavaExec, dependsOn: classes) {
   outputs.file file("$buildDir/gemfire.properties")
-  main 'org.apache.geode.distributed.internal.DistributionConfigImpl'
+  main 'org.apache.geode.distributed.internal.DefaultPropertiesGenerator'
   classpath project(':geode-core').sourceSets.main.runtimeClasspath
   workingDir buildDir
   

http://git-wip-us.apache.org/repos/asf/geode/blob/703fb143/geode-core/src/main/java/org/apache/geode/distributed/internal/DefaultPropertiesGenerator.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/distributed/internal/DefaultPropertiesGenerator.java
b/geode-core/src/main/java/org/apache/geode/distributed/internal/DefaultPropertiesGenerator.java
new file mode 100644
index 0000000..2120457
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/distributed/internal/DefaultPropertiesGenerator.java
@@ -0,0 +1,52 @@
+/*
+ * 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.distributed.internal;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Used by build to generate a default configuration properties file for use by applications
+ */
+public class DefaultPropertiesGenerator {
+
+  DefaultPropertiesGenerator() {}
+
+  public static void main(final String args[]) throws IOException {
+    String targetFileName = null;
+    if (ArrayUtils.isNotEmpty(args)) {
+      targetFileName = args[0];
+    }
+    DefaultPropertiesGenerator generator = new DefaultPropertiesGenerator();
+    generator.generateDefaultPropertiesFile(targetFileName);
+  }
+
+  static String getDefaultFileName() {
+    return DistributionConfig.GEMFIRE_PREFIX + "properties";
+  }
+
+  void generateDefaultPropertiesFile(final String targetFileName) throws IOException {
+    String fileName = StringUtils.trimToNull(targetFileName);
+    if (fileName == null) {
+      fileName = getDefaultFileName();
+    }
+
+    DistributionConfig config = DistributionConfigImpl.createDefaultInstance();
+    config.toFile(new File(fileName));
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/703fb143/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionConfigImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionConfigImpl.java
b/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionConfigImpl.java
index fa6d13f..fbe894c 100644
--- a/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionConfigImpl.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/internal/DistributionConfigImpl.java
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.HashCodeBuilder;
@@ -978,6 +979,13 @@ public class DistributionConfigImpl extends AbstractDistributionConfig
implement
 
   }
 
+  /**
+   * Used by tests to create a default instance without reading in properties.
+   */
+  static DistributionConfig createDefaultInstance() {
+    return new DistributionConfigImpl();
+  }
+
   private boolean isAliasCorrectlyConfiguredForComponents(
       final SecurableCommunicationChannel component) {
     switch (component) {
@@ -3022,21 +3030,6 @@ public class DistributionConfigImpl extends AbstractDistributionConfig
implement
   }
 
   /**
-   * Used by gemfire build.xml to generate a default gemfire.properties for use by applications.
See
-   * bug 30995 for the feature request.
-   */
-  public static void main(String args[]) throws IOException {
-    DistributionConfigImpl cfg = new DistributionConfigImpl();
-    String fileName = DistributionConfig.GEMFIRE_PREFIX + "properties";
-    if (args != null && args.length > 0) {
-      String temp = args[0].trim();
-      fileName = "".equals(temp) ? fileName : temp;
-    }
-    cfg.toFile(new File(fileName));
-  }
-
-
-  /**
    * For dunit tests we do not allow use of the default multicast address/port. Please use
    * AvailablePort.getRandomAvailablePort(AvailablePort.JGROUPS) to obtain a free port for
your
    * test.

http://git-wip-us.apache.org/repos/asf/geode/blob/703fb143/geode-core/src/test/java/org/apache/geode/distributed/internal/DefaultPropertiesGeneratorIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/distributed/internal/DefaultPropertiesGeneratorIntegrationTest.java
b/geode-core/src/test/java/org/apache/geode/distributed/internal/DefaultPropertiesGeneratorIntegrationTest.java
new file mode 100644
index 0000000..ac3eaa3
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/distributed/internal/DefaultPropertiesGeneratorIntegrationTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.distributed.internal;
+
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
+import static org.apache.geode.distributed.internal.DefaultPropertiesGenerator.getDefaultFileName;
+import static org.apache.geode.internal.lang.SystemUtils.getClassPath;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.test.junit.categories.IntegrationTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestName;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+@Category(IntegrationTest.class)
+public class DefaultPropertiesGeneratorIntegrationTest {
+
+  private Process process;
+  private String propertiesFile;
+  private Cache cache;
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public TestName testName = new TestName();
+
+  @Before
+  public void before() throws Exception {
+    String tmp = this.temporaryFolder.getRoot().getAbsolutePath();
+    this.propertiesFile = tmp + "gf" + System.nanoTime() + ".properties";
+    DefaultPropertiesGenerator generator = new DefaultPropertiesGenerator();
+    generator.generateDefaultPropertiesFile(this.propertiesFile);
+    assertThat(new File(this.propertiesFile)).exists();
+  }
+
+  @After
+  public void after() throws Exception {
+    if (this.cache != null) {
+      this.cache.close();
+    }
+    if (this.process != null) {
+      this.process.destroyForcibly();
+    }
+  }
+
+  @Test
+  public void propertiesShouldNotBeEmpty() throws Exception {
+    Properties properties = loadProperties();
+    assertThat(properties).isNotEmpty();
+  }
+
+  /**
+   * test that the gemfire.properties generated by default is able to start a server
+   */
+  @Test
+  public void propertiesShouldCreateValidCache() throws Exception {
+    Properties properties = loadProperties();
+    CacheFactory cacheFactory = new CacheFactory(properties);
+    this.cache = cacheFactory.create();
+    assertThat(this.cache).isNotNull();
+  }
+
+  @Test
+  public void shouldGeneratePropertiesFile() throws Exception {
+    String[] command = {"java", "-cp", getClassPath(), DefaultPropertiesGenerator.class.getName()};
+    ProcessBuilder launcher = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot());
+    this.process = launcher.start();
+    this.process.waitFor(2, MINUTES);
+
+    String tmp = this.temporaryFolder.getRoot().getAbsolutePath();
+    File file = new File(tmp + File.separator + getDefaultFileName());
+    assertThat(file).exists();
+
+    Properties properties = loadProperties();
+    assertThat(properties).isNotEmpty();
+  }
+
+  @Test
+  public void shouldUseSpecifiedPropertiesFile() throws Exception {
+    String targetFileName = "gf" + this.testName.getMethodName() + ".properties";
+    String[] command =
+        {"java", "-cp", getClassPath(), DefaultPropertiesGenerator.class.getName(), targetFileName};
+    ProcessBuilder launcher = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot());
+    this.process = launcher.start();
+    this.process.waitFor(2, MINUTES);
+
+    String tmp = this.temporaryFolder.getRoot().getAbsolutePath();
+    File file = new File(tmp + File.separator + targetFileName);
+    assertThat(file).exists();
+
+    Properties properties = loadProperties();
+    assertThat(properties).isNotEmpty();
+  }
+
+  private Properties loadProperties() throws IOException {
+    Properties properties = new Properties();
+    properties.load(new FileInputStream(this.propertiesFile));
+    assertThat(properties.getProperty(MCAST_PORT)).isEqualTo("0");
+    properties.setProperty(MCAST_PORT, "0");
+    return properties;
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/703fb143/geode-core/src/test/java/org/apache/geode/internal/GemFireVersionIntegrationJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/GemFireVersionIntegrationJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/GemFireVersionIntegrationJUnitTest.java
deleted file mode 100644
index cae3313..0000000
--- a/geode-core/src/test/java/org/apache/geode/internal/GemFireVersionIntegrationJUnitTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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;
-
-import org.apache.geode.cache.Cache;
-import org.apache.geode.cache.CacheFactory;
-import org.apache.geode.distributed.internal.DistributionConfigImpl;
-import org.apache.geode.test.junit.categories.IntegrationTest;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.Properties;
-
-import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
-import static org.junit.Assert.assertNotNull;
-
-@Category(IntegrationTest.class)
-public class GemFireVersionIntegrationJUnitTest {
-
-  /**
-   * test that the gemfire.properties generated by default is able to start a server
-   */
-  @Test
-  public void testDefaultConfig() throws IOException {
-    String[] args = new String[1];
-    args[0] = "gf" + System.nanoTime() + ".properties";
-    DistributionConfigImpl.main(args);
-    Properties props = new Properties();
-    props.load(new FileInputStream(args[0]));
-    props.setProperty(MCAST_PORT, "0");
-    CacheFactory cacheFactory = new CacheFactory(props);
-    Cache c = cacheFactory.create();
-    assertNotNull(c);
-    c.close();
-  }
-}


Mime
View raw message