001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.widget.support;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.common.io.IOTools;
026: import org.jboss.portal.common.junit.ExtendedAssert;
027:
028: import java.io.BufferedInputStream;
029: import java.io.BufferedOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.LineNumberReader;
034: import java.io.OutputStreamWriter;
035: import java.net.ServerSocket;
036: import java.net.Socket;
037: import java.util.concurrent.atomic.AtomicInteger;
038:
039: /**
040: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
041: * @version $Revision: 1.1 $
042: */
043: public abstract class AbstractSynchronizedServer extends AbstractServer {
044:
045: /** . */
046: private static final Logger log = Logger
047: .getLogger(AbstractSynchronizedServer.class);
048:
049: /** . */
050: private final Object lock = new Object();
051:
052: /** . */
053: private final AtomicInteger b = new AtomicInteger(0);
054:
055: /** . */
056: private Throwable failure;
057:
058: protected AbstractSynchronizedServer(int port) {
059: super (port);
060: }
061:
062: protected void run(ServerSocket server) throws Exception {
063: synchronized (lock) {
064: b.set(1);
065: lock.notifyAll();
066: }
067:
068: //
069: log.debug("Ready for accept");
070:
071: //
072: try {
073: doServer(server);
074: } catch (Throwable throwable) {
075: failure = throwable;
076: }
077:
078: //
079: synchronized (lock) {
080: lock.wait();
081: }
082:
083: //
084: log.debug("Shutting down");
085: }
086:
087: protected abstract void doServer(ServerSocket server)
088: throws Exception;
089:
090: protected abstract void doClient() throws Exception;
091:
092: public void performInteraction() throws Exception {
093: try {
094: start();
095:
096: // Wait until the we know the server will accept
097: synchronized (lock) {
098: while (b.get() != 1) {
099: lock.wait();
100: }
101: }
102:
103: // Perform client action
104: doClient();
105: } finally {
106: synchronized (lock) {
107: lock.notify();
108: }
109: stop();
110: }
111:
112: //
113: if (failure != null) {
114: log.error("The server reported a failure", failure);
115:
116: ExtendedAssert.fail("The server reported a failure");
117: }
118: }
119:
120: public static abstract class AbstractTimeoutServer extends
121: AbstractSynchronizedServer {
122:
123: protected AbstractTimeoutServer(int port) {
124: super (port);
125: }
126:
127: protected void doServer(ServerSocket server) throws Exception {
128: server.accept();
129: }
130:
131: }
132:
133: public static abstract class AbstractOKServer extends
134: AbstractSynchronizedServer {
135:
136: protected AbstractOKServer(int port) {
137: super (port);
138: }
139:
140: protected void doServer(ServerSocket server) throws Exception {
141: //
142: Socket s = server.accept();
143: BufferedInputStream in = new BufferedInputStream(s
144: .getInputStream());
145: LineNumberReader reader = new LineNumberReader(
146: new InputStreamReader(in, "ISO-8859-1"));
147: for (String line = reader.readLine(); line.length() > 0; line = reader
148: .readLine()) {
149: log.debug("server received = " + line);
150: }
151:
152: //
153: log.debug("Finished reading");
154:
155: //
156: BufferedOutputStream out = new BufferedOutputStream(s
157: .getOutputStream());
158: OutputStreamWriter writer = new OutputStreamWriter(out,
159: "UTF-8");
160: writer.write("HTTP/1.1 200 OK\r\n");
161: writer.write("\r\n");
162: writer.flush();
163: out.write("CAFEBABE".getBytes("UTF-8"));
164: out.close();
165: }
166: }
167:
168: public static abstract class AbstractReturnFileServer extends
169: AbstractSynchronizedServer {
170:
171: /** . */
172: private final String file;
173:
174: protected AbstractReturnFileServer(int port, String file) {
175: super (port);
176: this .file = file;
177: }
178:
179: protected void doServer(ServerSocket server) throws Exception {
180: //
181: Socket s = server.accept();
182: BufferedInputStream in = new BufferedInputStream(s
183: .getInputStream());
184: LineNumberReader reader = new LineNumberReader(
185: new InputStreamReader(in, "ISO-8859-1"));
186: for (String line = reader.readLine(); line.length() > 0; line = reader
187: .readLine()) {
188: log.debug("server received = " + line);
189: }
190:
191: //
192: log.debug("Finished reading");
193:
194: // get file content
195: byte[] bytes = getFileContent();
196:
197: //
198: BufferedOutputStream out = new BufferedOutputStream(s
199: .getOutputStream());
200: OutputStreamWriter writer = new OutputStreamWriter(out,
201: "UTF-8");
202: writer.write("HTTP/1.1 200 OK\r\n");
203: writer.write("\r\n");
204: writer.flush();
205: out.write(bytes);
206: out.close();
207: }
208:
209: private byte[] getFileContent()
210: throws IllegalArgumentException, IOException {
211: InputStream io = Thread.currentThread()
212: .getContextClassLoader().getResourceAsStream(
213: this .file);
214: byte[] bytes = IOTools.getBytes(io);
215: io.close();
216: return bytes;
217: }
218:
219: }
220:
221: public static abstract class Abstract404Server extends
222: AbstractSynchronizedServer {
223:
224: protected Abstract404Server(int port) {
225: super (port);
226: }
227:
228: protected void doServer(ServerSocket server) throws Exception {
229: //
230: Socket s = server.accept();
231: BufferedInputStream in = new BufferedInputStream(s
232: .getInputStream());
233: LineNumberReader reader = new LineNumberReader(
234: new InputStreamReader(in, "ISO-8859-1"));
235: for (String line = reader.readLine(); line.length() > 0; line = reader
236: .readLine()) {
237: log.debug("server received = " + line);
238: }
239:
240: //
241: log.debug("Finished reading");
242:
243: //
244: BufferedOutputStream out = new BufferedOutputStream(s
245: .getOutputStream());
246: OutputStreamWriter writer = new OutputStreamWriter(out,
247: "UTF-8");
248: writer.write("HTTP/1.1 404 Not Found\r\n");
249: writer.write("\r\n");
250: writer.flush();
251: out.close();
252: }
253: }
254:
255: }
|