1 /* 2 * Created on 15/Jan/2004 3 */ 4 package replica.server; 5 6 import java.io.IOException; 7 import java.net.InetAddress; 8 import java.net.Socket; 9 10 import org.springframework.context.support.ClassPathXmlApplicationContext; 11 import org.springframework.context.support.StaticApplicationContext; 12 13 import junit.framework.TestCase; 14 15 /*** 16 * @author Pedro Costa 17 */ 18 public class ListenerTest extends TestCase { 19 20 String configFileLocation = "replica/server/test-applicationContext.xml"; 21 22 /*** 23 * Constructor for ListenerTest. 24 * @param name 25 */ 26 public ListenerTest(String name) { 27 super(name); 28 } 29 30 Listener getNewListener(){ 31 32 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(configFileLocation); 33 34 StaticApplicationContext staticApp = (StaticApplicationContext)appContext.getBean("applicationContext"); 35 36 staticApp.setParent( appContext ); 37 38 staticApp.getBeanFactory().setParentBeanFactory( appContext.getBeanFactory() ); 39 40 return (Listener)staticApp.getBean("serverListener"); 41 } 42 43 public void testCreateNewServerProcess() { 44 45 Listener l = getNewListener(); 46 47 Thread lt = new Thread( l ); 48 49 try{ 50 lt.start(); 51 52 try{ Thread.sleep( 500 ); }catch(InterruptedException e){ } 53 54 Socket s = new Socket( InetAddress.getLocalHost(), l.getPort() ); 55 56 try{ Thread.sleep( 500 ); }catch(InterruptedException e){ } 57 58 assertEquals(1, l.getServerProcesses().size()); 59 60 Thread t = (Thread)l.getServerProcesses().get(0); 61 62 assertNotNull( t ); 63 64 assertEquals(true, t.isAlive() ); 65 66 try{ s.close(); }catch(IOException e){ } 67 68 try{ t.join(); }catch(InterruptedException e){} 69 70 try{ Thread.sleep( 500 ); }catch(InterruptedException e){ } 71 72 assertEquals(0, l.getServerProcesses().size()); 73 } 74 catch(IOException e){ 75 e.printStackTrace(); 76 } 77 finally{ 78 l.stop(); 79 try{ lt.join(); }catch(InterruptedException e){} 80 } 81 } 82 83 public void testInitializeServerProcessesList(){ 84 85 Listener l = new Listener(); 86 87 assertNotNull( l.getServerProcesses() ); 88 89 assertEquals(0, l.getServerProcesses().size()); 90 } 91 92 public void testGetNewServerProcess() { 93 94 Listener l = getNewListener(); 95 96 Socket s = new Socket(); 97 ServerProcess sp = l.getNewServerProcess(s); 98 99 assertNotNull(sp); 100 assertEquals(s, sp.getSocket()); 101 } 102 103 public void testStartListener(){ 104 105 Listener l = new Listener(); 106 Thread t = new Thread( l ); 107 t.start(); 108 109 try{ 110 Thread.sleep(500); 111 } 112 catch(InterruptedException e){ 113 } 114 115 Socket s = null; 116 try{ 117 s = new Socket(InetAddress.getByName("localhost"), l.getPort()); 118 119 assertEquals(true, s.isConnected()); 120 } 121 catch(IOException e){ 122 e.printStackTrace(); 123 } 124 finally{ 125 if( s != null ){ 126 try{ 127 s.close(); 128 } 129 catch(IOException e){ 130 } 131 } 132 } 133 134 synchronized(l.getServerProcesses()){ 135 if( l.getServerProcesses().size() > 0 ){ 136 try{ 137 ((Thread)l.getServerProcesses().get(0)).join(); 138 } 139 catch(InterruptedException e){ 140 } 141 } 142 } 143 144 l.stop(); 145 146 try{ t.join(1000); }catch(InterruptedException e){ } 147 148 } 149 150 public void testStopListener() { 151 Listener l = new Listener(); 152 Thread t = new Thread( l ); 153 t.start(); 154 155 assertEquals(true, t.isAlive() ); 156 157 try{ 158 Thread.sleep(500); 159 } 160 catch(InterruptedException e){ 161 } 162 163 l.stop(); 164 165 try{ 166 Thread.sleep(500); 167 } 168 catch(InterruptedException e){ 169 } 170 171 assertEquals(false, t.isAlive() ); 172 173 // Check that the server socket is closed when the listener ends 174 if( l.server != null ){ 175 assertEquals(true, l.server.isClosed() ); 176 } 177 } 178 }

This page was automatically generated by Maven