Skip to content

Commit

Permalink
Fix tests that break with Java 17 base on suggestions from @mcculls #…
Browse files Browse the repository at this point in the history
…1536 (comment)

- skip reflection fallback test that only works for java version < 17
- add required jvm flags to mirror maven setup
- upgrade mockito version

PiperOrigin-RevId: 423389524
  • Loading branch information
java-team-github-bot authored and Guice Team committed Jan 21, 2022
1 parent 02c4687 commit c4854d5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ maven_install(
"org.hsqldb", "hsqldb-j5", "2.0.0", testonly = True
),
maven.artifact(
"org.mockito", "mockito-core", "1.9.5", testonly = True
"org.mockito", "mockito-core", "4.2.0", testonly = True
),
],
repositories = [
Expand Down
15 changes: 15 additions & 0 deletions core/test/com/google/inject/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ java_library(

guice_test_suites(
name = "gen_tests",
jvm_flags = [
# those 2 options are required for some tests that checks stack traces
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+ShowHiddenFrames",
],
sizes = [
"small",
"medium",
Expand All @@ -84,6 +89,11 @@ guice_test_suites(
args = [
"--guice_include_stack_traces=%s" % include_stack_trace_option,
],
jvm_flags = [
# those 2 options are required for some tests that checks stack traces
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+ShowHiddenFrames",
],
sizes = [
"small",
"medium",
Expand All @@ -99,6 +109,11 @@ guice_test_suites(
args = [
"--guice_custom_class_loading=%s" % custom_class_loading_option,
],
jvm_flags = [
# those 2 options are required for some tests that checks stack traces
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+ShowHiddenFrames",
],
sizes = [
"small",
"medium",
Expand Down
10 changes: 10 additions & 0 deletions core/test/com/googlecode/guice/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ java_library(

guice_test_suites(
name = "gen_tests",
jvm_flags = [
# those 2 options are required for some tests that checks stack traces
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+ShowHiddenFrames",
],
sizes = [
"small",
"medium",
Expand All @@ -40,6 +45,11 @@ guice_test_suites(
args = [
"--guice_custom_class_loading=%s" % custom_class_loading_option,
],
jvm_flags = [
# those 2 options are required for some tests that checks stack traces
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+ShowHiddenFrames",
],
sizes = [
"small",
"medium",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_library(
"//core/test/com/google/inject:testsupport",
"//extensions/assistedinject/src/com/google/inject/assistedinject",
"//third_party/java/aopalliance",
"//third_party/java/guava/base",
"//third_party/java/guava/collect",
"//third_party/java/jsr330_inject",
"//third_party/java/junit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import com.google.common.base.StandardSystemProperty;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
Expand Down Expand Up @@ -36,7 +37,8 @@
*/
@RunWith(JUnit4.class)
public final class SubpackageTest {

private static final double JAVA_VERSION =
Double.parseDouble(StandardSystemProperty.JAVA_SPECIFICATION_VERSION.value());
private final Logger loggerToWatch = Logger.getLogger(AssistedInject.class.getName());

private final List<LogRecord> logRecords = Lists.newArrayList();
Expand Down Expand Up @@ -144,6 +146,7 @@ protected void configure() {

@Test
public void testReflectionFallbackWorks() throws Exception {
assumeTrue(JAVA_VERSION < 17);
Injector injector =
Guice.createInjector(
new AbstractModule() {
Expand All @@ -167,7 +170,7 @@ protected void configure() {
public void testGeneratedDefaultMethodsForwardCorrectly() throws Exception {
// This test requires above java 1.8.
// 1.8's reflection capability is tested via "testReflectionFallbackWorks".
assumeTrue(Double.parseDouble(System.getProperty("java.specification.version")) > 1.8);
assumeTrue(JAVA_VERSION > 1.8);

setLookupReflection(false);
final Key<AbstractAssisted.Factory<ConcreteAssisted, String>> concreteKey =
Expand Down
2 changes: 1 addition & 1 deletion extensions/persist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
13 changes: 8 additions & 5 deletions test_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ _gen_suite = rule(
implementation = _impl,
)

def guice_test_suites(name, deps, srcs = None, args = [], suffix = "", sizes = None):
def guice_test_suites(name, deps, srcs = None, args = [], suffix = "", sizes = None, jvm_flags = []):
"""
Generates tests for test file in srcs ending in "Test.java"
Expand All @@ -53,17 +53,20 @@ def guice_test_suites(name, deps, srcs = None, args = [], suffix = "", sizes = N
srcs: list of test source files, uses 'glob(["**/*Test.java"])' if not specified
deps: list of runtime dependencies requried to run the test
args: list of flags to pass to the test
jvm_flags: list of JVM flags to pass to the test
suffix: suffix to apend to the generated test name
sizes: not used, exists only so that the opensource guice_test_suites mirror exactly the internal one
"""
jvm_flags = []

flags = []
flags.extend(jvm_flags)

# transform flags to JVM options used externally
for arg in args:
if arg.startswith("--"):
jvm_flags.append(arg.replace("--", "-D"))
flags.append(arg.replace("--", "-D"))
else:
jvm_flags.append(arg)
flags.append(arg)

package_name = native.package_name()

Expand All @@ -89,7 +92,7 @@ def guice_test_suites(name, deps, srcs = None, args = [], suffix = "", sizes = N
native.java_test(
name = "AllTestsSuite" + suffix,
test_class = (package_name + "/" + suite_name).replace("/", "."),
jvm_flags = jvm_flags,
jvm_flags = flags,
srcs = [":" + suite_name],
deps = deps + [
"//third_party/java/junit",
Expand Down

0 comments on commit c4854d5

Please sign in to comment.