lundi 30 décembre 2019

Java library for command line Code Coverage calculation?

I am writing a Java program (using swing UI) that takes a java program (.java file with main function) and an excel file of large number of test cases as input.

It then uses Genetic Algorithm to return a limited size small subset of test cases that are best according to a criteria.

On of these criteria is Code Coverage.

I can easily write a fitness function that runs a command in cli and reads output. The only problem is that I need a tool that runs in command line and returns the value of code coverage (of bytecode or any other level). My fitness function will read this code coverage value and then try to maximize it for the entire subset.

Bottom line:- I need a tool to check code coverage of *.java files.

There are 2 classes in my case:-

TestingClass.java

import interest_calculator.CompoundInterest;

public class TestingClass {

    static CompoundInterest obj;
    public static final int ARGS_COUNT = 3;

    public static void main( String[] args ) {
        initializeClass();
        setValues( args );
        printResult();
    }

    private static void initializeClass() {
        obj = new CompoundInterest();
    }

    private static void setValues( String[] args ) {
        obj.principle = Double.parseDouble(args[0]);
        obj.rate = Double.parseDouble(args[1]);
        obj.time = Double.parseDouble(args[2]);
    }

    private static void printResult() {
        System.out.println( obj.calcCompoundInterest() );
    }

}

CompoundInterest.java (in a subfolder "interest_calculator"):-

package interest_calculator;

public class CompoundInterest {

    public double principle;
    public double rate;
    public double time;

    public double calcCompoundInterest() {
        double ci = principle * ( java.lang.Math.pow((1 + rate / 100), time) );
        ci = round( ci, 2 );
        return ci;
    }

    public double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }

}

I need a tool that lets me call TestingClass from commandline using three commandline arguments and returns me code coverage of any kind (Literally Any Kind). I just need to demonstrate working of a Genetic Algorithm for code coverage.

Is there any suh tool. And how exactly do I use that tool for calculating code coverage with commandline arguments to my program.

The genetic algorithm is working for other criteria by the way. I am using Jenetics library for this purpose.

Aucun commentaire:

Enregistrer un commentaire