/**
*
*/
package info.walnutstreet.vs.junit.ps04;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.Set;
import info.walnutstreet.vs.ps04.p2p.P2PFingerTable;
import info.walnutstreet.vs.ps04.p2p.ring.Node;
import info.walnutstreet.vs.ps04.p2p.ring.interfaces.NodeInterface;
import junit.framework.TestCase;
/**
* @author Christoph Gostner
* @version 0.4
*
* This class tests mainly the join function of the Node.
* It tests the Node with a ring with 3 and 4 elements in all
* possible permutations.
*/
public class TestNode extends TestCase {
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPerm1() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node0" "node1" "node2"
assertTrue(node.join(node1));
assertTrue(node.join(node2));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void testPerm2() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node1" "node0" "node2"
assertTrue(node1.join(node));
assertTrue(node1.join(node2));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void testPerm3() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node1" "node2" "node0"
assertTrue(node1.join(node2));
assertTrue(node1.join(node));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void testPerm4() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node0" "node2" "node1"
assertTrue(node.join(node2));
assertTrue(node.join(node1));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void testPerm5() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node2" "node0" "node1"
assertTrue(node2.join(node));
assertTrue(node2.join(node1));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void testPerm6() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
// "node2" "node1" "node0"
assertTrue(node2.join(node1));
assertTrue(node2.join(node));
this.testOrder(node, node2, 3);
assertEquals(node2.getNext(), node);
assertEquals(node2.getPrevious(), node1);
assertEquals(node1.getNext(), node2);
assertEquals(node1.getPrevious(), node);
assertEquals(node.getNext(), node1);
assertEquals(node.getPrevious(), node2);
}
public void test4Perm01() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node1" "node2" "node3"
assertTrue(node.join(node1));
assertTrue(node.join(node2));
assertTrue(node.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node1));
assertFalse(node.join(node2));
assertFalse(node.join(node3));
}
public void test4Perm02() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node0" "node2" "node3"
assertTrue(node1.join(node));
assertTrue(node1.join(node2));
assertTrue(node1.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node));
assertFalse(node1.join(node2));
assertFalse(node1.join(node3));
}
public void test4Perm03() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node2" "node0" "node3"
assertTrue(node1.join(node2));
assertTrue(node1.join(node));
assertTrue(node1.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node2));
assertFalse(node1.join(node));
assertFalse(node1.join(node3));
}
public void test4Perm04() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node2" "node3" "node0"
assertTrue(node1.join(node2));
assertTrue(node1.join(node3));
assertTrue(node1.join(node));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node2));
assertFalse(node1.join(node3));
assertFalse(node1.join(node));
}
public void test4Perm05() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node2" "node1" "node3"
assertTrue(node.join(node2));
assertTrue(node.join(node1));
assertTrue(node.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node2));
assertFalse(node.join(node1));
assertFalse(node.join(node3));
}
public void test4Perm06() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node0" "node1" "node3"
assertTrue(node2.join(node));
assertTrue(node2.join(node1));
assertTrue(node2.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node));
assertFalse(node2.join(node1));
assertFalse(node2.join(node3));
}
public void test4Perm07() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node1" "node0" "node3"
assertTrue(node2.join(node1));
assertTrue(node2.join(node));
assertTrue(node2.join(node3));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node1));
assertFalse(node2.join(node));
assertFalse(node2.join(node3));
}
public void test4Perm08() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node1" "node3" "node0"
assertTrue(node2.join(node1));
assertTrue(node2.join(node3));
assertTrue(node2.join(node));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node1));
assertFalse(node2.join(node3));
assertFalse(node2.join(node));
}
public void test4Perm09() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node2" "node3" "node1"
assertTrue(node.join(node2));
assertTrue(node.join(node3));
assertTrue(node.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node2));
assertFalse(node.join(node3));
assertFalse(node.join(node1));
}
public void test4Perm10() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node0" "node3" "node1"
assertTrue(node2.join(node));
assertTrue(node2.join(node3));
assertTrue(node2.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node));
assertFalse(node2.join(node3));
assertFalse(node2.join(node1));
}
public void test4Perm11() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node3" "node0" "node1"
assertTrue(node2.join(node3));
assertTrue(node2.join(node));
assertTrue(node2.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node3));
assertFalse(node2.join(node));
assertFalse(node2.join(node1));
}
public void test4Perm12() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node2" "node3" "node1" "node0"
assertTrue(node2.join(node3));
assertTrue(node2.join(node1));
assertTrue(node2.join(node));
this.testOrder(node, node3, 4);
assertFalse(node2.join(node2));
assertFalse(node2.join(node3));
assertFalse(node2.join(node1));
assertFalse(node2.join(node));
}
public void test4Perm13() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node1" "node3" "node2"
assertTrue(node.join(node1));
assertTrue(node.join(node3));
assertTrue(node.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node1));
assertFalse(node.join(node3));
assertFalse(node.join(node2));
}
public void test4Perm14() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node0" "node3" "node2"
assertTrue(node1.join(node));
assertTrue(node1.join(node3));
assertTrue(node1.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node));
assertFalse(node1.join(node3));
assertFalse(node1.join(node2));
}
public void test4Perm15() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node3" "node0" "node2"
assertTrue(node1.join(node3));
assertTrue(node1.join(node));
assertTrue(node1.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node3));
assertFalse(node1.join(node));
assertFalse(node1.join(node2));
}
public void test4Perm16() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node1" "node3" "node2" "node0"
assertTrue(node1.join(node3));
assertTrue(node1.join(node2));
assertTrue(node1.join(node));
this.testOrder(node, node3, 4);
assertFalse(node1.join(node1));
assertFalse(node1.join(node3));
assertFalse(node1.join(node2));
assertFalse(node1.join(node));
}
public void test4Perm17() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node3" "node1" "node2"
assertTrue(node.join(node3));
assertTrue(node.join(node1));
assertTrue(node.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node3));
assertFalse(node.join(node1));
assertFalse(node.join(node2));
}
public void test4Perm18() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node0" "node1" "node2"
assertTrue(node3.join(node));
assertTrue(node3.join(node1));
assertTrue(node3.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node));
assertFalse(node3.join(node1));
assertFalse(node3.join(node2));
}
public void test4Perm19() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node1" "node0" "node2"
assertTrue(node3.join(node1));
assertTrue(node3.join(node));
assertTrue(node3.join(node2));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node1));
assertFalse(node3.join(node));
assertFalse(node3.join(node2));
}
public void test4Perm20() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node1" "node2" "node0"
assertTrue(node3.join(node1));
assertTrue(node3.join(node2));
assertTrue(node3.join(node));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node1));
assertFalse(node3.join(node2));
assertFalse(node3.join(node));
}
public void test4Perm21() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node0" "node3" "node2" "node1"
assertTrue(node.join(node3));
assertTrue(node.join(node2));
assertTrue(node.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node.join(node));
assertFalse(node.join(node3));
assertFalse(node.join(node2));
assertFalse(node.join(node1));
}
public void test4Perm22() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node0" "node2" "node1"
assertTrue(node3.join(node));
assertTrue(node3.join(node2));
assertTrue(node3.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node));
assertFalse(node3.join(node2));
assertFalse(node3.join(node1));
}
public void test4Perm23() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node2" "node0" "node1"
assertTrue(node3.join(node2));
assertTrue(node3.join(node));
assertTrue(node3.join(node1));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node2));
assertFalse(node3.join(node));
assertFalse(node3.join(node1));
}
public void test4Perm24() throws RemoteException {
Node node = new Node("node0");
Node node1 = new Node("node1");
Node node2 = new Node("node2");
Node node3 = new Node("node3");
// "node3" "node2" "node1" "node0"
assertTrue(node3.join(node2));
assertTrue(node3.join(node1));
assertTrue(node3.join(node));
this.testOrder(node, node3, 4);
assertFalse(node3.join(node3));
assertFalse(node3.join(node2));
assertFalse(node3.join(node1));
assertFalse(node3.join(node));
}
public void testNumberOfNodesOnSingleNode() throws RemoteException {
Node node = new Node("node0");
assertEquals(node.numberOfNodes(), 1);
}
private void testOrder(Node min, Node max, int size) throws RemoteException {
NodeInterface n = min;
do {
assertTrue(n.getId() < n.getNext().getId());
assertEquals(min.getId(), n.getMinId());
assertEquals(max.getMaxId(), max.getId());
assertEquals(n.numberOfNodes(), size);
} while ((n = n.getNext()).getNext() != min);
n = max;
do {
assertTrue(n.getId() > n.getPrevious().getId());
} while ((n = n.getPrevious()) != min);
assertEquals(max.getNext(), min);
assertEquals(min.getPrevious(), max);
}
public void testA() throws RemoteException {
int i = 0;
String n = "n";
Node node1 = new Node(n+Integer.toString(i));
Node node2 = new Node(n+Integer.toString(i+2));
node1.join(node2);
for (i = 3; i < 50; i++) {
if (i % 2 == 0)
node1.join(new Node(n+Integer.toString(i)));
}
for (i = 1; i < 50; i++)
if (i % 2 == 1)
assertNull(node1.lookup(n+Integer.toString(i)));
P2PFingerTable ft = new P2PFingerTable(node1);
ft.createFingerTable();
Map<Integer, NodeInterface> abc = ft.getRemoteNodes();
Set<Integer> set = abc.keySet();
for (Integer integer : set) {
NodeInterface na = abc.get(integer);
System.out.println(integer + ": " + na.getId() + " - " + na.getName());
}
NodeInterface searchNode = node1.lookupChord("n2");
assertEquals(searchNode, node2);
}
}