Skip to content

Commit

Permalink
Refactor AbstractLocateRegexCheck to detect and return null if the lo…
Browse files Browse the repository at this point in the history
…cale file only contains the null delimiter
  • Loading branch information
md5sha256 committed Aug 11, 2024
1 parent fee926c commit e3cb2ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.thebusybiscuit.slimefun4.core.services.localization;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
Expand All @@ -16,7 +16,9 @@
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -50,16 +52,23 @@ public static void unload() {

@ParametersAreNonnullByDefault
@Nullable
BufferedReader readLanguageFile(LanguagePreset lang, LanguageFile file) {
FileConfiguration readLanguageFile(LanguagePreset lang, LanguageFile file) throws IOException, InvalidConfigurationException {
String path = file.getFilePath(lang.getLanguageCode());
InputStream inputStream = getClass().getResourceAsStream(path);

if (inputStream == null) {
// This file does not exist, we consider it "passed".
byte[] bytes;
try (InputStream inputStream = getClass().getResourceAsStream(path)) {
if (inputStream == null) {
// This file does not exist, we consider it "passed".
return null;
}
bytes = inputStream.readAllBytes();
}
String contents = new String(bytes, StandardCharsets.UTF_8);
if (contents.trim().equals("---")) {
return null;
}

return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
YamlConfiguration configuration = new YamlConfiguration();
configuration.loadFromString(contents);
return configuration;
}

@ParametersAreNonnullByDefault
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package io.github.thebusybiscuit.slimefun4.core.services.localization;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.regex.Pattern;

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.util.regex.Pattern;

/**
* We need to make sure that color codes are still working properly.
* <p>
Expand All @@ -24,7 +22,7 @@
* <p>
* The test will catch occurences like "a& ", "b&Hello" or "7&", "5& a".
* The test will however ignore valid color codes such as "a&a".
*
*
* @author TheBusyBiscuit
*
*/
Expand All @@ -38,15 +36,12 @@ class TestColorCodes extends AbstractLocaleRegexChecker {
@ParametersAreNonnullByDefault
@MethodSource("getAllLanguageFiles")
@DisplayName("Test for mistakes in color codes for Slimefun locale files")
void testSpelling(LanguagePreset lang, LanguageFile file) throws IOException {
try (BufferedReader reader = readLanguageFile(lang, file)) {
if (reader == null) {
void testSpelling(LanguagePreset lang, LanguageFile file) throws IOException, InvalidConfigurationException {
FileConfiguration config = readLanguageFile(lang, file);
if (config == null) {
return;
}

FileConfiguration config = YamlConfiguration.loadConfiguration(reader);
assertNoRegexMatchesForAllEntries(lang, file, config);
}
assertNoRegexMatchesForAllEntries(lang, file, config);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.github.thebusybiscuit.slimefun4.core.services.localization;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.regex.Pattern;

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -16,7 +15,7 @@
* Your friendly neighbourhood spellcheck.
* Brought to you by our Discord bot "@WalshBot".
* No more incorrect spelling of "Slimefun".
*
*
* @author TheBusyBiscuit
*
*/
Expand All @@ -30,15 +29,12 @@ class TestSlimefunSpelling extends AbstractLocaleRegexChecker {
@ParametersAreNonnullByDefault
@MethodSource("getAllLanguageFiles")
@DisplayName("Test correct spelling of Slimefun in language files")
void testSpelling(LanguagePreset lang, LanguageFile file) throws IOException {
try (BufferedReader reader = readLanguageFile(lang, file)) {
if (reader == null) {
return;
}

FileConfiguration config = YamlConfiguration.loadConfiguration(reader);
assertNoRegexMatchesForAllEntries(lang, file, config);
void testSpelling(LanguagePreset lang, LanguageFile file) throws IOException, InvalidConfigurationException {
FileConfiguration config = readLanguageFile(lang, file);
if (config == null) {
return;
}
assertNoRegexMatchesForAllEntries(lang, file, config);
}

}

0 comments on commit e3cb2ee

Please sign in to comment.