001/* 002 * $Id: ExecutableServerTest.java 5867 2018-07-20 15:34:13Z 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 013import org.apache.logging.log4j.Logger; 014import org.apache.logging.log4j.LogManager; 015 016//import edu.unima.ky.parallel.ChannelFactory; 017//import edu.unima.ky.parallel.SocketChannel; 018 019 020/** 021 * ExecutableServer tests with JUnit. 022 * @author Heinz Kredel 023 */ 024 025public class ExecutableServerTest extends TestCase { 026 027/** 028 * main. 029 */ 030 public static void main (String[] args) { 031 junit.textui.TestRunner.run( suite() ); 032 } 033 034/** 035 * Constructs a <CODE>ExecutableServerTest</CODE> object. 036 * @param name String. 037 */ 038 public ExecutableServerTest(String name) { 039 super(name); 040 } 041 042/** 043 * suite. 044 */ 045 public static Test suite() { 046 TestSuite suite= new TestSuite(ExecutableServerTest.class); 047 return suite; 048 } 049 050 private static final String host = "localhost"; 051 private static final int port = ChannelFactory.DEFAULT_PORT; 052 053 private ExecutableServer es; 054 private ChannelFactory cf; 055 056 protected void setUp() { 057 es = new ExecutableServer(port); 058 es.init(); 059 cf = new ChannelFactory(); 060 } 061 062 protected void tearDown() { 063 es.terminate(); 064 es = null; 065 cf.terminate(); 066 cf = null; 067 } 068 069 070/** 071 * Tests if the ExecutableServer could be started and terminated. 072 */ 073 public void testExecutableServer1() { 074 assertTrue("should never fail", true ); 075 } 076 077 078/** 079 * Tests if the ExecutableServer can execute a RemoteExecutable. 080 */ 081 public void testExecutableServer2() { 082 RemoteExecutable e1 = new Executable("2"); 083 try { 084 SocketChannel sc = cf.getChannel(host,port); 085 sc.send( e1 ); 086 Object o = sc.receive(); 087 assertTrue("o:String", o instanceof String); 088 assertEquals("o==done", (String)o, ExecutableServer.DONE ); 089 sc.close(); 090 } catch (IOException e) { 091 e.printStackTrace(); 092 fail("IOException"); 093 } catch (ClassNotFoundException e) { 094 e.printStackTrace(); 095 fail("ClassNotFoundException"); 096 } 097 } 098 099 100/** 101 * Tests if the ExecutableServer can execute more RemoteExecutable. 102 */ 103 public void testExecutableServer3() { 104 RemoteExecutable e1 = new Executable("3"); 105 int numloops = 1; // can be changed in ExecutableServer.run() 106 Object o; 107 try { 108 SocketChannel sc = cf.getChannel(host,port); 109 for (int i = 0; i < numloops; i++ ) { 110 sc.send( e1 ); 111 o = sc.receive(); 112 assertTrue("o:String", o instanceof String); 113 assertEquals("o==done", (String)o, ExecutableServer.DONE ); 114 } 115 sc.close(); 116 } catch (IOException e) { 117 e.printStackTrace(); 118 fail("IOException"); 119 } catch (ClassNotFoundException e) { 120 e.printStackTrace(); 121 fail("ClassNotFoundException"); 122 } 123 } 124 125 126/** 127 * Tests if the ExecutableServer can execute a RemoteExecutable. 128 */ 129 public void testExecutableServer4() { 130 RemoteExecutable e1 = null; 131 SocketChannel sc = null; 132 Object o; 133 try { 134 for (int i = 0; i < 4; i++ ) { 135 e1 = new Executable("4-"+i); 136 sc = cf.getChannel(host,port); 137 138 sc.send( e1 ); 139 o = sc.receive(); 140 assertTrue("o:String", o instanceof String); 141 assertEquals("o==done", (String)o, ExecutableServer.DONE ); 142 143 e1 = null; 144 sc.close(); 145 sc = null; 146 } 147 } catch (IOException e) { 148 e.printStackTrace(); 149 fail("IOException"); 150 } catch (ClassNotFoundException e) { 151 e.printStackTrace(); 152 fail("ClassNotFoundException"); 153 } 154 } 155 156} 157 158 159/** 160 * Unit Test Class which implements interface RemoteExecutable. 161 */ 162 163class Executable implements RemoteExecutable { 164 165 private static final Logger logger = LogManager.getLogger(Executable.class); 166 167 private String param = null; 168 169 /** 170 * Executable. 171 * @param param String. 172 */ 173 public Executable(String param) { 174 this.param = param; 175 } 176 177 /** 178 * run. 179 */ 180 public void run() { 181 logger.debug(this + " has been run"); 182 } 183 184 /** 185 * toString. 186 */ 187 public String toString() { 188 return "Executable(" + param + ")"; 189 } 190 191}