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