1 19 20 package org.netbeans.test.gui.web.util; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.BufferedReader ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.BufferedWriter ; 27 import java.net.Socket ; 28 import java.net.ServerSocket ; 29 30 31 import org.netbeans.jemmy.Waitable; 32 import org.netbeans.core.NbTopManager; 33 34 public class JSPServletResponseWaitable implements Waitable { 35 private String id = null; 36 private String real = null; 37 private String userAgent = null; 38 private String answer = null; 39 private int port = -1; 40 private boolean started = false; 41 public JSPServletResponseWaitable(String id, String answer, int port) { 42 this.id = id; 43 this.answer = answer; 44 this.port = port; 45 46 (new Thread () { 47 public void run() { 48 try { 49 String answer = getDefaultAnswer(); 50 int port = getDefaultPort(); 51 ServerSocket ss = new ServerSocket (port); 52 System.out.println("Listen on " + port); 53 started = true; 54 Socket s = ss.accept(); 55 InputStream is = s.getInputStream(); 56 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 57 String line = br.readLine(); 58 while(!(line == null)) { 59 System.out.println("READ: \"" + line + "\""); 60 if(line.startsWith("ID")) { 61 setId(line); 62 } 63 if(line.startsWith("UserAgent")) { 64 setUserAgent(line); 65 } 66 line = br.readLine(); 67 if(line.equals("")) { 68 line = null; 69 } 70 } 71 OutputStream os = s.getOutputStream(); 72 BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (os)); 73 bw.write(answer,0,answer.length()); 74 System.out.println("CL is " + answer.length()); 75 bw.flush(); 76 s.close(); 77 ss.close(); 78 }catch(Exception e) { 79 real = "Exception at READ/WRITE http opeartions"; 80 System.out.println("ERROR!"); 81 e.printStackTrace(); 82 } 83 } 84 }).start(); 85 while(!started); 86 } 87 88 public Object actionProduced(Object o) { 89 if((real!=null)&&(real.indexOf(id)!=-1)) { 90 return Boolean.TRUE; 91 } 92 if((real!=null)&&(real.indexOf(id)==-1)) { 93 System.out.println("ID is " + real + " instead of " + id); 94 } 95 return null; 96 } 97 98 public String getDescription() { 99 return "Waiter for ID: " + id; 100 } 101 102 private int getDefaultPort() { 103 return port; 104 } 105 private String getDefaultAnswer() { 106 return answer; 107 } 108 private void setId(String id) { 109 real = id; 110 } 111 112 private void setUserAgent(String agent) { 113 userAgent = agent; 114 } 115 private String getUserAgent() { 116 return userAgent; 117 } 118 public String getId() { 119 return real; 120 } 121 } 122 123 | Popular Tags |