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

org.pitest.coverage.execute.Receive Maven / Gradle / Ivy

There is a newer version: 1.17.1
Show newest version
package org.pitest.coverage.execute;

import org.pitest.classinfo.ClassName;
import org.pitest.coverage.BlockLocation;
import org.pitest.coverage.CoverageResult;
import org.pitest.mutationtest.engine.Location;
import org.pitest.testapi.Description;
import org.pitest.util.Id;
import org.pitest.util.ReceiveStrategy;
import org.pitest.util.SafeDataInputStream;
import sun.pitest.CodeCoverageStore;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

final class Receive implements ReceiveStrategy {

  private final Map     classIdToName = new ConcurrentHashMap<>();
  private final Map    probeToBlock  = new ConcurrentHashMap<>();

  private final Consumer handler;

  Receive(final Consumer handler) {
    this.handler = handler;
  }

  @Override
  public void apply(final byte control, final SafeDataInputStream is) {
    switch (control) {
    case Id.CLAZZ:
      final int id = is.readInt();
      final String name = is.readString();
      this.classIdToName.put(id, ClassName.fromString(name));
      break;
    case Id.PROBES:
      handleProbes(is);
      break;
    case Id.OUTCOME:
      handleTestEnd(is);
      break;
    case Id.DONE:
      // nothing to do ?
    }
  }

  private void handleProbes(final SafeDataInputStream is) {
    final int classId = is.readInt();
    final String methodName = is.readString();
    final String methodSig = is.readString();
    final int first = is.readInt();
    final int last = is.readInt();
    final Location loc = Location.location(this.classIdToName.get(classId),
        methodName, methodSig);
    for (int i = first; i != (last + 1); i++) {
      // nb, convert from classwide id to method scoped index within
      // BlockLocation
      this.probeToBlock.put(CodeCoverageStore.encode(classId, i),
          new BlockLocation(loc, i - first));
    }
  }

  private void handleTestEnd(final SafeDataInputStream is) {
    final Description d = is.read(Description.class);
    final int numberOfResults = is.readInt();

    final Set hits = new HashSet<>(numberOfResults);

    for (int i = 0; i != numberOfResults; i++) {
      readProbeHit(is, hits);
    }

    this.handler.accept(createCoverageResult(is, d, hits));
  }

  private void readProbeHit(final SafeDataInputStream is,
      final Set hits) {
    final long encoded = is.readLong();
    final BlockLocation location = probeToBlock(encoded);
    hits.add(location);
  }

  private BlockLocation probeToBlock(long encoded) {
    return this.probeToBlock.get(encoded);
  }

  private CoverageResult createCoverageResult(final SafeDataInputStream is,
      final Description d, Collection visitedBlocks) {
    final boolean isGreen = is.readBoolean();
    final int executionTime = is.readInt();
    return new CoverageResult(d, executionTime, isGreen,
        visitedBlocks);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy