1   /***
2    * Copyright (C) 2005 The Java Community
3    *
4    * This program is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU General Public License as published by the Free Software
6    * Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12   * details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place, Suite 330, Boston, MA 02111-1307 USA.
17   *
18   */
19  package org.bejug.javacareers.acceptance;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.bejug.javacareers.jobs.view.jsf.util.Utils;
31  import org.xml.sax.SAXException;
32  
33  import com.meterware.httpunit.GetMethodWebRequest;
34  import com.meterware.httpunit.WebConversation;
35  import com.meterware.httpunit.WebForm;
36  import com.meterware.httpunit.WebLink;
37  import com.meterware.httpunit.WebRequest;
38  import com.meterware.httpunit.WebResponse;
39  
40  /***
41   * Disabled from project.xml, see JAVACAREERS-208
42   *
43   * @author : Peter Symoens (Last modified by $Author: shally $)
44   * @version $Revision: 1.18 $ - $Date: 2005/12/21 11:38:42 $:
45   */
46  public class WelcomePageTests extends TestCase {
47  
48      /***
49       * The Logger
50       */
51       private static final Log LOG = LogFactory.getLog(WelcomePageTests.class);
52  
53  
54      /***
55       * @param args are the arguments of this main procedure
56       */
57      public static void main(String args[]) {
58          junit.textui.TestRunner.run(suite());
59      }
60  
61  
62      /***
63       * @return Test
64       */
65      public static Test suite() {
66          return new TestSuite(WelcomePageTests.class);
67      }
68  
69  
70      /***
71       * @param name to be used
72       */
73      public WelcomePageTests(String name) {
74          super(name);
75      }
76  
77  
78      /***
79       * Verifies that a welcome page has been defined
80       *
81       * @throws IOException  e
82       * @throws SAXException e
83       */
84      public void testWelcomePage() throws IOException, SAXException {
85          setResponse("/javacareers-webclient");
86          String pageTitle = response.getTitle();
87          assertEquals("Welcome page not found", pageTitle, Utils.getMessage("nav_javacareers"));
88      }
89  
90      /***
91       * Tests the login page and login link
92       *
93       * @throws IOException  e
94       * @throws SAXException e
95       */
96      public void testLoginPage() throws IOException, SAXException {
97          setResponse("/javacareers-webclient/");
98          URL homepageUrl = conversation.getCurrentPage().getURL();
99          WebLink loginLink = response.getLinkWithID("mainTemplate_headerView:menuBar_loginPanel_loginForm:menuBar_loginPanel_loginLink");
100         loginLink.click();
101         response = conversation.getCurrentPage();
102         URL loginUrl = conversation.getCurrentPage().getURL();
103         WebForm loginForm = response.getForms()[1];
104         WebRequest request = loginForm.getRequest();
105         //login with valid credentials
106         request.setParameter("j_username", "user");
107         request.setParameter("j_password", "user");
108         response = conversation.getResponse(request);
109         URL pageUrl = response.getURL();
110         assertTrue("Should have been redirected to the homepage after login", pageUrl.getPath().equals(homepageUrl.getPath()));
111         //login with invalid credentials
112         request.setParameter("j_username", "inexisting user");
113         request.setParameter("j_password", " inexisting pass");
114         response = conversation.getResponse(request);
115         URL newUrl = response.getURL();
116         assertTrue("Should have stayed on the login page: " + loginUrl.getPath().toString() + " but went to: " + newUrl.getPath().toString(), loginUrl.getPath().equals(newUrl.getPath()));
117     }
118 
119     /***
120      * Tests the signup link
121      *
122      * @throws IOException  e
123      * @throws SAXException e
124      */
125     public void testSigupLink() throws IOException, SAXException {
126         setResponse("/javacareers-webclient/");
127         WebLink signupLink = response.getLinkWithID("mainTemplate_headerView:menuBar_loginPanel_loginForm:menuBar_loginPanel_registerLink");
128         signupLink.click();
129         response = conversation.getCurrentPage();
130         String pageTitle = response.getTitle();
131         String expectedTitle = "Java Careers - ".concat(Utils.getMessage("title_edit_profile"));
132         assertTrue("Expected signup page here", pageTitle.equals(expectedTitle));
133 
134     }
135 
136     /***
137      * Tests all the menuLinks
138      *
139      * @throws IOException  e
140      * @throws SAXException e
141      */
142     public void testMenuLinks() throws IOException, SAXException {
143         setResponse("/javacareers-webclient");
144         WebLink[] welcomePageLinks = response.getLinks();
145 
146         for (int i = 0; i < welcomePageLinks.length; i++) {
147             WebLink menuLink = welcomePageLinks[i];
148             String linkId = menuLink.getID();
149             LOG.info("Debug: link = " + linkId);
150             String pageTitle = null;
151             if (linkId.startsWith("mainTemplate_menu2:navigation_form1:navigation_link")) {
152 
153                 menuLink.click();
154                 response = conversation.getCurrentPage();
155                 pageTitle = response.getTitle();
156                 //if the page is protected, login first
157                 if (pageTitle.equals("Java Careers - Login")) {
158                     WebForm loginForm = response.getForms()[0];
159                     WebRequest request = loginForm.getRequest();
160                     request.setParameter("j_username", "user");
161                     request.setParameter("j_password", "user");
162                     response = conversation.getResponse(request);
163                     pageTitle = response.getTitle();
164                 }
165 
166                 LOG.info("Debug: Pagetitle = " + pageTitle);
167                 assertTrue("All links in the menu should link to a Page with title starting with Java Careers",
168                         pageTitle.indexOf("Java Careers") != -1);
169             }
170             if (linkId.equals("mainTemplate_menu2:navigation_form1:navigation_group2_link1")) {
171                 menuLink.click();
172                 response = conversation.getCurrentPage();
173                 assertFalse("This should be a link to an XML page, not an html page", response.isHTML());
174             }
175         }
176     }
177 
178     /***
179      * Verify that when an unexisting url is entered, the JavaCareers 404 page is displayed
180      *
181      * @throws IOException  e
182      * @throws SAXException e
183      */
184     public void testFouroFourErrorPage() throws IOException, SAXException {
185 
186         //For some reason the 404 error handler doesn't work. JSF related?
187         /*setResponse("unexistingwebpage.htm");
188         String pageTitle = response.getTitle();
189         assertTrue("Page title should be error page title", pageTitle.equals(Utils.getMessage("title_not_found"))); */
190     }
191 
192 
193     /***
194      * Test all the links on the welcome page
195      *
196      * @throws IOException  e
197      * @throws SAXException e
198      */
199     public void testAllWelcomePageLinks() throws IOException, SAXException {
200         setResponse("/javacareers-webclient");
201         WebLink[] welcomePageLinks = response.getLinks();
202         for (int i = 0; i < welcomePageLinks.length; i++) {
203             WebLink welcomePageLink = welcomePageLinks[i];
204             String linkId = welcomePageLink.getID();
205             LOG.info("Debug: linkid = " + linkId);
206             //skip the javacareers.xml and the footer links which are tested separately
207             if (!linkId.equals("mainTemplate_menu2:navigation_form1:navigation_group2_link1")
208             && (linkId.indexOf("mainTemplate_FooterView:")== -1)) {
209                  welcomePageLink.click();
210                 String pageTitle = response.getTitle();
211                 LOG.info("Debug: pageTitle = " + pageTitle);
212                 assertTrue("Did not expect page " + pageTitle, pageTitle.indexOf("error") == -1);
213                 assertTrue("Did not expect page " + pageTitle, pageTitle.indexOf("Error") == -1);
214                 assertFalse("Did not expect page " + pageTitle, pageTitle.equals(Utils.getMessage("title_not_found")));
215             }
216         }
217     }
218 
219 
220     /***
221      * Sets the web response.
222      * @param url of the response
223      *
224      * @throws IOException  e
225      * @throws SAXException e
226      */
227     private void setResponse(String url) throws IOException, SAXException {
228         String baseUrl = "http://127.0.0.1:8080/";
229         conversation = new WebConversation();
230         WebRequest request = new GetMethodWebRequest(baseUrl.concat(url));
231         response = conversation.getResponse(request);
232 
233     }
234 
235     /***
236      * The WebResponse
237      */
238     private WebResponse response;
239 
240     /***
241      * The web conversation
242      */
243     private WebConversation conversation;
244 
245 }
246 
247 
248 /***
249  * $Log: WelcomePageTests.java,v $
250  * Revision 1.18  2005/12/21 11:38:42  shally
251  * *** empty log message ***
252  *
253  * Revision 1.17  2005/12/08 14:53:46  shally
254  * Opkuis voor checkstyle.
255  *
256  * Revision 1.16  2005/09/30 14:38:08  bavo_jcs
257  * Fixed URL
258  *
259  * Revision 1.15  2005/09/13 08:11:06  schauwvliege
260  * organize imports
261  *
262  * Revision 1.14  2005/08/30 14:55:18  psong09
263  * cleanup
264  *
265  * Revision 1.13  2005/08/30 13:07:48  psong09
266  * renamed author: psong09
267  *
268  * Revision 1.12  2005/08/19 12:56:52  schauwvliege
269  * Fixed tests
270  *
271  * Revision 1.11  2005/08/17 09:20:12  schauwvliege
272  * Fixed all unit tests
273  *
274  * Revision 1.10  2005/08/17 09:09:30  schauwvliege
275  * Fixed all unit tests
276  *
277  * Revision 1.9  2005/08/05 15:01:50  psong09
278  * replaced system.out with logging
279  *
280  * Revision 1.8  2005/08/05 13:54:07  psong09
281  * fixed allWelcomePageLinks test
282  *
283  * Revision 1.7  2005/08/05 13:29:11  psong09
284  * added signupLink test
285  *
286  * Revision 1.6  2005/08/05 13:07:04  psong09
287  * completed tests
288  *
289  * Revision 1.5  2005/08/04 14:52:42  ge0ffrey
290  * xml file should be tested seperattely (commit approved by Peter)
291  *
292  * Revision 1.4  2005/08/04 12:32:41  bme_jcs
293  * resolved checkstyle errors
294  *
295  * Revision 1.3  2005/07/20 13:34:45  ge0ffrey
296  * creation of JAVACAREERS-208 (not the resolution)
297  *
298  * Revision 1.2  2005/07/15 10:09:03  psong09
299  * added testAllWelcomePageLinks
300  *
301  * Revision 1.1  2005/07/14 15:02:58  psong09
302  * first release
303  *
304  */