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

Last change on this file since 1390 was 1390, checked in by pharms, 10 years ago
  • added test cases for SOAP message exchange
File size: 6.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.catalina.Context;
25import org.apache.catalina.startup.Tomcat;
26import org.apache.http.HttpEntity;
27import org.apache.http.HttpResponse;
28import org.apache.http.client.methods.HttpGet;
29import org.apache.http.client.methods.HttpPost;
30import org.apache.http.client.methods.HttpRequestBase;
31import org.apache.http.entity.ContentType;
32import org.apache.http.entity.StringEntity;
33import org.apache.http.impl.client.DefaultHttpClient;
34import org.junit.After;
35import org.junit.Before;
36
37import de.ugoe.cs.util.console.TextConsole;
38
39/**
40 *
41 */
42public abstract class AbstractTC {
43   
44    /**
45     *
46     */
47    protected static final TextConsole CONSOLE = new TextConsole(Level.FINEST);
48   
49    /**
50     *
51     */
52    protected final static String LOG_FILE_DIR = "target/tmp/logfiles/";
53   
54    /**
55     *
56     */
57    protected static final int DUMMY_SERVER_PORT = 19098;
58   
59    /**
60     *
61     */
62    protected static final int PROXY_PORT = 19099;
63   
64    /**
65     *
66     */
67    protected static final int MONITOR_PORT = 19100;
68   
69    /**
70     * the servlet of the dummy web server responding to requests
71     */
72    private DummyServlet dummyServlet;
73   
74    /** */
75    private static Tomcat tomcat = new Tomcat();
76
77    /**
78     *
79     */
80    @Before
81    public void setUp() throws Exception {
82        // setup a simple HTTP server
83        dummyServlet = new DummyServlet();
84       
85        /*dummyServer = new Server(DUMMY_SERVER_PORT);
86       
87        ServletContextHandler root =
88            new ServletContextHandler(dummyServer, "/dummyServer", ServletContextHandler.SESSIONS);
89
90       
91        root.addServlet(new ServletHolder(dummyServlet), "/*");
92       
93        dummyServer.start();*/
94       
95        File tomcatDir = new File("target/test/tomcat");
96        File webappRootDir = new File(tomcatDir, "webapp");
97       
98        tomcat = new Tomcat();
99        tomcat.setPort(DUMMY_SERVER_PORT);
100        tomcat.setBaseDir(tomcatDir.getAbsolutePath());
101
102        File warFile = new File(webappRootDir, "dummyService.war");
103        tomcat.addWebapp("/dummyWebapp", warFile.getAbsolutePath());
104        System.out.println("configuring Dummy Service from " + warFile.getAbsolutePath());
105       
106        File servletRootDir = new File(tomcatDir, "servlet");
107        servletRootDir.mkdirs();
108        Context ctx = tomcat.addContext("/", servletRootDir.getAbsolutePath());
109        Tomcat.addServlet(ctx, "dummyServlet", dummyServlet);
110        ctx.addServletMapping("/dummyServlet", "dummyServlet");
111       
112        tomcat.start();
113       
114        setUpHook();
115    }
116
117    /**
118     *
119     */
120    protected abstract void setUpHook() throws Exception;
121
122    /**
123     *
124     */
125    @After
126    public void tearDown() throws Exception {
127        // give all started servers a second to finalize their internal work after the test
128        // execution. This prevents some unnecessary exceptions due to requests already processed
129        // by the test case but not finalized on server side.
130        Thread.sleep(1000);
131        tearDownHook();
132
133        if (tomcat != null) {
134            try {
135                tomcat.stop();
136                tomcat.getServer().await();
137                tomcat.destroy();
138            }
139            finally {
140                tomcat = null;
141            }
142        }
143       
144        deleteFiles(new File(LOG_FILE_DIR));
145    }
146
147    /**
148     *
149     */
150    protected abstract void tearDownHook() throws Exception;
151
152    /**
153     *
154     */
155    protected String sendDummyMessage(String type, String message, String respMsg)
156        throws Exception
157    {
158        dummyServlet.setResponse(respMsg);
159       
160        System.err.println("sending message: " + message);
161        DefaultHttpClient httpclient = new DefaultHttpClient();
162        HttpRequestBase httpRequest = null;
163       
164        if ("POST".equals(type)) {
165            httpRequest = new HttpPost("http://localhost:" + PROXY_PORT + "/dummyServlet");
166            HttpEntity entity = new StringEntity(message, ContentType.TEXT_PLAIN);
167            ((HttpPost) httpRequest).setEntity(entity);
168        }
169        else if ("GET".equals(type)) {
170            httpRequest = new HttpGet("http://localhost:" + PROXY_PORT + "/dummyServlet");
171        }
172       
173        try {
174            HttpResponse response = httpclient.execute(httpRequest);
175            System.err.println(response.getStatusLine());
176            String responseStr = readStreamContentToString(response.getEntity().getContent());
177            System.err.println("received response: " + responseStr);
178            return responseStr;
179        }
180        finally {
181            httpRequest.releaseConnection();
182        }
183    }
184   
185    /**
186     *
187     */
188    protected String readStreamContentToString(InputStream stream) throws IOException {
189        return readToString(new BufferedReader(new InputStreamReader(stream)));
190    }
191   
192    /**
193     *
194     */
195    protected String readToString(BufferedReader reader) throws IOException {
196        StringBuffer message = new StringBuffer();
197       
198        String line = reader.readLine();
199        while (line != null) {
200            System.err.println(line);
201            if (message.length() > 0) {
202                message.append('\n');
203            }
204            message.append(line);
205            line = reader.readLine();
206        }
207       
208        return message.toString();
209    }
210
211    /**
212     *
213     */
214    protected void deleteFiles(File file) {
215        if (file.exists()) {
216            if (file.isDirectory()) {
217                for (File child : file.listFiles()) {
218                    deleteFiles(child);
219                }
220            }
221           
222            try {
223                file.delete();
224            }
225            catch (Exception e) {
226                // ignore and delete as much as possible
227            }
228        }
229    }
230
231}
Note: See TracBrowser for help on using the repository browser.