001/* 002 * $Id: DistributedListTest.java 4096 2012-08-12 11:56:27Z kredel $ 003 */ 004 005//package edu.unima.ky.parallel; 006package edu.jas.util; 007 008import java.util.Iterator; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014import org.apache.log4j.BasicConfigurator; 015 016 017/** 018 * DistributedList tests with JUnit. 019 * @author Heinz Kredel 020 */ 021public class DistributedListTest extends TestCase { 022 023 /** 024 * main 025 */ 026 public static void main (String[] args) { 027 BasicConfigurator.configure(); 028 junit.textui.TestRunner.run( suite() ); 029 } 030 031 /** 032 * Constructs a <CODE>DistributedListTest</CODE> object. 033 * @param name String. 034 */ 035 public DistributedListTest(String name) { 036 super(name); 037 } 038 039 /** 040 */ 041 public static Test suite() { 042 TestSuite suite= new TestSuite(DistributedListTest.class); 043 return suite; 044 } 045 046 private static final String host = "localhost"; 047 048 private DistributedList l1; 049 private DistributedList l2; 050 private DistributedList l3; 051 052 private DistributedListServer dls; 053 054 int rl = 7; 055 int kl = 10; 056 int ll = 10; 057 int el = 5; 058 float q = 0.5f; 059 060 protected void setUp() { 061 dls = new DistributedListServer(); 062 dls.init(); 063 } 064 065 protected void tearDown() { 066 dls.terminate(); 067 dls = null; 068 if ( l1 != null ) l1.terminate(); 069 if ( l2 != null ) l2.terminate(); 070 if ( l3 != null ) l3.terminate(); 071 l1 = l2 = l3 = null; 072 } 073 074 075 /** 076 * Tests if the created DistributedList has #n objects as content. 077 */ 078 public void testDistributedList1() { 079 l1 = new DistributedList(host); 080 l1.init(); 081 assertTrue("l1==empty",l1.isEmpty()); 082 l1.add( Integer.valueOf(1) ); 083 assertFalse("l1!=empty",l1.isEmpty()); 084 assertTrue("#l1==1", l1.size() == 1 ); 085 l1.add( Integer.valueOf(2) ); 086 assertTrue("#l1==2", l1.size() == 2 ); 087 l1.add( Integer.valueOf(3) ); 088 assertTrue("#l1==3", l1.size() == 3 ); 089 090 Iterator it = null; 091 it = l1.iterator(); 092 int i = 0; 093 while ( it.hasNext() ) { 094 Object o = it.next(); 095 Integer x = Integer.valueOf( ++i ); 096 assertEquals("l1(i)==(i)", x, o ); 097 } 098 099 l1.clear(); 100 assertTrue("#l1==0", l1.size() == 0 ); 101 } 102 103 104 /** 105 * Tests if the two created DistributedLists have #n objects as content. 106 */ 107 public void testDistributedList2() { 108 l2 = new DistributedList(host); 109 l2.init(); 110 assertTrue("l2==empty",l2.isEmpty()); 111 l1 = new DistributedList(host); 112 l1.init(); 113 assertTrue("l1==empty",l1.isEmpty()); 114 115 int i = 0, loops = 10; 116 while ( i < loops ) { 117 Integer x = Integer.valueOf( ++i ); 118 l1.add( x ); 119 assertTrue("#l1==i", l1.size() == i ); 120 } 121 assertTrue("#l1==10", l1.size() == loops ); 122 123 while ( l2.size() < loops ) { 124 try { 125 System.out.print("2"); 126 Thread.sleep(100); 127 } catch (InterruptedException e) { 128 } 129 } 130 Iterator it = null; 131 it = l2.iterator(); 132 i = 0; 133 while ( it.hasNext() ) { 134 Object o = it.next(); 135 Integer x = Integer.valueOf( ++i ); 136 //System.out.println("o = " + o + " x = "+ x); 137 assertEquals("l2(i)==(i)", x, o ); 138 } 139 assertTrue("#l2==10", l2.size() == loops ); 140 } 141 142 143 /** 144 * Tests if the three created DistributedLists have #n objects as content. 145 */ 146 public void testDistributedList3() { 147 l1 = new DistributedList(host); 148 l1.init(); 149 assertTrue("l1==empty",l1.isEmpty()); 150 l2 = new DistributedList(host); 151 l2.init(); 152 assertTrue("l2==empty",l2.isEmpty()); 153 l3 = new DistributedList(host); 154 l3.init(); 155 assertTrue("l3==empty",l3.isEmpty()); 156 157 int i = 0, loops = 10; 158 while ( i < loops ) { 159 Integer x = Integer.valueOf( ++i ); 160 l3.add( x ); 161 assertTrue("#l3==i", l3.size() == i ); 162 } 163 assertTrue("#l3==10", l3.size() == loops ); 164 165 while ( l2.size() < loops || l1.size() < loops-1 ) { 166 try { 167 System.out.print("3"); 168 Thread.sleep(100); 169 } catch (InterruptedException e) { 170 } 171 } 172 assertTrue("#l2==10", l2.size() == loops ); 173 assertTrue("#l1==10", l1.size() == loops ); 174 Iterator it = null; 175 it = l2.iterator(); 176 Iterator it3 = null; 177 it3 = l1.iterator(); 178 i = 0; 179 /* sequence of elements is not correct at moment 180 */ 181 while ( it.hasNext() && it3.hasNext() ) { 182 Object o = it.next(); 183 Object p = it3.next(); 184 Integer x = Integer.valueOf( ++i ); 185 //System.out.println("o = " + o + " x = "+ x); 186 assertEquals("l2(i)==(i)", x, o ); 187 assertEquals("l1(i)==(i)", x, p ); 188 } 189 } 190 191 192/** 193 * Tests if the two created DistributedLists have #n objects as content 194 * when one is created later. 195 */ 196 public void testDistributedList6() { 197 l1 = new DistributedList(host); 198 l1.init(); 199 assertTrue("l1==empty",l1.isEmpty()); 200 201 int i = 0, loops = 10; 202 while ( i < loops ) { 203 Integer x = Integer.valueOf( ++i ); 204 l1.add( x ); 205 assertTrue("#l1==i", l1.size() == i ); 206 } 207 assertTrue("#l1==10", l1.size() == loops ); 208 209 l2 = new DistributedList(host); 210 l2.init(); 211 // assertTrue("l2==empty",l2.isEmpty()); 212 while ( l2.size() < loops ) { 213 try { 214 //System.out.print("2"); 215 Thread.sleep(100); 216 } catch (InterruptedException e) { 217 } 218 } 219 Iterator it = null; 220 it = l2.iterator(); 221 i = 0; 222 while ( it.hasNext() ) { 223 Object o = it.next(); 224 Integer x = Integer.valueOf( ++i ); 225 //System.out.println("o = " + o + " x = "+ x); 226 assertEquals("l2(i)==(i)", x, o ); 227 } 228 assertTrue("#l2==10", l2.size() == loops ); 229 } 230 231}