You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gobblin.apache.org by ab...@apache.org on 2017/12/18 23:57:50 UTC
incubator-gobblin git commit: [GOBBLIN-336] Add a method to save a
config to a file
Repository: incubator-gobblin
Updated Branches:
refs/heads/master 1fe5f952a -> 067043d5b
[GOBBLIN-336] Add a method to save a config to a file
Added unit tests.
Closes #2216 from HappyRay/add-persist-config-
method
Project: http://git-wip-us.apache.org/repos/asf/incubator-gobblin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-gobblin/commit/067043d5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-gobblin/tree/067043d5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-gobblin/diff/067043d5
Branch: refs/heads/master
Commit: 067043d5bc24539cecb9670f0c0318a94c406c31
Parents: 1fe5f95
Author: Ray Yang <ru...@gmail.com>
Authored: Mon Dec 18 15:57:44 2017 -0800
Committer: Abhishek Tiwari <ab...@gmail.com>
Committed: Mon Dec 18 15:57:44 2017 -0800
----------------------------------------------------------------------
.../org/apache/gobblin/util/ConfigUtils.java | 12 ++++++
.../java/org/apache/gobblin/util/FileUtils.java | 36 ++++++++++++++++
.../apache/gobblin/util/ConfigUtilsTest.java | 23 +++++++++++
.../org/apache/gobblin/util/FileUtilsTest.java | 43 ++++++++++++++++++++
4 files changed, 114 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-gobblin/blob/067043d5/gobblin-utility/src/main/java/org/apache/gobblin/util/ConfigUtils.java
----------------------------------------------------------------------
diff --git a/gobblin-utility/src/main/java/org/apache/gobblin/util/ConfigUtils.java b/gobblin-utility/src/main/java/org/apache/gobblin/util/ConfigUtils.java
index 4ca747c..e1d70f1 100644
--- a/gobblin-utility/src/main/java/org/apache/gobblin/util/ConfigUtils.java
+++ b/gobblin-utility/src/main/java/org/apache/gobblin/util/ConfigUtils.java
@@ -19,6 +19,7 @@ package org.apache.gobblin.util;
import java.io.IOException;
import java.io.StringReader;
+import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -53,6 +54,7 @@ import org.apache.gobblin.password.PasswordManager;
* Utility class for dealing with {@link Config} objects.
*/
public class ConfigUtils {
+ private final FileUtils fileUtils;
/**
* List of keys that should be excluded when converting to typesafe config.
@@ -66,6 +68,16 @@ public class ConfigUtils {
* typesafe config does not allow such properties. */
public static final String STRIP_SUFFIX = ".ROOT_VALUE";
+ public ConfigUtils(FileUtils fileUtils) {
+ this.fileUtils = fileUtils;
+ }
+
+ public void saveConfigToFile(final Config config, final Path destPath)
+ throws IOException {
+ final String configAsHoconString = config.root().render();
+ this.fileUtils.saveToFile(configAsHoconString, destPath);
+ }
+
/**
* Convert a given {@link Config} instance to a {@link Properties} instance.
*
http://git-wip-us.apache.org/repos/asf/incubator-gobblin/blob/067043d5/gobblin-utility/src/main/java/org/apache/gobblin/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/gobblin-utility/src/main/java/org/apache/gobblin/util/FileUtils.java b/gobblin-utility/src/main/java/org/apache/gobblin/util/FileUtils.java
new file mode 100644
index 0000000..49bf6dd
--- /dev/null
+++ b/gobblin-utility/src/main/java/org/apache/gobblin/util/FileUtils.java
@@ -0,0 +1,36 @@
+/*
+ * 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.gobblin.util;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+
+public class FileUtils {
+ public void saveToFile(final String text, final Path destPath)
+ throws IOException {
+ try (PrintWriter out = new PrintWriter(
+ Files.newBufferedWriter(destPath, StandardCharsets.UTF_8))) {
+ out.println(text);
+ out.flush();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-gobblin/blob/067043d5/gobblin-utility/src/test/java/org/apache/gobblin/util/ConfigUtilsTest.java
----------------------------------------------------------------------
diff --git a/gobblin-utility/src/test/java/org/apache/gobblin/util/ConfigUtilsTest.java b/gobblin-utility/src/test/java/org/apache/gobblin/util/ConfigUtilsTest.java
index 4ac527d..d4395f7 100644
--- a/gobblin-utility/src/test/java/org/apache/gobblin/util/ConfigUtilsTest.java
+++ b/gobblin-utility/src/test/java/org/apache/gobblin/util/ConfigUtilsTest.java
@@ -20,6 +20,8 @@ package org.apache.gobblin.util;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
@@ -43,10 +45,31 @@ import com.typesafe.config.ConfigValueFactory;
import org.apache.gobblin.configuration.ConfigurationKeys;
import org.apache.gobblin.configuration.State;
+import static org.assertj.core.api.Assertions.assertThat;
+
public class ConfigUtilsTest {
@Test
+ public void testSaveConfigToFile()
+ throws IOException {
+ FileUtils fileUtils = new FileUtils();
+ ConfigUtils configUtils = new ConfigUtils(fileUtils);
+ ImmutableMap<String, String> configMap = ImmutableMap.of("k1", "v1", "k2", "v2");
+ Config config = ConfigFactory.parseMap(configMap);
+ Path destPath = Paths.get("test-config-file.txt");
+
+ configUtils.saveConfigToFile(config, destPath);
+ Config restoredConfig = ConfigFactory.parseFile(destPath.toFile());
+
+ assertThat(restoredConfig.getString("k1")).isEqualTo("v1");
+ assertThat(restoredConfig.getString("k2")).isEqualTo("v2");
+
+ java.nio.file.Files.deleteIfExists(destPath);
+ }
+
+
+ @Test
public void testPropertiesToConfig() {
Properties properties = new Properties();
http://git-wip-us.apache.org/repos/asf/incubator-gobblin/blob/067043d5/gobblin-utility/src/test/java/org/apache/gobblin/util/FileUtilsTest.java
----------------------------------------------------------------------
diff --git a/gobblin-utility/src/test/java/org/apache/gobblin/util/FileUtilsTest.java b/gobblin-utility/src/test/java/org/apache/gobblin/util/FileUtilsTest.java
new file mode 100644
index 0000000..3956da3
--- /dev/null
+++ b/gobblin-utility/src/test/java/org/apache/gobblin/util/FileUtilsTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.gobblin.util;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.testng.annotations.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class FileUtilsTest {
+
+ @Test
+ public void testSaveToFile()
+ throws IOException {
+ FileUtils utils = new FileUtils();
+ Path destPath = Paths.get("fileUtilTest.txt");
+ utils.saveToFile("foo", destPath);
+
+ assertThat(destPath).exists().isReadable().hasContent("foo\n");
+
+ Files.deleteIfExists(destPath);
+ }
+}