KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > test > JSFIntegrationUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.web.test;
23
24 //import java.net.HttpURLConnection;
25
import java.net.URL JavaDoc;
26 //import javax.management.ObjectName;
27

28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.jboss.test.JBossTestCase;
32 import org.jboss.test.JBossTestSetup;
33 import org.jboss.test.util.web.HttpUtils;
34 import org.apache.commons.httpclient.HttpClient;
35 import org.apache.commons.httpclient.HttpMethodBase;
36 import org.apache.commons.httpclient.methods.GetMethod;
37
38 /** Tests of JSF integration into the JBoss server. This test
39  requires than a web container and JSF implementation be integrated
40  into the JBoss server. The tests currently do NOT use the
41  java.net.HttpURLConnection and associated http client and these do
42  not return valid HTTP error codes so if a failure occurs it is best
43  to connect the webserver using a browser to look for additional error
44  info.
45  
46  @author Stan.Silvert@jboss.org
47  @version $Revision: 56129 $
48  */

49 public class JSFIntegrationUnitTestCase extends JBossTestCase
50 {
51    private String JavaDoc baseURL = HttpUtils.getBaseURL();
52    private HttpClient client = new HttpClient();
53    
54    public JSFIntegrationUnitTestCase(String JavaDoc name)
55    {
56       super(name);
57    }
58    
59    /** Access the http://localhost/jbosstest-jsf/index.jsf.
60     */

61    public void testJSFIntegrated() throws Exception JavaDoc
62    {
63       client.executeMethod(makeRequest());
64
65       HttpMethodBase result = makeRequest();
66
67       // need to hit it twice with the same session for test to pass
68
client.executeMethod(result);
69
70       String JavaDoc responseBody = result.getResponseBodyAsString();
71       if (responseBody == null) {
72          throw new Exception JavaDoc("Unable to get response from server.");
73       }
74
75       assertTrue(contains(responseBody, "@PostConstruct was called."));
76       assertTrue(contains(responseBody, "@PreDestroy was called."));
77       assertTrue(contains(responseBody, "Datasource was injected."));
78
79       // Tests JSF/JSTL integration
80
assertTrue(contains(responseBody, "number one"));
81       assertTrue(contains(responseBody, "number two"));
82       assertTrue(contains(responseBody, "number three"));
83
84       // Tests enum support
85
assertTrue(contains(responseBody, "JBoss Color selection is PURPLE"));
86    }
87
88    private boolean contains(String JavaDoc base, String JavaDoc target) {
89       return base.indexOf(target) != -1;
90    }
91
92    private GetMethod makeRequest() {
93       return new GetMethod(baseURL+"jbosstest-jsf/index.jsf");
94    }
95
96    public static Test suite() throws Exception JavaDoc
97    {
98       TestSuite suite = new TestSuite();
99       suite.addTest(new TestSuite(JSFIntegrationUnitTestCase.class));
100
101       // Create an initializer for the test suite
102
Test wrapper = new JBossTestSetup(suite)
103       {
104          protected void setUp() throws Exception JavaDoc
105          {
106             super.setUp();
107             deploy("jbosstest-jsf.war");
108          }
109          protected void tearDown() throws Exception JavaDoc
110          {
111             undeploy("jbosstest-jsf.war");
112             super.tearDown();
113          }
114       };
115       return wrapper;
116    }
117    
118
119 }
120
Popular Tags