KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandler > ServerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.uihandler;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.SocketTimeoutException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Queue JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36 import org.netbeans.junit.NbTestCase;
37 import org.openide.util.RequestProcessor;
38
39 /**
40  *
41  * @author Jaroslav Tulach
42  */

43 public class ServerTest extends NbTestCase {
44     public ServerTest(String JavaDoc s) {
45         super(s);
46     }
47     
48     
49     public static int startServer(final Queue JavaDoc<String JavaDoc> replies, final Queue JavaDoc<String JavaDoc> queries) throws IOException JavaDoc {
50         final ServerSocket JavaDoc ss = new ServerSocket JavaDoc(0);
51         
52         class Run implements Runnable JavaDoc {
53             private void doRun(String JavaDoc reply) throws IOException JavaDoc {
54                 Socket JavaDoc s = ss.accept();
55                 s.setSoTimeout(500);
56                 InputStream JavaDoc is = s.getInputStream();
57                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc ex) {
67                     // ok
68
}
69                 
70                 queries.add(sb.toString());
71                 
72                 OutputStream JavaDoc 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 JavaDoc reply = replies.poll();
83                     if (reply == null) {
84                         break;
85                     }
86                     try {
87                         doRun(reply);
88                     } catch (IOException JavaDoc 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 JavaDoc {
102         LinkedList JavaDoc<String JavaDoc> query = new LinkedList JavaDoc<String JavaDoc>();
103         query.add("<meta http-equiv=\"Refresh\" conteNT='URL=http://www.netbeans.org'>");
104         LinkedList JavaDoc<String JavaDoc> reply = new LinkedList JavaDoc<String JavaDoc>();
105         int port = startServer(query, reply);
106         
107         URL JavaDoc u = new URL JavaDoc("http://localhost:" + port);
108         
109         List JavaDoc<LogRecord JavaDoc> recs = new ArrayList JavaDoc<LogRecord JavaDoc>();
110         recs.add(new LogRecord JavaDoc(Level.WARNING, "MSG_MISTAKE"));
111         URL JavaDoc redir = Installer.uploadLogs(u, null, Collections.<String JavaDoc,String JavaDoc>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 JavaDoc("http://www.netbeans.org"), redir);
116     }
117
118
119     public void testRedirectsLogsWithTime() throws Exception JavaDoc {
120         LinkedList JavaDoc<String JavaDoc> query = new LinkedList JavaDoc<String JavaDoc>();
121         query.add("<meta http-equiv='Refresh' content='3; URL=http://logger.netbeans.org/welcome/use.html'>");
122         LinkedList JavaDoc<String JavaDoc> reply = new LinkedList JavaDoc<String JavaDoc>();
123         int port = startServer(query, reply);
124         
125         URL JavaDoc u = new URL JavaDoc("http://localhost:" + port);
126         
127         List JavaDoc<LogRecord JavaDoc> recs = new ArrayList JavaDoc<LogRecord JavaDoc>();
128         recs.add(new LogRecord JavaDoc(Level.WARNING, "MSG_MISTAKE"));
129         URL JavaDoc redir = Installer.uploadLogs(u, null, Collections.<String JavaDoc,String JavaDoc>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 JavaDoc("http://logger.netbeans.org/welcome/use.html"), redir);
134     }
135
136     
137
138
139
140 }
141
142
143
Popular Tags