source: trunk/autoquest-httpmonitor-test/src/test/java/de/ugoe/cs/autoquest/httpmonitor/AbstractTC.java @ 1376

Last change on this file since 1376 was 1376, checked in by pharms, 10 years ago

Initial import.

File size: 5.5 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.httpmonitor;
16
17import java.io.BufferedReader;
18import java.io.File;
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.InputStreamReader;
22import java.util.logging.Level;
23
24import org.apache.http.HttpEntity;
25import org.apache.http.HttpResponse;
26import org.apache.http.client.methods.HttpGet;
27import org.apache.http.client.methods.HttpPost;
28import org.apache.http.client.methods.HttpRequestBase;
29import org.apache.http.entity.ContentType;
30import org.apache.http.entity.StringEntity;
31import org.apache.http.impl.client.DefaultHttpClient;
32import org.eclipse.jetty.server.Server;
33import org.eclipse.jetty.servlet.ServletContextHandler;
34import org.eclipse.jetty.servlet.ServletHolder;
35import org.junit.After;
36import org.junit.Before;
37
38import de.ugoe.cs.util.console.TextConsole;
39
40/**
41 *
42 */
43public abstract class AbstractTC {
44   
45    /**
46     *
47     */
48    protected static final TextConsole CONSOLE = new TextConsole(Level.FINEST);
49   
50    /**
51     *
52     */
53    protected final static String LOG_FILE_DIR = "target/tmp/logfiles/";
54   
55    /**
56     *
57     */
58    protected static final int PORT = 19098;
59   
60    /**
61     * the jetty web server used for receiving messages
62     */
63    private Server dummyServer;
64   
65    /**
66     * the servlet of the dummy web server responding to requests
67     */
68    private DummyServlet dummyServlet;
69
70    /**
71     *
72     */
73    @Before
74    public void setUp() throws Exception {
75        // setup a simple HTTP server
76        dummyServer = new Server(PORT + 1);
77       
78        ServletContextHandler root =
79            new ServletContextHandler(dummyServer, "/", ServletContextHandler.SESSIONS);
80
81        dummyServlet = new DummyServlet();
82        root.addServlet(new ServletHolder(dummyServlet), "/*");
83       
84        dummyServer.start();
85       
86        setUpHook();
87    }
88
89    /**
90     *
91     */
92    protected abstract void setUpHook() throws Exception;
93
94    /**
95     *
96     */
97    @After
98    public void tearDown() throws Exception {
99        // give all started servers a second to finalize their internal work after the test
100        // execution. This prevents some unnecessary exceptions due to requests already processed
101        // by the test case but not finalized on server side.
102        Thread.sleep(1000);
103        tearDownHook();
104
105        if (dummyServer != null) {
106            try {
107                dummyServer.stop();
108            }
109            finally {
110                dummyServer = null;
111            }
112        }
113       
114        deleteFiles(new File(LOG_FILE_DIR));
115    }
116
117    /**
118     *
119     */
120    protected abstract void tearDownHook() throws Exception;
121
122    /**
123     *
124     */
125    protected String sendDummyMessage(String type, String message, String respMsg)
126        throws Exception
127    {
128        dummyServlet.setResponse(respMsg);
129       
130        System.err.println("sending message: " + message);
131        DefaultHttpClient httpclient = new DefaultHttpClient();
132        HttpRequestBase httpRequest = null;
133       
134        if ("POST".equals(type)) {
135            httpRequest = new HttpPost("http://localhost:" + PORT + "/");
136            HttpEntity entity = new StringEntity(message, ContentType.TEXT_PLAIN);
137            ((HttpPost) httpRequest).setEntity(entity);
138        }
139        else if ("GET".equals(type)) {
140            httpRequest = new HttpGet("http://localhost:" + PORT + "/");
141        }
142       
143        try {
144            HttpResponse response = httpclient.execute(httpRequest);
145            String responseStr = readStreamContentToString(response.getEntity().getContent());
146            System.err.println("received response: " + message);
147            return responseStr;
148        }
149        finally {
150            httpRequest.releaseConnection();
151        }
152    }
153   
154    /**
155     *
156     */
157    protected String readStreamContentToString(InputStream stream) throws IOException {
158        return readToString(new BufferedReader(new InputStreamReader(stream)));
159    }
160   
161    /**
162     *
163     */
164    protected String readToString(BufferedReader reader) throws IOException {
165        StringBuffer message = new StringBuffer();
166       
167        String line = reader.readLine();
168        while (line != null) {
169            System.err.println(line);
170            if (message.length() > 0) {
171                message.append('\n');
172            }
173            message.append(line);
174            line = reader.readLine();
175        }
176       
177        return message.toString();
178    }
179
180    /**
181     *
182     */
183    protected void deleteFiles(File file) {
184        if (file.exists()) {
185            if (file.isDirectory()) {
186                for (File child : file.listFiles()) {
187                    deleteFiles(child);
188                }
189            }
190           
191            try {
192                file.delete();
193            }
194            catch (Exception e) {
195                // ignore and delete as much as possible
196            }
197        }
198    }
199
200}
Note: See TracBrowser for help on using the repository browser.