package javaflow.network.impl;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import javaflow.components.api.Packet;
import javaflow.network.api.PacketListener;
import org.testng.Assert;
import org.testng.annotations.Test;
public class PacketImplTest {
@Test
public void copyMetaData() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
p1.setMetadata("a", "a");
p2.copyMetadataFrom(p1);
p1.setMetadata("a", "b");
Assert.assertEquals(p2.getMetadata("a"), "a");
Assert.assertEquals(p1.getMetadata("a"), "b");
}
int packetCount = 0;
PacketImpl createPacket(String name) {
return new PacketImpl(name, null);
}
@Test
public void copyMetaDataWhenItDoesNotExist() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
p2.copyMetadataFrom(p1);
p1.setMetadata("a", "b");
Assert.assertEquals(p2.getMetadata("a"), null);
Assert.assertEquals(p1.getMetadata("a"), "b");
}
@Test
public void attachingPacketsMakesTree() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
Assert.assertFalse(p1.isTree());
p1.attach(p2);
Assert.assertTrue(p1.isTree());
}
@Test
public void testGetSubchainOnNonTreePacket() {
PacketImpl p1 = createPacket("a");
Assert.assertNull(p1.getSubchain("jee"));
}
@Test
public void testAttachedTo() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
Assert.assertNull(p1.attachedTo());
Assert.assertNull(p2.attachedTo());
p1.attach(p2);
Assert.assertNull(p1.attachedTo());
Assert.assertEquals(p2.attachedTo(),p1);
}
@Test
public void attachingRemovesPreviousAttachement() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
p1.attach(p2);
Assert.assertTrue(p1.isTree());
Assert.assertFalse(p3.isTree());
p3.attach(p2);
Assert.assertFalse(p1.isTree());
Assert.assertTrue(p3.isTree());
}
@Test
public void testChainIterating() {
PacketImpl packet = createPacket("a");
PacketImpl p1 = createPacket("b");
PacketImpl p2 = createPacket("c");
PacketImpl p3 = createPacket("d");
packet.attach(p1);
packet.attach(p2);
packet.attach(p3);
Iterator<Packet> iterator = packet.iterator();
Assert.assertTrue(iterator.hasNext());
Packet p;
p = iterator.next();
Assert.assertEquals(p,p1);
p1.detach();
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(p,p2);
iterator.remove();
Assert.assertTrue(iterator.hasNext());
p = iterator.next();
Assert.assertEquals(p,p3);
Assert.assertFalse(iterator.hasNext());
}
@Test(expectedExceptions = IllegalStateException.class)
public void testRemovalBeforeNextWithIterator() {
PacketImpl packet = createPacket("a");
PacketImpl p1 = createPacket("b");
Iterator<Packet> iterator = packet.iterator();
iterator.remove();
}
@Test
public void testIteratorCanHandleChangesMadeOutsideTheIterator() {
PacketImpl packet = createPacket("a");
PacketImpl p1 = createPacket("b");
PacketImpl p2 = createPacket("c");
PacketImpl p3 = createPacket("d");
PacketImpl p4 = createPacket("e");
PacketImpl p5 = createPacket("f");
PacketImpl p6 = createPacket("g");
packet.attach(p1);
packet.attach(p2);
packet.attach(p3);
packet.attach(p4);
packet.attach(p5);
Iterator<Packet> iterator = packet.iterator();
p1.detach();
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(iterator.next(),p2);
p2.detach();
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(iterator.next(),p3);
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(iterator.next(),p4);
p3.detach();
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(iterator.next(),p5);
Assert.assertFalse(iterator.hasNext());
p4.detach();
Assert.assertFalse(iterator.hasNext());
packet.attach(p6);
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(iterator.next(),p6);
}
@Test
public void treeCanBeQueried() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
Assert.assertFalse(p1.isTree());
p1.attach(p2);
p1.attach(p3);
Assert.assertNull(p1.getSubchain("d"));
Assert.assertNotNull(p1.getSubchain("b"));
}
@Test
public void treeCanBeDissassemble() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
Assert.assertFalse(p1.isTree());
p1.attach(p2);
p1.getSubchain("b").detach();
Assert.assertFalse(p1.isTree());
}
@Test
public void chainCanBeIterated() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
Assert.assertFalse(p1.isTree());
p1.attach(p2);
p1.attach(p3);
Iterator<? extends Packet> i = p1.iterator();
Assert.assertEquals(i.next().getContent(), "b");
Assert.assertEquals(i.next().getContent(), "c");
}
@Test
public void packetTreeSize() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
Assert.assertEquals(p1.totalTreeSize(), 1);
Assert.assertEquals(p2.totalTreeSize(), 1);
p1.attach(p2);
Assert.assertEquals(p1.totalTreeSize(), 2);
Assert.assertEquals(p2.totalTreeSize(), 1);
p2.detach();
Assert.assertEquals(p1.totalTreeSize(), 1);
Assert.assertEquals(p2.totalTreeSize(), 1);
p1.attach(p2);
p2.attach(p3);
Assert.assertEquals(p1.totalTreeSize(), 3);
Assert.assertEquals(p2.totalTreeSize(), 2);
Assert.assertEquals(p3.totalTreeSize(), 1);
p2.detach();
Assert.assertEquals(p1.totalTreeSize(), 1);
Assert.assertEquals(p2.totalTreeSize(), 2);
Assert.assertEquals(p3.totalTreeSize(), 1);
}
@Test
public void packetChainSize() {
PacketImpl p1 = createPacket("a");
PacketImpl p2 = createPacket("b");
PacketImpl p3 = createPacket("c");
Assert.assertEquals(p1.chainSize(), 0);
Assert.assertEquals(p2.chainSize(), 0);
Assert.assertEquals(p3.chainSize(), 0);
p1.attach(p2);
Assert.assertEquals(p1.chainSize(), 1);
Assert.assertEquals(p2.chainSize(), 0);
Assert.assertEquals(p3.chainSize(), 0);
p2.detach();
Assert.assertEquals(p1.chainSize(), 0);
Assert.assertEquals(p2.chainSize(), 0);
Assert.assertEquals(p3.chainSize(), 0);
p1.attach(p2);
p1.attach(p3);
Assert.assertEquals(p1.chainSize(), 2);
Assert.assertEquals(p2.chainSize(), 0);
Assert.assertEquals(p3.chainSize(), 0);
}
}