1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id$ 20 */ 21 22 package org.apache.qetest; 23 24 import java.util.Properties; 25 26 /** 27 * Factory constructor for various qetest-related objects. 28 * 29 * Currently only supports finding an appropriate instance 30 * of a file-based CheckService. 31 * 32 * @author Shane_Curcuru@lotus.com 33 * @version $Id$ 34 */ 35 public abstract class QetestFactory 36 { 37 38 /** Constant denoting a default CheckService for Files. */ 39 public static final String TYPE_FILES = "QetestFactory.FILECHECK"; 40 41 /** Default TYPE_FILES implementation class. */ 42 public static final String DEFAULT_TYPE_FILES = "org.apache.qetest.xsl.XHTFileCheckService"; 43 44 /** 45 * Get a new CheckService of a specified type. 46 * 47 * Currently only supports the TYPE_FILES or your own FQCN. 48 * 49 * @param type FQCN or constant type of CheckService needed 50 * @return CheckService of the appropriate type; or a fallback 51 * type if not found and a fallback is available. 52 */ newCheckService(Logger logger, String type)53 public static CheckService newCheckService(Logger logger, String type) 54 { 55 CheckService service = null; 56 if (null == type) 57 { 58 logger.logMsg(Logger.ERRORMSG, "Warning: no type specified for newCheckService!"); 59 return null; 60 } 61 else if (TYPE_FILES.equals(type)) 62 { 63 // Return our default impl 64 Class fClazz = QetestUtils.testClassForName(DEFAULT_TYPE_FILES, null, null); 65 if (null == fClazz) 66 { 67 logger.logMsg(Logger.ERRORMSG, "Warning: no default fileChecker is available: " + DEFAULT_TYPE_FILES); 68 return null; //@todo should we throw exception instead? 69 } 70 71 try 72 { 73 service = (CheckService)fClazz.newInstance(); 74 //logger.logMsg(Logger.TRACEMSG, TYPE_FILES + " is " + service); 75 } 76 catch (Exception e) 77 { 78 logger.logThrowable(Logger.ERRORMSG, e, "newCheckService(" + TYPE_FILES + ") threw"); 79 } 80 return service; 81 } 82 else // Assume a classname of your impl 83 { 84 // Return our default impl 85 Class fClazz = QetestUtils.testClassForName(type, QetestUtils.defaultPackages, null); 86 if (null == fClazz) 87 { 88 logger.logMsg(Logger.ERRORMSG, "Warning: no fileChecker is available of type: " + type); 89 return null; //@todo should we throw exception instead? 90 } 91 92 try 93 { 94 service = (CheckService)fClazz.newInstance(); 95 logger.logMsg(Logger.TRACEMSG, TYPE_FILES + " is " + service); 96 } 97 catch (Exception e) 98 { 99 logger.logThrowable(Logger.ERRORMSG, e, "newCheckService(" + TYPE_FILES + ") threw"); 100 } 101 return service; 102 } 103 } 104 105 106 /** 107 * Get a new Reporter with some defaults. 108 * 109 * Will attempt to initialize the appropriate Reporter 110 * depending on the options passed in; if all else fails, will 111 * return at least a ConsoleLogger. 112 * 113 * @param options to create Reporter from 114 * @return appropriate Reporter instance, or a default one. 115 */ newReporter(Properties options)116 public static Reporter newReporter(Properties options) 117 { 118 Reporter reporter = null; 119 if (null == options) 120 { 121 // Return a default Reporter 122 reporter = new Reporter(null); 123 reporter.addDefaultLogger(); // add default logger automatically 124 return reporter; 125 } 126 127 // Setup appropriate defaults for the Reporter 128 // Ensure we have an XMLFileLogger if we have a logName 129 String logF = options.getProperty(Logger.OPT_LOGFILE); 130 131 if ((logF != null) && (!logF.equals(""))) 132 { 133 134 // We should ensure there's an XMLFileReporter 135 String r = options.getProperty(Reporter.OPT_LOGGERS); 136 137 if (r == null) 138 { 139 // Create the property if needed... 140 options.put(Reporter.OPT_LOGGERS, 141 "org.apache.qetest.XMLFileLogger"); 142 } 143 else if (r.indexOf("XMLFileLogger") <= 0) 144 { 145 // ...otherwise append to existing list 146 options.put(Reporter.OPT_LOGGERS, 147 r + Reporter.LOGGER_SEPARATOR 148 + "org.apache.qetest.XMLFileLogger"); 149 } 150 } 151 152 // Ensure we have a ConsoleLogger unless asked not to 153 // @todo improve and document this feature 154 String noDefault = options.getProperty("noDefaultReporter"); 155 156 if (noDefault == null) 157 { 158 159 // We should ensure there's an XMLFileReporter 160 String r = options.getProperty(Reporter.OPT_LOGGERS); 161 162 if (r == null) 163 { 164 options.put(Reporter.OPT_LOGGERS, 165 "org.apache.qetest.ConsoleLogger"); 166 } 167 else if (r.indexOf("ConsoleLogger") <= 0) 168 { 169 options.put(Reporter.OPT_LOGGERS, 170 r + Reporter.LOGGER_SEPARATOR 171 + "org.apache.qetest.ConsoleLogger"); 172 } 173 } 174 175 // Pass our options directly to the reporter 176 // so it can use the same values in initialization 177 // A Reporter will auto-initialize from the values 178 // in the properties block 179 reporter = new Reporter(options); 180 return reporter; 181 } 182 183 184 /** 185 * Get a new Logger with some defaults. 186 * 187 * Currently a redirect to call (Logger)newReporter(options). 188 * 189 * @param options to create Logger from 190 * @return appropriate Logger instance, or a default one. 191 */ newLogger(Properties options)192 public static Logger newLogger(Properties options) 193 { 194 return (Logger)newReporter(options); 195 } 196 197 198 /** Prevent instantiation - private constructor. */ QetestFactory()199 private QetestFactory() { /* no-op */ } 200 201 } // end of class CheckService 202 203