KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
35  * Exception-type element contains a fully qualified class name of a Java
36  * exception type.
37  */

38 public class ExceptionType extends WebTest implements WebCheck {
39
40     
41     /**
42      * Exception-type element contains a fully qualified class name of a Java
43      * exception type.
44      *
45      * @param descriptor the Web deployment descriptor
46      *
47      * @return <code>Result</code> the results for this assertion
48      */

49     public Result check(WebBundleDescriptor descriptor) {
50
51     Result result = loadWarFile(descriptor);
52     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
53
54     if (descriptor.getErrorPageDescriptors().hasMoreElements()) {
55         boolean oneFailed = false;
56         int oneExceptionType = 0;
57         int oneNA = 0;
58         boolean foundIt = false;
59         // get the errorpage's in this .war
60
for (Enumeration e = descriptor.getErrorPageDescriptors() ; e.hasMoreElements() ;) {
61         foundIt = false;
62                 oneExceptionType++;
63         ErrorPageDescriptorImpl errorpage = (ErrorPageDescriptorImpl) e.nextElement();
64                 if (errorpage.getErrorCode() == 0) {
65             String JavaDoc exceptionType = errorpage.getExceptionType();
66             if ((exceptionType != null) && (exceptionType.length() > 0)) {
67                 boolean isValidExceptionType = false;
68             try {
69                 Class JavaDoc c = loadClass(result, exceptionType);
70                 if (isSubclassOf(c, "java.lang.Exception")) {
71                   isValidExceptionType = true;
72                 }
73             } catch (Exception JavaDoc ex) {
74               // should already be set
75
isValidExceptionType = false;
76             }
77             
78             if (isValidExceptionType) {
79                 foundIt = true;
80             } else {
81                 foundIt = false;
82             }
83    
84             if (foundIt) {
85                 result.addGoodDetails(smh.getLocalString
86                        ("tests.componentNameConstructor",
87                         "For [ {0} ]",
88                         new Object JavaDoc[] {compName.toString()}));
89
90                 result.addGoodDetails(smh.getLocalString
91                           (getClass().getName() + ".passed",
92                            "Exception type [ {0} ] contains a fully qualified class name of a Java exception type within web application [ {1} ]",
93                            new Object JavaDoc[] {exceptionType, descriptor.getName()}));
94             } else {
95                 if (!oneFailed) {
96                 oneFailed = true;
97                 }
98                 result.addErrorDetails(smh.getLocalString
99                        ("tests.componentNameConstructor",
100                         "For [ {0} ]",
101                         new Object JavaDoc[] {compName.toString()}));
102
103                 result.addErrorDetails(smh.getLocalString
104                            (getClass().getName() + ".failed",
105                             "Error: Exception type [ {0} ] does not contain a fully qualified class name of a Java exception type within web application [ {1} ]",
106                             new Object JavaDoc[] {exceptionType, descriptor.getName()}));
107             }
108             } else {
109             if (!oneFailed) {
110                 oneFailed = true;
111             }
112             Integer JavaDoc errorCode = new Integer JavaDoc( errorpage.getErrorCode() );
113             result.addErrorDetails(smh.getLocalString
114                        ("tests.componentNameConstructor",
115                         "For [ {0} ]",
116                         new Object JavaDoc[] {compName.toString()}));
117
118             result.addErrorDetails(smh.getLocalString
119                            (getClass().getName() + ".failed",
120                         "Error: Exception type [ {0} ] does not contain a fully qualified class name of a Java exception type within web application [ {1} ]",
121                         new Object JavaDoc[] {errorCode.toString(), descriptor.getName()}));
122                 oneNA++;
123             }
124         } else {
125             // maybe Exception is null 'cause we are using ErrorCode
126
// if that is the case, then test is N/A,
127
Integer JavaDoc errorCode = new Integer JavaDoc( errorpage.getErrorCode() );
128             result.addNaDetails(smh.getLocalString
129                        ("tests.componentNameConstructor",
130                         "For [ {0} ]",
131                         new Object JavaDoc[] {compName.toString()}));
132
133             result.addNaDetails(smh.getLocalString
134                     (getClass().getName() + ".notApplicable1",
135                      "Exception type is null, using error-code [ {0} ] instead within web application [ {1} ]",
136                      new Object JavaDoc[] {errorCode.toString(), descriptor.getName()}));
137             oneNA++;
138         }
139         }
140         if (oneFailed) {
141         result.setStatus(Result.FAILED);
142         } else if (oneNA == oneExceptionType) {
143         result.setStatus(Result.NOT_APPLICABLE);
144         } else {
145         result.setStatus(Result.PASSED);
146         }
147     } else {
148         result.addNaDetails(smh.getLocalString
149                        ("tests.componentNameConstructor",
150                         "For [ {0} ]",
151                         new Object JavaDoc[] {compName.toString()}));
152
153         result.notApplicable(smh.getLocalString
154                  (getClass().getName() + ".notApplicable",
155                   "There are no exception-type elements within the web archive [ {0} ]",
156                   new Object JavaDoc[] {descriptor.getName()}));
157     }
158
159     return result;
160     }
161 }
162
Popular Tags