Skip to content

Commit

Permalink
Merge pull request BetonQuest#3080 from Wolf2323/LazyConfigurationSec…
Browse files Browse the repository at this point in the history
…tionTest

added Tests for LazyConfigurationSection
  • Loading branch information
TheosRee authored Dec 25, 2024
2 parents bfde223 + 501d950 commit 47d2e7d
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.betonquest.betonquest.api.bukkit.config.custom.lazy;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@Tag("ConfigurationSection")
@SuppressWarnings("PMD.UnitTestAssertionsShouldIncludeMessage")
class LazyConfigurationSectionLazynessTest {
private ConfigurationSection config;

private ConfigurationSection lazy;

private ConfigurationSection getConfig() {
final MemoryConfiguration config = new MemoryConfiguration();
final LazyConfigurationSection lazy = new LazyConfigurationSection(config, "lazy");

final MemoryConfiguration spy = spy(config);
when(spy.getConfigurationSection("lazy")).thenReturn(lazy);

return spy;
}

@BeforeEach
void setUp() {
config = getConfig();
lazy = Objects.requireNonNull(config.getConfigurationSection("lazy"));
}

@Test
void testLazySet() {
lazy.set("key", "value");
assertEquals("value", config.getString("lazy.key"));
}

@Test
void testLazyAddDefault() {
lazy.addDefault("key", "value");
assertTrue(config.isSet("lazy"));
assertEquals("value", config.getString("lazy.key"));
}

@Test
void testLazyCreateSection() {
lazy.createSection("key");
assertNotNull(config.getConfigurationSection("lazy.key"));
}

@Test
void testLazyCreateSectionWithMap() {
final Map<String, String> map = Map.of("key", "value");
lazy.createSection("section", map);
assertEquals("value", config.getString("lazy.section.key"));
}

@Test
void testLazySetComments() {
lazy.setComments("key", null);
assertTrue(config.isSet("lazy"));
}

@Test
void testLazySetInlineComments() {
lazy.setInlineComments("key", null);
assertTrue(config.isSet("lazy"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.betonquest.betonquest.api.bukkit.config.custom.lazy;

import org.betonquest.betonquest.api.bukkit.config.util.ConfigurationSectionBaseTest;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.*;

@Tag("ConfigurationSection")
@SuppressWarnings({"PMD.JUnit5TestShouldBePackagePrivate", "PMD.UnitTestAssertionsShouldIncludeMessage"})
class LazyConfigurationSectionTest extends ConfigurationSectionBaseTest {
private MemoryConfiguration root;

@Override
public ConfigurationSection getConfig() {
root = new MemoryConfiguration();
final LazyConfigurationSection lazy = new LazyConfigurationSection(root, "lazy");
root.set("lazy", lazy);
final Configuration defaultConfig = super.getDefaultConfig();
defaultConfig.getValues(true).forEach((key, value) -> {
if (!(value instanceof ConfigurationSection)) {
lazy.set(key, value);
lazy.setComments(key, defaultConfig.getComments(key));
lazy.setInlineComments(key, defaultConfig.getInlineComments(key));
}
});
defaultConfig.getDefaults().getValues(true).forEach((key, value) -> {
if (!(value instanceof ConfigurationSection)) {
lazy.addDefault(key, value);
final ConfigurationSection defaultSection = lazy.getDefaultSection();
if (defaultSection != null) {
defaultSection.setComments(key, defaultConfig.getComments(key));
defaultSection.setInlineComments(key, defaultConfig.getInlineComments(key));
}
}
});
return lazy;
}

@Test
@Override
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
public void testGetName() {
assertEquals("lazy", config.getName());
final ConfigurationSection nestedChild = config.getConfigurationSection("childSection.nestedChildSection");
assertNotNull(nestedChild);
assertEquals("nestedChildSection", nestedChild.getName());
}

@Test
@Override
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
public void testGetCurrentPath() {
assertEquals("lazy", config.getCurrentPath());
final ConfigurationSection nestedChild = config.getConfigurationSection("childSection.nestedChildSection");
assertNotNull(nestedChild);
assertEquals("lazy.childSection.nestedChildSection", nestedChild.getCurrentPath());
}

@Test
@Override
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
public void testGetParent() {
assertEquals(root, config.getParent());

final ConfigurationSection nestedChild = config.getConfigurationSection("childSection.nestedChildSection");
assertNotNull(nestedChild);
final ConfigurationSection nestedChildParent = nestedChild.getParent();
assertNotNull(nestedChildParent);
final ConfigurationSection parentSection = config.getConfigurationSection("childSection");
assertNotNull(parentSection);
assertEquals(parentSection.getValues(true).toString(), nestedChildParent.getValues(true).toString());
}

@Test
@Override
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
public void testGetRoot() {
final ConfigurationSection root = config.getRoot();
assertNotNull(root);
assertEquals(this.root.getValues(true).toString(), root.getValues(true).toString());

final ConfigurationSection nestedChild = config.getConfigurationSection("childSection.nestedChildSection");
assertNotNull(nestedChild);
final ConfigurationSection nestedChildRoot = nestedChild.getRoot();
assertNotNull(nestedChildRoot);
assertEquals(this.root.getValues(true).toString(), nestedChildRoot.getValues(true).toString());
}

@Test
@Override
public void testGetValuesDeepFalse() {
final ConfigurationSection section = config.getConfigurationSection("childSection");
assertNotNull(section);
final Pattern pattern = Pattern.compile(Pattern.quote("{nestedChildSection=") + "\\w+"
+ Pattern.quote("[path='lazy.childSection.nestedChildSection', root='") + "\\w+"
+ Pattern.quote("']}"));
final String sectionString = section.getValues(false).toString();
final Matcher matcher = pattern.matcher(sectionString);
assertTrue(matcher.matches(), "Didn't match regex: " + pattern + "\n" + "Actual string: " + sectionString);
}

@Test
@Override
public void testGetValuesDeepTrue() {
final ConfigurationSection section = config.getConfigurationSection("childSection");
assertNotNull(section);
final Pattern pattern = Pattern.compile(Pattern.quote("{nestedChildSection=") + "\\w+"
+ Pattern.quote("[path='lazy.childSection.nestedChildSection', root='") + "\\w+"
+ Pattern.quote("'], nestedChildSection.key=value}"));
final String sectionString = section.getValues(true).toString();
final Matcher matcher = pattern.matcher(sectionString);
assertTrue(matcher.matches(), "Didn't match regex: " + pattern + "\n" + "Actual string: " + sectionString);
}

@Test
@Override
public void testToString() {
final Pattern pattern = Pattern.compile("\\w+"
+ Pattern.quote("[path='lazy', root='") + "\\w+"
+ Pattern.quote("']"));
final String sectionString = config.toString();
final Matcher matcher = pattern.matcher(sectionString);
assertTrue(matcher.matches(), "Didn't match regex: " + pattern + "\n" + "Actual string: " + sectionString);
}
}

0 comments on commit 47d2e7d

Please sign in to comment.