2
* Copyright (C) 2012 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.testing;
19
import static com.google.common.base.Preconditions.checkNotNull;
20
import static com.google.common.truth.Truth.assertThat;
21
import static org.junit.Assert.assertThrows;
23
import com.google.common.base.Functions;
24
import com.google.common.base.Optional;
25
import com.google.common.collect.ImmutableList;
26
import com.google.common.testing.ClassSanityTester.FactoryMethodReturnsNullException;
27
import com.google.common.testing.ClassSanityTester.ParameterHasNoDistinctValueException;
28
import com.google.common.testing.ClassSanityTester.ParameterNotInstantiableException;
29
import com.google.common.testing.NullPointerTester.Visibility;
30
import java.io.Serializable;
31
import java.lang.reflect.InvocationTargetException;
32
import java.util.AbstractList;
33
import java.util.ArrayList;
37
import java.util.concurrent.TimeUnit;
38
import java.util.stream.Collectors;
39
import java.util.stream.Stream;
40
import junit.framework.TestCase;
41
import org.checkerframework.checker.nullness.qual.Nullable;
44
* Unit tests for {@link ClassSanityTester}.
48
public class ClassSanityTesterTest extends TestCase {
50
private final ClassSanityTester tester = new ClassSanityTester();
52
public void testEqualsOnReturnValues_good() throws Exception {
53
tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEquals();
56
public static class GoodEqualsFactory {
57
public static Object good(
60
// oneConstantOnly doesn't matter since it's not nullable and can be only 1 value.
61
@SuppressWarnings("unused") OneConstantEnum oneConstantOnly,
62
// noConstant doesn't matter since it can only be null
63
@SuppressWarnings("unused") @Nullable NoConstantEnum noConstant) {
64
return new GoodEquals(a, b);
67
// instance method ignored
68
public Object badIgnored() {
69
return new BadEquals();
73
public int returnsInt() {
74
throw new UnsupportedOperationException();
78
public void voidMethod() {
79
throw new UnsupportedOperationException();
82
// non-public method ignored
83
static Object badButNotPublic() {
84
return new BadEquals();
88
public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Exception {
90
tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals();
91
} catch (AssertionError expected) {
95
"No public static methods that return java.lang.Object or subtype are found in "
96
+ NoPublicStaticMethods.class
103
public void testEqualsOnReturnValues_bad() throws Exception {
105
tester.forAllPublicStaticMethods(BadEqualsFactory.class).testEquals();
106
} catch (AssertionError expected) {
112
private static class BadEqualsFactory {
113
/** oneConstantOnly matters now since it can be either null or the constant. */
114
@SuppressWarnings("unused") // Called by reflection
115
public static Object bad(String a, int b, @Nullable OneConstantEnum oneConstantOnly) {
116
return new GoodEquals(a, b);
120
public void testNullsOnReturnValues_good() throws Exception {
121
tester.forAllPublicStaticMethods(GoodNullsFactory.class).testNulls();
124
private static class GoodNullsFactory {
125
@SuppressWarnings("unused") // Called by reflection
126
public static Object good(String s) {
127
return new GoodNulls(s);
131
public void testNullsOnReturnValues_bad() throws Exception {
133
tester.forAllPublicStaticMethods(BadNullsFactory.class).thatReturn(Object.class).testNulls();
134
} catch (AssertionError expected) {
140
public void testNullsOnReturnValues_returnTypeFiltered() throws Exception {
143
.forAllPublicStaticMethods(BadNullsFactory.class)
144
.thatReturn(Iterable.class)
146
} catch (AssertionError expected) {
150
"No public static methods that return java.lang.Iterable or subtype are found in "
151
+ BadNullsFactory.class
158
public static class BadNullsFactory {
159
public static Object bad(@SuppressWarnings("unused") String a) {
160
return new BadNulls();
164
@AndroidIncompatible // TODO(cpovirk): ClassNotFoundException... ClassSanityTesterTest$AnInterface
165
public void testSerializableOnReturnValues_good() throws Exception {
166
tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testSerializable();
169
public static class GoodSerializableFactory {
170
public static Object good(Runnable r) {
174
public static Object good(AnInterface i) {
179
public void testSerializableOnReturnValues_bad() throws Exception {
181
tester.forAllPublicStaticMethods(BadSerializableFactory.class).testSerializable();
182
} catch (AssertionError expected) {
188
public static class BadSerializableFactory {
189
public static Object bad() {
190
return new Serializable() {
191
@SuppressWarnings("unused")
192
private final Object notSerializable = new Object();
197
public void testEqualsAndSerializableOnReturnValues_equalsIsGoodButNotSerializable()
200
tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEqualsAndSerializable();
201
} catch (AssertionError expected) {
204
fail("should have failed");
207
public void testEqualsAndSerializableOnReturnValues_serializableButNotEquals() throws Exception {
209
tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testEqualsAndSerializable();
210
} catch (AssertionError expected) {
213
fail("should have failed");
216
@AndroidIncompatible // TODO(cpovirk): ClassNotFoundException... ClassSanityTesterTest$AnInterface
217
public void testEqualsAndSerializableOnReturnValues_good() throws Exception {
219
.forAllPublicStaticMethods(GoodEqualsAndSerializableFactory.class)
220
.testEqualsAndSerializable();
223
public static class GoodEqualsAndSerializableFactory {
224
public static Object good(AnInterface s) {
225
return Functions.constant(s);
229
public void testEqualsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
231
tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testEquals();
232
} catch (AssertionError expected) {
238
public void testNullsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
240
tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class).testNulls();
241
} catch (AssertionError expected) {
247
public void testSerializableForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
250
.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
252
} catch (AssertionError expected) {
258
public void testEqualsAndSerializableForReturnValues_factoryReturnsNullButNotAnnotated()
262
.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
263
.testEqualsAndSerializable();
264
} catch (AssertionError expected) {
270
public static class FactoryThatReturnsNullButNotAnnotated {
271
public static Object bad() {
276
public void testEqualsForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
277
tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class).testEquals();
280
public void testNullsForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
281
tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class).testNulls();
284
public void testSerializableForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
285
tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class).testSerializable();
288
public void testEqualsAndSerializableForReturnValues_factoryReturnsNullAndAnnotated()
291
.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class)
292
.testEqualsAndSerializable();
295
public static class FactoryThatReturnsNullAndAnnotated {
296
public static @Nullable Object bad() {
301
public void testGoodEquals() throws Exception {
302
tester.testEquals(GoodEquals.class);
305
public void testEquals_interface() {
306
tester.testEquals(AnInterface.class);
309
public void testEquals_abstractClass() {
310
tester.testEquals(AnAbstractClass.class);
313
public void testEquals_enum() {
314
tester.testEquals(OneConstantEnum.class);
317
public void testBadEquals() throws Exception {
319
tester.testEquals(BadEquals.class);
320
} catch (AssertionError expected) {
321
assertThat(expected).hasMessageThat().contains("create(null)");
324
fail("should have failed");
327
public void testBadEquals_withParameterizedType() throws Exception {
329
tester.testEquals(BadEqualsWithParameterizedType.class);
330
} catch (AssertionError expected) {
331
assertThat(expected).hasMessageThat().contains("create([[1]])");
334
fail("should have failed");
337
public void testBadEquals_withSingleParameterValue() throws Exception {
339
ParameterHasNoDistinctValueException.class,
340
() -> tester.doTestEquals(ConstructorParameterWithOptionalNotInstantiable.class));
343
public void testGoodReferentialEqualityComparison() throws Exception {
344
tester.testEquals(UsesEnum.class);
345
tester.testEquals(UsesReferentialEquality.class);
346
tester.testEquals(SameListInstance.class);
349
public void testStreamParameterSkippedForNullTesting() throws Exception {
350
tester.testNulls(WithStreamParameter.class);
353
@AndroidIncompatible // problem with equality of Type objects?
354
public void testEqualsUsingReferentialEquality() throws Exception {
355
assertBadUseOfReferentialEquality(SameIntegerInstance.class);
356
assertBadUseOfReferentialEquality(SameLongInstance.class);
357
assertBadUseOfReferentialEquality(SameFloatInstance.class);
358
assertBadUseOfReferentialEquality(SameDoubleInstance.class);
359
assertBadUseOfReferentialEquality(SameShortInstance.class);
360
assertBadUseOfReferentialEquality(SameByteInstance.class);
361
assertBadUseOfReferentialEquality(SameCharacterInstance.class);
362
assertBadUseOfReferentialEquality(SameBooleanInstance.class);
363
assertBadUseOfReferentialEquality(SameObjectInstance.class);
364
assertBadUseOfReferentialEquality(SameStringInstance.class);
365
assertBadUseOfReferentialEquality(SameInterfaceInstance.class);
368
private void assertBadUseOfReferentialEquality(Class<?> cls) throws Exception {
370
tester.testEquals(cls);
371
} catch (AssertionError expected) {
372
assertThat(expected).hasMessageThat().contains(cls.getSimpleName() + "(");
375
fail("should have failed for " + cls);
378
public void testParameterNotInstantiableForEqualsTest() throws Exception {
380
ParameterNotInstantiableException.class,
381
() -> tester.doTestEquals(ConstructorParameterNotInstantiable.class));
384
public void testNoDistinctValueForEqualsTest() throws Exception {
386
ParameterHasNoDistinctValueException.class,
387
() -> tester.doTestEquals(ConstructorParameterSingleValue.class));
390
public void testConstructorThrowsForEqualsTest() throws Exception {
392
InvocationTargetException.class, () -> tester.doTestEquals(ConstructorThrows.class));
395
public void testFactoryMethodReturnsNullForEqualsTest() throws Exception {
397
FactoryMethodReturnsNullException.class,
398
() -> tester.doTestEquals(FactoryMethodReturnsNullAndAnnotated.class));
401
public void testFactoryMethodReturnsNullButNotAnnotatedInEqualsTest() throws Exception {
403
tester.testEquals(FactoryMethodReturnsNullButNotAnnotated.class);
404
} catch (AssertionError expected) {
407
fail("should have failed");
410
public void testNoEqualsChecksOnEnum() throws Exception {
411
tester.testEquals(OneConstantEnum.class);
412
tester.testEquals(NoConstantEnum.class);
413
tester.testEquals(TimeUnit.class);
416
public void testNoEqualsChecksOnInterface() throws Exception {
417
tester.testEquals(Runnable.class);
420
public void testNoEqualsChecksOnAnnotation() throws Exception {
421
tester.testEquals(MyAnnotation.class);
424
public void testGoodNulls() throws Exception {
425
tester.testNulls(GoodNulls.class);
428
public void testNoNullCheckNeededDespiteNotInstantiable() throws Exception {
429
tester.doTestNulls(NoNullCheckNeededDespiteNotInstantiable.class, Visibility.PACKAGE);
432
public void testNulls_interface() {
433
tester.testNulls(AnInterface.class);
436
public void testNulls_abstractClass() {
437
tester.testNulls(AnAbstractClass.class);
440
public void testNulls_enum() throws Exception {
441
tester.testNulls(OneConstantEnum.class);
442
tester.testNulls(NoConstantEnum.class);
443
tester.testNulls(TimeUnit.class);
446
public void testNulls_parameterOptionalNotInstantiable() throws Exception {
447
tester.testNulls(ConstructorParameterWithOptionalNotInstantiable.class);
450
public void testEnumFailsToCheckNull() throws Exception {
452
tester.testNulls(EnumFailsToCheckNull.class);
453
} catch (AssertionError expected) {
456
fail("should have failed");
459
public void testNoNullChecksOnInterface() throws Exception {
460
tester.testNulls(Runnable.class);
463
public void testNoNullChecksOnAnnotation() throws Exception {
464
tester.testNulls(MyAnnotation.class);
467
public void testBadNulls() throws Exception {
469
tester.testNulls(BadNulls.class);
470
} catch (AssertionError expected) {
473
fail("should have failed");
476
public void testInstantiate_factoryMethodReturnsNullButNotAnnotated() throws Exception {
478
FactoryMethodReturnsNullButNotAnnotated unused =
479
tester.instantiate(FactoryMethodReturnsNullButNotAnnotated.class);
480
} catch (AssertionError expected) {
481
assertThat(expected).hasMessageThat().contains("@Nullable");
484
fail("should have failed");
487
public void testInstantiate_factoryMethodReturnsNullAndAnnotated() throws Exception {
489
FactoryMethodReturnsNullException.class,
490
() -> tester.instantiate(FactoryMethodReturnsNullAndAnnotated.class));
493
public void testInstantiate_factoryMethodAcceptsNull() throws Exception {
494
assertNull(tester.instantiate(FactoryMethodAcceptsNull.class).name);
497
public void testInstantiate_factoryMethodDoesNotAcceptNull() throws Exception {
498
assertNotNull(tester.instantiate(FactoryMethodDoesNotAcceptNull.class).name);
501
public void testInstantiate_constructorAcceptsNull() throws Exception {
502
assertNull(tester.instantiate(ConstructorAcceptsNull.class).name);
505
public void testInstantiate_constructorDoesNotAcceptNull() throws Exception {
506
assertNotNull(tester.instantiate(ConstructorDoesNotAcceptNull.class).name);
509
public void testInstantiate_notInstantiable() throws Exception {
510
assertNull(tester.instantiate(NotInstantiable.class));
513
public void testInstantiate_noConstantEnum() throws Exception {
514
assertNull(tester.instantiate(NoConstantEnum.class));
517
public void testInstantiate_oneConstantEnum() throws Exception {
518
assertEquals(OneConstantEnum.A, tester.instantiate(OneConstantEnum.class));
521
public void testInstantiate_interface() throws Exception {
522
assertNull(tester.instantiate(Runnable.class));
525
public void testInstantiate_abstractClass() throws Exception {
526
assertNull(tester.instantiate(AbstractList.class));
529
public void testInstantiate_annotation() throws Exception {
530
assertNull(tester.instantiate(MyAnnotation.class));
533
public void testInstantiate_setDefault() throws Exception {
534
NotInstantiable x = new NotInstantiable();
535
tester.setDefault(NotInstantiable.class, x);
536
assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class));
539
public void testSetDistinctValues_equalInstances() {
541
IllegalArgumentException.class, () -> tester.setDistinctValues(String.class, "", ""));
544
public void testInstantiate_setDistinctValues() throws Exception {
545
NotInstantiable x = new NotInstantiable();
546
NotInstantiable y = new NotInstantiable();
547
tester.setDistinctValues(NotInstantiable.class, x, y);
548
assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class));
549
tester.testEquals(ConstructorParameterMapOfNotInstantiable.class);
552
public void testInstantiate_constructorThrows() throws Exception {
554
InvocationTargetException.class, () -> tester.instantiate(ConstructorThrows.class));
557
public void testInstantiate_factoryMethodThrows() throws Exception {
559
InvocationTargetException.class, () -> tester.instantiate(FactoryMethodThrows.class));
562
public void testInstantiate_constructorParameterNotInstantiable() throws Exception {
564
ParameterNotInstantiableException.class,
565
() -> tester.instantiate(ConstructorParameterNotInstantiable.class));
568
public void testInstantiate_factoryMethodParameterNotInstantiable() throws Exception {
570
ParameterNotInstantiableException.class,
571
() -> tester.instantiate(FactoryMethodParameterNotInstantiable.class));
574
public void testInstantiate_instantiableFactoryMethodChosen() throws Exception {
575
assertEquals("good", tester.instantiate(InstantiableFactoryMethodChosen.class).name);
578
@AndroidIncompatible // TODO(cpovirk): ClassNotFoundException... ClassSanityTesterTest$AnInterface
579
public void testInterfaceProxySerializable() throws Exception {
580
SerializableTester.reserializeAndAssert(tester.instantiate(HasAnInterface.class));
583
public void testReturnValuesFromAnotherPackageIgnoredForNullTests() throws Exception {
584
new ClassSanityTester().forAllPublicStaticMethods(JdkObjectFactory.class).testNulls();
587
/** String doesn't check nulls as we expect. But the framework should ignore. */
588
private static class JdkObjectFactory {
589
@SuppressWarnings("unused") // Called by reflection
590
public static Object create() {
591
return new ArrayList<>();
595
static class HasAnInterface implements Serializable {
596
private final AnInterface i;
598
public HasAnInterface(AnInterface i) {
603
public boolean equals(@Nullable Object obj) {
604
if (obj instanceof HasAnInterface) {
605
HasAnInterface that = (HasAnInterface) obj;
606
return i.equals(that.i);
613
public int hashCode() {
618
static class InstantiableFactoryMethodChosen {
621
private InstantiableFactoryMethodChosen(String name) {
625
public InstantiableFactoryMethodChosen(NotInstantiable x) {
630
public static InstantiableFactoryMethodChosen create(NotInstantiable x) {
631
return new InstantiableFactoryMethodChosen(x);
634
public static InstantiableFactoryMethodChosen create(String s) {
636
return new InstantiableFactoryMethodChosen("good");
640
public void testInstantiate_instantiableConstructorChosen() throws Exception {
641
assertEquals("good", tester.instantiate(InstantiableConstructorChosen.class).name);
644
public void testEquals_setOfNonInstantiable() throws Exception {
646
ParameterNotInstantiableException.class,
647
() -> new ClassSanityTester().doTestEquals(SetWrapper.class));
650
private abstract static class Wrapper {
651
private final Object wrapped;
653
Wrapper(Object wrapped) {
654
this.wrapped = checkNotNull(wrapped);
658
public boolean equals(@Nullable Object obj) {
659
// In general getClass().isInstance() is bad for equals.
660
// But here we fully control the subclasses to ensure symmetry.
661
if (getClass().isInstance(obj)) {
662
Wrapper that = (Wrapper) obj;
663
return wrapped.equals(that.wrapped);
669
public int hashCode() {
670
return wrapped.hashCode();
674
public String toString() {
675
return wrapped.toString();
679
private static class SetWrapper extends Wrapper {
680
public SetWrapper(Set<NotInstantiable> wrapped) {
685
static class InstantiableConstructorChosen {
688
public InstantiableConstructorChosen(String name) {
693
public InstantiableConstructorChosen(NotInstantiable x) {
698
public static InstantiableFactoryMethodChosen create(NotInstantiable x) {
699
return new InstantiableFactoryMethodChosen(x);
703
static class GoodEquals {
705
private final String a;
708
private GoodEquals(String a, int b) {
709
this.a = checkNotNull(a);
713
// ignored by testEquals()
714
GoodEquals(@SuppressWarnings("unused") NotInstantiable x) {
720
public GoodEquals(@SuppressWarnings("unused") NotInstantiable x, int b) {
726
@SuppressWarnings("unused")
727
static GoodEquals create(int a, int b) {
728
throw new RuntimeException();
732
static GoodEquals create(String a, int b) {
733
return new GoodEquals(a, b);
737
@SuppressWarnings("unused")
738
public static @Nullable GoodEquals createMayReturnNull(int a, int b) {
743
public boolean equals(@Nullable Object obj) {
744
if (obj instanceof GoodEquals) {
745
GoodEquals that = (GoodEquals) obj;
746
return a.equals(that.a) && b == that.b;
753
public int hashCode() {
758
static class BadEquals {
760
public BadEquals() {} // ignored by testEquals() since it has less parameters.
762
public static BadEquals create(@SuppressWarnings("unused") @Nullable String s) {
763
return new BadEquals();
767
public boolean equals(@Nullable Object obj) {
768
return obj instanceof BadEquals;
772
public int hashCode() {
777
static class SameIntegerInstance {
778
private final Integer i;
780
public SameIntegerInstance(Integer i) {
781
this.i = checkNotNull(i);
785
public int hashCode() {
790
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
791
public boolean equals(@Nullable Object obj) {
792
if (obj instanceof SameIntegerInstance) {
793
SameIntegerInstance that = (SameIntegerInstance) obj;
800
static class SameLongInstance {
801
private final Long i;
803
public SameLongInstance(Long i) {
804
this.i = checkNotNull(i);
808
public int hashCode() {
813
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
814
public boolean equals(@Nullable Object obj) {
815
if (obj instanceof SameLongInstance) {
816
SameLongInstance that = (SameLongInstance) obj;
823
static class SameFloatInstance {
824
private final Float i;
826
public SameFloatInstance(Float i) {
827
this.i = checkNotNull(i);
831
public int hashCode() {
836
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
837
public boolean equals(@Nullable Object obj) {
838
if (obj instanceof SameFloatInstance) {
839
SameFloatInstance that = (SameFloatInstance) obj;
846
static class SameDoubleInstance {
847
private final Double i;
849
public SameDoubleInstance(Double i) {
850
this.i = checkNotNull(i);
854
public int hashCode() {
859
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
860
public boolean equals(@Nullable Object obj) {
861
if (obj instanceof SameDoubleInstance) {
862
SameDoubleInstance that = (SameDoubleInstance) obj;
869
static class SameShortInstance {
870
private final Short i;
872
public SameShortInstance(Short i) {
873
this.i = checkNotNull(i);
877
public int hashCode() {
882
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
883
public boolean equals(@Nullable Object obj) {
884
if (obj instanceof SameShortInstance) {
885
SameShortInstance that = (SameShortInstance) obj;
892
static class SameByteInstance {
893
private final Byte i;
895
public SameByteInstance(Byte i) {
896
this.i = checkNotNull(i);
900
public int hashCode() {
905
@SuppressWarnings({"BoxedPrimitiveEquality", "NumericEquality"})
906
public boolean equals(@Nullable Object obj) {
907
if (obj instanceof SameByteInstance) {
908
SameByteInstance that = (SameByteInstance) obj;
915
static class SameCharacterInstance {
916
private final Character i;
918
public SameCharacterInstance(Character i) {
919
this.i = checkNotNull(i);
923
public int hashCode() {
928
@SuppressWarnings("BoxedPrimitiveEquality")
929
public boolean equals(@Nullable Object obj) {
930
if (obj instanceof SameCharacterInstance) {
931
SameCharacterInstance that = (SameCharacterInstance) obj;
938
static class SameBooleanInstance {
939
private final Boolean i;
941
public SameBooleanInstance(Boolean i) {
942
this.i = checkNotNull(i);
946
public int hashCode() {
951
@SuppressWarnings("BoxedPrimitiveEquality")
952
public boolean equals(@Nullable Object obj) {
953
if (obj instanceof SameBooleanInstance) {
954
SameBooleanInstance that = (SameBooleanInstance) obj;
961
static class SameStringInstance {
962
private final String s;
964
public SameStringInstance(String s) {
965
this.s = checkNotNull(s);
969
public int hashCode() {
974
public boolean equals(@Nullable Object obj) {
975
if (obj instanceof SameStringInstance) {
976
SameStringInstance that = (SameStringInstance) obj;
983
static class SameObjectInstance {
984
private final Object s;
986
public SameObjectInstance(Object s) {
987
this.s = checkNotNull(s);
991
public int hashCode() {
996
public boolean equals(@Nullable Object obj) {
997
if (obj instanceof SameObjectInstance) {
998
SameObjectInstance that = (SameObjectInstance) obj;
1005
static class SameInterfaceInstance {
1006
private final Runnable s;
1008
public SameInterfaceInstance(Runnable s) {
1009
this.s = checkNotNull(s);
1013
public int hashCode() {
1014
return s.hashCode();
1018
public boolean equals(@Nullable Object obj) {
1019
if (obj instanceof SameInterfaceInstance) {
1020
SameInterfaceInstance that = (SameInterfaceInstance) obj;
1027
static class SameListInstance {
1028
private final List<?> s;
1030
public SameListInstance(List<?> s) {
1031
this.s = checkNotNull(s);
1035
public int hashCode() {
1036
return System.identityHashCode(s);
1040
public boolean equals(@Nullable Object obj) {
1041
if (obj instanceof SameListInstance) {
1042
SameListInstance that = (SameListInstance) obj;
1049
static class WithStreamParameter {
1050
private final List<?> list;
1052
// This should be ignored.
1053
public WithStreamParameter(Stream<?> s, String str) {
1054
this.list = s.collect(Collectors.toList());
1059
static class UsesReferentialEquality {
1060
private final ReferentialEquality s;
1062
public UsesReferentialEquality(ReferentialEquality s) {
1063
this.s = checkNotNull(s);
1067
public int hashCode() {
1068
return s.hashCode();
1072
public boolean equals(@Nullable Object obj) {
1073
if (obj instanceof UsesReferentialEquality) {
1074
UsesReferentialEquality that = (UsesReferentialEquality) obj;
1081
static class UsesEnum {
1082
private final TimeUnit s;
1084
public UsesEnum(TimeUnit s) {
1085
this.s = checkNotNull(s);
1089
public int hashCode() {
1090
return s.hashCode();
1094
public boolean equals(@Nullable Object obj) {
1095
if (obj instanceof UsesEnum) {
1096
UsesEnum that = (UsesEnum) obj;
1103
public static class ReferentialEquality {
1104
public ReferentialEquality() {}
1107
static class BadEqualsWithParameterizedType {
1109
// ignored by testEquals() since it has less parameters.
1110
public BadEqualsWithParameterizedType() {}
1112
public static BadEqualsWithParameterizedType create(
1113
@SuppressWarnings("unused") ImmutableList<Iterable<? extends String>> s) {
1114
return new BadEqualsWithParameterizedType();
1118
public boolean equals(@Nullable Object obj) {
1119
return obj instanceof BadEqualsWithParameterizedType;
1123
public int hashCode() {
1128
static class GoodNulls {
1129
public GoodNulls(String s) {
1133
public void rejectNull(String s) {
1138
public static class BadNulls {
1139
public void failsToRejectNull(@SuppressWarnings("unused") String s) {}
1142
public static class NoNullCheckNeededDespiteNotInstantiable {
1144
public NoNullCheckNeededDespiteNotInstantiable(NotInstantiable x) {
1148
@SuppressWarnings("unused") // reflected
1149
void primitiveOnly(int i) {}
1151
@SuppressWarnings("unused") // reflected
1152
void nullableOnly(@Nullable String s) {}
1154
public void noParameter() {}
1156
@SuppressWarnings("unused") // reflected
1157
void primitiveAndNullable(@Nullable String s, int i) {}
1160
static class FactoryMethodReturnsNullButNotAnnotated {
1161
private FactoryMethodReturnsNullButNotAnnotated() {}
1163
static FactoryMethodReturnsNullButNotAnnotated returnsNull() {
1168
static class FactoryMethodReturnsNullAndAnnotated {
1169
private FactoryMethodReturnsNullAndAnnotated() {}
1171
public static @Nullable FactoryMethodReturnsNullAndAnnotated returnsNull() {
1176
static class FactoryMethodAcceptsNull {
1180
private FactoryMethodAcceptsNull(String name) {
1184
static FactoryMethodAcceptsNull create(@Nullable String name) {
1185
return new FactoryMethodAcceptsNull(name);
1189
static class FactoryMethodDoesNotAcceptNull {
1193
private FactoryMethodDoesNotAcceptNull(String name) {
1194
this.name = checkNotNull(name);
1197
public static FactoryMethodDoesNotAcceptNull create(String name) {
1198
return new FactoryMethodDoesNotAcceptNull(name);
1202
static class ConstructorAcceptsNull {
1206
public ConstructorAcceptsNull(@Nullable String name) {
1211
static class ConstructorDoesNotAcceptNull {
1215
ConstructorDoesNotAcceptNull(String name) {
1216
this.name = checkNotNull(name);
1220
static class ConstructorParameterNotInstantiable {
1221
public ConstructorParameterNotInstantiable(@SuppressWarnings("unused") NotInstantiable x) {}
1224
static class ConstructorParameterMapOfNotInstantiable {
1225
private final Map<NotInstantiable, NotInstantiable> m;
1227
public ConstructorParameterMapOfNotInstantiable(Map<NotInstantiable, NotInstantiable> m) {
1228
this.m = checkNotNull(m);
1232
public boolean equals(@Nullable Object obj) {
1233
if (obj instanceof ConstructorParameterMapOfNotInstantiable) {
1234
return m.equals(((ConstructorParameterMapOfNotInstantiable) obj).m);
1241
public int hashCode() {
1242
return m.hashCode();
1246
// Test that we should get a distinct parameter error when doing equals test.
1247
static class ConstructorParameterWithOptionalNotInstantiable {
1248
public ConstructorParameterWithOptionalNotInstantiable(Optional<NotInstantiable> x) {
1253
public boolean equals(@Nullable Object obj) {
1254
throw new UnsupportedOperationException();
1258
public int hashCode() {
1259
throw new UnsupportedOperationException();
1263
static class ConstructorParameterSingleValue {
1264
public ConstructorParameterSingleValue(@SuppressWarnings("unused") Singleton s) {}
1267
public boolean equals(@Nullable Object obj) {
1268
return obj instanceof ConstructorParameterSingleValue;
1272
public int hashCode() {
1276
public static class Singleton {
1277
public static final Singleton INSTANCE = new Singleton();
1279
private Singleton() {}
1283
static class FactoryMethodParameterNotInstantiable {
1285
private FactoryMethodParameterNotInstantiable() {}
1287
static FactoryMethodParameterNotInstantiable create(
1288
@SuppressWarnings("unused") NotInstantiable x) {
1289
return new FactoryMethodParameterNotInstantiable();
1293
static class ConstructorThrows {
1294
public ConstructorThrows() {
1295
throw new RuntimeException();
1299
static class FactoryMethodThrows {
1300
private FactoryMethodThrows() {}
1302
public static FactoryMethodThrows create() {
1303
throw new RuntimeException();
1307
static class NotInstantiable {
1308
private NotInstantiable() {}
1311
private enum NoConstantEnum {}
1313
private enum OneConstantEnum {
1317
private enum EnumFailsToCheckNull {
1320
@SuppressWarnings("unused")
1321
public void failToCheckNull(String s) {}
1324
private interface AnInterface {}
1326
private abstract static class AnAbstractClass {
1327
@SuppressWarnings("unused")
1328
public AnAbstractClass(String s) {}
1330
@SuppressWarnings("unused")
1331
public void failsToCheckNull(String s) {}
1334
private static class NoPublicStaticMethods {
1335
@SuppressWarnings("unused") // To test non-public factory isn't used.
1336
static String notPublic() {
1341
@interface MyAnnotation {}