|
| 1 | +/** |
| 2 | + * Factory that assembles a list of pairs of method calls. |
| 3 | + * Part of EvoSuite tutorial. |
| 4 | + * |
| 5 | + * @author Gregory Gay |
| 6 | + * Copyright (C) 2010-2016 Gordon Fraser, Andrea Arcuri and EvoSuite |
| 7 | + * contributors |
| 8 | + * |
| 9 | + * This file is part of EvoSuite. |
| 10 | + * |
| 11 | + * EvoSuite is free software: you can redistribute it and/or modify it |
| 12 | + * under the terms of the GNU Lesser General Public License as published |
| 13 | + * by the Free Software Foundation, either version 3.0 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * EvoSuite is distributed in the hope that it will be useful, but |
| 17 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | + * Lesser Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public |
| 22 | + * License along with EvoSuite. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.evosuite.coverage.methodpair; |
| 26 | + |
| 27 | +import java.lang.reflect.Constructor; |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.LinkedHashSet; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +import org.evosuite.Properties; |
| 35 | +import org.evosuite.setup.TestUsageChecker; |
| 36 | +import org.evosuite.testsuite.AbstractFitnessFactory; |
| 37 | +import org.mockito.asm.Type; |
| 38 | + |
| 39 | +public class MethodPairCoverageFactory extends AbstractFitnessFactory<MethodPairTestFitness> { |
| 40 | + |
| 41 | + @Override |
| 42 | + public List getCoverageGoals() { |
| 43 | + List<MethodPairTestFitness> goals = new ArrayList<>(); |
| 44 | + |
| 45 | + String className = Properties.TARGET_CLASS; |
| 46 | + Class<?> clazz = Properties.getInitializedTargetClass(); |
| 47 | + Set<String> constructors = getUsableConstructors(clazz); |
| 48 | + Set<String> methods = getUsableMethods(clazz); |
| 49 | + |
| 50 | + |
| 51 | + // Pair each constructor with each method and add to goals. |
| 52 | + for(String constructor:constructors){ |
| 53 | + for(String method: methods){ |
| 54 | + goals.add(new MethodPairTestFitness(className, constructor, method)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Pair each method with each other method and add to goals. |
| 59 | + for(String method1: methods){ |
| 60 | + for(String method2: methods){ |
| 61 | + goals.add(new MethodPairTestFitness(className,method1,method2)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return goals; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a list of constructors in the correct format (name + descriptor). |
| 70 | + * Uses reflection to get a list of constructors declared by CUT. |
| 71 | + * For each, produces name and descriptor. |
| 72 | + * @param clazz - class under test |
| 73 | + * @return set of constructors |
| 74 | + */ |
| 75 | + protected Set<String> getUsableConstructors(Class<?> clazz){ |
| 76 | + Set<String> constructors = new LinkedHashSet<>(); |
| 77 | + |
| 78 | + Constructor<?>[] allConstructors = clazz.getDeclaredConstructors(); |
| 79 | + for(Constructor<?> c: allConstructors){ |
| 80 | + if(TestUsageChecker.canUse(c)){ |
| 81 | + String methodName = "<init>"+Type.getConstructorDescriptor(c); |
| 82 | + constructors.add(methodName); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return constructors; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns a list of methods in the correct format (name + descriptor). |
| 91 | + * Uses reflection to get a list of methods declared by CUT. |
| 92 | + * For each, produces name and descriptor. |
| 93 | + * @param clazz - class under test |
| 94 | + * @return set of constructors |
| 95 | + */ |
| 96 | + protected Set<String> getUsableMethods(Class<?> clazz){ |
| 97 | + Set<String> methods = new LinkedHashSet<>(); |
| 98 | + |
| 99 | + Method[] allMethods = clazz.getDeclaredMethods(); |
| 100 | + for(Method m: allMethods){ |
| 101 | + if(TestUsageChecker.canUse(m)){ |
| 102 | + String methodName = m.getName()+Type.getMethodDescriptor(m); |
| 103 | + methods.add(methodName); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return methods; |
| 108 | + } |
| 109 | +} |
0 commit comments