001/*
002 * $Id$
003 */
004
005// from package edu.unima.ky.parallel;
006package edu.jas.util;
007
008import java.io.IOException;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013
014
015
016/**
017 * SocketChannel tests with JUnit.
018 * Refactored for java.util.concurrent.
019 * @author Akitoshi Yoshida
020 * @author Heinz Kredel
021 */
022
023public class SocketChannelTest extends TestCase {
024
025   public static void main (String[] args) {
026       junit.textui.TestRunner.run( suite() );
027   }
028
029   public SocketChannelTest(String name) {
030          super(name);
031   }
032
033   public static Test suite() {
034     TestSuite suite= new TestSuite(SocketChannelTest.class);
035     return suite;
036   }
037
038   private ChannelFactory cf;
039   private SocketChannel sc1;
040   private SocketChannel sc2;
041   private String host;
042   private int port;
043
044   protected void setUp() {
045       host = "localhost";
046       port = 4711;
047       cf = new ChannelFactory(port);       
048       cf.init();
049       try {
050        sc1 = cf.getChannel(host,port);
051        sc2 = cf.getChannel();
052       } catch(IOException e) {
053           fail("IOException"+e);
054       } catch (InterruptedException e) {
055        fail("InterruptedException"+e);
056       }
057   }
058
059   protected void tearDown() {
060       cf.terminate();
061       sc1.close();
062       sc2.close();
063       try {
064           Thread.sleep(1);
065       } catch(InterruptedException e) {
066           fail("InterruptedException"+e);
067       }
068   }
069
070   public void testSocketChannel0() {
071       // test setUp() and tearDown()
072   }
073
074   public void testSocketChannel1() {
075       Object o = Integer.valueOf(0);
076       try {
077           sc1.send(o);
078           assertEquals(o,sc2.receive());
079       } catch(IOException e) {
080           fail("Exception"+e);
081       } catch(ClassNotFoundException e) {
082           fail("Exception"+e);
083       }
084   }
085
086   public void testSocketChannel2() {
087       Object o = Integer.valueOf(0);
088       try {
089           sc1.send(o);
090           sc2.send(o);
091           assertEquals(o,sc1.receive());
092           assertEquals(o,sc2.receive());
093       } catch(IOException e) {
094           fail("Exception"+e);
095       } catch(ClassNotFoundException e) {
096           fail("Exception"+e);
097       }
098   }
099
100   public void testSocketChannel3() {
101       int n = 10;
102       Object o;
103       try {
104           for (int i = 0; i < n; i++) {
105               o = Integer.valueOf(i);
106               sc1.send(o);
107           }
108           for (int i = 0; i < n; i++) {
109               o = Integer.valueOf(i);
110               assertEquals(o,sc2.receive());
111           }
112       } catch(IOException e) {
113           fail("Exception"+e);
114       } catch(ClassNotFoundException e) {
115           fail("Exception"+e);
116       }
117   }
118
119}
120