From 4d144f4fced4c8972b88ffc111f8486896a47e5f Mon Sep 17 00:00:00 2001 From: Irina Iancu Date: Wed, 31 Aug 2016 09:27:43 +0000 Subject: Removing GUAVA Suppliers and Supplier as dependency from junitrunner. Bazel users that are using a different Guava version than the one in the junitrunner jar are getting an IncompatibleClassChangeError. Rewriting parts of junitrunner code so it won't depend on Guava anymore. Continuing progress on issue #1150. -- MOS_MIGRATED_REVID=131807036 --- .../testing/junit/runner/junit4/JUnit4Runner.java | 3 +- .../runner/junit4/JUnit4RunnerBaseModule.java | 7 ++-- .../runner/junit4/JUnit4TestModelBuilder.java | 2 +- .../junit/runner/junit4/JUnit4TestXmlListener.java | 3 +- .../junit/runner/util/MemoizingSupplier.java | 49 ++++++++++++++++++++++ .../google/testing/junit/runner/util/Supplier.java | 31 ++++++++++++++ 6 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/MemoizingSupplier.java create mode 100644 src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/Supplier.java (limited to 'src/java_tools/junitrunner/java/com/google/testing/junit/runner') diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java index 1c67697eab..b288298363 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java @@ -14,12 +14,11 @@ package com.google.testing.junit.runner.junit4; -import com.google.common.base.Supplier; import com.google.testing.junit.junit4.runner.SuiteTrimmingFilter; import com.google.testing.junit.runner.internal.Stdout; import com.google.testing.junit.runner.model.TestSuiteModel; import com.google.testing.junit.runner.util.GoogleTestSecurityManager; - +import com.google.testing.junit.runner.util.Supplier; import org.junit.internal.runners.ErrorReportingRunner; import org.junit.runner.Description; import org.junit.runner.JUnitCore; diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java index 2da89af2bd..395daaffe0 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java @@ -16,14 +16,13 @@ package com.google.testing.junit.runner.junit4; import static com.google.testing.junit.runner.sharding.ShardingFilters.DEFAULT_SHARDING_STRATEGY; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; import com.google.testing.junit.junit4.runner.MemoizingRequest; import com.google.testing.junit.runner.internal.Stdout; import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.SuiteClass; import com.google.testing.junit.runner.model.TestSuiteModel; import com.google.testing.junit.runner.sharding.api.ShardingFilterFactory; - +import com.google.testing.junit.runner.util.MemoizingSupplier; +import com.google.testing.junit.runner.util.Supplier; import dagger.Module; import dagger.Multibindings; import dagger.Provides; @@ -70,7 +69,7 @@ public final class JUnit4RunnerBaseModule { @Provides @Singleton static Supplier provideTestSuiteModelSupplier(JUnit4TestModelBuilder builder) { - return Suppliers.memoize(builder); + return new MemoizingSupplier<>(builder); } @Provides diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java index c0b50e989d..56d57c1d86 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java @@ -14,9 +14,9 @@ package com.google.testing.junit.runner.junit4; -import com.google.common.base.Supplier; import com.google.testing.junit.runner.model.TestSuiteModel; import com.google.testing.junit.runner.model.TestSuiteModel.Builder; +import com.google.testing.junit.runner.util.Supplier; import org.junit.runner.Description; import org.junit.runner.Request; diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java index d76c7f6f0c..5ff9dc24f1 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java @@ -14,11 +14,10 @@ package com.google.testing.junit.runner.junit4; -import com.google.common.base.Supplier; import com.google.testing.junit.runner.internal.SignalHandlers; import com.google.testing.junit.runner.internal.Stderr; import com.google.testing.junit.runner.model.TestSuiteModel; - +import com.google.testing.junit.runner.util.Supplier; import org.junit.Ignore; import org.junit.runner.Description; import org.junit.runner.Result; diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/MemoizingSupplier.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/MemoizingSupplier.java new file mode 100644 index 0000000000..5f01d20ad3 --- /dev/null +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/MemoizingSupplier.java @@ -0,0 +1,49 @@ +// Copyright 2016 The Bazel Authors. All Rights Reserved. +// +// 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.testing.junit.runner.util; + +/** + * Returns a {@link Supplier} which caches the instance retrieved during the first call to + * {@code get()} and returns that value on subsequent calls to {@code get()}. See: + * memoization. + * + *

The returned supplier is thread-safe. The delegate's {@code get()} method will be invoked at + * most once. + * + *

The returned supplier is not serializable. + */ +public class MemoizingSupplier implements Supplier { + private final Supplier delegate; + private volatile boolean initialized; + private T instance; + + public MemoizingSupplier(Supplier delegate) { + this.delegate = delegate; + } + + @Override + public T get() { + if (!initialized) { + synchronized (this) { + if (!initialized) { + initialized = true; + instance = delegate.get(); + } + } + } + return instance; + } +} + diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/Supplier.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/Supplier.java new file mode 100644 index 0000000000..97de3fdb7c --- /dev/null +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/util/Supplier.java @@ -0,0 +1,31 @@ +// Copyright 2016 The Bazel Authors. All Rights Reserved. +// +// 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.testing.junit.runner.util; + + +/** + * A class that can supply objects of a single type. Semantically, this could be a factory, + * generator, builder, closure, or something else entirely. No guarantees are implied by this + * interface. + */ +public interface Supplier { + /** + * Retrieves an instance of the appropriate type. The returned object may or may not be a new + * instance, depending on the implementation. + * + * @return an instance of the appropriate type + */ + T get(); +} \ No newline at end of file -- cgit v1.2.3