/** * This methods derive the dependencies between {@link CBranchTestFitness} and branches. * Therefore, it is used to update 'this.dependencies' */ @SuppressWarnings("unchecked") private void addDependencies4CBranch() { logger.debug("Added dependencies for CBranch"); CallGraph callGraph = DependencyAnalysis.getCallGraph(); for (BranchCoverageTestFitness branch : this.dependencies.keySet()) { for (CallContext context : callGraph.getMethodEntryPoint(branch.getClassName(), branch.getMethod())) { CBranchTestFitness cBranch = new CBranchTestFitness(branch.getBranchGoal(), context); this.dependencies.get(branch).add((FitnessFunction<T>) cBranch); logger.debug("Added context branch: " + cBranch.toString()); } } }
@Override public int compareTo(TestFitnessFunction other) { if (other instanceof IBranchTestFitness) { IBranchTestFitness otherBranchFitness = (IBranchTestFitness) other; return branchGoal.compareTo(otherBranchFitness.branchGoal); } else if (other instanceof BranchCoverageTestFitness) { BranchCoverageTestFitness otherBranchFitness = (BranchCoverageTestFitness) other; return branchGoal.compareTo(otherBranchFitness.getBranchGoal()); } return compareClassName(other); // return -1; }
@Override public List<CBranchTestFitness> getCoverageGoals() { //TODO this creates duplicate goals. Momentary fixed using a Set, but it should be optimised Set<CBranchTestFitness> goals = new HashSet<>(); // retrieve set of branches BranchCoverageFactory branchFactory = new BranchCoverageFactory(); List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoals(); CallGraph callGraph = DependencyAnalysis.getCallGraph(); // try to find all occurrences of this branch in the call tree for (BranchCoverageTestFitness branchGoal : branchGoals) { logger.info("Adding context branches for " + branchGoal.toString()); for (CallContext context : callGraph.getMethodEntryPoint(branchGoal.getClassName(), branchGoal.getMethod())) { goals.add(new CBranchTestFitness(branchGoal.getBranchGoal(), context)); } } logger.info("Created " + goals.size() + " goals"); return new ArrayList<CBranchTestFitness>(goals); } }
@Override public List<IBranchTestFitness> getCoverageGoals() { //TODO this creates duplicate goals. Momentary fixed using a Set. Set<IBranchTestFitness> goals = new HashSet<IBranchTestFitness>(); // retrieve set of branches BranchCoverageFactory branchFactory = new BranchCoverageFactory(); List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoalsForAllKnownClasses(); CallGraph callGraph = DependencyAnalysis.getCallGraph(); // try to find all occurrences of this branch in the call tree for (BranchCoverageTestFitness branchGoal : branchGoals) { logger.info("Adding context branches for " + branchGoal.toString()); for (CallContext context : callGraph.getAllContextsFromTargetClass(branchGoal.getClassName(), branchGoal.getMethod())) { //if is not possible to reach this branch from the target class, continue. if(context.isEmpty()) continue; goals.add(new IBranchTestFitness(branchGoal.getBranchGoal(), context)); } } assert(goals.size()>=branchFactory.getCoverageGoals().size()); logger.info("Created " + goals.size() + " goals"); return new ArrayList<IBranchTestFitness>(goals); } }