View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: UrlCase.java,v 1.1.1.1 2004/05/19 12:33:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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        } // end of if ()
64        String u = prop.getProperty(PropertyConstants.HTTP_URL);
65        if ( u != null) {
66           url = new URL(u);
67        } // end of if ()
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        } // end of if ()
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          } // end of if ()
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                } // end of if ()
115             
116             } // end of if ()
117          
118          }else {
119             throw new AlertError("Error in retriving page: "+ url.toString() + response.getReasonLine() + " code: " + response.getStatusCode());
120          } // end of else
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       } // end of try-catch
127       
128    }
129    
130    public static void main(String[] args){
131       
132    }
133 } // UrlCase