001/*
002 * $Id: TaggedSocketChannelTest.java 4093 2012-08-12 10:51:06Z kredel $
003 */
004
005package edu.jas.util;
006
007import java.io.IOException;
008
009import junit.framework.Test;
010import junit.framework.TestCase;
011import junit.framework.TestSuite;
012
013
014import org.apache.log4j.BasicConfigurator;
015
016/**
017 * TaggedSocketChannel tests with JUnit.
018 * @author Heinz Kredel.
019 */
020
021public class TaggedSocketChannelTest extends TestCase {
022
023   public static void main (String[] args) {
024       BasicConfigurator.configure();
025       junit.textui.TestRunner.run( suite() );
026   }
027
028   public TaggedSocketChannelTest(String name) {
029          super(name);
030   }
031
032   public static Test suite() {
033     TestSuite suite= new TestSuite(TaggedSocketChannelTest.class);
034     return suite;
035   }
036
037   private ChannelFactory cf;
038   private SocketChannel sc1;
039   private SocketChannel sc2;
040   private TaggedSocketChannel tsc1;
041   private TaggedSocketChannel tsc2;
042   private String host;
043   private int port;
044
045   final Integer tag1 = Integer.valueOf(1);
046
047
048    protected void setUp() {
049        host = "localhost";
050        port = 4711;
051        cf = new ChannelFactory(port);       
052        cf.init();
053        try {
054            sc1 = cf.getChannel(host,port);
055            sc2 = cf.getChannel();
056            tsc1 = new TaggedSocketChannel(sc1);
057            tsc1.init();
058            tsc2 = new TaggedSocketChannel(sc2);
059            tsc2.init();
060        } catch(IOException e) {
061            fail("IOException"+e);
062        } catch (InterruptedException e) {
063            fail("InterruptedException"+e);
064        }
065    }
066
067    protected void tearDown() {
068        cf.terminate();
069        tsc1.close();
070        tsc2.close();
071        sc1.close();
072        sc2.close();
073        try {
074            Thread.sleep(1);
075        } catch(InterruptedException e) {
076            fail("InterruptedException"+e);
077        }
078    }
079
080   public void testTaggedSocketChannel0() {
081       // test setUp() and tearDown()
082   }
083
084
085   public void testTaggedSocketChannel1() {
086       Object o = new IllegalArgumentException("leer");
087       Integer err = Integer.valueOf(-1);
088       try {
089           tsc1.send(err,o);
090           fail("no Exception thrown");
091       } catch(IOException e) {
092           fail("Exception"+e);
093       } catch(IllegalArgumentException e) {
094           // ok
095       }
096       err = null;
097       try {
098           tsc1.send(err,o);
099           fail("no Exception thrown");
100       } catch(IOException e) {
101           fail("Exception"+e);
102       } catch(IllegalArgumentException e) {
103           // ok
104       }
105   }
106
107
108   public void testTaggedSocketChannel2() {
109       Object o = Integer.valueOf(0);
110       try {
111           tsc1.send(tag1,o);
112           assertEquals(o,tsc2.receive(tag1));
113       } catch(IOException e) {
114           fail("Exception"+e);
115       } catch(InterruptedException e) {
116           fail("Exception"+e);
117       } catch(ClassNotFoundException e) {
118           fail("Exception"+e);
119       }
120   }
121
122
123   public void testTaggedSocketChannel3() {
124       Object o = Integer.valueOf(0);
125       try {
126           tsc1.send(tag1,o);
127           tsc2.send(tag1,o);
128           assertEquals(o,tsc1.receive(tag1));
129           assertEquals(o,tsc2.receive(tag1));
130       } catch(IOException e) {
131           fail("Exception"+e);
132       } catch(InterruptedException e) {
133           fail("Exception"+e);
134       } catch(ClassNotFoundException e) {
135           fail("Exception"+e);
136       }
137   }
138
139
140   public void testTaggedSocketChannel4() {
141       int n = 10;
142       Object o;
143       try {
144           for (int i = 0; i < n; i++) {
145               o = Integer.valueOf(i);
146               tsc1.send(tag1,o);
147           }
148           assertEquals("#tags == 0 ", 0, tsc1.tagSize());
149           for (int i = 0; i < n; i++) {
150               o = Integer.valueOf(i);
151               assertEquals(o,tsc2.receive(tag1));
152           }
153           assertTrue("#tags == 1 ", tsc2.tagSize() == 1);
154           assertTrue("#messages == 0 ", tsc1.messages() == 0);
155           assertTrue("#messages == 0 ", tsc2.messages() == 0);
156       } catch(IOException e) {
157           fail("Exception"+e);
158       } catch(InterruptedException e) {
159           fail("Exception"+e);
160       } catch(ClassNotFoundException e) {
161           fail("Exception"+e);
162       }
163   }
164
165
166   public void testTaggedSocketChannel5() {
167       int n = 10;
168       String msg = "Hello_";
169       Integer o;
170       try {
171           for (int i = 0; i < n; i++) {
172               o = Integer.valueOf(i);
173               tsc1.send(o,msg+i);
174           }
175           assertTrue("#tags == 0 ", tsc1.tagSize() == 0);
176           assertTrue("#messages == 0 ", tsc1.messages() == 0);
177           assertTrue("#messages >= 0 ", tsc2.messages() >= 0); // not all 10 arrive in time
178           for (int i = 0; i < n; i++) {
179               o = Integer.valueOf(i);
180               assertEquals(msg+i,tsc2.receive(o));
181           }
182           //System.out.println("tsc2 = " + tsc2);
183           assertTrue("#tags == 10 ", tsc2.tagSize() == 10);
184           assertTrue("#messages == 0 ", tsc2.messages() <= 1);
185       } catch(IOException e) {
186           fail("Exception"+e);
187       } catch(InterruptedException e) {
188           fail("Exception"+e);
189       } catch(ClassNotFoundException e) {
190           fail("Exception"+e);
191       }
192   }
193
194}
195