diff options
Diffstat (limited to 'guava-tests/test/com/google/common/base')
12 files changed, 95 insertions, 138 deletions
diff --git a/guava-tests/test/com/google/common/base/ConverterTest.java b/guava-tests/test/com/google/common/base/ConverterTest.java index c787ef004..87e93282b 100644 --- a/guava-tests/test/com/google/common/base/ConverterTest.java +++ b/guava-tests/test/com/google/common/base/ConverterTest.java @@ -19,6 +19,7 @@ package com.google.common.base; import static com.google.common.base.Functions.toStringFunction; import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.primitives.Longs; @@ -29,7 +30,7 @@ import java.util.List; import junit.framework.TestCase; /** Unit tests for {@link Converter}. */ -@GwtCompatible +@GwtCompatible(emulated = true) public class ConverterTest extends TestCase { private static final Converter<String, Long> STR_TO_LONG = @@ -111,6 +112,7 @@ public class ConverterTest extends TestCase { } } + @GwtIncompatible // J2CL generics problem public void testAndThen() { Converter<StringWrapper, String> first = new Converter<StringWrapper, String>() { @@ -140,6 +142,7 @@ public class ConverterTest extends TestCase { assertEquals(first.andThen(STR_TO_LONG), first.andThen(STR_TO_LONG)); } + @GwtIncompatible // J2CL generics problem public void testIdentityConverter() { Converter<String, String> stringIdentityConverter = Converter.identity(); @@ -213,6 +216,7 @@ public class ConverterTest extends TestCase { SerializableTester.reserializeAndAssert(reverseConverter); } + @GwtIncompatible // J2CL generics problem public void testSerialization_andThen() { Converter<String, Long> converterA = Longs.stringConverter(); Converter<Long, String> reverseConverter = Longs.stringConverter().reverse(); diff --git a/guava-tests/test/com/google/common/base/DefaultsTest.java b/guava-tests/test/com/google/common/base/DefaultsTest.java index 7b990ba52..06b3c5da3 100644 --- a/guava-tests/test/com/google/common/base/DefaultsTest.java +++ b/guava-tests/test/com/google/common/base/DefaultsTest.java @@ -16,6 +16,7 @@ package com.google.common.base; +import com.google.common.annotations.GwtIncompatible; import junit.framework.TestCase; /** @@ -23,6 +24,7 @@ import junit.framework.TestCase; * * @author Jige Yu */ +@GwtIncompatible public class DefaultsTest extends TestCase { public void testGetDefaultValue() { assertEquals(false, Defaults.defaultValue(boolean.class).booleanValue()); diff --git a/guava-tests/test/com/google/common/base/EnumsTest.java b/guava-tests/test/com/google/common/base/EnumsTest.java index 66dfe591a..df821d361 100644 --- a/guava-tests/test/com/google/common/base/EnumsTest.java +++ b/guava-tests/test/com/google/common/base/EnumsTest.java @@ -19,6 +19,7 @@ package com.google.common.base; import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH; import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; @@ -129,11 +130,7 @@ public class EnumsTest extends TestCase { @GwtIncompatible // stringConverter public void testStringConverter_convertError() { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); - try { - converter.convert("xxx"); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> converter.convert("xxx")); } @GwtIncompatible // stringConverter diff --git a/guava-tests/test/com/google/common/base/FinalizableReferenceQueueTest.java b/guava-tests/test/com/google/common/base/FinalizableReferenceQueueTest.java index 08ead4365..8bddca227 100644 --- a/guava-tests/test/com/google/common/base/FinalizableReferenceQueueTest.java +++ b/guava-tests/test/com/google/common/base/FinalizableReferenceQueueTest.java @@ -16,6 +16,7 @@ package com.google.common.base; +import com.google.common.annotations.GwtIncompatible; import com.google.common.base.internal.Finalizer; import com.google.common.testing.GcFinalization; import java.lang.ref.ReferenceQueue; @@ -36,6 +37,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; // - .class files aren't available // - possibly no real concept of separate ClassLoaders? @AndroidIncompatible +@GwtIncompatible public class FinalizableReferenceQueueTest extends TestCase { private @Nullable FinalizableReferenceQueue frq; diff --git a/guava-tests/test/com/google/common/base/PackageSanityTests.java b/guava-tests/test/com/google/common/base/PackageSanityTests.java index f524fbb6c..c9eeed0de 100644 --- a/guava-tests/test/com/google/common/base/PackageSanityTests.java +++ b/guava-tests/test/com/google/common/base/PackageSanityTests.java @@ -16,10 +16,12 @@ package com.google.common.base; +import com.google.common.annotations.GwtIncompatible; import com.google.common.testing.AbstractPackageSanityTests; /** Basic sanity tests for classes in {@code common.base}. */ +@GwtIncompatible public class PackageSanityTests extends AbstractPackageSanityTests { public PackageSanityTests() { // package private classes like FunctionalEquivalence are tested through the public API. diff --git a/guava-tests/test/com/google/common/base/PreconditionsTest.java b/guava-tests/test/com/google/common/base/PreconditionsTest.java index 6209d3a06..acbd7cfd1 100644 --- a/guava-tests/test/com/google/common/base/PreconditionsTest.java +++ b/guava-tests/test/com/google/common/base/PreconditionsTest.java @@ -17,6 +17,7 @@ package com.google.common.base; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -397,12 +398,11 @@ public class PreconditionsTest extends TestCase { checkArgumentMethod.invoke(null /* static method */, getParametersForSignature(true, sig)); Object[] failingParams = getParametersForSignature(false, sig); - try { - checkArgumentMethod.invoke(null /* static method */, failingParams); - fail(); - } catch (InvocationTargetException ite) { - assertFailureCause(ite.getCause(), IllegalArgumentException.class, failingParams); - } + InvocationTargetException ite = + assertThrows( + InvocationTargetException.class, + () -> checkArgumentMethod.invoke(null /* static method */, failingParams)); + assertFailureCause(ite.getCause(), IllegalArgumentException.class, failingParams); } } @@ -415,12 +415,11 @@ public class PreconditionsTest extends TestCase { checkArgumentMethod.invoke(null /* static method */, getParametersForSignature(true, sig)); Object[] failingParams = getParametersForSignature(false, sig); - try { - checkArgumentMethod.invoke(null /* static method */, failingParams); - fail(); - } catch (InvocationTargetException ite) { - assertFailureCause(ite.getCause(), IllegalStateException.class, failingParams); - } + InvocationTargetException ite = + assertThrows( + InvocationTargetException.class, + () -> checkArgumentMethod.invoke(null /* static method */, failingParams)); + assertFailureCause(ite.getCause(), IllegalStateException.class, failingParams); } } @@ -434,12 +433,11 @@ public class PreconditionsTest extends TestCase { null /* static method */, getParametersForSignature(new Object(), sig)); Object[] failingParams = getParametersForSignature(null, sig); - try { - checkArgumentMethod.invoke(null /* static method */, failingParams); - fail(); - } catch (InvocationTargetException ite) { - assertFailureCause(ite.getCause(), NullPointerException.class, failingParams); - } + InvocationTargetException ite = + assertThrows( + InvocationTargetException.class, + () -> checkArgumentMethod.invoke(null /* static method */, failingParams)); + assertFailureCause(ite.getCause(), NullPointerException.class, failingParams); } } diff --git a/guava-tests/test/com/google/common/base/PredicatesTest.java b/guava-tests/test/com/google/common/base/PredicatesTest.java index 24b4e108b..ed017d00f 100644 --- a/guava-tests/test/com/google/common/base/PredicatesTest.java +++ b/guava-tests/test/com/google/common/base/PredicatesTest.java @@ -18,6 +18,7 @@ package com.google.common.base; import static com.google.common.base.CharMatcher.whitespace; import static com.google.common.collect.Lists.newArrayList; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -623,11 +624,7 @@ public class PredicatesTest extends TestCase { assertTrue(isInteger.apply(Integer.class)); assertFalse(isInteger.apply(Float.class)); - try { - isInteger.apply(null); - fail(); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> isInteger.apply(null)); } @J2ktIncompatible diff --git a/guava-tests/test/com/google/common/base/SplitterTest.java b/guava-tests/test/com/google/common/base/SplitterTest.java index ed14a8f6f..2f9ea2532 100644 --- a/guava-tests/test/com/google/common/base/SplitterTest.java +++ b/guava-tests/test/com/google/common/base/SplitterTest.java @@ -18,6 +18,7 @@ package com.google.common.base; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -471,11 +472,7 @@ public class SplitterTest extends TestCase { @J2ktIncompatible @GwtIncompatible // java.util.regex.Pattern public void testPatternSplitInvalidPattern() { - try { - Splitter.on(Pattern.compile("a*")); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Splitter.on(Pattern.compile("a*"))); } @J2ktIncompatible diff --git a/guava-tests/test/com/google/common/base/StandardSystemPropertyTest.java b/guava-tests/test/com/google/common/base/StandardSystemPropertyTest.java index 3a88366c0..f4907a337 100644 --- a/guava-tests/test/com/google/common/base/StandardSystemPropertyTest.java +++ b/guava-tests/test/com/google/common/base/StandardSystemPropertyTest.java @@ -20,6 +20,7 @@ import static com.google.common.base.StandardSystemProperty.JAVA_COMPILER; import static com.google.common.base.StandardSystemProperty.JAVA_EXT_DIRS; import static com.google.common.truth.Truth.assertWithMessage; +import com.google.common.annotations.GwtIncompatible; import junit.framework.TestCase; /** @@ -27,6 +28,7 @@ import junit.framework.TestCase; * * @author Kurt Alfred Kluever */ +@GwtIncompatible public class StandardSystemPropertyTest extends TestCase { public void testGetKeyMatchesString() { diff --git a/guava-tests/test/com/google/common/base/SuppliersTest.java b/guava-tests/test/com/google/common/base/SuppliersTest.java index 9befacc6f..fef3a2684 100644 --- a/guava-tests/test/com/google/common/base/SuppliersTest.java +++ b/guava-tests/test/com/google/common/base/SuppliersTest.java @@ -18,6 +18,7 @@ package com.google.common.base; import static com.google.common.testing.SerializableTester.reserialize; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -141,12 +142,8 @@ public class SuppliersTest extends TestCase { .isEqualTo("Suppliers.memoize(<supplier that returned 10>)"); // Should get an exception when we try to serialize. - try { - reserialize(memoizedSupplier); - fail(); - } catch (RuntimeException ex) { - assertThat(ex).hasCauseThat().isInstanceOf(java.io.NotSerializableException.class); - } + RuntimeException ex = assertThrows(RuntimeException.class, () -> reserialize(memoizedSupplier)); + assertThat(ex).hasCauseThat().isInstanceOf(java.io.NotSerializableException.class); } @J2ktIncompatible diff --git a/guava-tests/test/com/google/common/base/ThrowablesTest.java b/guava-tests/test/com/google/common/base/ThrowablesTest.java index 975765340..194b435a3 100644 --- a/guava-tests/test/com/google/common/base/ThrowablesTest.java +++ b/guava-tests/test/com/google/common/base/ThrowablesTest.java @@ -25,6 +25,7 @@ import static com.google.common.base.Throwables.throwIfUnchecked; import static com.google.common.truth.Truth.assertThat; import static java.util.Arrays.asList; import static java.util.regex.Pattern.quote; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -100,11 +101,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the unchecked exception to propagate as-is - try { - sample.noneDeclared(); - fail(); - } catch (SomeUncheckedException expected) { - } + assertThrows(SomeUncheckedException.class, () -> sample.noneDeclared()); } @J2ktIncompatible @@ -124,11 +121,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the undeclared exception to have been chained inside another - try { - sample.noneDeclared(); - fail(); - } catch (SomeChainingException expected) { - } + assertThrows(SomeChainingException.class, () -> sample.noneDeclared()); } @J2ktIncompatible @@ -170,11 +163,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the unchecked exception to propagate as-is - try { - sample.oneDeclared(); - fail(); - } catch (SomeUncheckedException expected) { - } + assertThrows(SomeUncheckedException.class, () -> sample.oneDeclared()); } @J2ktIncompatible @@ -194,11 +183,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the checked exception to propagate as-is - try { - sample.oneDeclared(); - fail(); - } catch (SomeCheckedException expected) { - } + assertThrows(SomeCheckedException.class, () -> sample.oneDeclared()); } @J2ktIncompatible @@ -218,11 +203,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the undeclared exception to have been chained inside another - try { - sample.oneDeclared(); - fail(); - } catch (SomeChainingException expected) { - } + assertThrows(SomeChainingException.class, () -> sample.oneDeclared()); } @J2ktIncompatible @@ -266,11 +247,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the unchecked exception to propagate as-is - try { - sample.twoDeclared(); - fail(); - } catch (SomeUncheckedException expected) { - } + assertThrows(SomeUncheckedException.class, () -> sample.twoDeclared()); } @J2ktIncompatible @@ -291,11 +268,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the checked exception to propagate as-is - try { - sample.twoDeclared(); - fail(); - } catch (SomeCheckedException expected) { - } + assertThrows(SomeCheckedException.class, () -> sample.twoDeclared()); } @J2ktIncompatible @@ -316,11 +289,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the checked exception to propagate as-is - try { - sample.twoDeclared(); - fail(); - } catch (SomeOtherCheckedException expected) { - } + assertThrows(SomeOtherCheckedException.class, () -> sample.twoDeclared()); } public void testThrowIfUnchecked_null() throws SomeCheckedException { @@ -384,11 +353,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the unchecked exception to propagate as-is - try { - sample.noneDeclared(); - fail(); - } catch (SomeUncheckedException expected) { - } + assertThrows(SomeUncheckedException.class, () -> sample.noneDeclared()); } @J2ktIncompatible @@ -407,11 +372,7 @@ public class ThrowablesTest extends TestCase { }; // Expect the error to propagate as-is - try { - sample.noneDeclared(); - fail(); - } catch (SomeError expected) { - } + assertThrows(SomeError.class, () -> sample.noneDeclared()); } @J2ktIncompatible @@ -430,12 +391,8 @@ public class ThrowablesTest extends TestCase { }; // Expect the undeclared exception to have been chained inside another - try { - sample.noneDeclared(); - fail(); - } catch (RuntimeException expected) { - assertThat(expected).hasCauseThat().isInstanceOf(SomeCheckedException.class); - } + RuntimeException expected = assertThrows(RuntimeException.class, () -> sample.noneDeclared()); + assertThat(expected).hasCauseThat().isInstanceOf(SomeCheckedException.class); } @J2ktIncompatible @@ -453,21 +410,17 @@ public class ThrowablesTest extends TestCase { @J2ktIncompatible @GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_CheckedSame() { - try { - throwIfInstanceOf(new SomeCheckedException(), SomeCheckedException.class); - fail(); - } catch (SomeCheckedException expected) { - } + assertThrows( + SomeCheckedException.class, + () -> throwIfInstanceOf(new SomeCheckedException(), SomeCheckedException.class)); } @J2ktIncompatible @GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_CheckedSubclass() { - try { - throwIfInstanceOf(new SomeCheckedException() {}, SomeCheckedException.class); - fail(); - } catch (SomeCheckedException expected) { - } + assertThrows( + SomeCheckedException.class, + () -> throwIfInstanceOf(new SomeCheckedException() {}, SomeCheckedException.class)); } @J2ktIncompatible @@ -507,11 +460,7 @@ public class ThrowablesTest extends TestCase { }; // Expect declared exception to be thrown as-is - try { - sample.oneDeclared(); - fail(); - } catch (SomeCheckedException expected) { - } + assertThrows(SomeCheckedException.class, () -> sample.oneDeclared()); } @J2ktIncompatible @@ -531,11 +480,7 @@ public class ThrowablesTest extends TestCase { }; // Expect unchecked exception to be thrown as-is - try { - sample.oneDeclared(); - fail(); - } catch (SomeUncheckedException expected) { - } + assertThrows(SomeUncheckedException.class, () -> sample.oneDeclared()); } @J2ktIncompatible @@ -555,22 +500,15 @@ public class ThrowablesTest extends TestCase { }; // Expect undeclared exception wrapped by RuntimeException to be thrown - try { - sample.oneDeclared(); - fail(); - } catch (RuntimeException expected) { - assertThat(expected).hasCauseThat().isInstanceOf(SomeOtherCheckedException.class); - } + RuntimeException expected = assertThrows(RuntimeException.class, () -> sample.oneDeclared()); + assertThat(expected).hasCauseThat().isInstanceOf(SomeOtherCheckedException.class); } @J2ktIncompatible @GwtIncompatible // throwIfInstanceOf public void testThrowIfInstanceOf_null() throws SomeCheckedException { - try { - throwIfInstanceOf(null, SomeCheckedException.class); - fail(); - } catch (NullPointerException expected) { - } + assertThrows( + NullPointerException.class, () -> throwIfInstanceOf(null, SomeCheckedException.class)); } @J2ktIncompatible @@ -720,12 +658,11 @@ public class ThrowablesTest extends TestCase { assertThat(Throwables.getCauseAs(thrown, SomeCheckedException.class)).isSameInstanceAs(cause); assertThat(Throwables.getCauseAs(thrown, Exception.class)).isSameInstanceAs(cause); - try { - Throwables.getCauseAs(thrown, IllegalStateException.class); - fail("Should have thrown CCE"); - } catch (ClassCastException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(thrown); - } + ClassCastException expected = + assertThrows( + ClassCastException.class, + () -> Throwables.getCauseAs(thrown, IllegalStateException.class)); + assertThat(expected).hasCauseThat().isSameInstanceAs(thrown); } @AndroidIncompatible // No getJavaLangAccess in Android (at least not in the version we use). @@ -749,11 +686,7 @@ public class ThrowablesTest extends TestCase { assertThat(lazyStackTrace(e)).containsExactly((Object[]) originalStackTrace).inOrder(); - try { - lazyStackTrace(e).set(0, null); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, () -> lazyStackTrace(e).set(0, null)); // Now we test a property that holds only for the lazy implementation. diff --git a/guava-tests/test/com/google/common/base/UnannotatedJavaClass.java b/guava-tests/test/com/google/common/base/UnannotatedJavaClass.java new file mode 100644 index 000000000..521035c61 --- /dev/null +++ b/guava-tests/test/com/google/common/base/UnannotatedJavaClass.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2023 The Guava Authors + * + * 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.common.base; + +/** Class containing an unannotated Java method for use from {@code OptionalExtensionsTest}. */ +final class UnannotatedJavaClass { + static Object getNull() { + return null; + } + + private UnannotatedJavaClass() {} +} |