-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to reset Objenesis and detect classloader issues
- Loading branch information
Showing
10 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/nl/jqno/equalsverifier/internal/util/ObjenesisWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package nl.jqno.equalsverifier.internal.util; | ||
|
||
import org.objenesis.Objenesis; | ||
import org.objenesis.ObjenesisStd; | ||
|
||
/** | ||
* A wrapper around Objenesis. Objenesis keeps caches of objects it has instantiated, so we want a | ||
* way to easily re-use the same instance of `Objenesis`. This class reflects the usage in | ||
* {@link org.objenesis.ObjenesisHelper}, but with the added benefit that now we can reset the | ||
* caches if needed (for instance if the test framework used does some "clever" tricks with | ||
* ClassLoaders) by re-initializing the Objenesis instance. | ||
* | ||
* Note: I realise that a wrapper around a static reference is not very architecturally sound; | ||
* however, doing it properly would require major re-writes. Maybe some other time. | ||
*/ | ||
public final class ObjenesisWrapper { | ||
|
||
private static Objenesis objenesis = new ObjenesisStd(); | ||
|
||
private ObjenesisWrapper() {} | ||
|
||
public static Objenesis getObjenesis() { | ||
return objenesis; | ||
} | ||
|
||
public static void reset() { | ||
objenesis = new ObjenesisStd(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/nl/jqno/equalsverifier/integration/operational/WithResetCachesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package nl.jqno.equalsverifier.integration.operational; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.internal.util.ObjenesisWrapper; | ||
import nl.jqno.equalsverifier.testhelpers.packages.correct.A; | ||
import nl.jqno.equalsverifier.testhelpers.packages.correct.B; | ||
import org.junit.jupiter.api.Test; | ||
import org.objenesis.Objenesis; | ||
|
||
public class WithResetCachesTest { | ||
|
||
@Test | ||
public void single() { | ||
Objenesis original = ObjenesisWrapper.getObjenesis(); | ||
EqualsVerifier.forClass(A.class).withResetCaches(); | ||
Objenesis reset = ObjenesisWrapper.getObjenesis(); | ||
assertNotEquals(original, reset); | ||
} | ||
|
||
@Test | ||
public void multiple() { | ||
Objenesis original = ObjenesisWrapper.getObjenesis(); | ||
EqualsVerifier.forClasses(A.class, B.class).withResetCaches(); | ||
Objenesis reset = ObjenesisWrapper.getObjenesis(); | ||
assertNotEquals(original, reset); | ||
} | ||
|
||
@Test | ||
public void configured() { | ||
Objenesis original = ObjenesisWrapper.getObjenesis(); | ||
EqualsVerifier.configure().withResetCaches(); | ||
Objenesis reset = ObjenesisWrapper.getObjenesis(); | ||
assertNotEquals(original, reset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters