1 /* 2 * Created on 16/Jan/2004 3 */ 4 package replica.server; 5 6 import java.io.ByteArrayInputStream; 7 import java.io.IOException; 8 9 import replica.command.CommandProcessor; 10 import replica.core.ApplicationMessage; 11 12 import junit.framework.TestCase; 13 14 /*** 15 * @author Pedro Costa 16 */ 17 public class ServerProcessTest extends TestCase { 18 19 /*** 20 * Constructor for ServerProcessTest. 21 * @param name 22 */ 23 public ServerProcessTest(String name) { 24 super(name); 25 } 26 27 public void testEndOfCommands(){ 28 ServerProcess s = new ServerProcess( null ); 29 s.sessionId = "sessionId"; 30 31 s.setCommandProcessor(new CommandProcessor(){ 32 public Object process(Object c){ 33 assertEquals("select * from table ;", ((ApplicationMessage)c).getBody()); 34 return null; 35 }; 36 } ); 37 38 ByteArrayInputStream a = new ByteArrayInputStream( ("select * from table;" + "some trash").getBytes() ); 39 40 try{ 41 s.processStream( a ); 42 } 43 catch(IOException e){ 44 } 45 } 46 47 public void testSendingSeveralCommands(){ 48 ServerProcess s = new ServerProcess(null); 49 s.sessionId ="sessionId"; 50 51 final String[] commands = {"select * from table1", 52 "select * from table2", 53 "select * from table3"}; 54 s.setCommandProcessor(new CommandProcessor(){ 55 int i = 0; 56 public Object process(Object command) { 57 assertEquals(commands[i++]+" ;", ((ApplicationMessage)command).getBody().toString()); 58 return null; 59 } 60 } ); 61 62 ByteArrayInputStream a = new ByteArrayInputStream( ( commands[0] + ";" + 63 commands[1] + ";" + 64 commands[2] + ";").getBytes() ); 65 66 try{ 67 s.processStream( a ); 68 } 69 catch(IOException e){ 70 } 71 } 72 73 public void testIsFromServer(){ 74 75 ServerProcess s = new ServerProcess( null ); 76 s.sessionId ="sessionId"; 77 78 final String command = "select * \r\n from \r table;"; 79 80 s.setCommandProcessor(new CommandProcessor(){ 81 public Object process(Object c){ 82 assertEquals( ApplicationMessage.class, c.getClass()); 83 ApplicationMessage appMsg = (ApplicationMessage)c; 84 assertTrue(appMsg.isFromServer()); 85 return null; 86 } 87 } ); 88 89 ByteArrayInputStream a = new ByteArrayInputStream( command.getBytes() ); 90 91 try{ 92 s.processStream( a ); 93 } 94 catch(IOException e){ 95 } 96 } 97 98 public void testNoReturnsOrLineFeedsInCommands(){ 99 100 ServerProcess s = new ServerProcess( null ); 101 s.sessionId ="sessionId"; 102 103 final String command = "select * \r\n from \r table;"; 104 105 s.setCommandProcessor(new CommandProcessor(){ 106 public Object process(Object c){ 107 assertEquals( ApplicationMessage.class, c.getClass()); 108 ApplicationMessage appMsg = (ApplicationMessage)c; 109 String s = appMsg.getBody().toString(); 110 assertEquals(-1, s.indexOf('\r')); 111 assertEquals(-1, s.indexOf('\n')); 112 return null; 113 } 114 } ); 115 116 ByteArrayInputStream a = new ByteArrayInputStream( command.getBytes() ); 117 118 try{ 119 s.processStream( a ); 120 } 121 catch(IOException e){ 122 } 123 } 124 125 }

This page was automatically generated by Maven