View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library 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 GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.plugin.jwebunit;
20  
21  import net.sourceforge.jwebunit.WebTester;
22  import net.sf.jameleon.SessionTag;
23  
24  /***
25   * This is a JWebUnit implementation of the @see SessionTag. It basically, starts creates a new session for
26   * the function points to share.
27   * @jameleon.function name="http-session"
28   * @jameleon.step If beginSession is set to true, then start the application at the URL provided by baseUrl + beginAt
29   */
30  public class HttpSessionTag extends SessionTag {
31  
32      /***
33       * The session of the function points being tested. This gives full access to the JWebUnit API.
34       */
35      protected WebTester httpSession;
36  
37      /***
38       * Sets the ending path of the url to start at.
39       * @param beginAt - the ending path of the url to start at. For example,
40       *                  if the URL to start at is http://sf.net/projects/jameleon,
41       *                  then the baseUrl would be http://sf.net/ and the beginAt
42       *                  would be /projects/jameleon. This variable is optional
43       *                  This value is set and used from the context. In other words,
44       *                  this value can be set via an Applications.properties file or
45       *                  a CSV file.
46       * @jameleon.attribute required="false"
47       */
48      public void setBeginAt(String beginAt){
49          context.setVariable("beginAt", beginAt);
50      }
51  
52      /***
53       * Sets the base url to where the application begins.
54       * @param baseUrl - protocol, and hostname of the URL. For example,
55       *                  if the URL to start at is http://sf.net/projects/jameleon,
56       *                  then the baseUrl would be http://sf.net/ and the beginAt
57       *                  would be /projects/jameleon.
58       *                  This value is set and used from the context. In other words,
59       *                  this value can be set via an Applications.properties file or
60       *                  a CSV file.
61       * @jameleon.attribute required="false"
62       */
63      public void setBaseUrl(String baseUrl){
64          context.setVariable("baseUrl", baseUrl);
65      }
66  
67      /***
68       * @return the session which is shared among all HttpFuntionTags encapsulated inside this testcase
69       */
70      public WebTester getHttpSession(){
71          return httpSession;
72      }
73  
74      public void setUpSession(){
75          httpSession = new WebTester();
76      }
77  
78      public void tearDownSession(){
79          httpSession = null;
80          context.removeVariable("baseUrl");
81          context.removeVariable("beginAt");
82      }
83  
84      public void startApplication(){
85          if ( null != context.getVariable("baseUrl") ) {
86              String url = "";
87              try {
88                  String baseUrl = getBaseUrl();
89                  url = baseUrl;
90                  String beginAt = getBeginAt();
91                  url += beginAt;
92                  httpSession.getTestContext().setBaseUrl(baseUrl);
93                  httpSession.beginAt(beginAt);
94              } catch ( Exception e ) {
95                  throw new RuntimeException("Could not connect to "+ url, e);
96              }
97          } else {
98              throw new RuntimeException("'baseUrl' must be defined to begin the session from a http-session tag");
99          }
100     }
101 
102     protected String getBaseUrl(){
103         String baseUrl = (String)context.getVariable("baseUrl");
104         String beginAt = (String)context.getVariable("beginAt");
105         if ( null ==  beginAt || beginAt.length() == 0) {
106             int index = baseUrl.indexOf("/","http://".length());
107             if (index > -1) {
108                 baseUrl = baseUrl.substring(0, index);
109             }
110         }
111         return baseUrl;
112     }
113 
114     protected String getBeginAt(){
115         String baseUrl = (String)context.getVariable("baseUrl");
116         String beginAt = (String)context.getVariable("beginAt");
117         if ( beginAt == null || beginAt.length() == 0) {
118             String baseUrlFixed = getBaseUrl();
119             int length = baseUrlFixed.length();
120             if (length < baseUrl.length()) {
121                 beginAt = baseUrl.substring(baseUrlFixed.length());
122             }else{
123                 beginAt = "/";
124             }
125         }
126         return beginAt;
127     }
128 
129 }