Add more test cases

This commit is contained in:
MrLetsplay 2024-06-07 07:54:33 +02:00
parent 9d7bba24b3
commit 905a0a5fa6
Signed by: mr
SSH Key Fingerprint: SHA256:0zWNiRisbQ4dq/CCQAaMLoF3UfkF5wKPXO7DcjfFBEU
3 changed files with 72 additions and 7 deletions

View File

@ -1,9 +1,31 @@
package me.mrletsplay.shareclientcore.connection; package me.mrletsplay.shareclientcore.connection;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import me.mrletsplay.shareclientcore.connection.message.Message; import me.mrletsplay.shareclientcore.connection.message.Message;
public class DummyConnection implements RemoteConnection { public class DummyConnection implements RemoteConnection {
private int siteID;
private Consumer<Message> sendMessageHandler;
private List<MessageListener> listeners;
public DummyConnection(int siteID, Consumer<Message> sendMessageHandler) {
this.siteID = siteID;
this.sendMessageHandler = sendMessageHandler;
this.listeners = new ArrayList<>();
}
public DummyConnection(int siteID) {
this(siteID, null);
}
public void setSendMessageHandler(Consumer<Message> sendMessageHandler) {
this.sendMessageHandler = sendMessageHandler;
}
@Override @Override
public void connect(String sessionID) throws ConnectionException { public void connect(String sessionID) throws ConnectionException {
@ -16,22 +38,28 @@ public class DummyConnection implements RemoteConnection {
@Override @Override
public int getSiteID() { public int getSiteID() {
return 0; return siteID;
} }
@Override @Override
public void send(Message message) { public void send(Message message) {
if(sendMessageHandler != null) {
sendMessageHandler.accept(message);
}
}
public void receive(Message message) {
listeners.forEach(l -> l.onMessage(message));
} }
@Override @Override
public void addListener(MessageListener listener) { public void addListener(MessageListener listener) {
listeners.add(listener);
} }
@Override @Override
public void removeListener(MessageListener listener) { public void removeListener(MessageListener listener) {
listeners.remove(listener);
} }
@Override @Override

View File

@ -12,7 +12,7 @@ public class DocumentTest {
@Test @Test
public void testLocalInsert() { public void testLocalInsert() {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
doc.localInsert(0, "Hello"); doc.localInsert(0, "Hello");
assertEquals("Hello", doc.getContentsAsString()); assertEquals("Hello", doc.getContentsAsString());
doc.localInsert(5, " World"); doc.localInsert(5, " World");
@ -23,7 +23,7 @@ public class DocumentTest {
@Test @Test
public void testLocalInsertInvalidIndexFails() { public void testLocalInsertInvalidIndexFails() {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
doc.localInsert(0, "Hello"); doc.localInsert(0, "Hello");
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(-1, "Test")); assertThrows(IllegalArgumentException.class, () -> doc.localInsert(-1, "Test"));
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(6, "Test")); assertThrows(IllegalArgumentException.class, () -> doc.localInsert(6, "Test"));
@ -31,7 +31,7 @@ public class DocumentTest {
@Test @Test
public void testLocalDelete() { public void testLocalDelete() {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
doc.localInsert(0, "Hello World!"); doc.localInsert(0, "Hello World!");
doc.localDelete(5, 6); doc.localDelete(5, 6);
assertEquals("Hello!", doc.getContentsAsString()); assertEquals("Hello!", doc.getContentsAsString());
@ -39,7 +39,7 @@ public class DocumentTest {
@Test @Test
public void testLocalDeleteInvalidIndexFails() { public void testLocalDeleteInvalidIndexFails() {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
doc.localInsert(0, "Hello World!"); doc.localInsert(0, "Hello World!");
assertThrows(IllegalArgumentException.class, () -> doc.localDelete(-1, 10)); assertThrows(IllegalArgumentException.class, () -> doc.localDelete(-1, 10));
assertThrows(IllegalArgumentException.class, () -> doc.localDelete(12, 1)); assertThrows(IllegalArgumentException.class, () -> doc.localDelete(12, 1));

View File

@ -0,0 +1,37 @@
package me.mrletsplay.shareclientcore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import me.mrletsplay.shareclientcore.connection.DummyConnection;
import me.mrletsplay.shareclientcore.document.SharedDocument;
public class SharingTest {
@Test
public void testSharedDocument() {
DummyConnection a = new DummyConnection(0);
DummyConnection b = new DummyConnection(1);
a.setSendMessageHandler(m -> b.receive(m));
b.setSendMessageHandler(m -> a.receive(m));
SharedDocument sharedA = new SharedDocument(a, "doc");
SharedDocument sharedB = new SharedDocument(b, "doc");
a.addListener(sharedA);
b.addListener(sharedB);
sharedA.localInsert(0, "Hello World!");
assertEquals("Hello World!", sharedA.getContentsAsString());
assertEquals("Hello World!", sharedB.getContentsAsString());
sharedB.localInsert(0, "This is a test!");
assertEquals("This is a test!Hello World!", sharedA.getContentsAsString());
assertEquals("This is a test!Hello World!", sharedB.getContentsAsString());
}
}