-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for default config value validity
- Loading branch information
Showing
6 changed files
with
566 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
Plan/common/src/test/java/com/djrapitops/plan/system/settings/ConfigSettingKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* This file is part of Player Analytics (Plan). | ||
* | ||
* Plan is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License v3 as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Plan 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Plan. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.djrapitops.plan.system.settings; | ||
|
||
import com.djrapitops.plan.system.settings.config.PlanConfig; | ||
import com.djrapitops.plan.system.settings.paths.*; | ||
import com.djrapitops.plan.system.settings.paths.key.Setting; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import utilities.FieldFetcher; | ||
import utilities.TestResources; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* Test to check that configs contain all values required to run the plugin. | ||
* | ||
* @author Rsl1122 | ||
*/ | ||
public class ConfigSettingKeyTest { | ||
|
||
@ClassRule | ||
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void serverConfigHasValidDefaultValues() throws IOException, IllegalAccessException { | ||
PlanConfig planConfig = createConfig("config.yml"); | ||
Collection<Setting> settings = getServerSettings(); | ||
hasValidDefaultValuesForAllSettings(planConfig, settings); | ||
} | ||
|
||
@Test | ||
public void networkConfigHasValidDefaultValues() throws IOException, IllegalAccessException { | ||
PlanConfig planConfig = createConfig("bungeeconfig.yml"); | ||
Collection<Setting> settings = getNetworkSettings(); | ||
hasValidDefaultValuesForAllSettings(planConfig, settings); | ||
} | ||
|
||
private void hasValidDefaultValuesForAllSettings(PlanConfig config, Iterable<Setting> settings) { | ||
List<String> fails = new ArrayList<>(); | ||
for (Setting setting : settings) { | ||
checkSettingForFailures(config, setting).ifPresent(fails::add); | ||
} | ||
assertTrue(fails.isEmpty(), fails::toString); | ||
} | ||
|
||
private Optional<String> checkSettingForFailures(PlanConfig config, Setting setting) { | ||
try { | ||
if (!config.contains(setting.getPath())) { | ||
return Optional.of("Did not contain " + setting.getPath()); | ||
} else { | ||
config.get(setting); | ||
return Optional.empty(); | ||
} | ||
} catch (IllegalStateException validationFailed) { | ||
return Optional.of(validationFailed.getMessage()); | ||
} | ||
} | ||
|
||
private Collection<Setting> getServerSettings() throws IllegalAccessException { | ||
List<Setting> settings = new ArrayList<>(); | ||
for (Class settingKeyClass : new Class[]{ | ||
DatabaseSettings.class, | ||
DataGatheringSettings.class, | ||
DisplaySettings.class, | ||
ExportSettings.class, | ||
FormatSettings.class, | ||
PluginDataSettings.class, | ||
PluginSettings.class, | ||
TimeSettings.class, | ||
WebserverSettings.class | ||
}) { | ||
settings.addAll(FieldFetcher.getPublicStaticFields(settingKeyClass, Setting.class)); | ||
} | ||
return settings; | ||
} | ||
|
||
private Collection<Setting> getNetworkSettings() throws IllegalAccessException { | ||
List<Setting> settings = new ArrayList<>(); | ||
for (Class settingKeyClass : new Class[]{ | ||
DatabaseSettings.class, | ||
DisplaySettings.class, | ||
ExportSettings.class, | ||
FormatSettings.class, | ||
PluginSettings.class, | ||
ProxySettings.class, | ||
TimeSettings.class, | ||
WebserverSettings.class | ||
}) { | ||
settings.addAll(FieldFetcher.getPublicStaticFields(settingKeyClass, Setting.class)); | ||
} | ||
settings.add(PluginDataSettings.PLUGIN_BUYCRAFT_SECRET); | ||
|
||
// Server settings contained in the key classes, remove | ||
settings.remove(PluginSettings.SERVER_NAME); | ||
settings.remove(PluginSettings.BUNGEE_COPY_CONFIG); | ||
settings.remove(DatabaseSettings.TYPE); | ||
settings.remove(DisplaySettings.PLAYERS_PER_SERVER_PAGE); | ||
settings.remove(DisplaySettings.GRAPH_TPS_THRESHOLD_MED); | ||
settings.remove(DisplaySettings.GRAPH_TPS_THRESHOLD_HIGH); | ||
settings.remove(DisplaySettings.GRAPH_DISK_THRESHOLD_MED); | ||
settings.remove(DisplaySettings.GRAPH_DISK_THRESHOLD_HIGH); | ||
settings.remove(DisplaySettings.GRAPH_DISK_THRESHOLD_HIGH); | ||
settings.remove(DisplaySettings.WORLD_ALIASES); | ||
settings.remove(TimeSettings.ANALYSIS_REFRESH_PERIOD); | ||
return settings; | ||
} | ||
|
||
private PlanConfig createConfig(String copyDefaultSettingsFrom) throws IOException { | ||
File configFile = temporaryFolder.newFile(); | ||
TestResources.copyResourceIntoFile(configFile, "/" + copyDefaultSettingsFrom); | ||
return createConfig(configFile); | ||
} | ||
|
||
private PlanConfig createConfig(File configFile) throws IOException { | ||
PlanConfig config = new PlanConfig(configFile, null, null); | ||
config.save(); | ||
return config; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This file is part of Player Analytics (Plan). | ||
* | ||
* Plan is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License v3 as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Plan 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Plan. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package utilities; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FieldFetcher { | ||
|
||
private FieldFetcher() { | ||
/* static method class*/ | ||
} | ||
|
||
public static <T> List<T> getPublicStaticFields(Class fromClass, Class<T> ofType) throws IllegalAccessException { | ||
List<T> list = new ArrayList<>(); | ||
for (Field field : fromClass.getDeclaredFields()) { | ||
if (!Modifier.isPublic(field.getModifiers())) { | ||
continue; | ||
} | ||
T key = (T) field.get(null); | ||
list.add(key); | ||
} | ||
return list; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This file is part of Player Analytics (Plan). | ||
* | ||
* Plan is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License v3 as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Plan 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Plan. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package utilities; | ||
|
||
import com.djrapitops.plan.PlanPlugin; | ||
|
||
import java.io.*; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Paths; | ||
|
||
public class TestResources { | ||
|
||
private TestResources() { | ||
/* static method class */ | ||
} | ||
|
||
public static File getTestResourceFile(String called, Class testClass) throws URISyntaxException { | ||
return Paths.get(testClass.getResource("/" + called).toURI()).toFile(); | ||
} | ||
|
||
public static void copyResourceIntoFile(File toFile, String resourcePath) { | ||
createEmptyFile(toFile); | ||
writeResourceToFile(toFile, resourcePath); | ||
} | ||
|
||
public static void copyTestResourceIntoFile(File toFile, InputStream testResource) { | ||
createEmptyFile(toFile); | ||
copyResourceToFile(toFile, testResource); | ||
} | ||
|
||
private static void copyResourceToFile(File toFile, InputStream testResource) { | ||
try (InputStream in = testResource; | ||
OutputStream out = new FileOutputStream(toFile)) { | ||
copy(in, out); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private static void writeResourceToFile(File toFile, String resourcePath) { | ||
try (InputStream in = PlanPlugin.class.getResourceAsStream(resourcePath); | ||
OutputStream out = new FileOutputStream(toFile)) { | ||
if (in == null) { | ||
throw new FileNotFoundException("Resource with name '" + resourcePath + "' not found"); | ||
} | ||
copy(in, out); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private static void copy(InputStream in, OutputStream out) throws IOException { | ||
int read; | ||
byte[] bytes = new byte[1024]; | ||
|
||
while ((read = in.read(bytes)) != -1) { | ||
out.write(bytes, 0, read); | ||
} | ||
} | ||
|
||
private static void createEmptyFile(File toFile) { | ||
String path = toFile.getAbsolutePath(); | ||
try { | ||
toFile.getParentFile().mkdirs(); | ||
if (!toFile.exists() && !toFile.createNewFile()) { | ||
throw new FileNotFoundException("Could not create file: " + path); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.