mockit.coverage.CoveragePercentage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external
APIs; JUnit (4 & 5) and TestNG test runners are supported.
It also contains an advanced code coverage tool.
The newest version!
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.checkerframework.checker.index.qual.NonNegative;
public final class CoveragePercentage {
private CoveragePercentage() {
}
public static int calculate(@NonNegative int coveredCount, @NonNegative int totalCount) {
if (totalCount == 0) {
return -1;
}
// noinspection NumericCastThatLosesPrecision
return (int) (100.0 * coveredCount / totalCount + 0.5);
}
@NonNull
public static String percentageColor(@NonNegative int coveredCount, @NonNegative int totalCount) {
if (coveredCount == 0) {
return "ff0000";
}
if (coveredCount == totalCount) {
return "00ff00";
}
double percentage = 100.0 * coveredCount / totalCount;
// noinspection NumericCastThatLosesPrecision
int green = (int) (0xFF * percentage / 100.0 + 0.5);
int red = 0xFF - green;
StringBuilder color = new StringBuilder(6);
appendColorInHexadecimal(color, red);
appendColorInHexadecimal(color, green);
color.append("00");
return color.toString();
}
private static void appendColorInHexadecimal(@NonNull StringBuilder colorInHexa, @NonNegative int rgb) {
String hex = Integer.toHexString(rgb);
if (hex.length() == 1) {
colorInHexa.append('0');
}
colorInHexa.append(hex);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy