1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.alert.agent;
21 import java.net.URL;
22 import HTTPClient.*;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.backsource.alert.PropertyConstants;
28 import org.backsource.alert.AlertError;
29 /***
30 * BaseClass for URL based testcases.
31 *
32 * <p>This class may be used in itself, or as a superclass for other
33 * agents that needs to test an URL. It is NOT based on the java.net.URL, becuse* of the need to be able to timeout.It uses two properties {@link org.backsource.alert.PropertyConstants#HTTP_TIMEOUT} and {@link org.backsource.alert.PropertyConstants#HTTP_URL}.</p>
34 * <p>It is also possible to give authentication information:</p>
35
36 * <ul>
37 <li>{@link org.backsource.alert.PropertyConstants#HTTP_REALM},the realm to authenticate against.</li>
38 <li>{@link org.backsource.alert.PropertyConstants#HTTP_USER}, user to login as.</li>
39 <li>{@link org.backsource.alert.PropertyConstants#HTTP_PWD}, the password.</li>
40 </ul>
41 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
42 * @version $Revision: 1.1.1.1 $
43 */
44
45 public class UrlCase extends BaseCase {
46 static final Log log = LogFactory.getLog( UrlCase.class);
47 final int DEFAULT_TIMEOUT = 3000;
48
49 int timeOut = DEFAULT_TIMEOUT;
50 URL url;
51 String realm;
52 String user;
53 String pwd;
54 public UrlCase (String name){
55 super(name);
56 }
57
58 protected void setUp() throws Exception{
59 super.setUp();
60 String t = prop.getProperty(PropertyConstants.HTTP_TIMEOUT);
61 if ( t != null) {
62 timeOut = Integer.parseInt( t );
63 }
64 String u = prop.getProperty(PropertyConstants.HTTP_URL);
65 if ( u != null) {
66 url = new URL(u);
67 }
68 user =prop.getProperty(PropertyConstants.HTTP_USER);
69 realm =prop.getProperty(PropertyConstants.HTTP_REALM);
70 pwd = prop.getProperty(PropertyConstants.HTTP_PWD);
71
72 }
73
74 /***
75 * Run the test agains the url given in property {@link org.backsource.alert.PropertyConstants#HTTP_URL}.
76 */
77 public void testUrl() throws Exception {
78 checkUrl(url,null);
79 }
80
81 /***
82 * Check the given url, ok if 200 is returned.
83 */
84 protected void checkUrl(URL url) {
85 checkUrl(url,null);
86 }
87
88 /***
89 * Fetch the given url and check if it contains the string given i find.
90 */
91 protected void checkUrl(URL url, String find) {
92 if ( url == null) {
93 throw new AlertError("URL may not be null");
94 }
95
96 try {
97
98 HTTPConnection connection = new HTTPConnection(url);
99 connection.setTimeout(timeOut);
100 connection.setAllowUserInteraction(false);
101 if ( realm!=null && user !=null && pwd != null) {
102 connection.addBasicAuthorization(realm, user, pwd);
103 }
104
105
106 String file = url.getPath() != null ? url.getPath() : "";
107 file += url.getQuery() != null ? "?"+url.getQuery():"";
108 HTTPResponse response = connection.Get(file);
109 if (response.getStatusCode() == 200) {
110 if ( find != null) {
111 String data = new String( response.getData() );
112 if ( data.indexOf(find) == -1) {
113 throw new AlertError("Response did not contain: " + find+": " +data);
114 }
115
116 }
117
118 }else {
119 throw new AlertError("Error in retriving page: "+ url.toString() + response.getReasonLine() + " code: " + response.getStatusCode());
120 }
121
122 } catch (Exception e) {
123 String m = "Error in checking page " +url + ": " + e;
124 log.error(m,e);
125 throw new AlertError(m);
126 }
127
128 }
129
130 public static void main(String[] args){
131
132 }
133 }