Add more test cases
This commit is contained in:
parent
9d7bba24b3
commit
905a0a5fa6
@ -1,9 +1,31 @@
|
||||
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;
|
||||
|
||||
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
|
||||
public void connect(String sessionID) throws ConnectionException {
|
||||
|
||||
@ -16,22 +38,28 @@ public class DummyConnection implements RemoteConnection {
|
||||
|
||||
@Override
|
||||
public int getSiteID() {
|
||||
return 0;
|
||||
return siteID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Message message) {
|
||||
if(sendMessageHandler != null) {
|
||||
sendMessageHandler.accept(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void receive(Message message) {
|
||||
listeners.forEach(l -> l.onMessage(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(MessageListener listener) {
|
||||
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(MessageListener listener) {
|
||||
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -12,7 +12,7 @@ public class DocumentTest {
|
||||
|
||||
@Test
|
||||
public void testLocalInsert() {
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
|
||||
doc.localInsert(0, "Hello");
|
||||
assertEquals("Hello", doc.getContentsAsString());
|
||||
doc.localInsert(5, " World");
|
||||
@ -23,7 +23,7 @@ public class DocumentTest {
|
||||
|
||||
@Test
|
||||
public void testLocalInsertInvalidIndexFails() {
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
|
||||
doc.localInsert(0, "Hello");
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(-1, "Test"));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(6, "Test"));
|
||||
@ -31,7 +31,7 @@ public class DocumentTest {
|
||||
|
||||
@Test
|
||||
public void testLocalDelete() {
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
|
||||
doc.localInsert(0, "Hello World!");
|
||||
doc.localDelete(5, 6);
|
||||
assertEquals("Hello!", doc.getContentsAsString());
|
||||
@ -39,7 +39,7 @@ public class DocumentTest {
|
||||
|
||||
@Test
|
||||
public void testLocalDeleteInvalidIndexFails() {
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
|
||||
SharedDocument doc = new SharedDocument(new DummyConnection(0), "test");
|
||||
doc.localInsert(0, "Hello World!");
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.localDelete(-1, 10));
|
||||
assertThrows(IllegalArgumentException.class, () -> doc.localDelete(12, 1));
|
||||
|
37
src/test/java/me/mrletsplay/shareclientcore/SharingTest.java
Normal file
37
src/test/java/me/mrletsplay/shareclientcore/SharingTest.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user