1 18 package org.apache.activemq; 19 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashSet ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 69 public abstract class CombinationTestSupport extends AutoFailTestSupport { 70 71 protected static final Log log = LogFactory.getLog(CombinationTestSupport.class); 72 73 private HashMap comboOptions = new HashMap (); 74 private boolean combosEvaluated; 75 private Map options; 76 77 static class ComboOption { 78 final String attribute; 79 final LinkedHashSet values = new LinkedHashSet (); 80 81 public ComboOption(String attribute, Collection options) { 82 this.attribute = attribute; 83 this.values.addAll(options); 84 } 85 } 86 87 public void addCombinationValues(String attribute, Object [] options) { 88 ComboOption co = (ComboOption) this.comboOptions.get(attribute); 89 if (co == null) { 90 this.comboOptions.put(attribute, new ComboOption(attribute, Arrays.asList(options))); 91 } else { 92 co.values.addAll(Arrays.asList(options)); 93 } 94 } 95 96 public void runBare() throws Throwable { 97 if (combosEvaluated) { 98 super.runBare(); 99 } else { 100 CombinationTestSupport[] combinations = getCombinations(); 101 for (int i = 0; i < combinations.length; i++) { 102 CombinationTestSupport test = combinations[i]; 103 log.info("Running " + test.getName()); 104 test.runBare(); 105 } 106 } 107 } 108 109 private void setOptions(Map options) throws NoSuchFieldException , IllegalAccessException { 110 this.options = options; 111 for (Iterator iterator = options.keySet().iterator(); iterator.hasNext();) { 112 String attribute = (String ) iterator.next(); 113 Object value = options.get(attribute); 114 try { 115 Field field = getClass().getField(attribute); 116 field.set(this, value); 117 } catch (Throwable e) { 118 log.info("Could not set field '" + attribute + "' to value '" + value 119 + "', make sure the field exists and is public."); 120 } 121 } 122 } 123 124 private CombinationTestSupport[] getCombinations() { 125 try { 126 Method method = getClass().getMethod("initCombos", null); 127 method.invoke(this, null); 128 } catch (Throwable e) { 129 } 130 131 String name = getName(); 132 String comboSetupMethodName = "initCombosFor" + Character.toUpperCase(name.charAt(0)) + name.substring(1); 133 try { 134 Method method = getClass().getMethod(comboSetupMethodName, null); 135 method.invoke(this, null); 136 } catch (Throwable e) { 137 } 138 139 try { 140 ArrayList expandedOptions = new ArrayList (); 141 expandCombinations(new ArrayList (comboOptions.values()), expandedOptions); 142 143 if (expandedOptions.isEmpty()) { 144 combosEvaluated = true; 145 return new CombinationTestSupport[] { this }; 146 } else { 147 148 ArrayList result = new ArrayList (); 149 for (Iterator iter = expandedOptions.iterator(); iter.hasNext();) { 151 CombinationTestSupport combo = (CombinationTestSupport) TestSuite.createTest(getClass(), getName()); 152 combo.combosEvaluated = true; 153 combo.setOptions((Map ) iter.next()); 154 result.add(combo); 155 } 156 157 CombinationTestSupport rc[] = new CombinationTestSupport[result.size()]; 158 result.toArray(rc); 159 return rc; 160 } 161 } catch (Throwable e) { 162 combosEvaluated = true; 163 return new CombinationTestSupport[] { this }; 164 } 165 166 } 167 168 private void expandCombinations(List optionsLeft, List expandedCombos) { 169 if (!optionsLeft.isEmpty()) { 170 HashMap map; 171 if (comboOptions.size() == optionsLeft.size()) { 172 map = new HashMap (); 173 expandedCombos.add(map); 174 } else { 175 map = (HashMap ) expandedCombos.get(expandedCombos.size() - 1); 176 } 177 178 LinkedList l = new LinkedList (optionsLeft); 179 ComboOption comboOption = (ComboOption) l.removeLast(); 180 int i = 0; 181 for (Iterator iter = comboOption.values.iterator(); iter.hasNext();) { 182 Object value = (Object ) iter.next(); 183 if (i != 0) { 184 map = new HashMap (map); 185 expandedCombos.add(map); 186 } 187 map.put(comboOption.attribute, value); 188 expandCombinations(l, expandedCombos); 189 i++; 190 } 191 } 192 } 193 194 public static Test suite(Class clazz) { 195 TestSuite suite = new TestSuite(); 196 197 ArrayList names = new ArrayList (); 198 Method [] methods = clazz.getMethods(); 199 for (int i = 0; i < methods.length; i++) { 200 String name = methods[i].getName(); 201 if (names.contains(name) || !isPublicTestMethod(methods[i])) 202 continue; 203 names.add(name); 204 Test test = TestSuite.createTest(clazz, name); 205 if (test instanceof CombinationTestSupport) { 206 CombinationTestSupport[] combinations = ((CombinationTestSupport) test).getCombinations(); 207 for (int j = 0; j < combinations.length; j++) { 208 suite.addTest(combinations[j]); 209 } 210 } else { 211 suite.addTest(test); 212 } 213 } 214 return suite; 215 } 216 217 static private boolean isPublicTestMethod(Method m) { 218 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 219 } 220 221 static private boolean isTestMethod(Method m) { 222 String name = m.getName(); 223 Class [] parameters = m.getParameterTypes(); 224 Class returnType = m.getReturnType(); 225 return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); 226 } 227 228 public String getName() { 229 if (options != null) { 230 return super.getName() + " " + options; 231 } 232 return super.getName(); 233 } 234 } 235 | Popular Tags |