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