From d51b8db0bfc3b58a4eed98f756c34960ffe0d3e0 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 1 May 2022 22:13:37 +0200 Subject: [PATCH] :art: Refactor --- .../network/packets/PacketHandler.java | 11 ++-- .../steamwar/network/PacketHandlerTest.java | 52 +++++++++++++++++++ testsrc/de/steamwar/network/PacketTest.java | 6 +-- testsrc/de/steamwar/network/TestPacket.java | 30 +++++++++++ .../steamwar/network/TestPacketHandler.java | 30 +++++++++++ 5 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 testsrc/de/steamwar/network/PacketHandlerTest.java create mode 100644 testsrc/de/steamwar/network/TestPacket.java create mode 100644 testsrc/de/steamwar/network/TestPacketHandler.java diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index 2f01ab0..f23afe5 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -19,6 +19,8 @@ package de.steamwar.network.packets; +import lombok.SneakyThrows; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @@ -39,7 +41,7 @@ public abstract class PacketHandler { } } - public PacketHandler() { + protected PacketHandler() { Method[] methods = getClass().getMethods(); for (Method method : methods) { if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { @@ -60,16 +62,13 @@ public abstract class PacketHandler { PACKET_HANDLERS.remove(this); } + @SneakyThrows public void handle(NetworkPacket packet) { Method method = HANDLER_MAP.get(packet.getClass()); if (method == null) { return; } - try { - method.invoke(this, packet); - } catch (Exception e) { - e.printStackTrace(); - } + method.invoke(this, packet); } @Retention(RetentionPolicy.RUNTIME) diff --git a/testsrc/de/steamwar/network/PacketHandlerTest.java b/testsrc/de/steamwar/network/PacketHandlerTest.java new file mode 100644 index 0000000..ac2299c --- /dev/null +++ b/testsrc/de/steamwar/network/PacketHandlerTest.java @@ -0,0 +1,52 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.network; + +import de.steamwar.network.packets.PacketHandler; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +public class PacketHandlerTest { + + TestPacketHandler packetHandler; + + @Before + public void setUp() { + packetHandler = new TestPacketHandler(); + packetHandler.register(); + } + + @Test + public void testPacketHandler() { + TestPacket packet = new TestPacket(); + try { + PacketHandler.handlePacket(packet); + } catch (Exception e) { + assertThat(e, instanceOf(InvocationTargetException.class)); + assertThat(e.getCause(), instanceOf(SecurityException.class)); + assertThat(e.getCause().getMessage(), equalTo(packet.getClass().getName())); + } + } +} diff --git a/testsrc/de/steamwar/network/PacketTest.java b/testsrc/de/steamwar/network/PacketTest.java index 5a65da6..316ea6f 100644 --- a/testsrc/de/steamwar/network/PacketTest.java +++ b/testsrc/de/steamwar/network/PacketTest.java @@ -22,7 +22,8 @@ package de.steamwar.network; import de.steamwar.network.packets.NetworkPacket; import org.junit.Test; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.MatcherAssert.*; public class PacketTest { @@ -31,8 +32,7 @@ public class PacketTest { for (NetworkPacket allPacket : AllPackets.getAllPackets()) { byte[] data = allPacket.serialize(); NetworkPacket deserializedPacket = NetworkPacket.deserialize(data); - assertEquals("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, deserializedPacket); - System.out.println("Packet: " + allPacket.getClass().getName() + " is correctly deserialized"); + assertThat("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, equalTo(deserializedPacket)); } } } diff --git a/testsrc/de/steamwar/network/TestPacket.java b/testsrc/de/steamwar/network/TestPacket.java new file mode 100644 index 0000000..7f95f03 --- /dev/null +++ b/testsrc/de/steamwar/network/TestPacket.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.network; + +import de.steamwar.network.packets.NetworkPacket; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class TestPacket extends NetworkPacket { + private static final long serialVersionUID = -9079104421295590626L; +} diff --git a/testsrc/de/steamwar/network/TestPacketHandler.java b/testsrc/de/steamwar/network/TestPacketHandler.java new file mode 100644 index 0000000..2d525be --- /dev/null +++ b/testsrc/de/steamwar/network/TestPacketHandler.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.network; + +import de.steamwar.network.packets.PacketHandler; + +public class TestPacketHandler extends PacketHandler { + + @Handler + public void handle(TestPacket packet) { + throw new SecurityException(packet.getClass().getName()); + } +}