All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.coverage.CoveragePercentage Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.coverage;

import javax.annotation.*;

public final class CoveragePercentage
{
   private CoveragePercentage() {}

   public static int calculate(int coveredCount, int totalCount)
   {
      if (totalCount <= 0) {
         return -1;
      }

      //noinspection NumericCastThatLosesPrecision
      return (int) (100.0 * coveredCount / totalCount + 0.5);
   }

   @Nonnull
   public static String percentageColor(int coveredCount, int totalCount)
   {
      if (coveredCount == 0) {
         return "ff0000";
      }
      else if (coveredCount == totalCount) {
         return "00ff00";
      }

      double percentage = 100.0 * coveredCount / totalCount;
      //noinspection NumericCastThatLosesPrecision
      int green = (int) (0xFF * percentage / 100 + 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, int rgb)
   {
      String hex = Integer.toHexString(rgb);

      if (hex.length() == 1) {
         colorInHexa.append('0');
      }

      colorInHexa.append(hex);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy