From b937ba0f92c549b0627355673eee9661f46b5ede Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Mon, 19 Apr 2021 19:06:55 +0100 Subject: [PATCH] workaroud shading issues --- .../org/pitest/classinfo/HashFunction.java | 0 .../java/org/pitest/classpath/CodeSource.java | 8 +- .../org/pitest/classinfo/ClassInfoMother.java | 41 ------ .../org/pitest/classinfo/ClassInfoTest.java | 124 ------------------ .../org/pitest/classinfo/ClassInfoSource.java | 7 - .../org/pitest/classinfo/NameToClassInfo.java | 20 --- .../pitest/coverage/analysis/LineMapper.java | 8 +- .../coverage/codeassist/LineMapperTest.java | 8 +- 8 files changed, 14 insertions(+), 202 deletions(-) rename {pitest => pitest-entry}/src/main/java/org/pitest/classinfo/HashFunction.java (100%) delete mode 100644 pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoMother.java delete mode 100644 pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoTest.java delete mode 100644 pitest/src/main/java/org/pitest/classinfo/ClassInfoSource.java delete mode 100644 pitest/src/main/java/org/pitest/classinfo/NameToClassInfo.java rename {pitest-entry => pitest}/src/main/java/org/pitest/coverage/analysis/LineMapper.java (87%) rename {pitest-entry => pitest}/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java (95%) diff --git a/pitest/src/main/java/org/pitest/classinfo/HashFunction.java b/pitest-entry/src/main/java/org/pitest/classinfo/HashFunction.java similarity index 100% rename from pitest/src/main/java/org/pitest/classinfo/HashFunction.java rename to pitest-entry/src/main/java/org/pitest/classinfo/HashFunction.java diff --git a/pitest-entry/src/main/java/org/pitest/classpath/CodeSource.java b/pitest-entry/src/main/java/org/pitest/classpath/CodeSource.java index 187cff82d..5faf10e94 100644 --- a/pitest-entry/src/main/java/org/pitest/classpath/CodeSource.java +++ b/pitest-entry/src/main/java/org/pitest/classpath/CodeSource.java @@ -9,6 +9,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.pitest.classinfo.ClassByteArraySource; import org.pitest.classinfo.ClassInfo; import org.pitest.classinfo.ClassInfoSource; import org.pitest.classinfo.ClassName; @@ -21,7 +22,7 @@ /** * Provides access to code and tests on the classpath */ -public class CodeSource implements ClassInfoSource { +public class CodeSource implements ClassInfoSource, ClassByteArraySource { private final ProjectClassPaths classPath; private final Repository classRepository; @@ -75,7 +76,6 @@ public Collection getClassInfo(final Collection classes) { .collect(Collectors.toList()); } - // not used but keep to allow plugins to query bytecode public Optional fetchClassBytes(final ClassName clazz) { return this.classRepository.querySource(clazz); } @@ -90,4 +90,8 @@ private Function> nameToClassInfo() { .andThen(Streams::fromOptional); } + @Override + public Optional getBytes(String clazz) { + return fetchClassBytes(ClassName.fromString(clazz)); + } } diff --git a/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoMother.java b/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoMother.java deleted file mode 100644 index b46b77e33..000000000 --- a/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoMother.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.pitest.classinfo; - -import org.pitest.quickbuilder.Builder; - -public class ClassInfoMother { - - interface ClassInfoBuilderf extends Builder { - ClassInfoBuilder withId(Builder id); - } - - public static ClassInfo make(final ClassIdentifier id) { - return make(id, new DefaultClassPointer(null)); - } - - public static ClassInfo make(final ClassIdentifier id, final ClassInfo parent) { - return make(id, new DefaultClassPointer(parent)); - } - - public static ClassInfo make(final ClassIdentifier id, - final ClassPointer parent) { - final ClassInfoBuilder data = new ClassInfoBuilder(); - data.id = id; - return new ClassInfo(parent, new DefaultClassPointer(null), data); - } - - public static ClassInfo make(final ClassName name) { - return make(new ClassIdentifier(1, name)); - } - - public static ClassInfo make(final ClassName name, final String fileName) { - final DefaultClassPointer parent = new DefaultClassPointer(null); - final ClassInfoBuilder data = new ClassInfoBuilder(); - data.id = new ClassIdentifier(1, name); - data.sourceFile = fileName; - return new ClassInfo(parent, new DefaultClassPointer(null), data); - } - - public static ClassInfo make(final String name) { - return make(new ClassIdentifier(1, ClassName.fromString(name))); - } -} diff --git a/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoTest.java b/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoTest.java deleted file mode 100644 index 1958b3d79..000000000 --- a/pitest-entry/src/test/java/org/pitest/classinfo/ClassInfoTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2010 Henry Coles - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and limitations under the License. - */ -package org.pitest.classinfo; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.objectweb.asm.Opcodes; -import java.util.Optional; - -public class ClassInfoTest { - - private ClassInfoBuilder data; - private ClassInfo testee; - - @Before - public void setUp() { - this.data = new ClassInfoBuilder(); - this.data.id = new ClassIdentifier(1, ClassName.fromString("foo")); - } - - @Test - public void shouldCreateDifferentHierarchicalHashWhenParentClassChanges() { - final ClassInfo parent = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - final ClassInfo classA = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - final ClassInfo classB = new ClassInfo(pointerTo(parent), - emptyClassPointer(), this.data); - - assertFalse(classA.getHierarchicalId().getHierarchicalHash() - .equals(classB.getHierarchicalId().getHierarchicalHash())); - } - - @Test - public void shouldCreateDifferentHierarchicalHashWhenOuterClassChanges() { - final ClassInfo outer = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - final ClassInfo classA = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - final ClassInfo classB = new ClassInfo(emptyClassPointer(), - pointerTo(outer), this.data); - - assertFalse(classA.getHierarchicalId().getHierarchicalHash() - .equals(classB.getHierarchicalId().getHierarchicalHash())); - } - - @Test - public void shouldReportWhenClassIsSynthetic() { - this.data.access = Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC; - final ClassInfo testee = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - assertTrue(testee.isSynthetic()); - } - - @Test - public void shouldReportWhenClassIsNotSynthetic() { - this.data.access = Opcodes.ACC_PUBLIC; - final ClassInfo testee = new ClassInfo(emptyClassPointer(), - emptyClassPointer(), this.data); - assertFalse(testee.isSynthetic()); - } - - private ClassPointer emptyClassPointer() { - return () -> Optional.empty(); - } - - private ClassPointer pointerTo(final ClassInfo ci) { - return () -> Optional.ofNullable(ci); - } - - @Test - public void isCodeLineReturnShouldTrueForCodeLines() { - final List codeLines = Arrays.asList(1, 2, 3, 4, 5, 6, 10); - addCodeLines(codeLines); - makeTestee(); - for (final int each : codeLines) { - assertTrue(this.testee.isCodeLine(each)); - } - } - - @Test - public void isCodeLineShouldReturnFalseForNonCodeLines() { - final List codeLines = Arrays.asList(1); - addCodeLines(codeLines); - makeTestee(); - assertFalse(this.testee.isCodeLine(2)); - } - - @Test - public void matchIfAbstractShouldReturnTrueForAbstractClasses() { - this.data.access = Opcodes.ACC_ABSTRACT; - makeTestee(); - assertTrue(ClassInfo.matchIfAbstract().test(this.testee)); - } - - private void makeTestee() { - this.testee = new ClassInfo(null, null, this.data); - } - - private void addCodeLines(final List lines) { - for (final int each : lines) { - this.data.registerCodeLine(each); - } - } - -} diff --git a/pitest/src/main/java/org/pitest/classinfo/ClassInfoSource.java b/pitest/src/main/java/org/pitest/classinfo/ClassInfoSource.java deleted file mode 100644 index 6c225c49d..000000000 --- a/pitest/src/main/java/org/pitest/classinfo/ClassInfoSource.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.pitest.classinfo; - -import java.util.Optional; - -public interface ClassInfoSource { - Optional fetchClass(ClassName name); -} diff --git a/pitest/src/main/java/org/pitest/classinfo/NameToClassInfo.java b/pitest/src/main/java/org/pitest/classinfo/NameToClassInfo.java deleted file mode 100644 index 89e6da00c..000000000 --- a/pitest/src/main/java/org/pitest/classinfo/NameToClassInfo.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.pitest.classinfo; - -import java.util.function.Function; - -import java.util.Optional; - -public class NameToClassInfo implements Function> { - - private final ClassInfoSource repository; - - public NameToClassInfo(final ClassInfoSource repository) { - this.repository = repository; - } - - @Override - public Optional apply(final ClassName a) { - return this.repository.fetchClass(a); - } - -} \ No newline at end of file diff --git a/pitest-entry/src/main/java/org/pitest/coverage/analysis/LineMapper.java b/pitest/src/main/java/org/pitest/coverage/analysis/LineMapper.java similarity index 87% rename from pitest-entry/src/main/java/org/pitest/coverage/analysis/LineMapper.java rename to pitest/src/main/java/org/pitest/coverage/analysis/LineMapper.java index 40696ed04..2efd7b396 100644 --- a/pitest-entry/src/main/java/org/pitest/coverage/analysis/LineMapper.java +++ b/pitest/src/main/java/org/pitest/coverage/analysis/LineMapper.java @@ -8,8 +8,8 @@ import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; +import org.pitest.classinfo.ClassByteArraySource; import org.pitest.classinfo.ClassName; -import org.pitest.classpath.CodeSource; import org.pitest.coverage.BlockLocation; import org.pitest.coverage.LineMap; import java.util.Optional; @@ -18,9 +18,9 @@ public class LineMapper implements LineMap { - private final CodeSource source; + private final ClassByteArraySource source; - public LineMapper(final CodeSource source) { + public LineMapper(final ClassByteArraySource source) { this.source = source; } @@ -29,7 +29,7 @@ public Map> mapLines(final ClassName clazz) { final Map> map = new HashMap<>(); - final Optional maybeBytes = this.source.fetchClassBytes(clazz); + final Optional maybeBytes = this.source.getBytes(clazz.asInternalName()); // classes generated at runtime eg by mocking frameworks // will be instrumented but not available on the classpath if (maybeBytes.isPresent()) { diff --git a/pitest-entry/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java b/pitest/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java similarity index 95% rename from pitest-entry/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java rename to pitest/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java index c570ec57f..b481276c9 100644 --- a/pitest-entry/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java +++ b/pitest/src/test/java/org/pitest/coverage/codeassist/LineMapperTest.java @@ -1,7 +1,7 @@ package org.pitest.coverage.codeassist; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; import java.util.Map; @@ -11,8 +11,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.pitest.classinfo.ClassByteArraySource; import org.pitest.classinfo.ClassName; -import org.pitest.classpath.CodeSource; import org.pitest.coverage.BlockLocation; import org.pitest.coverage.LineMap; import org.pitest.coverage.analysis.LineMapper; @@ -29,7 +29,7 @@ public class LineMapperTest { @Mock - CodeSource source; + ClassByteArraySource source; @Test public void shouldMapAllLinesWhenMethodContainsSingleBlock() throws Exception { @@ -114,7 +114,7 @@ int foo(int i) { private Map> analyse(Class clazz) throws ClassNotFoundException { - when(this.source.fetchClassBytes(any(ClassName.class))).thenReturn( + when(this.source.getBytes(anyString())).thenReturn( Optional.ofNullable(ClassUtils.classAsBytes(clazz))); final LineMap testee = new LineMapper(this.source); return testee.mapLines(ClassName.fromClass(clazz));