KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.tools.verifier.tests;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import com.sun.enterprise.tools.verifier.tests.web.WebTest;
31
32 /**
33  * @author Sudipto Ghosh
34  */

35 public abstract class TagLibTest extends WebTest {
36
37     /**
38      * This method parses the function-signature argument and returns the list of
39      * parameters as String array.
40      *
41      * signature param to this method contains the value of this element in tld.
42      * <function-signature>java.lana.String nickName( java.lang.String, int )
43      * </function-signature>
44      *
45      * @param signature
46      * @return The String array containing the parameter class type.
47      */

48     public String JavaDoc[] getParameters(String JavaDoc signature) {
49         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
50         String JavaDoc[] tokens = (signature.split("\\s"));
51         for (int i = 1; i < tokens.length; i++)
52             sb.append(tokens[i]);
53         String JavaDoc fullParamString = sb.toString().substring(sb.indexOf("(") + 1, sb.indexOf(")"));
54         String JavaDoc[] params = getParams(fullParamString);
55         return params;
56     }
57
58     /**
59      *
60      * @param fullParamString This is the parsed string from which the parameters
61      * will be separated and added as a list of Strings
62      * eg: for <function-signature>java.lana.String nickName( java.lang.String, int )
63      * </function-signature>. The parsed String will look like this
64      * java.lang.String,int
65      * @return String array containing all the parameter Types
66      */

67
68     public String JavaDoc[] getParams(String JavaDoc fullParamString) {
69         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
70         if (fullParamString.contains(",")) {
71             String JavaDoc rest = fullParamString.substring(fullParamString.indexOf(",") + 1, fullParamString.length());
72             fullParamString = fullParamString.substring(0, fullParamString.indexOf(","));
73             list.add(fullParamString);
74             getParams(rest);
75         } else list.add(fullParamString);
76
77         return list.toArray(new String JavaDoc[0]);
78     }
79
80
81     /**
82      * This method returns the first token of this string, which must be the return
83      * type of the method.
84      * @param signature string from the function-signature element of tld
85      * @return return type of the method.
86      */

87     public String JavaDoc getRetType(String JavaDoc signature) {
88         String JavaDoc[] tokens = (signature.split("\\s"));
89         return tokens[0];
90     }
91
92     /**
93      * This method will return the method name from the signature String passed
94      * as argument.
95      * eg: for <function-signature>java.lana.String nickName( java.lang.String, int )
96      * </function-signature>
97      * @param signature
98      * @return method name
99      */

100     public String JavaDoc getName(String JavaDoc signature) {
101         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
102         String JavaDoc[] tokens = (signature.split("\\s"));
103         for (int i = 1; i < tokens.length; i++)
104             sb.append(tokens[i]);
105         String JavaDoc name = sb.toString();
106         name = name.substring(0, name.indexOf("("));
107         return name;
108     }
109
110
111     /**
112      * Checks if the parameter array matches with this method's parameters
113      * @param m method object
114      * @param param parameter class array
115      * @return true if parameters match, false otherwise
116      */

117     public boolean parametersMatch(Method JavaDoc m, Class JavaDoc[] param) {
118         boolean match = false;
119         Class JavaDoc[] types = m.getParameterTypes();
120         if (types.length!=param.length)
121             return false;
122         for (int i=0; i<types.length; i++) {
123             match = types[i].equals(param[i]);
124         }
125         return match;
126     }
127
128     /**
129      * Checks if the return type specified in this method m and the return type
130      * required matches
131      * @param m
132      * @param retType
133      * @return true if retType and m.getReturnType matches, false otherwise
134      */

135     public boolean returnTypeMatch(Method JavaDoc m, String JavaDoc retType) {
136         Class JavaDoc ret = m.getReturnType();
137         Class JavaDoc retTypeClass = checkIfPrimitive(retType);
138         if (retTypeClass == null);
139         try {
140             retTypeClass = Class.forName(retType);
141         } catch (ClassNotFoundException JavaDoc e) {
142             //do nothing. If return type is incorrect, it will be caught by
143
// another test in verifier.
144
}
145         if(retTypeClass != null) //this may happen if retType is
146
return retTypeClass.equals(ret);//non-primitive and invalid
147
else return false;
148     }
149
150     /**
151      * given a parameter array contained in a method, this method returns the
152      * Class array representation of the parameters
153      * @param par
154      * @param cl
155      * @return class array representation of the parameters
156      */

157     public Class JavaDoc[] getParamTypeClass(String JavaDoc[] par, ClassLoader JavaDoc cl) {
158         List JavaDoc<Class JavaDoc> list = new ArrayList JavaDoc<Class JavaDoc>();
159         for(String JavaDoc s : par) {
160             Class JavaDoc c = checkIfPrimitive(s);
161             if (c != null)
162                 list.add(c);
163             else {
164                 try {
165                     c = Class.forName(s, false, cl);
166                     list.add(c);
167                 } catch (ClassNotFoundException JavaDoc e) {
168                   //do nothing. Other test will report the problem if parameter
169
// is not specified correctly
170
}
171             }
172         }
173         return list.toArray(new Class JavaDoc[0]);
174     }
175 }
176
Popular Tags