KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > httpunit > HttpTestRunner


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.httpunit;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import com.maverick.http.GetMethod;
32 import com.maverick.http.HttpClient;
33 import com.maverick.http.HttpMethod;
34 import com.maverick.http.HttpResponse;
35 import com.maverick.http.PostMethod;
36
37 /**
38  */

39 final class HttpTestRunner {
40
41     private final HttpClient client;
42     private final HttpTestContainer container;
43     private final HttpTestEntry entry;
44
45     HttpTestRunner(HttpTestContainer container, HttpTestEntry entry) {
46         client = new HttpClient(container.getRootUrl(), container.getPort(), false);
47         this.container = container;
48         this.entry = entry;
49     }
50
51     void setUp() throws Exception JavaDoc {
52         if (entry.isAuthenticated()) {
53             authenticate(client, container, entry);
54         }
55     }
56
57     void tearDown() throws Exception JavaDoc {
58         logOff(client);
59     }
60
61     void runTest() throws Exception JavaDoc {
62         for (HttpTestEntryStep step : entry.getSteps()) {
63             runStep(client, step);
64         }
65     }
66
67     private static void authenticate(HttpClient client, HttpTestContainer container, HttpTestEntry entry) throws Exception JavaDoc {
68         String JavaDoc username = isEmpty(entry.getUsername()) ? container.getDefaultUsername() : entry.getUsername();
69         String JavaDoc password = isEmpty(entry.getPassword()) ? container.getDefaultPassword() : entry.getPassword();
70
71         GetMethod usernameLogon = new GetMethod("/usernameLogon.do");
72         usernameLogon.setParameter("username", username);
73         usernameLogon.setParameter("password", password);
74         HttpResponse usernameLogonResponse = client.execute(usernameLogon);
75         assertEquals("Authenticated Failed", 200, usernameLogonResponse.getStatus());
76
77         GetMethod logon = new GetMethod("/logon.do");
78         logon.setParameter("username", username);
79         logon.setParameter("password", password);
80         HttpResponse logonResponse = client.execute(logon);
81         assertEquals("Authenticated Failed", 200, logonResponse.getStatus());
82     }
83
84     private static void logOff(HttpClient client) throws Exception JavaDoc {
85         GetMethod getMethod = new GetMethod("/logoff.do");
86         HttpResponse httpResponse = client.execute(getMethod);
87         assertEquals("Log Off Failed", 302, httpResponse.getStatus());
88         assertRedirect(httpResponse, "showHome.do");
89     }
90
91     private static boolean isEmpty(String JavaDoc value) {
92         return value == null || value.length() == 0;
93     }
94
95     private static void runStep(HttpClient client, HttpTestEntryStep step) throws Exception JavaDoc {
96         String JavaDoc url = "/" + step.getUrl();
97         HttpMethod get = step.isPost() ? new PostMethod(url) : new GetMethod(url);
98         for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : step.getParameters().entrySet()) {
99             get.setParameter(entry.getKey(), entry.getValue());
100         }
101
102         HttpResponse response = client.execute(get);
103         int responseCode = response.getStatus();
104         assertEquals("Unexpected Status", step.getExpectedCode(), responseCode);
105         assertRedirect(response, step.getRedirectUrl());
106
107         Collection JavaDoc<String JavaDoc> messages = getActionMessages(response, "unitTestMessages");
108         assertEquals("The messages differ", step.getMessages(), messages);
109         Collection JavaDoc<String JavaDoc> errors = getActionMessages(response, "unitTestErrors");
110         assertEquals("The errors differ", step.getErrors(), errors);
111     }
112
113     private static void assertRedirect(HttpResponse response, String JavaDoc redirectUrl) {
114         String JavaDoc[] headerFields = response.getHeaderFields("location");
115         if (redirectUrl != null) {
116             if (headerFields == null || headerFields.length == 0) {
117                 fail("Redirect expected but not found");
118             } else {
119                 String JavaDoc strippedLocation = getStrippedLocation(headerFields[0]);
120                 if (!redirectUrl.equals(strippedLocation)) {
121                     fail("Redirect was incorrect : expected = '" + redirectUrl + "' actual = '" + strippedLocation + "'");
122                 }
123             }
124         }
125     }
126
127     private static String JavaDoc getStrippedLocation(String JavaDoc value) {
128         String JavaDoc strippedPrefix = value.substring(value.lastIndexOf("/") + 1);
129         String JavaDoc strippedPostfix = strippedPrefix.substring(0, strippedPrefix.indexOf(".") + 3);
130         return strippedPostfix;
131     }
132
133     private static Collection JavaDoc<String JavaDoc> getActionMessages(HttpResponse response, String JavaDoc headerName) {
134         String JavaDoc headerField = response.getHeaderField(headerName);
135         if (headerField == null) {
136             return Collections.<String JavaDoc> emptySet();
137         }
138
139         Collection JavaDoc<String JavaDoc> messages = new HashSet JavaDoc<String JavaDoc>();
140         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(headerField, ","); tokenizer.hasMoreTokens();) {
141             messages.add(tokenizer.nextToken());
142         }
143         return messages;
144     }
145 }
Popular Tags