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