KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > ErrorCode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.web;
24
25 import com.sun.enterprise.tools.verifier.tests.web.WebTest;
26 import java.util.*;
27 import java.io.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import com.sun.enterprise.util.FileClassLoader;
32
33
34 /** Error code element contains an HTTP error code within web application test.
35  * i.e. 404
36  */

37 public class ErrorCode extends WebTest implements WebCheck {
38
39     
40     /**
41      * Determine is Error Code is valid.
42      *
43      * @param errorCodeTypeInteger error code type
44      *
45      * @return <code>boolean</code> true if error code is valid, false otherwise
46      */

47     public static boolean isValidErrorCode(Integer JavaDoc errorCodeTypeInteger) {
48     int errorCodeType = errorCodeTypeInteger.intValue();
49     if ((errorCodeType == ErrorCodeTypes.CONTINUE) ||
50         (errorCodeType == ErrorCodeTypes.SWITCHING_PROTOCOLS) ||
51         (errorCodeType == ErrorCodeTypes.OK) ||
52         (errorCodeType == ErrorCodeTypes.CREATED) ||
53         (errorCodeType == ErrorCodeTypes.ACCEPTED) ||
54         (errorCodeType == ErrorCodeTypes.NON_AUTHORITATIVE_INFORMATION) ||
55         (errorCodeType == ErrorCodeTypes.NO_CONTENT) ||
56         (errorCodeType == ErrorCodeTypes.RESET_CONTENT) ||
57         (errorCodeType == ErrorCodeTypes.PARTIAL_CONTENT) ||
58         (errorCodeType == ErrorCodeTypes.MULTIPLE_CHOICES) ||
59         (errorCodeType == ErrorCodeTypes.MOVED_PERMANENTLY) ||
60         (errorCodeType == ErrorCodeTypes.FOUND) ||
61         (errorCodeType == ErrorCodeTypes.SEE_OTHER) ||
62         (errorCodeType == ErrorCodeTypes.NOT_MODIFIED) ||
63         (errorCodeType == ErrorCodeTypes.USE_PROXY) ||
64         (errorCodeType == ErrorCodeTypes.UNUSED) ||
65         (errorCodeType == ErrorCodeTypes.TEMPORARY_REDIRECT) ||
66         (errorCodeType == ErrorCodeTypes.BAD_REQUEST) ||
67         (errorCodeType == ErrorCodeTypes.UNAUTHORIZED) ||
68         (errorCodeType == ErrorCodeTypes.PAYMENT_REQUIRED) ||
69         (errorCodeType == ErrorCodeTypes.FORBIDDEN) ||
70         (errorCodeType == ErrorCodeTypes.NOT_FOUND) ||
71         (errorCodeType == ErrorCodeTypes.METHOD_NOT_ALLOWED) ||
72         (errorCodeType == ErrorCodeTypes.NOT_ACCEPTABLE) ||
73         (errorCodeType == ErrorCodeTypes.PROXY_AUTHENTICATION_REQUIRED) ||
74         (errorCodeType == ErrorCodeTypes.REQUEST_TIMEOUT) ||
75         (errorCodeType == ErrorCodeTypes.CONFLICT) ||
76         (errorCodeType == ErrorCodeTypes.GONE) ||
77         (errorCodeType == ErrorCodeTypes.LENGTH_REQUIRED) ||
78         (errorCodeType == ErrorCodeTypes.PRECONDITION_FAILED) ||
79         (errorCodeType == ErrorCodeTypes.REQUEST_ENTITY_TOO_LARGE) ||
80         (errorCodeType == ErrorCodeTypes.REQUEST_URI_TOO_LONG) ||
81         (errorCodeType == ErrorCodeTypes.UNSUPPORTED_MEDIA_TYPE) ||
82         (errorCodeType == ErrorCodeTypes.REQUESTED_RANGE_NOT_SATISFIABLE) ||
83         (errorCodeType == ErrorCodeTypes.EXPECTATION_FAILED) ||
84         (errorCodeType == ErrorCodeTypes.INTERNAL_SERVER_ERROR) ||
85         (errorCodeType == ErrorCodeTypes.NOT_IMPLEMENTED) ||
86         (errorCodeType == ErrorCodeTypes.BAD_GATEWAY) ||
87         (errorCodeType == ErrorCodeTypes.SERVICE_UNAVAILABLE) ||
88         (errorCodeType == ErrorCodeTypes.GATEWAY_TIMEOUT) ||
89         (errorCodeType == ErrorCodeTypes.HTTP_VERSION_NOT_SUPPORTED)) {
90         return true;
91     } else {
92         return false;
93     }
94  
95     }
96
97     /** Error code element contains an HTTP error code within web application test.
98      * i.e. 404
99      *
100      * @param descriptor the Web deployment descriptor
101      *
102      * @return <code>Result</code> the results for this assertion
103      */

104     public Result check(WebBundleDescriptor descriptor) {
105
106     Result result = getInitializedResult();
107     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
108
109     if (descriptor.getErrorPageDescriptors().hasMoreElements()) {
110         boolean oneFailed = false;
111         boolean foundIt = false;
112             int oneErrorCode = 0;
113             int oneNA = 0;
114         // get the errorpage's in this .war
115
for (Enumeration e = descriptor.getErrorPageDescriptors() ; e.hasMoreElements() ;) {
116         foundIt = false;
117                 oneErrorCode++;
118         ErrorPageDescriptorImpl errorpage = (ErrorPageDescriptorImpl) e.nextElement();
119                 String JavaDoc exceptionType = errorpage.getExceptionType();
120                 if (!((exceptionType != null) && (exceptionType.length() > 0))) {
121             Integer JavaDoc errorCode = new Integer JavaDoc( errorpage.getErrorCode() );
122             if (isValidErrorCode(errorCode)) {
123             foundIt = true;
124             } else {
125             foundIt = false;
126             }
127    
128             if (foundIt) {
129             result.addGoodDetails(smh.getLocalString
130                        ("tests.componentNameConstructor",
131                         "For [ {0} ]",
132                         new Object JavaDoc[] {compName.toString()}));
133             result.addGoodDetails(smh.getLocalString
134                           (getClass().getName() + ".passed",
135                            "Error code [ {0} ] contains valid HTTP error code within web application [ {1} ]",
136                            new Object JavaDoc[] {errorCode.toString(), descriptor.getName()}));
137             } else {
138             if (!oneFailed) {
139                 oneFailed = true;
140             }
141             result.addErrorDetails(smh.getLocalString
142                        ("tests.componentNameConstructor",
143                         "For [ {0} ]",
144                         new Object JavaDoc[] {compName.toString()}));
145             result.addErrorDetails(smh.getLocalString
146                            (getClass().getName() + ".failed",
147                         "Error: error-code [ {0} ] does not contain valid HTTP error code within web application [ {1} ]",
148                         new Object JavaDoc[] {errorCode.toString(), descriptor.getName()}));
149             }
150                 } else {
151                     // maybe ErrorCode is not used 'cause we are using Exception
152
// if that is the case, then test is N/A,
153
result.addNaDetails(smh.getLocalString
154                        ("tests.componentNameConstructor",
155                         "For [ {0} ]",
156                         new Object JavaDoc[] {compName.toString()}));
157                     result.addNaDetails(smh.getLocalString
158                     (getClass().getName() + ".notApplicable1",
159                      "Not Applicable: Error-code is [ {0} ], using [ {1} ] instead within web application [ {2} ]",
160                      new Object JavaDoc[] {new Integer JavaDoc(errorpage.getErrorCode()), exceptionType, descriptor.getName()}));
161                     oneNA++;
162                 }
163         }
164         if (oneFailed) {
165         result.setStatus(Result.FAILED);
166             } else if (oneNA == oneErrorCode) {
167                 result.setStatus(Result.NOT_APPLICABLE);
168         } else {
169         result.setStatus(Result.PASSED);
170         }
171     } else {
172         result.addNaDetails(smh.getLocalString
173                        ("tests.componentNameConstructor",
174                         "For [ {0} ]",
175                         new Object JavaDoc[] {compName.toString()}));
176         result.notApplicable(smh.getLocalString
177                  (getClass().getName() + ".notApplicable",
178                   "There are no error-code elements within the web archive [ {0} ]",
179                   new Object JavaDoc[] {descriptor.getName()}));
180     }
181
182     return result;
183     }
184 }
185
Popular Tags