KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, 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 JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.MBeanServerInvocationHandler JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.apache.commons.httpclient.Cookie;
33 import org.apache.commons.httpclient.Header;
34 import org.apache.commons.httpclient.HttpClient;
35 import org.apache.commons.httpclient.HttpState;
36 import org.apache.commons.httpclient.HttpMethodBase;
37 import org.apache.commons.httpclient.methods.PostMethod;
38 import org.apache.commons.httpclient.methods.GetMethod;
39 import org.jboss.test.JBossTestCase;
40 import org.jboss.test.JBossTestSetup;
41 import org.jboss.test.util.web.HttpUtils;
42 import org.jboss.security.plugins.JaasSecurityManagerServiceMBean;
43 import junit.framework.Test;
44 import junit.framework.TestSuite;
45
46 /** Tests of custom error forwarding
47  *
48  * @author Scott.Stark@jboss.org
49  * @version $Revision: 37406 $
50  */

51 public class CustomErrorsUnitTestCase extends JBossTestCase
52 {
53    private String JavaDoc baseURLNoAuth = HttpUtils.getBaseURLNoAuth();
54
55    public CustomErrorsUnitTestCase(String JavaDoc name)
56    {
57       super(name);
58    }
59
60    /** Test that the custom 404 error page is seen
61     *
62     * @throws Exception
63     */

64    public void test404Error() throws Exception JavaDoc
65    {
66       log.info("+++ test404Error");
67       int errorCode = HttpURLConnection.HTTP_NOT_FOUND;
68       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"error-producer/ErrorGeneratorServlet?errorCode="+errorCode);
69       HttpMethodBase request = HttpUtils.accessURL(url, "Realm",
70          HttpURLConnection.HTTP_NOT_FOUND);
71       Header errors = request.getResponseHeader("X-CustomErrorPage");
72       log.info("X-CustomErrorPage: "+errors);
73       assertTrue("X-CustomErrorPage("+errors+") is 404.jsp",
74          errors.getValue().equals("404.jsp"));
75    }
76
77    /** Test that the custom 500 error page is seen
78     *
79     * @throws Exception
80     */

81    public void test500Error() throws Exception JavaDoc
82    {
83       log.info("+++ test500Error");
84       int errorCode = HttpURLConnection.HTTP_INTERNAL_ERROR;
85       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"error-producer/ErrorGeneratorServlet?errorCode="+errorCode);
86       HttpMethodBase request = HttpUtils.accessURL(url, "Realm",
87          HttpURLConnection.HTTP_INTERNAL_ERROR);
88       Header errors = request.getResponseHeader("X-CustomErrorPage");
89       log.info("X-CustomErrorPage: "+errors);
90       assertTrue("X-CustomErrorPage("+errors+") is 500.jsp",
91          errors.getValue().equals("500.jsp"));
92    }
93
94    /** Test that the custom 500 error page is seen for an exception
95     *
96     * @throws Exception
97     */

98    public void testExceptionError() throws Exception JavaDoc
99    {
100       log.info("+++ testExceptionError");
101       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"error-producer/ErrorGeneratorServlet");
102       HttpMethodBase request = HttpUtils.accessURL(url, "Realm",
103          HttpURLConnection.HTTP_INTERNAL_ERROR);
104       Header page = request.getResponseHeader("X-CustomErrorPage");
105       log.info("X-CustomErrorPage: "+page);
106       assertTrue("X-CustomErrorPage("+page+") is 500.jsp",
107          page.getValue().equals("500.jsp"));
108       Header errors = request.getResponseHeader("X-ExceptionType");
109       log.info("X-ExceptionType: "+errors);
110       assertTrue("X-ExceptionType("+errors+") is 500.jsp",
111          errors.getValue().equals("java.lang.IllegalStateException"));
112    }
113
114    /** One time setup for all SingleSignOnUnitTestCase unit tests
115     */

116    public static Test suite() throws Exception JavaDoc
117    {
118       TestSuite suite = new TestSuite();
119       suite.addTest(new TestSuite(CustomErrorsUnitTestCase.class));
120
121       // Create an initializer for the test suite
122
Test wrapper = new JBossTestSetup(suite)
123       {
124          protected void setUp() throws Exception JavaDoc
125          {
126             super.setUp();
127             deploy("custom-errors.war");
128             deploy("error-producer.war");
129          }
130          protected void tearDown() throws Exception JavaDoc
131          {
132             undeploy("custom-errors.war");
133             undeploy("error-producer.war");
134             super.tearDown();
135          }
136       };
137       return wrapper;
138    }
139 }
140
Popular Tags