mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Update and add more unit tests
This commit is contained in:
1 parent
93dd15007a
commit
3a85e7fdf5
8 files changed
+322
-170
No files matched your search
@@ -4,31 +4,29 @@ import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.hook.EconomyHook;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BackCommand extends Command {
|
||||
public class BackCommand extends InGameCommand {
|
||||
|
||||
protected BackCommand(@NotNull HuskHomes plugin) {
|
||||
super("back", List.of(), "", plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
final OnlineUser player = (OnlineUser) executor;
|
||||
final Optional<Position> lastPosition = plugin.getDatabase().getLastPosition(player);
|
||||
public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
|
||||
final Optional<Position> lastPosition = plugin.getDatabase().getLastPosition(executor);
|
||||
if (lastPosition.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_no_last_position")
|
||||
.ifPresent(player::sendMessage);
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
Teleport.builder(plugin)
|
||||
.teleporter(player)
|
||||
.teleporter(executor)
|
||||
.target(lastPosition.get())
|
||||
.economyActions(EconomyHook.Action.BACK_COMMAND)
|
||||
.toTimedTeleport()
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package net.william278.huskhomes.config;
|
||||
|
||||
import net.william278.annotaml.Annotaml;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -11,30 +14,45 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@DisplayName("Locales Tests")
|
||||
public class LocalesTests {
|
||||
|
||||
private static Locales englishLocales;
|
||||
|
||||
@BeforeEach
|
||||
@DisplayName("Test Loading English Locales")
|
||||
@Test
|
||||
public void testAllLocalesPresent() {
|
||||
// Load locales/en-gb.yml as an InputStream
|
||||
try (InputStream localeStream = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) {
|
||||
Assertions.assertNotNull(localeStream, "en-gb.yml is missing from the locales folder");
|
||||
final Locales englishLocales = Annotaml.create(Locales.class, localeStream).get();
|
||||
final Set<String> keys = englishLocales.rawLocales.keySet();
|
||||
|
||||
// Iterate through every locale file in the locales folder
|
||||
URL url = LocalesTests.class.getClassLoader().getResource("locales");
|
||||
Assertions.assertNotNull(url, "locales folder is missing");
|
||||
|
||||
for (File file : Objects.requireNonNull(new File(url.getPath()).listFiles(file -> file.getName().endsWith("yml")
|
||||
&& !file.getName().equals("en-gb.yml")))) {
|
||||
final Set<String> fileKeys = Annotaml.create(file, Locales.class).get().rawLocales.keySet();
|
||||
keys.forEach(key -> Assertions.assertTrue(fileKeys.contains(key),
|
||||
"Locale key " + key + " is missing from " + file.getName()));
|
||||
}
|
||||
public void testLoadEnglishLocales() {
|
||||
try (InputStream resource = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) {
|
||||
Assertions.assertNotNull(resource, "en-gb.yml is missing from the locales folder");
|
||||
englishLocales = Annotaml.create(Locales.class, resource).get();
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{1} Locales")
|
||||
@DisplayName("Test All Locale Keys Present")
|
||||
@MethodSource("provideLocaleFiles")
|
||||
public void testAllLocaleKeysPresent(@NotNull File file, @SuppressWarnings("unused") @NotNull String keyName) {
|
||||
try {
|
||||
final Set<String> fileKeys = Annotaml.create(file, Locales.class).get().rawLocales.keySet();
|
||||
englishLocales.rawLocales.keySet()
|
||||
.forEach(key -> Assertions.assertTrue(fileKeys.contains(key),
|
||||
"Locale key " + key + " is missing from " + file.getName()));
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideLocaleFiles() {
|
||||
URL url = LocalesTests.class.getClassLoader().getResource("locales");
|
||||
Assertions.assertNotNull(url, "locales folder is missing");
|
||||
return Stream.of(Objects.requireNonNull(new File(url.getPath())
|
||||
.listFiles(file -> file.getName().endsWith("yml") && !file.getName().equals("en-gb.yml"))))
|
||||
.map(file -> Arguments.of(file, file.getName().replace(".yml", "")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@DisplayName("Plan Hook Tests")
|
||||
public class PlanDataExtensionTests {
|
||||
|
||||
// Throws IllegalArgumentException if there is an implementation error or warning.
|
||||
@Test
|
||||
public void testPlanDataExtension() {
|
||||
@DisplayName("Test Plan Hook Implementation")
|
||||
public void testPlanHookImplementation() {
|
||||
new ExtensionExtractor(new PlanHook.PlanDataExtension()).validateAnnotations();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@DisplayName("Message Serialization Tests")
|
||||
public class MessageSerializationTests {
|
||||
|
||||
private final static List<Message> TEST_MESSAGES = List.of(
|
||||
Message.builder()
|
||||
.type(Message.Type.REQUEST_PLAYER_LIST)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.empty())
|
||||
.build(),
|
||||
Message.builder()
|
||||
.type(Message.Type.TELEPORT_REQUEST)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.withPosition(
|
||||
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
||||
.build(),
|
||||
Message.builder()
|
||||
.type(Message.Type.TELEPORT_TO_NETWORKED_USER)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.withString("TestString"))
|
||||
.build(),
|
||||
Message.builder()
|
||||
.type(Message.Type.PLAYER_LIST)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.withStringList(List.of("TestString1", "TestString2", "TestString3")))
|
||||
.build(),
|
||||
Message.builder()
|
||||
.type(Message.Type.TELEPORT_TO_POSITION)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.withPosition(
|
||||
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
||||
.build()
|
||||
);
|
||||
|
||||
// Parameterized test for Message serialization
|
||||
@DisplayName("Test Message Serialization/Deserialization")
|
||||
@ParameterizedTest(name = "{1} Message")
|
||||
@MethodSource("provideMessages")
|
||||
public void testMessageSerialization(@NotNull Message message, @SuppressWarnings("unused") String ignored) {
|
||||
final Gson gson = createGson();
|
||||
final String serializedMessage = gson.toJson(message);
|
||||
Assertions.assertNotNull(serializedMessage);
|
||||
final Message deserializedMessage = gson.fromJson(serializedMessage, Message.class);
|
||||
Assertions.assertNotNull(deserializedMessage);
|
||||
Assertions.assertEquals(message.getType(), deserializedMessage.getType());
|
||||
Assertions.assertEquals(message.getScope(), deserializedMessage.getScope());
|
||||
Assertions.assertEquals(message.getTarget(), deserializedMessage.getTarget());
|
||||
Assertions.assertEquals(message.getPayload().getPosition().isPresent(), deserializedMessage.getPayload().getPosition().isPresent());
|
||||
Assertions.assertEquals(message.getPayload().getString().isPresent(), deserializedMessage.getPayload().getString().isPresent());
|
||||
Assertions.assertEquals(message.getPayload().getStringList().isPresent(), deserializedMessage.getPayload().getStringList().isPresent());
|
||||
Assertions.assertEquals(message.getPayload().getStringList().isPresent(), deserializedMessage.getPayload().getStringList().isPresent());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Gson createGson() {
|
||||
return new GsonBuilder().create();
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideMessages() {
|
||||
return TEST_MESSAGES.stream().map(message -> Arguments.of(message, message.getType().name()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class MessageTests {
|
||||
|
||||
private final static Gson GSON = new GsonBuilder().create();
|
||||
private final static Message DUMMY_NO_PAYLOAD = Message.builder()
|
||||
.type(Message.Type.TELEPORT_REQUEST)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.empty())
|
||||
.build();
|
||||
|
||||
private final static Message DUMMY_PAYLOAD = Message.builder()
|
||||
.type(Message.Type.TELEPORT_REQUEST)
|
||||
.target("TestTarget")
|
||||
.payload(Payload.withPosition(
|
||||
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
||||
.build();
|
||||
|
||||
@Test
|
||||
@DisplayName("Test message serialization with empty payload")
|
||||
public void testMessageSerializationWithEmptyPayload() {
|
||||
Assertions.assertNotNull(GSON.toJson(DUMMY_NO_PAYLOAD));
|
||||
|
||||
final Message readMessage = GSON.fromJson(GSON.toJson(DUMMY_NO_PAYLOAD), Message.class);
|
||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getUuid(), readMessage.getUuid());
|
||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getTarget(), readMessage.getTarget());
|
||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getType(), readMessage.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test message serialization with position payload")
|
||||
public void testMessageSerializationWithPositionPayload() {
|
||||
Assertions.assertNotNull(GSON.toJson(DUMMY_PAYLOAD));
|
||||
|
||||
final Message readMessage = GSON.fromJson(GSON.toJson(DUMMY_PAYLOAD), Message.class);
|
||||
Assertions.assertEquals(DUMMY_PAYLOAD.getUuid(), readMessage.getUuid());
|
||||
Assertions.assertNotNull(readMessage.getPayload().getPosition());
|
||||
|
||||
assert readMessage.getPayload().getPosition().isPresent() && DUMMY_PAYLOAD.getPayload().getPosition().isPresent();
|
||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getX(),
|
||||
readMessage.getPayload().getPosition().get().getX());
|
||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getY(),
|
||||
readMessage.getPayload().getPosition().get().getY());
|
||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getZ(),
|
||||
readMessage.getPayload().getPosition().get().getZ());
|
||||
}
|
||||
}
|
||||
@@ -4,46 +4,82 @@ import com.github.plot.Plot;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@DisplayName("Normal Distribution Tests")
|
||||
public class NormalDistributionTests {
|
||||
|
||||
private final static float MEAN = 0.75f;
|
||||
private final static float STANDARD_DEVIATION = 2f;
|
||||
private final static float SPAWN_RADIUS = 500f;
|
||||
private final static float MAX_RADIUS = 3000f;
|
||||
|
||||
@Test
|
||||
public void testNormalDistributionRandomizer() {
|
||||
@DisplayName("Test Point Generation")
|
||||
public void testPointGenerator() {
|
||||
Assertions.assertEquals(1000, generateLocations(1000).size());
|
||||
}
|
||||
|
||||
// Plots location distribution
|
||||
public static void generateRtpLocationDistributionImage() {
|
||||
final List<Location> locations = generateLocations(2000);
|
||||
final double[] xValues = new double[locations.size()];
|
||||
final double[] zValues = new double[locations.size()];
|
||||
for (int i = 0; i < locations.size(); i++) {
|
||||
xValues[i] = locations.get(i).getX();
|
||||
zValues[i] = locations.get(i).getZ();
|
||||
// Test points are within range
|
||||
@Test
|
||||
@DisplayName("Test Points Within Range")
|
||||
public void testPointRange() {
|
||||
final List<Location> locations = generateLocations(1000);
|
||||
for (Location location : locations) {
|
||||
Assertions.assertTrue(location.getX() >= -MAX_RADIUS && location.getX() <= MAX_RADIUS);
|
||||
Assertions.assertTrue(location.getZ() >= -MAX_RADIUS && location.getZ() <= MAX_RADIUS);
|
||||
}
|
||||
final Plot plot = Plot.plot(Plot.plotOpts()
|
||||
.title("RTP Normal Distribution demo")
|
||||
.legend(Plot.LegendFormat.BOTTOM))
|
||||
.xAxis("x", Plot.axisOpts().range(-3250, 3250))
|
||||
.yAxis("z", Plot.axisOpts().range(-3250, 3250))
|
||||
.series("Data", Plot.data()
|
||||
.xy(xValues, zValues),
|
||||
Plot.seriesOpts()
|
||||
.marker(Plot.Marker.CIRCLE)
|
||||
.markerColor(new Color(0, 60, 255, 180))
|
||||
.color(new Color(0, 0, 0, 0)));
|
||||
try {
|
||||
plot.save("images/rtp-location-distribution", "png");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plots location distribution to an image for testing
|
||||
*/
|
||||
public static class DistributionModeller {
|
||||
|
||||
/**
|
||||
* Plot a demo of the normal distribution algorithm
|
||||
*
|
||||
* @param args file path to save image to
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void main(String[] args) {
|
||||
final List<Location> locations = generateLocations(2000);
|
||||
final double[] xValues = new double[locations.size()];
|
||||
final double[] zValues = new double[locations.size()];
|
||||
for (int i = 0; i < locations.size(); i++) {
|
||||
xValues[i] = locations.get(i).getX();
|
||||
zValues[i] = locations.get(i).getZ();
|
||||
}
|
||||
final Plot plot = Plot.plot(Plot.plotOpts()
|
||||
.title("RTP Normal Distribution demo")
|
||||
.legend(Plot.LegendFormat.BOTTOM))
|
||||
.xAxis("x", Plot.axisOpts().range(-3250, 3250))
|
||||
.yAxis("z", Plot.axisOpts().range(-3250, 3250))
|
||||
.series("Data", Plot.data()
|
||||
.xy(xValues, zValues),
|
||||
Plot.seriesOpts()
|
||||
.marker(Plot.Marker.CIRCLE)
|
||||
.markerColor(new Color(0, 60, 255, 180))
|
||||
.color(new Color(0, 0, 0, 0)));
|
||||
|
||||
try {
|
||||
final File output = args.length == 0 ? new File("target", "normal_distribution")
|
||||
: new File(args[0], "normal_distribution");
|
||||
|
||||
plot.save(output.getAbsolutePath(), "png");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static List<Location> generateLocations(int amount) {
|
||||
@@ -51,7 +87,7 @@ public class NormalDistributionTests {
|
||||
for (int i = 0; i < amount; i++) {
|
||||
locations.add(NormalDistributionEngine.generateLocation(
|
||||
new Location(0, 0, 0, 0, 0, new World("TestWorld", UUID.randomUUID())),
|
||||
0.75f, 2f, 500f, 3000f));
|
||||
MEAN, STANDARD_DEVIATION, SPAWN_RADIUS, MAX_RADIUS));
|
||||
}
|
||||
return locations;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user