View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.util;
20  
21  import java.util.Hashtable;
22  
23  import net.sourceforge.jwebunit.api.ITestingEngine;
24  import net.sourceforge.jwebunit.exception.TestingEngineRegistryException;
25  
26  /**
27   * This will maintain a registry of known testing engines to be used by JWebUnit.
28   * 
29   * @author Julien Henry
30   */
31  public class TestingEngineRegistry {
32  
33      /**
34       * Key of HtmlUnit testing engine.
35       */
36      public final static String TESTING_ENGINE_HTMLUNIT = "TestingEngineHtmlUnit";
37  
38      /**
39       * Key of Webdriver testing engine.
40       */
41      public final static String TESTING_ENGINE_WEBDRIVER = "TestingEngineWebdriver";
42  
43      private static Hashtable<String,Class<?>> testingEngineMap = new Hashtable<String,Class<?>>();
44  
45      static {
46          String cp = "net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl";
47          // Try to load HtmlUnit Testing Engine to check if it is present.
48          try {
49              addTestingEngine(TESTING_ENGINE_HTMLUNIT, cp);
50          } catch (ClassNotFoundException e) {
51              // HtmlUnit Testing Engine is not present in the classpath. Nothing to do.
52          }
53          cp = "net.sourceforge.jwebunit.webdriver.WebDriverTestingEngineImpl";
54          // Try to load Webdriver Testing Engine to check if it is present.
55          try {
56              addTestingEngine(TESTING_ENGINE_WEBDRIVER, cp);
57          } catch (ClassNotFoundException e) {
58              // Webdriver Testing Engine is not present in the classpath. Nothing to do.
59          }
60      }
61  
62      /**
63       * Gets the class based on the key of the class.
64       * 
65       * @param aKey Key of the testing engine
66       * @return the testing engine class.
67       */
68      public static Class<?> getTestingEngineClass(String aKey)
69              throws ClassNotFoundException {
70          Class<?> theClass = (Class<?>) testingEngineMap.get(aKey);
71          return theClass;
72      }
73  
74      /**
75       * Add a new testing engine.
76       * 
77       * @param key A string to identify the testing engine.
78       * @param classpath The full class name.
79       * @throws ClassNotFoundException If the class is not in the classpath.
80       */
81      public static void addTestingEngine(String key, String classpath)
82              throws ClassNotFoundException {
83          Class<?> c = Class.forName(classpath);
84          if (ITestingEngine.class.isAssignableFrom(c)) {
85              testingEngineMap.put(key, c);
86          } else {
87              throw new TestingEngineRegistryException(classpath
88                      + " is not an instance of ITestingEngine");
89          }
90      }
91  
92      /**
93       * Get first available testing engine key.
94       * 
95       * @return key of a testing engine, or null is none is available.
96       */
97      public static String getFirstAvailable() {
98          if (testingEngineMap.isEmpty()) {
99              return null;
100         } else {
101             return (String) testingEngineMap.keys().nextElement();
102         }
103     }
104 
105 }