Dieser Commit ist enthalten in:
Chaoscaot 2022-05-01 22:13:37 +02:00
Ursprung ddc7353ad1
Commit d51b8db0bf
5 geänderte Dateien mit 120 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -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)

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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()));
}
}
}

Datei anzeigen

@ -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));
}
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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());
}
}