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

mockit.coverage.reporting.dataCoverage.DataCoverageOutput Maven / Gradle / Ivy

Go to download

JMockit Coverage is a code coverage tool with several metrics (line, path, data) capable of generating HTML reports. It is designed with ease of use in mind, avoiding the need for complex configuration. Instead, smart (but overridable) defaults are employed, such as the selection of which classes to consider for coverage, and where to find sources files for report generation.

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

import javax.annotation.*;

import mockit.coverage.dataItems.*;
import mockit.coverage.reporting.parsing.*;

public final class DataCoverageOutput
{
   @Nonnull private final StringBuilder openingTag;
   @Nonnull private final PerFileDataCoverage coverageInfo;
   private int nextField;
   @Nullable private String classAndFieldNames;
   @Nullable private String className;
   @Nullable private String fieldName;

   public DataCoverageOutput(@Nonnull PerFileDataCoverage coverageInfo)
   {
      openingTag = new StringBuilder(50);
      this.coverageInfo = coverageInfo;
      moveToNextField();
   }

   private void moveToNextField()
   {
      if (nextField >= coverageInfo.allFields.size()) {
         classAndFieldNames = null;
         className = null;
         fieldName = null;
         return;
      }

      classAndFieldNames = coverageInfo.allFields.get(nextField);
      nextField++;

      int p = classAndFieldNames.indexOf('.');
      className = classAndFieldNames.substring(0, p);
      fieldName = classAndFieldNames.substring(p + 1);
   }

   public void writeCoverageInfoIfLineStartsANewFieldDeclaration(@Nonnull FileParser fileParser)
   {
      if (classAndFieldNames != null) {
         assert className != null;

         if (className.equals(fileParser.getCurrentlyPendingClass())) {
            LineElement initialLineElement = fileParser.lineParser.getInitialElement();

            assert fieldName != null;
            LineElement elementWithFieldName = initialLineElement.findWord(fieldName);

            if (elementWithFieldName != null) {
               buildOpeningTagForFieldWrapper();
               elementWithFieldName.wrapText(openingTag.toString(), "");
               moveToNextField();
            }
         }
      }
   }

   private void buildOpeningTagForFieldWrapper()
   {
      openingTag.setLength(0);
      openingTag.append("");
   }

   private void appendAccessCounts(@Nonnull FieldData fieldData)
   {
      openingTag.append("Reads: ").append(fieldData.getReadCount());
      openingTag.append(" Writes: ").append(fieldData.getWriteCount());
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy