1 /* 2 * Created on 24/Jan/2004 3 * 4 * To change the template for this generated file go to 5 * Window>Preferences>Java>Code Generation>Code and Comments 6 */ 7 package replica.textui; 8 9 import java.io.BufferedReader; 10 import java.io.IOException; 11 import java.io.InputStreamReader; 12 import java.net.ServerSocket; 13 import java.net.Socket; 14 15 import junit.framework.TestCase; 16 17 /*** 18 * @author Pedro 19 * 20 * To change the template for this generated type comment go to 21 * Window>Preferences>Java>Code Generation>Code and Comments 22 */ 23 public class SimpleInterfaceTest extends TestCase { 24 25 int port = 8078; 26 Socket socket; 27 ServerSocket server; 28 29 /*** 30 * Constructor for SimpleTextInterfaceTest. 31 * @param arg0 32 */ 33 public SimpleInterfaceTest(String arg0) { 34 super(arg0); 35 } 36 37 public void testProcessLineConnect() { 38 39 final String connectArgs = "localhost:1234 user/password@database"; 40 41 SimpleInterface si = new SimpleInterface(){ 42 String connect(String args){ 43 assertEquals(connectArgs, args.trim()); 44 return ""; 45 } 46 }; 47 48 si.processLine("connect " + connectArgs); 49 } 50 51 public void testProcessLineSendToServer() { 52 53 final String msg = "select * from table;"; 54 55 SimpleInterface si = new SimpleInterface(){ 56 String sendToServer(String args){ 57 assertEquals(msg, args.trim()); 58 return ""; 59 } 60 }; 61 62 si.processLine(msg); 63 } 64 65 /* 66 * Test for Object[] getConnectArgs(String) 67 */ 68 public void testGetConnectArgsAllValues() { 69 SimpleInterface si = new SimpleInterface(); 70 71 Object[] args = si.getConnectArgs("localhost:1123 user@database"); 72 73 assertEquals(3, args.length); 74 assertEquals("localhost", args[0].toString()); 75 assertEquals(1123, ((Integer)args[1]).intValue()); 76 assertEquals("user@database", args[2].toString()); 77 } 78 79 /* 80 * Test for Object[] getConnectArgs(String) 81 */ 82 public void testGetConnectArgsHostUser() { 83 SimpleInterface si = new SimpleInterface(); 84 si.setPort(1123); 85 86 Object[] args = si.getConnectArgs("localhost user@database"); 87 88 assertEquals(3, args.length); 89 assertEquals("localhost", args[0].toString()); 90 assertEquals(1123, ((Integer)args[1]).intValue()); 91 assertEquals("user@database", args[2].toString()); 92 } 93 94 /* 95 * Test for Object[] getConnectArgs(String) 96 */ 97 public void testGetConnectArgsUser() { 98 SimpleInterface si = new SimpleInterface(); 99 si.setPort(1123); 100 si.setAddress("localhost"); 101 102 Object[] args = si.getConnectArgs("user@database"); 103 104 assertEquals(3, args.length); 105 assertEquals("localhost", args[0].toString()); 106 assertEquals(1123, ((Integer)args[1]).intValue()); 107 assertEquals("user@database", args[2].toString()); 108 } 109 110 /* 111 * Test for Object[] getConnectArgs(String) 112 */ 113 public void testGetConnectArgsInvalidPort() { 114 SimpleInterface si = new SimpleInterface(); 115 116 try{ 117 Object[] args = si.getConnectArgs("localhost:umdoistresquatro user@database"); 118 } 119 catch(IllegalArgumentException e){ 120 return; 121 } 122 123 fail(); 124 } 125 126 /* 127 * Test for Object[] getConnectArgs(String) 128 */ 129 public void testGetConnectArgsInvalidArgs() { 130 SimpleInterface si = new SimpleInterface(); 131 132 try{ 133 Object[] args = si.getConnectArgs(""); 134 } 135 catch(IllegalArgumentException e){ 136 return; 137 } 138 139 fail(); 140 } 141 142 /* 143 * Test for void connect(String, int, String) 144 */ 145 public void testConnectStringintString() { 146 147 // SimpleInterface si = new SimpleInterface(); 148 // 149 // try{ 150 // si.connect("localhost", port, "connect user@database"); 151 // si.sendToServer("\n"); 152 // 153 // assertTrue( si.isConnected() ); 154 // 155 // assertEquals("connect user@database;", readLineOnServer()); 156 // } 157 // catch(IOException e){ 158 // e.printStackTrace(); 159 // fail(); 160 // } 161 } 162 163 public void testSendToServer() { 164 165 // SimpleInterface si = new SimpleInterface(); 166 // 167 // try{ 168 // si.connect("localhost", port, "connect user@database"); 169 // 170 // String msg = "message to server;\n"; 171 // 172 // si.sendToServer(msg); 173 // 174 // assertEquals(msg, readLineOnServer()); 175 // } 176 // catch(IOException e){ 177 // e.printStackTrace(); 178 // fail(); 179 // } 180 } 181 182 String readLineOnServer(){ 183 184 try{ 185 BufferedReader reader = new BufferedReader( new InputStreamReader( 186 socket.getInputStream() ) ); 187 188 return reader.readLine(); 189 } 190 catch(IOException e){ 191 fail(e.getMessage()); 192 return null; 193 } 194 } 195 196 /* (non-Javadoc) 197 * @see junit.framework.TestCase#setUp() 198 */ 199 protected void setUp() throws Exception { 200 super.setUp(); 201 202 // Thread t = new Thread( new Runnable(){ 203 // public void run(){ 204 // try{ 205 // while(true){ 206 // server = new ServerSocket(port); 207 // socket = server.accept(); 208 // System.out.println("Server ready on port " + port); 209 // this.notifyAll(); 210 // } 211 // } 212 // catch(Exception e){ 213 // throw new RuntimeException(e); 214 // } 215 // finally{ 216 // try{socket.close();}catch(Throwable t){} 217 // try{server.close();}catch(Throwable t){} 218 // } 219 // } 220 // }); 221 // t.start(); 222 // 223 // t.wait(); 224 225 } 226 227 /* (non-Javadoc) 228 * @see junit.framework.TestCase#tearDown() 229 */ 230 protected void tearDown() throws Exception { 231 try{socket.close();}catch(Throwable t){} 232 try{server.close();}catch(Throwable t){} 233 } 234 235 }

This page was automatically generated by Maven