KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > HttpUnitTest


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war;
22
23 import com.meterware.httpunit.*;
24
25 import junit.framework.*;
26 import java.net.*;
27
28 /**
29  * The test cases are for:
30  * - Connecting the Jasper Server Startup Page
31  * - checking the Jasper Server Home Page
32  * - checking the Jasper Server List Reports Page
33  * - checking the Jasper Server View Report Page
34  */

35 public class HttpUnitTest extends TestCase {
36
37     private static String JavaDoc homePageUrl = JasperServerConstants.BASE_URL+"/jasperserver/home.html";
38    
39     private static String JavaDoc homePgText = "The world's most popular open source reporting engine";
40     private static String JavaDoc intRepLink = "REPORTS";
41     private static String JavaDoc indRepLink = "AllAccounts";
42     private static String JavaDoc pageText1 = "Accounts";
43     private static String JavaDoc pageText2 = "Burnaby";
44     
45     private static WebResponse wResponse ;
46
47     /**
48      * Constructor
49      * @param s
50      */

51     public HttpUnitTest(String JavaDoc s) {
52         super(s);
53     }
54
55     /**
56      * the main method for calling all the test cases whichever is being added
57      * into the suite.
58      * @param args
59      */

60     public static void main(String JavaDoc args[]) {
61         try {
62             junit.textui.TestRunner.run(suite());
63         } catch (Exception JavaDoc _ex) {
64             _ex.printStackTrace();
65         }
66     }
67
68     public void setUp() throws Exception JavaDoc{
69         if (wResponse == null){
70             wResponse = commonLoginFunction(homePageUrl);
71         }
72     }
73
74     /**
75      * this method is for adding which all test case/s method/s need to be
76      * @return Test
77      */

78     public static Test suite() throws Exception JavaDoc {
79         TestSuite suite = new TestSuite();
80         TestCase test1 = new HttpUnitTest("testStartURL");//verifies against Login Page
81
TestCase test2 = new HttpUnitTest("testHomePage");//verifies against Home Page
82
TestCase test3 = new HttpUnitTest("testLstRepLink");//verifies against List Report Link
83

84         suite.addTest(test1);
85         suite.addTest(test2);
86         suite.addTest(test3);
87         
88         return suite;
89     }
90
91     /**
92      * This test case method is for Login Page of JS application
93      * This checks for the "url" and the "textfield" for UserName
94      * On a proper response test is success else failure
95      * @return - void
96      */

97     public void testStartURL() throws Exception JavaDoc {
98         WebResponse response = gettingURLResponse(homePageUrl);
99         assertNotNull(homePageUrl+" Response is NULL ", response);
100         assertNotNull(" Login Page Element is Null ", response.getElementsWithName(homePgText));
101     }
102
103     /**
104      * This test case method is for Home Page of JS application
105      * This checks for the "url" and the "Recent Saved Reports" link
106      * On a proper response - test is success else - failure
107      * @return - void
108      */

109     public void testHomePage() throws Exception JavaDoc {
110         assertNotNull(homePageUrl+"Response is NULL ", wResponse);
111         assertNotNull(" Home Page Element is Null ", wResponse.getElementsWithName("j_username"));
112     }
113
114     /**
115      * This test case is for checking for ListReports link in response
116      * to the previous link and checks if "Interactive" link exists
117      * If link found - test success, else - failure
118      * @return - void
119      */

120     public void testLstRepLink() throws Exception JavaDoc {
121         assertNotNull(" List Response is Null ", wResponse);
122         WebLink link = wResponse.getLinkWith(intRepLink);
123         assertNotNull(intRepLink +"Link is NULL ", link);
124         wResponse = link.click();
125     }
126
127     /**
128      * This test case is validating for View Report page.
129      * In response for the previous page link it checks for "AllAccounts" link,
130      * and on click, it goes to next page and checks if the text content and the link(as button) exists.
131      * Also checks if clicking on the button, it goes to next page.
132      * Checks for DIV element for Report Content.
133      * If these are found - test success, else - failure
134      * @return - void
135      */

136     public void testIndRepLink() throws Exception JavaDoc {
137         boolean matchFound = false;
138         boolean valueFound = false;
139     
140         assertNotNull(" Interactive Reports - Response is NULL ", wResponse);
141         
142         WebLink link1 = wResponse.getLinkWith(indRepLink);
143         assertNotNull(indRepLink+" Link is NULL ", link1);
144         wResponse = link1.click();
145         assertNotNull(indRepLink+" Response is NULL ", wResponse);
146         
147         String JavaDoc opt = wResponse.getText();
148         assertNotNull(" Response is NULL ", opt);
149         if(opt.indexOf(pageText1)!= -1 && opt.indexOf(pageText2) != -1 )
150             matchFound = true;
151         
152         assertEquals(matchFound, true);
153         
154         WebLink[] linkArray = null;
155         
156         WebTable table = wResponse.getTableWithID("pageNumber");
157         try{
158             linkArray = table.getTableCell(0,5).getLinks();
159             assertNotNull("Button Link is Null",linkArray);
160             
161             String JavaDoc pageString = table.getCellAsText(0,1);
162             String JavaDoc lastPageNum = pageString.substring(9).trim();
163             
164             pageString = linkArray[0].click().getTableWithID("pageNumber").getCellAsText(0,1);
165             if(pageString.indexOf(lastPageNum)!= pageString.lastIndexOf(lastPageNum))
166                 valueFound = true;
167             assertEquals(valueFound,true);
168         }catch (NullPointerException JavaDoc e) {
169             e.printStackTrace();
170         }
171     }
172
173     /**
174      * This function is to perform the Common Login functionality
175      * for each individual URL-s in the JS application
176      * If link found - test success
177      * else - failure
178      * @return - void
179      */

180     private WebResponse commonLoginFunction(String JavaDoc url) throws Exception JavaDoc {
181         WebResponse response = null;
182         WebConversation wcon = new WebConversation();
183         WebRequest wreq = new GetMethodWebRequest(new URL(url), "");
184         response = wcon.getResponse(wreq);
185         WebForm form = response.getForms()[0];
186         assertEquals("Form Action", "j_acegi_security_check", form.getAction());
187         wreq = form.getRequest();
188         wreq.setParameter("j_username", JasperServerConstants.USERNAME);
189         wreq.setParameter("j_password", JasperServerConstants.PASSWORD);
190         response = wcon.getResponse(wreq);
191         return response;
192     }
193
194     /**
195      * This method is for getting the response from the site to be tested
196      * @return WebResponse
197      */

198     private WebResponse gettingURLResponse(String JavaDoc url) throws Exception JavaDoc {
199         WebResponse response = null;
200         URL serverUrl = new URL(url);
201         WebConversation conversation = new WebConversation();
202         WebRequest request = new GetMethodWebRequest(serverUrl, "");
203         response = conversation.getResponse(request);
204         return response;
205     }
206 }
Popular Tags