KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > IconImageTypeTest


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
24
25 package com.sun.enterprise.tools.verifier.tests;
26
27 import com.sun.enterprise.tools.verifier.Result;
28 import com.sun.enterprise.tools.verifier.SpecVersionMapper;
29 import com.sun.enterprise.tools.verifier.util.VerifierConstants;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.deployment.deploy.shared.Archive;
32
33 import java.util.Collection JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Enumeration JavaDoc;
36
37 /**
38  * This test is deried from Java EE platform spec.
39  * See javaee_5.xsd
40  * @author Sanjeeb.Sahoo@Sun.COM
41  */

42 public class IconImageTypeTest extends VerifierTest implements VerifierCheck{
43     private Collection JavaDoc<String JavaDoc> smallIconUris = new ArrayList JavaDoc<String JavaDoc>();
44     private Collection JavaDoc<String JavaDoc> largeIconUris = new ArrayList JavaDoc<String JavaDoc>();
45     private Descriptor descriptor;
46     private Result result;
47     ComponentNameConstructor compName;
48     private static final String JavaDoc[] allowableImageTypesForJavaEEOlderThan5 = {".gif", ".jpg"};
49     private static final String JavaDoc[] allowableImageTypesForJavaEE5 = {".gif", ".jpg", ".png"};
50     public Result check(Descriptor descriptor) {
51         this.descriptor = descriptor;
52         compName = getVerifierContext().getComponentNameConstructor();
53         result = getInitializedResult();
54         result.setStatus(Result.PASSED);
55         addGoodDetails(result, compName);
56         result.passed(smh.getLocalString
57                       (getClass().getName() + ".passed", //NOI18N
58
"No errors were detected.")); // NOI18N
59

60         // Now collect all the Icon URIs that we are going to test
61
collectIconURIs();
62         testIconURIType();
63         testIconURIExistence();
64         return result;
65     }
66
67     private void testIconURIType() {
68         String JavaDoc[] allowableImageTypes;
69         String JavaDoc JavaEESchemaVersion = getVerifierContext().getJavaEEVersion();
70         if (JavaEESchemaVersion.compareTo(SpecVersionMapper.JavaEEVersion_5) < 0){
71             allowableImageTypes = allowableImageTypesForJavaEEOlderThan5;
72         } else {
73             allowableImageTypes = allowableImageTypesForJavaEE5;
74         }
75
76         Collection JavaDoc<String JavaDoc> allURIs = new ArrayList JavaDoc<String JavaDoc>(smallIconUris);
77         allURIs.addAll(largeIconUris);
78         for(String JavaDoc uri : allURIs){
79             boolean passed = false;
80             for(String JavaDoc allowableType : allowableImageTypes) {
81                 if(uri.endsWith(allowableType)) {
82                     passed = true;
83                     break;
84                 }
85             }
86             if(!passed){
87                 addErrorDetails(result, compName);
88                 result.failed(smh.getLocalString
89                         (getClass().getName() + ".failedImageType",
90                                 "Error: Unsupported image type used in icon image URI [ {0} ].",
91                                 new Object JavaDoc[]{uri}));
92             }
93         }
94     }
95
96     private void testIconURIExistence() {
97         Collection JavaDoc<String JavaDoc> allURIs = new ArrayList JavaDoc<String JavaDoc>(smallIconUris);
98         allURIs.addAll(largeIconUris);
99         for(String JavaDoc uri : allURIs){
100             Archive moduleArchive = getVerifierContext().getModuleArchive();
101             boolean passed = false;
102             for(Enumeration JavaDoc entries = moduleArchive.entries(); entries.hasMoreElements();){
103                 if(uri.equals(entries.nextElement())) {
104                     passed = true;
105                     break;
106                 }
107             }
108             if(!passed){
109                 addErrorDetails(result, compName);
110                 result.failed(smh.getLocalString
111                         (getClass().getName() + ".failedExistence",
112                                 "Error: icon image URI [ {0} ] is not packaged inside [ {1} ].",
113                                 new Object JavaDoc[]{uri, getVerifierContext().getModuleArchive().getURI()}));
114             }
115         }
116     }
117
118     private void collectIconURIs(){
119         // in the absence of a proper Visitor pattern I am left with
120
// little option but to use instanceof
121
if(descriptor instanceof Application)
122             collectIconURIs((Application)descriptor);
123         else if(descriptor instanceof ApplicationClientDescriptor)
124             collectIconURIs((ApplicationClientDescriptor)descriptor);
125         else if(descriptor instanceof EjbDescriptor)
126             collectIconURIs((EjbDescriptor)descriptor);
127         else if(descriptor instanceof ConnectorDescriptor)
128             collectIconURIs((ConnectorDescriptor)descriptor);
129         else if(descriptor instanceof WebBundleDescriptor)
130             collectIconURIs((WebBundleDescriptor)descriptor);
131         else if(descriptor instanceof WebServiceEndpoint)
132             collectIconURIs((WebServiceEndpoint)descriptor);
133         else if(descriptor instanceof ServiceReferenceDescriptor)
134             collectIconURIs((ServiceReferenceDescriptor)descriptor);
135         else {
136             // every time we introduce a new CheckMgrImpl, this will fail
137
// that way we can be notified of the fact that this method needs
138
// to be modified as well.
139
throw new RuntimeException JavaDoc("Unexpected descriptor type.");
140         }
141     }
142
143     // implementation that is common to descriptors that only contain
144
// icon element at top level.
145
private void collectIconURIs(Descriptor desc){
146         String JavaDoc uri=desc.getSmallIconUri();
147         if(uri!=null && uri.length()>0) smallIconUris.add(uri);
148         uri = desc.getLargeIconUri();
149         if(uri!=null && uri.length()>0) largeIconUris.add(uri);
150     }
151
152     private void collectIconURIs(WebBundleDescriptor webBundleDescriptor) {
153         // this is for itself
154
collectIconURIs((Descriptor)webBundleDescriptor);
155         // now collect for each servlet
156
for (Object JavaDoc o : webBundleDescriptor.getWebComponentDescriptorsSet()){
157             collectIconURIs(WebComponentDescriptor.class.cast(o));
158         }
159         // now collect for each servlet filter
160
for (Object JavaDoc o : webBundleDescriptor.getServletFilterDescriptors()) {
161             collectIconURIs(ServletFilterDescriptor.class.cast(o));
162         }
163     }
164
165     private void collectIconURIs(WebServiceEndpoint webServiceEndpoint) {
166         // WebService.xml is organised like this:
167
// WebServicesDescriptor->WebService->WebServiceEndpoint
168
// Since we don't have a CheckMgr that runs test for WebService.xml,
169
// a work around would be to collect all Icons for all the parents
170
// and test them here.
171
// This means a problem there would be as many times as there are
172
// end points.
173
collectIconURIs(webServiceEndpoint.getWebService().getWebServicesDescriptor());
174         collectIconURIs(webServiceEndpoint.getWebService());
175
176         // this is for itself
177
collectIconURIs((Descriptor)webServiceEndpoint);
178         // now collect for each port-compont_handler in handler-chain
179
for (Object JavaDoc o : webServiceEndpoint.getHandlers()){
180             collectIconURIs(WebServiceHandler.class.cast(o));
181         }
182     }
183
184     private void collectIconURIs(EjbDescriptor desc){
185         // Since we don't have a CheckMgr that runs test for ejb-jar.xml,
186
// a work around would be to collect all Icons for the parent
187
// and test them here.
188
// This means a problem there would be as many times as there are
189
// beans.
190
collectIconURIs(desc.getEjbBundleDescriptor());
191         // this is for itself
192
collectIconURIs((Descriptor)descriptor);
193     }
194 }
195
Popular Tags