1 19 20 package org.netbeans.modules.uihandler; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.ServerSocket ; 26 import java.net.Socket ; 27 import java.net.SocketTimeoutException ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Queue ; 34 import java.util.logging.Level ; 35 import java.util.logging.LogRecord ; 36 import org.netbeans.junit.NbTestCase; 37 import org.openide.util.RequestProcessor; 38 39 43 public class ServerTest extends NbTestCase { 44 public ServerTest(String s) { 45 super(s); 46 } 47 48 49 public static int startServer(final Queue <String > replies, final Queue <String > queries) throws IOException { 50 final ServerSocket ss = new ServerSocket (0); 51 52 class Run implements Runnable { 53 private void doRun(String reply) throws IOException { 54 Socket s = ss.accept(); 55 s.setSoTimeout(500); 56 InputStream is = s.getInputStream(); 57 StringBuffer sb = new StringBuffer (); 58 try { 59 for (;;) { 60 int ch = is.read(); 61 if (ch == -1) { 62 break; 63 } 64 sb.append((char)ch); 65 } 66 } catch (SocketTimeoutException ex) { 67 } 69 70 queries.add(sb.toString()); 71 72 OutputStream os = s.getOutputStream(); 73 os.write(reply.getBytes()); 74 os.close(); 75 76 is.close(); 77 s.close(); 78 } 79 80 public void run() { 81 for (;;) { 82 String reply = replies.poll(); 83 if (reply == null) { 84 break; 85 } 86 try { 87 doRun(reply); 88 } catch (IOException ex) { 89 ex.printStackTrace(); 90 } 91 } 92 } 93 } 94 Run r = new Run(); 95 RequestProcessor.getDefault().post(r); 96 97 98 return ss.getLocalPort(); 99 } 100 101 public void testRedirectsLogs() throws Exception { 102 LinkedList <String > query = new LinkedList <String >(); 103 query.add("<meta http-equiv=\"Refresh\" conteNT='URL=http://www.netbeans.org'>"); 104 LinkedList <String > reply = new LinkedList <String >(); 105 int port = startServer(query, reply); 106 107 URL u = new URL ("http://localhost:" + port); 108 109 List <LogRecord > recs = new ArrayList <LogRecord >(); 110 recs.add(new LogRecord (Level.WARNING, "MSG_MISTAKE")); 111 URL redir = Installer.uploadLogs(u, null, Collections.<String ,String >emptyMap(), recs); 112 113 assertTrue("one query has been sent: " + query, query.isEmpty()); 114 assertEquals("One reply received", 1, reply.size()); 115 assertEquals("Redirected to nb.org", new URL ("http://www.netbeans.org"), redir); 116 } 117 118 119 public void testRedirectsLogsWithTime() throws Exception { 120 LinkedList <String > query = new LinkedList <String >(); 121 query.add("<meta http-equiv='Refresh' content='3; URL=http://logger.netbeans.org/welcome/use.html'>"); 122 LinkedList <String > reply = new LinkedList <String >(); 123 int port = startServer(query, reply); 124 125 URL u = new URL ("http://localhost:" + port); 126 127 List <LogRecord > recs = new ArrayList <LogRecord >(); 128 recs.add(new LogRecord (Level.WARNING, "MSG_MISTAKE")); 129 URL redir = Installer.uploadLogs(u, null, Collections.<String ,String >emptyMap(), recs); 130 131 assertTrue("one query has been sent: " + query, query.isEmpty()); 132 assertEquals("One reply received", 1, reply.size()); 133 assertEquals("Redirected to nb.org", new URL ("http://logger.netbeans.org/welcome/use.html"), redir); 134 } 135 136 137 138 139 140 } 141 142 143 | Popular Tags |