KickJava   Java API By Example, From Geeks To Geeks.

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


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.VerifierTest;
26 import com.sun.enterprise.tools.verifier.tests.*;
27 import com.sun.enterprise.tools.verifier.Verifier;
28 import com.sun.enterprise.tools.verifier.Result;
29 import com.sun.enterprise.deployment.Descriptor;
30 import com.sun.enterprise.deployment.WebBundleDescriptor;
31 import com.sun.enterprise.deployment.util.ModuleDescriptor;
32 import com.sun.enterprise.tools.verifier.Context;
33 import com.sun.enterprise.util.io.FileUtils;
34
35 import java.io.*;
36
37
38 /**
39  * Common code and helper methods and properties for all tests
40  * in the web-app space (jsp and servlets).
41  *
42  * @author Jerome Dochez
43  * @version 1.0
44  */

45 abstract public class WebTest extends VerifierTest implements VerifierCheck, WebCheck {
46
47     // variables ensuring that result details are added only once
48
private boolean addedError = false;
49     private boolean addedGood = false;
50     private boolean addedNa = false;
51     private boolean addedWarning = false;
52
53     final String JavaDoc separator= System.getProperty("file.separator");
54     Context context = null;
55
56     /**
57      * <p>
58      * run an individual test against the deployment descriptor for the
59      * archive the verifier is performing compliance tests against.
60      * </p>
61      *
62      * @param descriptor deployment descriptor for the archive
63      * @return result object containing the result of the individual test
64      * performed
65      */

66     public Result check(Descriptor descriptor) {
67         return check((WebBundleDescriptor) descriptor);
68     }
69    
70     /**
71      * <p>
72      * all connector tests should implement this method. it run an individual
73      * test against the resource adapter deployment descriptor.
74      * </p>
75      *
76      * @param descriptor deployment descriptor for the archive file
77      * @return result object containing the result of the individual test
78      * performed
79      */

80     public abstract Result check(WebBundleDescriptor descriptor);
81     
82     /**
83      * load the war file
84      *
85      * @param descriptor the Enumeration
86      *
87      * @return <code>Result</code> the results for this assertion
88      */

89     public Result loadWarFile(WebBundleDescriptor descriptor) {
90         
91         Result result = getInitializedResult();
92     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
93     context = getVerifierContext();
94         try {
95             // TODO : check whether this method is required?
96
WebTestsUtil webTestsUtil = WebTestsUtil.getUtil(context.getClassLoader());
97 // File f = Verifier.getArchiveFile(descriptor.getModuleDescriptor().
98
// getArchiveUri());
99
// if (f != null) {
100
// webTestsUtil.extractJarFile(f);
101
// }
102
// else {
103
// // dont bother about extracting JarFile
104
// }
105
//Sheetal: 09/30/02
106
//dont need to call Verifier's appendCLWithWebInfContents() since J2EEClassLoader takes care of it
107
//webTestsUtil.appendCLWithWebInfContents();
108
} catch (Throwable JavaDoc e) {
109 // e.printStackTrace();
110
Verifier.debug(e);
111         result.addErrorDetails(smh.getLocalString
112                        ("tests.componentNameConstructor",
113                     "For [ {0} ]",
114                     new Object JavaDoc[] {compName.toString()}));
115             result.addErrorDetails(smh.getLocalString
116         ("com.sun.enterprise.tools.verifier.tests.web.WebTest" + ".Exception",
117          "Error: Unexpected exception occurred [ {0} ]",
118          new Object JavaDoc[] {e.toString()}));
119         }
120         return result;
121     }
122     
123     /**
124      * <p>
125      * load a class from the web bundle archive file
126      * </p>
127      *
128      * @param result to put error if necessary
129      * @param className the class to load
130      * @return the loaded class or null is cannot be loaded from the archive
131      */

132     public Class JavaDoc loadClass(Result result, String JavaDoc className) {
133     
134         try {
135             WebTestsUtil webTestsUtil = WebTestsUtil.getUtil(context.getClassLoader());
136         //webTestsUtil.appendCLWithWebInfContents();
137
return webTestsUtil.loadClass(className);
138         } catch (Throwable JavaDoc e) {
139
140             // @see preVerify Method of Verifier.java
141
try {
142                 ClassLoader JavaDoc cl = getVerifierContext().getAlternateClassLoader();
143               if (cl == null) {
144                   throw e;
145                }
146                 Class JavaDoc c = cl.loadClass(className);
147                 return c;
148              }catch(Throwable JavaDoc ex) {
149                /*
150                result.addErrorDetails(smh.getLocalString
151                ("com.sun.enterprise.tools.verifier.tests.web.WebTest.Exception",
152                 "Error: Unexpected exception occurred [ {0} ]",
153                 new Object[] {ex.toString()}));
154                */

155             }
156         }
157         return null;
158     }
159
160     /**
161      * Method for recursively deleting all temporary directories
162      */

163
164     protected void deleteDirectory(String JavaDoc oneDir) {
165         
166         File[] listOfFiles;
167         File cleanDir;
168     
169         cleanDir = new File(oneDir);
170         if (!cleanDir.exists()) {// Nothing to do. Return;
171
return;
172     }
173         listOfFiles = cleanDir.listFiles();
174         if(listOfFiles != null) {
175             for(int countFiles = 0; countFiles < listOfFiles.length; countFiles++) {
176                 if (listOfFiles[countFiles].isFile()) {
177                     listOfFiles[countFiles].delete();
178                 } else { // It is a directory
179
String JavaDoc nextCleanDir = cleanDir + separator + listOfFiles[countFiles].getName();
180             File newCleanDir = new File(nextCleanDir);
181                     deleteDirectory(newCleanDir.getAbsolutePath());
182                 }
183             }// End for loop
184
} // End if statement
185
cleanDir.delete();
186     }
187
188     protected String JavaDoc getAbstractArchiveUri(WebBundleDescriptor desc) {
189         String JavaDoc archBase = getVerifierContext().getAbstractArchive().
190                 getArchiveUri();
191         final ModuleDescriptor moduleDescriptor = desc.getModuleDescriptor();
192         if (moduleDescriptor.isStandalone()) {
193             return archBase; // it must be a stand-alone module; no such physical dir exists
194
} else {
195             return archBase + File.separator +
196                     FileUtils.makeFriendlyFileName(moduleDescriptor.getArchiveUri());
197         }
198     }
199
200 }
201
Popular Tags