001    /*
002     * $Id: DistributedListTest.java 3443 2010-12-25 16:49:30Z kredel $
003     */
004    
005    //package edu.unima.ky.parallel;
006    package edu.jas.util;
007    
008    import java.util.Iterator;
009    
010    import junit.framework.Test;
011    import junit.framework.TestCase;
012    import junit.framework.TestSuite;
013    
014    import org.apache.log4j.BasicConfigurator;
015    
016    
017    /**
018     * DistributedList tests with JUnit.
019     * @author Heinz Kredel
020     */
021    public 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            assertTrue("l1==empty",l1.isEmpty());
081            l1.add( new Integer(1) );
082            assertFalse("l1!=empty",l1.isEmpty());
083            assertTrue("#l1==1", l1.size() == 1 );
084            l1.add( new Integer(2) );
085            assertTrue("#l1==2", l1.size() == 2 );
086            l1.add( new Integer(3) );
087            assertTrue("#l1==3", l1.size() == 3 );
088    
089            Iterator it = null;
090            it = l1.iterator();
091            int i = 0;
092            while ( it.hasNext() ) {
093                Object o = it.next();
094                Integer x = new Integer( ++i );
095                assertEquals("l1(i)==(i)", x, o );
096            }
097    
098            l1.clear();
099            assertTrue("#l1==0", l1.size() == 0 );
100        }
101    
102    
103        /**
104         * Tests if the two created DistributedLists have #n objects as content.
105         */
106        public void testDistributedList2() {
107            l2 = new DistributedList(host);
108            assertTrue("l2==empty",l2.isEmpty());
109            l1 = new DistributedList(host);
110            assertTrue("l1==empty",l1.isEmpty());
111    
112            int i = 0, loops = 10;
113            while ( i < loops ) {
114                Integer x = new Integer( ++i );
115                l1.add( x );
116                assertTrue("#l1==i", l1.size() == i );
117            }
118            assertTrue("#l1==10", l1.size() == loops );
119    
120            while ( l2.size() < loops ) {
121                try {
122                    System.out.print("2");
123                    Thread.sleep(100);
124                } catch (InterruptedException e) {
125                }
126            }
127            Iterator it = null;
128            it = l2.iterator();
129            i = 0;
130            while ( it.hasNext() ) {
131                Object o = it.next();
132                Integer x = new Integer( ++i );
133                //System.out.println("o = " + o + " x = "+ x);
134                assertEquals("l2(i)==(i)", x, o );
135            }
136            assertTrue("#l2==10", l2.size() == loops );
137        }
138    
139    
140        /**
141         * Tests if the three created DistributedLists have #n objects as content.
142         */
143        public void testDistributedList3() {
144            l1 = new DistributedList(host);
145            assertTrue("l1==empty",l1.isEmpty());
146            l2 = new DistributedList(host);
147            assertTrue("l2==empty",l2.isEmpty());
148            l3 = new DistributedList(host);
149            assertTrue("l3==empty",l3.isEmpty());
150    
151            int i = 0, loops = 10;
152            while ( i < loops ) {
153                Integer x = new Integer( ++i );
154                l3.add( x );
155                assertTrue("#l3==i", l3.size() == i );
156            }
157            assertTrue("#l3==10", l3.size() == loops );
158    
159            while ( l2.size() < loops || l1.size() < loops-1 ) {
160                try {
161                    System.out.print("3");
162                    Thread.sleep(100);
163                } catch (InterruptedException e) {
164                }
165            }
166            assertTrue("#l2==10", l2.size() == loops );
167            assertTrue("#l1==10", l1.size() == loops );
168            Iterator it = null;
169            it = l2.iterator();
170            Iterator it3 = null;
171            it3 = l1.iterator();
172            i = 0;
173            /* sequence of elements is not correct at moment
174             */
175            while ( it.hasNext() && it3.hasNext() ) {
176                Object o = it.next();
177                Object p = it3.next();
178                Integer x = new Integer( ++i );
179                //System.out.println("o = " + o + " x = "+ x);
180                assertEquals("l2(i)==(i)", x, o );
181                assertEquals("l1(i)==(i)", x, p );
182            }
183        }
184    
185    
186    /**
187     * Tests if the two created DistributedLists have #n objects as content 
188     * when one is created later.
189     */
190        public void testDistributedList6() {
191            l1 = new DistributedList(host);
192            assertTrue("l1==empty",l1.isEmpty());
193    
194            int i = 0, loops = 10;
195            while ( i < loops ) {
196                Integer x = new Integer( ++i );
197                l1.add( x );
198                assertTrue("#l1==i", l1.size() == i );
199            }
200            assertTrue("#l1==10", l1.size() == loops );
201    
202            l2 = new DistributedList(host);
203            // assertTrue("l2==empty",l2.isEmpty());
204            while ( l2.size() < loops ) {
205                try {
206                    //System.out.print("2");
207                    Thread.sleep(100);
208                } catch (InterruptedException e) {
209                }
210            }
211            Iterator it = null;
212            it = l2.iterator();
213            i = 0;
214            while ( it.hasNext() ) {
215                Object o = it.next();
216                Integer x = new Integer( ++i );
217                //System.out.println("o = " + o + " x = "+ x);
218                assertEquals("l2(i)==(i)", x, o );
219            }
220            assertTrue("#l2==10", l2.size() == loops );
221        }
222    
223    }