Skip to content
Snippets Groups Projects
Commit dade3aae authored by Griefed's avatar Griefed :joystick:
Browse files

refactor: Rewrite configparser in AddonConfiguration

parent 00545cff
No related branches found
No related tags found
No related merge requests found
......@@ -24,9 +24,10 @@ package de.griefed.serverpackcreatoraddonexample.configuration;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import de.griefed.serverpackcreatoraddonexample.Main;
import java.io.File;
import java.util.Objects;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Example class for reading the addon.conf-file and getting values from it.
......@@ -39,18 +40,23 @@ public class AddonConfiguration {
/**
* Constructor for the AddonConfiguration.class which automatically parses the addon.conf-file in the JAR-file.
* @author Griefed
* @param injectedAddonConfiguration Either instantiate this class with your own {@link Config} object, or let it create
* one using the addon.conf-file.
*/
public AddonConfiguration(Config injectedAddonConfiguration) {
public AddonConfiguration() {
if (injectedAddonConfiguration == null) {
ClassLoader classLoader = getClass().getClassLoader();
File addonConfigFile = new File(Objects.requireNonNull(classLoader.getResource("addon.conf")).getFile());
this.ADDONCONFIGURATION = ConfigFactory.parseFile(addonConfigFile);
} else {
this.ADDONCONFIGURATION = injectedAddonConfiguration;
URL urlToConfig = null;
String addonLocation = null;
try {
addonLocation = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();
urlToConfig = new URL(String.format("jar:file:%s!/addon.conf", addonLocation));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
assert urlToConfig != null;
this.ADDONCONFIGURATION = ConfigFactory.parseURL(urlToConfig);
}
/**
......@@ -69,7 +75,7 @@ public class AddonConfiguration {
* work with the addon.conf-file.
*/
public String exampleConfig() {
return getAddonConfiguration().getString("serverpackcreator.addon.someconfig");
return getAddonConfiguration().getString("someconfig");
}
}
# Valid values are 'serverpack'
serverpackcreator.addon.type=serverpack
serverpackcreator.addon.someconfig="This is an example for working with the addon.conf-file."
\ No newline at end of file
addontype="serverpack"
someconfig="This is an example for working with the addon.conf-file."
\ No newline at end of file
/*
* The MIT License (MIT)
*
* Copyright (C) 2021 Griefed
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.griefed.serverpackcreatorexampleaddon.configuration;
import de.griefed.serverpackcreatoraddonexample.configuration.AddonConfiguration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class AddonConfigurationTest {
private final AddonConfiguration addonConfiguration;
AddonConfigurationTest() {
this.addonConfiguration = new AddonConfiguration(null);
}
@Test
void getAddonConfigurationTest() {
Assertions.assertNotNull(addonConfiguration.getAddonConfiguration());
}
@Test
void exampleConfigTest() {
Assertions.assertEquals("This is an example for working with the addon.conf-file.", addonConfiguration.getAddonConfiguration().getString("serverpackcreator.addon.someconfig"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment