Add tests

This commit is contained in:
Flare Microsystems
2024-11-10 20:15:43 -08:00
parent dd47f1e242
commit 75eb55d341
21 changed files with 583 additions and 32 deletions

View File

@@ -0,0 +1,68 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
@RunWith(MockitoJUnitRunner.class)
public class Packet0IdentifyTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet0Identify packet = new Packet0Identify(1337);
assertEquals(1337, packet.getFlags());
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
packet.sendPacket(output());
assertEquals(0, input().readByte());
assertEquals(Packet.PROTOCOL_VERSION, input().readInt());
assertEquals(1337, input().readInt());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet0Identify packet = new Packet0Identify();
output().writeInt(Packet.PROTOCOL_VERSION);
output().writeInt(1337);
packet.recievePacket(input());
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
assertEquals(1337, packet.getFlags());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet0Identify packet = new Packet0Identify(1337);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet0Identify packet = new Packet0Identify();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,64 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
@RunWith(MockitoJUnitRunner.class)
public class Packet1StatusTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet1Status packet = new Packet1Status(1337);
assertEquals(1337, packet.getFlags());
packet.sendPacket(output());
assertEquals(1, input().readByte());
assertEquals(1337, input().readInt());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet1Status packet = new Packet1Status();
output().writeInt(1337);
packet.recievePacket(input());
assertEquals(1337, packet.getFlags());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet1Status packet = new Packet1Status(1337);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet1Status packet = new Packet1Status();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,66 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
@RunWith(MockitoJUnitRunner.class)
public class Packet2ScriptTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet2Script packet = new Packet2Script("1337");
assertEquals("1337", packet.script);
packet.sendPacket(output());
assertEquals(2, input().readByte());
assertEquals("1337", input().readUTF());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet2Script packet = new Packet2Script();
packet.script = "";
output().writeUTF("1337");
packet.recievePacket(input());
assertEquals("1337", packet.script);
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet2Script packet = new Packet2Script("1337");
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet2Script packet = new Packet2Script();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,68 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
@RunWith(MockitoJUnitRunner.class)
public class Packet3CloneTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet3Clone packet = new Packet3Clone(1, 2);
assertEquals(packet.getFileID(), 1);
assertEquals(packet.getFileSize(), 2);
packet.sendPacket(output());
assertEquals(3, input().readByte());
assertEquals(1, input().readLong());
assertEquals(2, input().readLong());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet3Clone packet = new Packet3Clone();
output().writeLong(1);
output().writeLong(2);
packet.recievePacket(input());
assertEquals(1, packet.getFileID());
assertEquals(2, packet.getFileSize());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet3Clone packet = new Packet3Clone(1, 2);
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet3Clone packet = new Packet3Clone();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,67 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
@RunWith(MockitoJUnitRunner.class)
public class Packet4FileDataTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
assertEquals(packet.getFileId(), 1);
assertEquals("file.txt", packet.getRelativeFile());
packet.sendPacket(output());
assertEquals(5, input().readByte());
assertEquals(1, input().readLong());
assertEquals("file.txt", input().readUTF());
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet5Artifact packet = new Packet5Artifact();
output().writeLong(1);
output().writeUTF("file.txt");
packet.recievePacket(input());
assertEquals(1, packet.getFileId());
assertEquals("file.txt", packet.getRelativeFile());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet5Artifact packet = new Packet5Artifact();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,74 @@
package com.flaremicro.crossjeeves.net.packet.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
@RunWith(MockitoJUnitRunner.class)
public class Packet5ArtifactTest extends PacketTestBase {
@Mock
NetworkHandler handler;
@Test
public void testWrite() throws IOException
{
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
assertEquals(packet.getFileId(), 1);
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
packet.sendPacket(output());
byte[] buffer = new byte[4];
assertEquals(4, input().readByte());
assertEquals(1, input().readLong());
assertEquals(4, input().readShort());
input().readFully(buffer);
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
assertBufferEmpty();
}
@Test
public void testRead() throws IOException
{
Packet4FileData packet = new Packet4FileData();
output().writeLong(1);
output().writeShort(4);
output().write(new byte[]{1,2,3,4});
packet.recievePacket(input());
assertEquals(1, packet.getFileId());
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
assertBufferEmpty();
}
@Test
public void testProcess() throws IOException
{
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
packet.processPacket(handler);
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
}
@Test
public void testTypeCopy()
{
Packet4FileData packet = new Packet4FileData();
Packet clonedPacket = packet.cloneTypeOnly();
assertEquals(packet.getClass(), clonedPacket.getClass());
}
}

View File

@@ -0,0 +1,44 @@
package com.flaremicro.crossjeeves.net.packet.test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.Before;
import com.flaremicro.test.util.InOutBuffer;
public abstract class PacketTestBase {
private static InOutBuffer buffer = new InOutBuffer();
private static DataInputStream input = new DataInputStream(buffer.getInputStream());
private static DataOutputStream output = new DataOutputStream(buffer.getOutputStream());
@AfterClass
public static void cleanUp() throws IOException {
buffer.empty();
input.close();
output.close();
}
@Before
public void clearBuffer()
{
buffer.empty();
}
public void assertBufferEmpty() {
assertEquals(0, buffer.size());
}
protected DataInputStream input() {
return input;
}
protected DataOutputStream output() {
return output;
}
}