Skip to content

Commit

Permalink
Coverage: fixed case where classes reloaded from a third-party jar fi…
Browse files Browse the repository at this point in the history
…le got erroneously modified for coverage.
  • Loading branch information
rliesenfeld committed Jul 17, 2015
1 parent cd41e70 commit 1db8166
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
21 changes: 15 additions & 6 deletions coverage/src/mockit/coverage/modification/ClassSelection.java
Expand Up @@ -99,11 +99,14 @@ boolean isSelected(@Nonnull String className, @Nonnull ProtectionDomain protecti

// It's from a custom class loader, so it may exist in the classpath.
String classFileName = className.replace('.', '/') + ".class";
URL classFile = THIS_CLASS_LOADER.getResource(classFileName);
return classFile != null;
codeSourceLocation = THIS_CLASS_LOADER.getResource(classFileName);

if (codeSourceLocation == null) {
return false;
}
}

return !isClassFromExternalLibrary(codeSourceLocation.getPath());
return !isClassFromExternalLibrary(codeSourceLocation);
}

private static boolean isIneligibleForSelection(@Nonnull String className)
Expand Down Expand Up @@ -144,10 +147,16 @@ else if (classesToInclude != null) {
return true;
}

private boolean isClassFromExternalLibrary(@Nonnull String location)
private boolean isClassFromExternalLibrary(@Nonnull URL location)
{
if ("jar".equals(location.getProtocol())) {
return true;
}

String path = location.getPath();

return
location.endsWith(".jar") || location.endsWith("/.cp/") ||
testCode != null && (location.endsWith("/test-classes/") || location.endsWith("/jmockit1.org/main/classes/"));
path.endsWith(".jar") || path.endsWith("/.cp/") ||
testCode != null && (path.endsWith("/test-classes/") || path.endsWith("/jmockit1.org/main/classes/"));
}
}
4 changes: 2 additions & 2 deletions coverageTests/test/integrationTests/CoverageTest.java
Expand Up @@ -26,7 +26,7 @@ public class CoverageTest
private int currentPathIndex = -1;

@Before
public void findCoverageData() throws Exception
public final void findCoverageData() throws Exception
{
Field testedField = getClass().getDeclaredField("tested");
Class<?> testedClass = testedField.getType();
Expand Down Expand Up @@ -252,7 +252,7 @@ protected static void assertInstanceFieldUncovered(@Nonnull String fieldName)
assertFalse("Instance field " + fieldName + " should not be covered", isInstanceFieldCovered(fieldName));
}

protected void assertInstanceFieldUncovered(@Nonnull String fieldName, @Nonnull Object... uncoveredInstances)
protected static void assertInstanceFieldUncovered(@Nonnull String fieldName, @Nonnull Object... uncoveredInstances)
{
String msg = "Instance field " + fieldName + " should not be covered";
InstanceFieldData fieldData = getInstanceFieldData(fieldName);
Expand Down
15 changes: 15 additions & 0 deletions coverageTests/test/integrationTests/CustomClassLoadingTest.java
Expand Up @@ -4,10 +4,16 @@
*/
package integrationTests;

import java.util.*;

import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;

import mockit.coverage.data.*;

import org.hamcrest.*;

@RunWith(CustomRunner.class)
public final class CustomClassLoadingTest extends CoverageTest
{
Expand Down Expand Up @@ -59,4 +65,13 @@ public void exerciseClassThatIsOnlyUsedHere()
assertPaths(1, 1, 1);
assertPath(2, 1);
}

@Test
public void exerciseClassFromNonJREJarFile()
{
CoreMatchers.anything();

Map<String, FileCoverageData> fileToFileData = CoverageData.instance().getRawFileToFileData();
assertFalse(fileToFileData.containsKey("org/hamcrest/CoreMatchers.java"));
}
}

0 comments on commit 1db8166

Please sign in to comment.