View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon;
20  
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import net.sf.jameleon.exception.JameleonException;
25  import net.sf.jameleon.exception.JameleonTagException;
26  import net.sf.jameleon.util.Configurator;
27  import net.sf.jameleon.util.SupportedTags;
28  
29  import org.apache.commons.jelly.JellyException;
30  import org.apache.commons.jelly.Tag;
31  import org.apache.commons.jelly.TagLibrary;
32  import org.xml.sax.Attributes;
33  
34  /***
35   * Registers the tag libraries based on what is set up in SupportedTags.
36   * SupportedTags by default reads its configuration from jameleon.conf' plugins
37   * directive.
38   * Even if no plug-ins are configured, then the following files should be read in:
39   * <ol>
40   *  <li>jameleon-core.properties</li>
41   *  <li>TestCaseTagDefs.properties</li>
42   * </ol>
43   * The format of those files is:<br>
44   * tagname=package.and.classname.of.function.point
45   */
46  public class TestCaseTagLibrary extends TagLibrary {
47  
48      protected static SupportedTags st = new SupportedTags();
49      protected static boolean warnOnNoPluginsFound;
50  
51      public TestCaseTagLibrary() {
52          warnOnNoPluginsFound = Boolean.valueOf(Configurator.getInstance().getValue("warnOnNoPluginsFound", "true")).booleanValue();
53          st.setWarnOnNoPluginsFile(warnOnNoPluginsFound);
54          registerTags(st.getSupportedTags());
55      }
56  
57      /***
58       * Loops through all keys in a file named by the <code>name</code parameter.
59       * @param tags - a tag name - class name map of tags to register
60       */
61      protected void registerTags(Map tags){
62          Iterator keys = tags.keySet().iterator();
63          String key = null;
64          while (keys.hasNext()) {
65              key = (String)keys.next();
66              String className = (String)tags.get(key);
67              registerTag(key,getClassLoader(className));
68          }
69      }
70  
71  
72      private Class getClassLoader(String className){
73          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
74          Class clzz = null;
75          if (classLoader == null) {
76              classLoader = this.getClass().getClassLoader();
77          } else {
78              if (findClass(className,classLoader) == null) {
79                  classLoader = this.getClass().getClassLoader();
80              }
81          }
82          clzz = findClass(className, classLoader);
83          if (clzz == null) {
84              throw new JameleonException("Couldn't find "+className);
85          }
86          return clzz;
87      }
88  
89      private Class findClass(String className, ClassLoader cl){
90          Class clzz = null;
91          try{
92             clzz = cl.loadClass(className);
93          }catch(ClassNotFoundException cnfe){
94              //
95          }
96          return clzz;
97      }
98  
99      public static void resetTags(){
100         st = new SupportedTags();
101         st.setWarnOnNoPluginsFile(warnOnNoPluginsFound);
102     }
103 
104     public static void setWarnOnNoPluginsFound(boolean warnOnNoPlugins){
105        warnOnNoPluginsFound = warnOnNoPlugins;
106        if (st != null) {
107            st.setWarnOnNoPluginsFile(warnOnNoPlugins);
108        }
109     }
110 
111     //TagLibrary Methods
112 
113     /*** Creates a new Tag for the given tag name and attributes */
114     public Tag createTag(String name, Attributes attributes)
115         throws JellyException {
116         Tag tag = super.createTag(name, attributes);
117         if (tag == null) {
118             throw new JameleonTagException(name, "is not a recognized Jameleon tag. Please check the spelling and try again.");
119         }
120         return tag;
121     }
122 
123     /*** Creates a new script to execute the given tag name and attributes */
124     //uncommenting this will cause a parse failure before the script
125     //is even executed if an unknown tag is encountered.
126 /*
127     public TagScript createTagScript(String name, Attributes attributes)
128         throws JellyException {
129 
130         TagScript tagScript = super.createTagScript(name, attributes);
131         if (tagScript == null) {
132             throw new JameleonTagException(name, "is not a recognized Jameleon tag. Please check the spelling and try again.");
133         }
134         return tagScript;
135     }
136 */
137 }