Skip to content

Commit

Permalink
[#122] Tests added.
Browse files Browse the repository at this point in the history
Code refactored.
  • Loading branch information
DjThunder committed Dec 31, 2023
1 parent 639efb3 commit d18d1db
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.b3dgs.lionengine.Constant;
import com.b3dgs.lionengine.LionEngineException;
import com.b3dgs.lionengine.UtilConversion;
import com.b3dgs.lionengine.network.client.InfoGet;

/**
* Network utility.
Expand Down Expand Up @@ -110,6 +111,27 @@ public static ByteBuffer getBuffer(DatagramPacket packet)
return buffer;
}

/**
* Get the server info.
*
* @param ip The server ip.
* @param port The server port.
* @return The info data.
* @throws IOException If error.
*/
public static ByteBuffer getInfo(String ip, int port) throws IOException
{
try (DatagramSocket socket = new DatagramSocket())
{
final ByteBuffer buffer = ByteBuffer.allocate(1);
buffer.put(UtilNetwork.toByte(MessageType.INFO));
final ByteBuffer send = UtilNetwork.createPacket(buffer);
socket.send(new DatagramPacket(send.array(), send.capacity(), InetAddress.getByName(ip), port));

return InfoGet.decode(socket);
}
}

/**
* Create full packet from data with header.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2013-2023 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.b3dgs.lionengine.network;

import static com.b3dgs.lionengine.UtilAssert.assertEquals;

import org.junit.jupiter.api.Test;

import com.b3dgs.lionengine.UtilTests;

/**
* Test {@link NetworkType}.
*/
final class NetworkTypeTest
{
/**
* Test the enum.
*
* @throws Exception If error.
*/
@Test
void testEnum() throws Exception
{
UtilTests.testEnum(NetworkType.class);
}

/**
* Test from.
*/
@Test
void testFrom()
{
assertEquals(NetworkType.SERVER, NetworkType.from("server"));
assertEquals(NetworkType.SERVER, NetworkType.from("SERVER"));

assertEquals(NetworkType.CLIENT, NetworkType.from("client"));
assertEquals(NetworkType.CLIENT, NetworkType.from("CLIENT"));

assertEquals(NetworkType.NONE, NetworkType.from(null));
assertEquals(NetworkType.NONE, NetworkType.from(""));
assertEquals(NetworkType.NONE, NetworkType.from("none"));
assertEquals(NetworkType.NONE, NetworkType.from("void"));
}
}
Loading

0 comments on commit d18d1db

Please sign in to comment.