Skip to content

Commit

Permalink
Extract the one test that requires add_opens to a separate file, run …
Browse files Browse the repository at this point in the history
…it with add_opens in the one classloading strategy that requires it. Also run it with other strategies/defaults w/o add_opens (in scenarios where it doesn't require it).

PiperOrigin-RevId: 475929909
  • Loading branch information
sameb authored and Guice Team committed Sep 21, 2022
1 parent dd76086 commit 8a38aa9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
1 change: 1 addition & 0 deletions core/test/com/google/inject/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static Test suite() {
suite.addTestSuite(EagerSingletonTest.class);
suite.addTestSuite(GenericInjectionTest.class);
suite.addTestSuite(ImplicitBindingTest.class);
suite.addTestSuite(ImplicitBindingJdkPackagePrivateTest.class);
suite.addTestSuite(InjectorTest.class);
suite.addTestSuite(JitBindingsTest.class);
suite.addTestSuite(Java8LanguageFeatureBindingTest.class);
Expand Down
38 changes: 33 additions & 5 deletions core/test/com/google/inject/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ TEST_SUPPORT_SRCS = [
"internal/WeakKeySetUtils.java",
]

ADD_OPENS_SRCS = [
"ImplicitBindingJdkPackagePrivateTest.java",
]

# Files that are shared by extensions also.
# Typically this would go in a java/../testing package,
# but we need to work with the open-source code structure,
Expand All @@ -37,7 +41,7 @@ java_library(
name = "tests",
srcs = glob(
["**/*.java"], # glob ignores subfolder that has its own BUILD files
exclude = TEST_SUPPORT_SRCS + [
exclude = TEST_SUPPORT_SRCS + ADD_OPENS_SRCS + [
"AllTests.java",
],
),
Expand All @@ -59,12 +63,27 @@ java_library(
],
)

java_library(
name = "add_opens_tests",
srcs = ADD_OPENS_SRCS,
javacopts = ["-Xep:BetaApi:OFF"],
visibility = [
"//:src",
],
deps = [
"//core/src/com/google/inject",
"//third_party/java/guava/base",
"//third_party/java/junit",
],
)

# This target is unused, but exists so that we can more easily
# ensure the opensource build maintains compiling code.
java_library(
name = "AllTests",
srcs = ["AllTests.java"],
deps = [
":add_opens_tests",
":tests",
"//core/test/com/googlecode/guice:tests",
"//third_party/java/junit",
Expand All @@ -82,7 +101,10 @@ guice_test_suites(
"small",
"medium",
],
deps = [":tests"],
deps = [
":add_opens_tests",
":tests",
],
)

[guice_test_suites(
Expand All @@ -100,7 +122,10 @@ guice_test_suites(
"medium",
],
suffix = "_stack_trace_%s" % include_stack_trace_option,
deps = [":tests"],
deps = [
":add_opens_tests",
":tests",
],
) for include_stack_trace_option in [
"OFF",
]]
Expand All @@ -120,9 +145,12 @@ guice_test_suites(
"medium",
],
suffix = "_custom_class_loading_%s" % custom_class_loading_option,
deps = [":tests"],
deps = [
":add_opens_tests",
":tests",
],
) for custom_class_loading_option in [
"OFF",
"CHILD",
"ANONYMOUS",
"CHILD",
]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2022 Google Inc.
*
* 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 com.google.inject;

import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;

import java.net.InetAddress;
import junit.framework.TestCase;

/**
* This test is split out from ImplicitBindingTest so that we can run it with --add_opens to
* InetAddress, so validate that it will work when opened.
*/
public class ImplicitBindingJdkPackagePrivateTest extends TestCase {

public void testImplicitJdkBindings_packagePrivateCxtor() {
Injector injector = Guice.createInjector();
// Validate that, when the JDK allows it, we can construct package private JDK things.
if (Double.parseDouble(JAVA_SPECIFICATION_VERSION.value()) < 17) {
assertNotNull(injector.getInstance(InetAddress.class));
}
}
}
20 changes: 8 additions & 12 deletions core/test/com/google/inject/ImplicitBindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package com.google.inject;

import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.Iterables;
import com.google.inject.internal.Annotations;
import com.google.inject.name.Names;
import com.google.inject.spi.Message;
import java.net.InetAddress;
import java.util.List;
import junit.framework.TestCase;

/** @author crazybob@google.com (Bob Lee) */
/**
* @author crazybob@google.com (Bob Lee)
*/
public class ImplicitBindingTest extends TestCase {

public void testCircularDependency() throws CreationException {
Expand Down Expand Up @@ -322,10 +322,10 @@ public String getValue() {
* sequence of JIT bindings:
*
* <ol>
* <li> A-> B
* <li> B -> C, A
* <li> C -> A, D
* <li> D not JITable
* <li>A-> B
* <li>B -> C, A
* <li>C -> A, D
* <li>D not JITable
* </ol>
*
* <p>The problem was that C cleaned up A's binding and then handed control back to B, which tried
Expand Down Expand Up @@ -434,13 +434,9 @@ enum EnumWithImplementedBy {}

private static class EnumWithImplementedByEnum {}

public void testImplicitJdkBindings() {
public void testImplicitJdkBindings_publicCxtor() {
Injector injector = Guice.createInjector();
// String has a public nullary constructor, so Guice will call it.
assertEquals("", injector.getInstance(String.class));
// InetAddress has a package private constructor. We probably shouldn't be calling it :(
if (Double.parseDouble(JAVA_SPECIFICATION_VERSION.value()) < 17) {
assertNotNull(injector.getInstance(InetAddress.class));
}
}
}

0 comments on commit 8a38aa9

Please sign in to comment.