Skip to content

Commit

Permalink
empty filter in resources does not work with java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Mar 10, 2022
1 parent fe16792 commit c8e8088
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.emptyreturns;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Optional;

public class AlreadyReturnsEmptyOptionalInTryWithResourcesBlock {
public Optional<String> a() throws IOException {
try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Double.parseDouble("12");
if (os.size() > 42) {
return Optional.empty();
}
return Optional.of("foo");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.pitest.mutationtest.build.intercept.equivalent;

import com.example.emptyreturns.AlreadyReturnsEmptyOptionalInTryWithResourcesBlock;
import org.junit.Ignore;
import org.junit.Test;
import org.pitest.mutationtest.build.InterceptorType;
import org.pitest.mutationtest.build.MutationInterceptor;
import org.pitest.mutationtest.build.intercept.javafeatures.FilterTester;
import org.pitest.mutationtest.engine.gregor.mutators.returns.EmptyObjectReturnValsMutator;
import org.pitest.mutationtest.engine.gregor.mutators.returns.NullReturnValsMutator;
import org.pitest.mutationtest.engine.gregor.mutators.returns.PrimitiveReturnsMutator;
import org.pitest.util.CurrentRuntime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -19,14 +20,15 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.pitest.mutationtest.engine.gregor.mutators.returns.BooleanFalseReturnValsMutator.FALSE_RETURNS;
import static org.pitest.mutationtest.engine.gregor.mutators.returns.BooleanTrueReturnValsMutator.TRUE_RETURNS;

public class EquivalentReturnMutationFilterTest {

MutationInterceptor testee = new EquivalentReturnMutationFilter().createInterceptor(null);

FilterTester verifier = new FilterTester("", this.testee, PrimitiveReturnsMutator.PRIMITIVE_RETURNS
FilterTester verifier = new FilterTester("emptyReturns/{0}_{1}", this.testee, PrimitiveReturnsMutator.PRIMITIVE_RETURNS
, EmptyObjectReturnValsMutator.EMPTY_RETURNS
, NullReturnValsMutator.NULL_RETURNS
, FALSE_RETURNS
Expand Down Expand Up @@ -199,9 +201,19 @@ public void filtersEquivalentOptionalMutantsInTryBlocks() {

@Test
public void filtersEquivalentOptionalMutantsInTryWithResourcesBlocks() {
// skip test if we are running/compiling with java 8 as out analysis can't yet
// handle the bytecode
assumeTrue(CurrentRuntime.version() >= 9);
verifier.assertFiltersNMutationFromClass(1, AlreadyReturnsEmptyOptionalInTryWithResourcesBlock.class);
}

@Test
@Ignore("need more complex analysis")
public void filtersEquivalentOptionalMutantsInTryWithResourcesBlocksForOtherCompilers() {
// javac sample is for java 8
verifier.assertFiltersNMutationFromSample(1, "AlreadyReturnsEmptyOptionalInTryWithResourcesBlock");
}

@Test
public void filtersEquivalentStreamMutants() {
verifier.assertFiltersNMutationFromClass(1, AlreadyReturnsEmptyStream.class);
Expand Down Expand Up @@ -435,19 +447,6 @@ public Optional<String> a() {
}
}

class AlreadyReturnsEmptyOptionalInTryWithResourcesBlock {
public Optional<String> a() throws IOException {
try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Double.parseDouble("12");
if (os.size() > 42) {
return Optional.empty();
}
return Optional.of("foo");
}
}

}

class AlreadyReturnsEmptyStream {
public Stream<String> a() {
return Stream.empty();
Expand Down
17 changes: 17 additions & 0 deletions pitest-entry/src/test/java/org/pitest/util/CurrentRuntime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.pitest.util;

public class CurrentRuntime {

public static int version() {
String version = System.getProperty("java.version").replace("-ea", "");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
}
Binary file not shown.

0 comments on commit c8e8088

Please sign in to comment.