From 60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 29 Jul 2024 18:33:08 +0100 Subject: Add some unit tests --- .../me/lucko/spark/common/util/Configuration.java | 153 ------------------- .../me/lucko/spark/common/util/JavaVersion.java | 5 +- .../common/util/config/CombinedConfiguration.java | 132 +++++++++++++++++ .../spark/common/util/config/Configuration.java | 54 +++++++ .../common/util/config/FileConfiguration.java | 165 +++++++++++++++++++++ .../common/util/config/RuntimeConfiguration.java | 106 +++++++++++++ 6 files changed, 461 insertions(+), 154 deletions(-) delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java (limited to 'spark-common/src/main/java/me/lucko/spark/common/util') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java deleted file mode 100644 index 586a845..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.common.util; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public final class Configuration { - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - - private final Path file; - private JsonObject root; - - public Configuration(Path file) { - this.file = file; - load(); - } - - public void load() { - JsonObject root = null; - if (Files.exists(this.file)) { - try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { - root = GSON.fromJson(reader, JsonObject.class); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (root == null) { - root = new JsonObject(); - root.addProperty("_header", "spark configuration file - https://spark.lucko.me/docs/Configuration"); - } - this.root = root; - } - - public void save() { - try { - Files.createDirectories(this.file.getParent()); - } catch (IOException e) { - // ignore - } - - try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { - GSON.toJson(this.root, writer); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public String getString(String path, String def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - return el.getAsJsonPrimitive().getAsString(); - } - - public boolean getBoolean(String path, boolean def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - JsonPrimitive val = el.getAsJsonPrimitive(); - return val.isBoolean() ? val.getAsBoolean() : def; - } - - public int getInteger(String path, int def) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonPrimitive()) { - return def; - } - - JsonPrimitive val = el.getAsJsonPrimitive(); - return val.isNumber() ? val.getAsInt() : def; - } - - public List getStringList(String path) { - JsonElement el = this.root.get(path); - if (el == null || !el.isJsonArray()) { - return Collections.emptyList(); - } - - List list = new ArrayList<>(); - for (JsonElement child : el.getAsJsonArray()) { - if (child.isJsonPrimitive()) { - list.add(child.getAsJsonPrimitive().getAsString()); - } - } - return list; - } - - public void setString(String path, String value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setBoolean(String path, boolean value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setInteger(String path, int value) { - this.root.add(path, new JsonPrimitive(value)); - } - - public void setStringList(String path, List value) { - JsonArray array = new JsonArray(); - for (String str : value) { - array.add(str); - } - this.root.add(path, array); - } - - public boolean contains(String path) { - return this.root.has(path); - } - - public void remove(String path) { - this.root.remove(path); - } - -} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java index efeabfc..1dad75b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/JavaVersion.java @@ -20,6 +20,8 @@ package me.lucko.spark.common.util; +import com.google.common.annotations.VisibleForTesting; + public class JavaVersion { ; @@ -28,7 +30,8 @@ public class JavaVersion { JAVA_VERSION = parseJavaVersion(System.getProperty("java.version")); } - private static int parseJavaVersion(String version) { + @VisibleForTesting + static int parseJavaVersion(String version) { if (version.startsWith("1.")) { // Java 8 and below return Integer.parseInt(version.substring(2, 3)); diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java new file mode 100644 index 0000000..ff7388a --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/CombinedConfiguration.java @@ -0,0 +1,132 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.config; + +import com.google.common.collect.ImmutableList; + +import java.util.Collections; +import java.util.List; + +class CombinedConfiguration implements Configuration { + + private final List configurations; + + CombinedConfiguration(Configuration... configurations) { + this.configurations = ImmutableList.copyOf(configurations).reverse(); + } + + @Override + public void load() { + for (Configuration configuration : this.configurations) { + configuration.load(); + } + } + + @Override + public void save() { + for (Configuration configuration : this.configurations) { + configuration.save(); + } + } + + @Override + public String getString(String path, String def) { + String result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getString(path, result); + } + return result; + } + + @Override + public boolean getBoolean(String path, boolean def) { + boolean result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getBoolean(path, result); + } + return result; + } + + @Override + public int getInteger(String path, int def) { + int result = def; + for (Configuration configuration : this.configurations) { + result = configuration.getInteger(path, result); + } + return result; + } + + @Override + public List getStringList(String path) { + for (Configuration configuration : this.configurations) { + List result = configuration.getStringList(path); + if (!result.isEmpty()) { + return result; + } + } + return Collections.emptyList(); + } + + @Override + public void setString(String path, String value) { + for (Configuration configuration : this.configurations) { + configuration.setString(path, value); + } + } + + @Override + public void setBoolean(String path, boolean value) { + for (Configuration configuration : this.configurations) { + configuration.setBoolean(path, value); + } + } + + @Override + public void setInteger(String path, int value) { + for (Configuration configuration : this.configurations) { + configuration.setInteger(path, value); + } + } + + @Override + public void setStringList(String path, List value) { + for (Configuration configuration : this.configurations) { + configuration.setStringList(path, value); + } + } + + @Override + public boolean contains(String path) { + for (Configuration configuration : this.configurations) { + if (configuration.contains(path)) { + return true; + } + } + return false; + } + + @Override + public void remove(String path) { + for (Configuration configuration : this.configurations) { + configuration.remove(path); + } + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java new file mode 100644 index 0000000..c2c2d88 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/Configuration.java @@ -0,0 +1,54 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.config; + +import java.util.List; + +public interface Configuration { + + static Configuration combining(Configuration... configurations) { + return new CombinedConfiguration(configurations); + } + + void load(); + + void save(); + + String getString(String path, String def); + + boolean getBoolean(String path, boolean def); + + int getInteger(String path, int def); + + List getStringList(String path); + + void setString(String path, String value); + + void setBoolean(String path, boolean value); + + void setInteger(String path, int value); + + void setStringList(String path, List value); + + boolean contains(String path); + + void remove(String path); +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java new file mode 100644 index 0000000..72a4681 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/FileConfiguration.java @@ -0,0 +1,165 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class FileConfiguration implements Configuration { + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + private final Path file; + private JsonObject root; + + public FileConfiguration(Path file) { + this.file = file; + load(); + } + + @Override + public void load() { + JsonObject root = null; + if (Files.exists(this.file)) { + try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { + root = GSON.fromJson(reader, JsonObject.class); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (root == null) { + root = new JsonObject(); + root.addProperty("_header", "spark configuration file - https://spark.lucko.me/docs/Configuration"); + } + this.root = root; + } + + @Override + public void save() { + try { + Files.createDirectories(this.file.getParent()); + } catch (IOException e) { + // ignore + } + + try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { + GSON.toJson(this.root, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public String getString(String path, String def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + return el.getAsJsonPrimitive().getAsString(); + } + + @Override + public boolean getBoolean(String path, boolean def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + JsonPrimitive val = el.getAsJsonPrimitive(); + return val.isBoolean() ? val.getAsBoolean() : def; + } + + @Override + public int getInteger(String path, int def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + JsonPrimitive val = el.getAsJsonPrimitive(); + return val.isNumber() ? val.getAsInt() : def; + } + + @Override + public List getStringList(String path) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonArray()) { + return Collections.emptyList(); + } + + List list = new ArrayList<>(); + for (JsonElement child : el.getAsJsonArray()) { + if (child.isJsonPrimitive()) { + list.add(child.getAsJsonPrimitive().getAsString()); + } + } + return list; + } + + @Override + public void setString(String path, String value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setBoolean(String path, boolean value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setInteger(String path, int value) { + this.root.add(path, new JsonPrimitive(value)); + } + + @Override + public void setStringList(String path, List value) { + JsonArray array = new JsonArray(); + for (String str : value) { + array.add(str); + } + this.root.add(path, array); + } + + @Override + public boolean contains(String path) { + return this.root.has(path); + } + + @Override + public void remove(String path) { + this.root.remove(path); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java new file mode 100644 index 0000000..d076554 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/config/RuntimeConfiguration.java @@ -0,0 +1,106 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.config; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public enum RuntimeConfiguration implements Configuration { + SYSTEM_PROPERTIES { + @Override + public String getString(String path, String def) { + return System.getProperty("spark." + path, def); + } + }, + + ENVIRONMENT_VARIABLES { + @Override + public String getString(String path, String def) { + String name = "SPARK_" + path.replace(".", "_").replace("-", "_").toUpperCase(); + String value = System.getenv(name); + return value != null ? value : def; + } + }; + + @Override + public boolean getBoolean(String path, boolean def) { + return Boolean.parseBoolean(getString(path, Boolean.toString(def))); + } + + @Override + public int getInteger(String path, int def) { + try { + return Integer.parseInt(getString(path, Integer.toString(def))); + } catch (NumberFormatException e) { + return def; + } + } + + @Override + public List getStringList(String path) { + String value = getString(path, ""); + if (value.isEmpty()) { + return Collections.emptyList(); + } + return Arrays.asList(value.split(",")); + } + + @Override + public boolean contains(String path) { + return getString(path, null) != null; + } + + @Override + public void load() { + // no-op + } + + @Override + public void save() { + // no-op + } + + @Override + public void setString(String path, String value) { + // no-op + } + + @Override + public void setBoolean(String path, boolean value) { + // no-op + } + + @Override + public void setInteger(String path, int value) { + // no-op + } + + @Override + public void setStringList(String path, List value) { + // no-op + } + + @Override + public void remove(String path) { + // no-op + } +} -- cgit