aboutsummaryrefslogtreecommitdiffstats
path: root/guava-testlib/test/com/google/common/testing/ClassSanityTesterTest.java
blob: 8c557909ef3478243754baeedfa57024faad513c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
/*
 * Copyright (C) 2012 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.testing;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.truth0.Truth.ASSERT;

import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList;
import com.google.common.testing.ClassSanityTester.FactoryMethodReturnsNullException;
import com.google.common.testing.ClassSanityTester.ParameterNotInstantiableException;
import com.google.common.testing.NullPointerTester.Visibility;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

/**
 * Unit tests for {@link ClassSanityTester}.
 *
 * @author Ben Yu
 */
public class ClassSanityTesterTest extends TestCase {

  private final ClassSanityTester tester = new ClassSanityTester();

  public void testEqualsOnReturnValues_good() throws Exception {
    tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEquals();
  }

  public static class GoodEqualsFactory {
    public static Object good(String a, int b,
        // oneConstantOnly doesn't matter since it's not nullable and can be only 1 value.
        @SuppressWarnings("unused") OneConstantEnum oneConstantOnly,
        // noConstant doesn't matter since it can only be null
        @SuppressWarnings("unused") @Nullable NoConstantEnum noConstant) {
      return new GoodEquals(a, b);
    }
    // instance method ignored
    public Object badIgnored() {
      return new BadEquals();
    }
    // primitive ignored
    public int returnsInt() {
      throw new UnsupportedOperationException();
    }
    // void ignored
    public void voidMethod() {
      throw new UnsupportedOperationException();
    }
    // non-public method ignored
    static Object badButNotPublic() {
      return new BadEquals();
    }
  }

  public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Exception {
    try {
      tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals();
    } catch (AssertionFailedError expected) {
      assertEquals(
          "No public static methods that return java.lang.Object or subtype are found in "
              + NoPublicStaticMethods.class + ".",
          expected.getMessage());
      return;
    }
    fail();
  }

  public void testEqualsOnReturnValues_bad() throws Exception {
    try {
      tester.forAllPublicStaticMethods(BadEqualsFactory.class).testEquals();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  private static class BadEqualsFactory {
    /** oneConstantOnly matters now since it can be either null or the constant. */
    @SuppressWarnings("unused") // Called by reflection
    public static Object bad(String a, int b,
        @Nullable OneConstantEnum oneConstantOnly) {
      return new GoodEquals(a, b);
    }
  }

  public void testNullsOnReturnValues_good() throws Exception {
    tester.forAllPublicStaticMethods(GoodNullsFactory.class).testNulls();
  }

  private static class GoodNullsFactory {
    @SuppressWarnings("unused") // Called by reflection
    public static Object good(String s) {
      return new GoodNulls(s);
    }
  }

  public void testNullsOnReturnValues_bad() throws Exception {
    try {
      tester
          .forAllPublicStaticMethods(BadNullsFactory.class)
          .thatReturn(Object.class)
          .testNulls();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  public void testNullsOnReturnValues_returnTypeFiltered() throws Exception {
    try {
      tester
          .forAllPublicStaticMethods(BadNullsFactory.class)
          .thatReturn(Iterable.class)
          .testNulls();
    } catch (AssertionFailedError expected) {
      assertEquals(
          "No public static methods that return java.lang.Iterable or subtype are found in "
              + BadNullsFactory.class + ".",
          expected.getMessage());
      return;
    }
    fail();
  }
  
  public static class BadNullsFactory {
    public static Object bad(@SuppressWarnings("unused") String a) {
      return new BadNulls();
    }
  }

  public void testSerializableOnReturnValues_good() throws Exception {
    tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testSerializable();
  }

  public static class GoodSerializableFactory {
    public static Object good(Runnable r) {
      return r;
    }
    public static Object good(AnInterface i) {
      return i;
    }
  }

  public void testSerializableOnReturnValues_bad() throws Exception {
    try {
      tester.forAllPublicStaticMethods(BadSerializableFactory.class).testSerializable();
    } catch (AssertionError expected) {
      return;
    }
    fail();
  }

  public static class BadSerializableFactory {
    public static Object bad() {
      return new Serializable() {
        @SuppressWarnings("unused")
        private final Object notSerializable = new Object();
      };
    }
  }

  public void testEqualsAndSerializableOnReturnValues_equalsIsGoodButNotSerializable()
      throws Exception {
    try {
      tester.forAllPublicStaticMethods(GoodEqualsFactory.class).testEqualsAndSerializable();
    } catch (AssertionError expected) {
      return;
    }
    fail("should have failed");
  }

  public void testEqualsAndSerializableOnReturnValues_serializableButNotEquals()
      throws Exception {
    try {
      tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testEqualsAndSerializable();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail("should have failed");
  }

  public void testEqualsAndSerializableOnReturnValues_good()
      throws Exception {
    tester.forAllPublicStaticMethods(GoodEqualsAndSerialiableFactory.class)
        .testEqualsAndSerializable();
  }

  public static class GoodEqualsAndSerialiableFactory {
    public static Object good(AnInterface s) {
      return Functions.constant(s);
    }
  }

  public void testEqualsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
    try {
      tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
          .testEquals();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  public void testNullsForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
    try {
      tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
          .testNulls();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  public void testSerializableForReturnValues_factoryReturnsNullButNotAnnotated() throws Exception {
    try {
      tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
          .testSerializable();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  public void testEqualsAndSerializableForReturnValues_factoryReturnsNullButNotAnnotated()
      throws Exception {
    try {
      tester.forAllPublicStaticMethods(FactoryThatReturnsNullButNotAnnotated.class)
          .testEqualsAndSerializable();
    } catch (AssertionFailedError expected) {
      return;
    }
    fail();
  }

  public static class FactoryThatReturnsNullButNotAnnotated {
    public static Object bad() {
      return null;
    }
  }

  public void testEqualsForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
    tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class)
        .testEquals();
  }

  public void testNullsForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
    tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class)
        .testNulls();
  }

  public void testSerializableForReturnValues_factoryReturnsNullAndAnnotated() throws Exception {
    tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class)
        .testSerializable();
  }

  public void testEqualsAndSerializableForReturnValues_factoryReturnsNullAndAnnotated()
      throws Exception {
    tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class)
        .testEqualsAndSerializable();
  }

  public static class FactoryThatReturnsNullAndAnnotated {
    @Nullable public static Object bad() {
      return null;
    }
  }

  public void testGoodEquals() throws Exception {
    tester.testEquals(GoodEquals.class);
  }

  public void testEquals_interface() {
    tester.testEquals(AnInterface.class);
  }

  public void testEquals_abstractClass() {
    tester.testEquals(AnAbstractClass.class);
  }

  public void testEquals_enum() {
    tester.testEquals(OneConstantEnum.class);
  }

  public void testBadEquals() throws Exception {
    try {
      tester.testEquals(BadEquals.class);
    } catch (AssertionFailedError expected) {
      ASSERT.that(expected.getMessage()).contains("create(null)");
      return;
    }
    fail("should have failed");
  }

  public void testBadEquals_withParameterizedType() throws Exception {
    try {
      tester.testEquals(BadEqualsWithParameterizedType.class);
    } catch (AssertionFailedError expected) {
      ASSERT.that(expected.getMessage()).contains("create([[1]])");
      return;
    }
    fail("should have failed");
  }

  public void testGoodReferentialEqualityComparison() throws Exception {
    tester.testEquals(UsesEnum.class);
    tester.testEquals(UsesReferentialEquality.class);
    tester.testEquals(SameListInstance.class);
  }

  public void testEqualsUsingReferentialEquality() throws Exception {
    assertBadUseOfReferentialEquality(SameIntegerInstance.class);
    assertBadUseOfReferentialEquality(SameLongInstance.class);
    assertBadUseOfReferentialEquality(SameFloatInstance.class);
    assertBadUseOfReferentialEquality(SameDoubleInstance.class);
    assertBadUseOfReferentialEquality(SameShortInstance.class);
    assertBadUseOfReferentialEquality(SameByteInstance.class);
    assertBadUseOfReferentialEquality(SameCharacterInstance.class);
    assertBadUseOfReferentialEquality(SameBooleanInstance.class);
    assertBadUseOfReferentialEquality(SameObjectInstance.class);
    assertBadUseOfReferentialEquality(SameStringInstance.class);
    assertBadUseOfReferentialEquality(SameInterfaceInstance.class);
  }

  private void assertBadUseOfReferentialEquality(Class<?> cls) throws Exception {
    try {
      tester.testEquals(cls);
    } catch (AssertionFailedError expected) {
      ASSERT.that(expected.getMessage()).contains(cls.getSimpleName() + "(");
      return;
    }
    fail("should have failed");
  }

  public void testParameterNotInstantiableForEqualsTest() throws Exception {
    try {
      tester.doTestEquals(ConstructorParameterNotInstantiable.class);
      fail("should have failed");
    } catch (ParameterNotInstantiableException expected) {}
  }

  public void testConstructorThrowsForEqualsTest() throws Exception {
    try {
      tester.doTestEquals(ConstructorThrows.class);
      fail("should have failed");
    } catch (InvocationTargetException expected) {}
  }

  public void testFactoryMethodReturnsNullForEqualsTest() throws Exception {
    try {
      tester.doTestEquals(FactoryMethodReturnsNullAndAnnotated.class);
      fail("should have failed");
    } catch (FactoryMethodReturnsNullException expected) {}
  }

  public void testFactoryMethodReturnsNullButNotAnnotatedInEqualsTest() throws Exception {
    try {
      tester.testEquals(FactoryMethodReturnsNullButNotAnnotated.class);
    } catch (AssertionFailedError expected) {
      return;
    }
    fail("should have failed");
  }

  public void testNoEqualsChecksOnEnum() throws Exception {
    tester.testEquals(OneConstantEnum.class);
    tester.testEquals(NoConstantEnum.class);
    tester.testEquals(TimeUnit.class);
  }

  public void testNoEqualsChecksOnInterface() throws Exception {
    tester.testEquals(Runnable.class);
  }

  public void testNoEqualsChecksOnAnnotation() throws Exception {
    tester.testEquals(Nullable.class);
  }

  public void testGoodNulls() throws Exception {
    tester.testNulls(GoodNulls.class);
  }

  public void testNoNullCheckNeededDespitNotInstantiable() throws Exception {
    tester.doTestNulls(NoNullCheckNeededDespitNotInstantiable.class, Visibility.PACKAGE);
  }

  public void testNulls_interface() {
    tester.testNulls(AnInterface.class);
  }

  public void testNulls_abstractClass() {
    tester.testNulls(AnAbstractClass.class);
  }

  public void testNulls_enum() throws Exception {
    tester.testNulls(OneConstantEnum.class);
    tester.testNulls(NoConstantEnum.class);
    tester.testNulls(TimeUnit.class);
  }

  public void testEnumFailsToCheckNull() throws Exception {
    try {
      tester.testNulls(EnumFailsToCheckNull.class);
    } catch (AssertionFailedError expected) {
      return;
    }
    fail("should have failed");
  }

  public void testNoNullChecksOnInterface() throws Exception {
    tester.testNulls(Runnable.class);
  }

  public void testNoNullChecksOnAnnotation() throws Exception {
    tester.testNulls(Nullable.class);
  }

  public void testBadNulls() throws Exception {
    try {
      tester.testNulls(BadNulls.class);
    } catch (AssertionFailedError expected) {
      return;
    }
    fail("should have failed");
  }

  public void testInstantiate_factoryMethodReturnsNullButNotAnnotated() throws Exception {
    try {
      tester.instantiate(FactoryMethodReturnsNullButNotAnnotated.class);
    } catch (AssertionFailedError expected) {
      ASSERT.that(expected.getMessage()).contains("@Nullable");
      return;
    }
    fail("should have failed");
  }

  public void testInstantiate_factoryMethodReturnsNullAndAnnotated() throws Exception {
    try {
      tester.instantiate(FactoryMethodReturnsNullAndAnnotated.class);
      fail("should have failed");
    } catch (FactoryMethodReturnsNullException expected) {}
  }

  public void testInstantiate_factoryMethodAcceptsNull() throws Exception {
    assertNull(tester.instantiate(FactoryMethodAcceptsNull.class).name);
  }

  public void testInstantiate_factoryMethodDoesNotAcceptNull() throws Exception {
    assertNotNull(tester.instantiate(FactoryMethodDoesNotAcceptNull.class).name);
  }

  public void testInstantiate_constructorAcceptsNull() throws Exception {
    assertNull(tester.instantiate(ConstructorAcceptsNull.class).name);
  }

  public void testInstantiate_constructorDoesNotAcceptNull() throws Exception {
    assertNotNull(tester.instantiate(ConstructorDoesNotAcceptNull.class).name);
  }

  public void testInstantiate_notInstantiable() throws Exception {
    assertNull(tester.instantiate(NotInstantiable.class));
  }

  public void testInstantiate_noConstantEnum() throws Exception {
    assertNull(tester.instantiate(NoConstantEnum.class));
  }

  public void testInstantiate_oneConstantEnum() throws Exception {
    assertEquals(OneConstantEnum.A, tester.instantiate(OneConstantEnum.class));
  }

  public void testInstantiate_interface() throws Exception {
    assertNull(tester.instantiate(Runnable.class));
  }

  public void testInstantiate_abstractClass() throws Exception {
    assertNull(tester.instantiate(AbstractList.class));
  }

  public void testInstantiate_annotation() throws Exception {
    assertNull(tester.instantiate(Nullable.class));
  }

  public void testInstantiate_setDefault() throws Exception {
    NotInstantiable x = new NotInstantiable();
    tester.setDefault(NotInstantiable.class, x);
    assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class));
  }

  public void testInstantiate_setSampleInstances() throws Exception {
    NotInstantiable x = new NotInstantiable();
    tester.setSampleInstances(NotInstantiable.class, ImmutableList.of(x));
    assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class));
  }

  public void testInstantiate_setSampleInstances_empty() throws Exception {
    tester.setSampleInstances(NotInstantiable.class, ImmutableList.<NotInstantiable>of());
    try {
      tester.instantiate(ConstructorParameterNotInstantiable.class);
      fail();
    } catch (ParameterNotInstantiableException expected) {}
  }

  public void testInstantiate_constructorThrows() throws Exception {
    try {
      tester.instantiate(ConstructorThrows.class);
      fail();
    } catch (InvocationTargetException expected) {}
  }

  public void testInstantiate_factoryMethodThrows() throws Exception {
    try {
      tester.instantiate(FactoryMethodThrows.class);
      fail();
    } catch (InvocationTargetException expected) {}
  }

  public void testInstantiate_constructorParameterNotInstantiable() throws Exception {
    try {
      tester.instantiate(ConstructorParameterNotInstantiable.class);
      fail();
    } catch (ParameterNotInstantiableException expected) {}
  }

  public void testInstantiate_factoryMethodParameterNotInstantiable() throws Exception {
    try {
      tester.instantiate(FactoryMethodParameterNotInstantiable.class);
      fail();
    } catch (ParameterNotInstantiableException expected) {}
  }

  public void testInstantiate_instantiableFactoryMethodChosen() throws Exception {
    assertEquals("good", tester.instantiate(InstantiableFactoryMethodChosen.class).name);
  }

  public void testInterfaceProxySerializable() throws Exception {
    SerializableTester.reserializeAndAssert(tester.instantiate(HasAnInterface.class));
  }

  public void testReturnValuesFromAnotherPackageIgnoredForNullTests() throws Exception {
    new ClassSanityTester().forAllPublicStaticMethods(JdkObjectFactory.class).testNulls();
  }

  /** String doesn't check nulls as we expect. But the framework should ignore. */
  private static class JdkObjectFactory {
    @SuppressWarnings("unused") // Called by reflection
    public static Object create() {
      return new ArrayList<String>();
    }
  }

  static class HasAnInterface implements Serializable {
    private final AnInterface i;

    public HasAnInterface(AnInterface i) {
      this.i = i;
    }

    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof HasAnInterface) {
        HasAnInterface that = (HasAnInterface) obj;
        return i.equals(that.i);
      } else {
        return false;
      }
    }

    @Override public int hashCode() {
      return i.hashCode();
    }
  }

  static class InstantiableFactoryMethodChosen {
    final String name;

    private InstantiableFactoryMethodChosen(String name) {
      this.name = name;
    }

    public InstantiableFactoryMethodChosen(NotInstantiable x) {
      checkNotNull(x);
      this.name = "x1";
    }

    public static InstantiableFactoryMethodChosen create(NotInstantiable x) {
      return new InstantiableFactoryMethodChosen(x);
    }

    public static InstantiableFactoryMethodChosen create(String s) {
      checkNotNull(s);
      return new InstantiableFactoryMethodChosen("good");
    }
  }

  public void testInstantiate_instantiableConstructorChosen() throws Exception {
    assertEquals("good", tester.instantiate(InstantiableConstructorChosen.class).name);
  }

  static class InstantiableConstructorChosen {
    final String name;

    public InstantiableConstructorChosen(String name) {
      checkNotNull(name);
      this.name = "good";
    }

    public InstantiableConstructorChosen(NotInstantiable x) {
      checkNotNull(x);
      this.name = "x1";
    }

    public static InstantiableFactoryMethodChosen create(NotInstantiable x) {
      return new InstantiableFactoryMethodChosen(x);
    }
  }

  static class GoodEquals {

    private final String a;
    private final int b;

    private GoodEquals(String a, int b) {
      this.a = checkNotNull(a);
      this.b = b;
    }

    // ignored by testEquals()
    GoodEquals(@SuppressWarnings("unused") NotInstantiable x) {
      this.a = "x";
      this.b = -1;
    }

    // will keep trying
    public GoodEquals(@SuppressWarnings("unused") NotInstantiable x, int b) {
      this.a = "x";
      this.b = b;
    }

    // keep trying
    @SuppressWarnings("unused")
    static GoodEquals create(int a, int b) {
      throw new RuntimeException();
    }

    // keep trying
    @SuppressWarnings("unused")
    @Nullable public static GoodEquals createMayReturnNull(int a, int b) {
      return null;
    }

    // Good!
    static GoodEquals create(String a, int b) {
      return new GoodEquals(a, b);
    }

    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof GoodEquals) {
        GoodEquals that = (GoodEquals) obj;
        return a.equals(that.a) && b == that.b;
      } else {
        return false;
      }
    }

    @Override public int hashCode() {
      return 0;
    }
  }

  static class BadEquals {

    public BadEquals() {} // ignored by testEquals() since it has less parameters.

    public static BadEquals create(@SuppressWarnings("unused") @Nullable String s) {
      return new BadEquals();
    }

    @Override public boolean equals(@Nullable Object obj) {
      return obj instanceof BadEquals;
    }

    @Override public int hashCode() {
      return 0;
    }
  }

  static class SameIntegerInstance {
    private final Integer i;

    public SameIntegerInstance(Integer i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameIntegerInstance) {
        SameIntegerInstance that = (SameIntegerInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameLongInstance {
    private final Long i;

    public SameLongInstance(Long i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameLongInstance) {
        SameLongInstance that = (SameLongInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameFloatInstance {
    private final Float i;

    public SameFloatInstance(Float i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameFloatInstance) {
        SameFloatInstance that = (SameFloatInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameDoubleInstance {
    private final Double i;

    public SameDoubleInstance(Double i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameDoubleInstance) {
        SameDoubleInstance that = (SameDoubleInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameShortInstance {
    private final Short i;

    public SameShortInstance(Short i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameShortInstance) {
        SameShortInstance that = (SameShortInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameByteInstance {
    private final Byte i;

    public SameByteInstance(Byte i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameByteInstance) {
        SameByteInstance that = (SameByteInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameCharacterInstance {
    private final Character i;

    public SameCharacterInstance(Character i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameCharacterInstance) {
        SameCharacterInstance that = (SameCharacterInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameBooleanInstance {
    private final Boolean i;

    public SameBooleanInstance(Boolean i) {
      this.i = checkNotNull(i);
    }

    @Override public int hashCode() {
      return i.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameBooleanInstance) {
        SameBooleanInstance that = (SameBooleanInstance) obj;
        return i == that.i;
      }
      return false;
    }
  }

  static class SameStringInstance {
    private final String s;

    public SameStringInstance(String s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return s.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameStringInstance) {
        SameStringInstance that = (SameStringInstance) obj;
        return s == that.s;
      }
      return false;
    }
  }

  static class SameObjectInstance {
    private final Object s;

    public SameObjectInstance(Object s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return s.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameObjectInstance) {
        SameObjectInstance that = (SameObjectInstance) obj;
        return s == that.s;
      }
      return false;
    }
  }

  static class SameInterfaceInstance {
    private final Runnable s;

    public SameInterfaceInstance(Runnable s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return s.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameInterfaceInstance) {
        SameInterfaceInstance that = (SameInterfaceInstance) obj;
        return s == that.s;
      }
      return false;
    }
  }

  static class SameListInstance {
    private final List<?> s;

    public SameListInstance(List<?> s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return System.identityHashCode(s);
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof SameListInstance) {
        SameListInstance that = (SameListInstance) obj;
        return s == that.s;
      }
      return false;
    }
  }

  static class UsesReferentialEquality {
    private final ReferentialEquality s;

    public UsesReferentialEquality(ReferentialEquality s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return s.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof UsesReferentialEquality) {
        UsesReferentialEquality that = (UsesReferentialEquality) obj;
        return s == that.s;
      }
      return false;
    }
  }

  static class UsesEnum {
    private final TimeUnit s;

    public UsesEnum(TimeUnit s) {
      this.s = checkNotNull(s);
    }

    @Override public int hashCode() {
      return s.hashCode();
    }

    @Override public boolean equals(Object obj) {
      if (obj instanceof UsesEnum) {
        UsesEnum that = (UsesEnum) obj;
        return s == that.s;
      }
      return false;
    }
  }

  public static class ReferentialEquality {
    public ReferentialEquality() {}
  }

  static class BadEqualsWithParameterizedType {

    // ignored by testEquals() since it has less parameters.
    public BadEqualsWithParameterizedType() {}

    public static BadEqualsWithParameterizedType create(
        @SuppressWarnings("unused") ImmutableList<Iterable<? extends String>> s) {
      return new BadEqualsWithParameterizedType();
    }

    @Override public boolean equals(@Nullable Object obj) {
      return obj instanceof BadEqualsWithParameterizedType;
    }

    @Override public int hashCode() {
      return 0;
    }
  }

  static class GoodNulls {
    public GoodNulls(String s) {
      checkNotNull(s);
    }

    public void rejectNull(String s) {
      checkNotNull(s);
    }
  }

  public static class BadNulls {
    public void failsToRejectNull(@SuppressWarnings("unused") String s) {}
  }

  public static class NoNullCheckNeededDespitNotInstantiable {

    public NoNullCheckNeededDespitNotInstantiable(NotInstantiable x) {
      checkNotNull(x);
    }

    @SuppressWarnings("unused") // reflected
    void primitiveOnly(int i) {}

    @SuppressWarnings("unused") //reflected
    void nullableOnly(@Nullable String s) {}
    public void noParameter() {}

    @SuppressWarnings("unused") //reflected
    void primitiveAndNullable(@Nullable String s, int i) {}
  }

  static class FactoryMethodReturnsNullButNotAnnotated {
    private FactoryMethodReturnsNullButNotAnnotated() {}

    static FactoryMethodReturnsNullButNotAnnotated returnsNull() {
      return null;
    }
  }

  static class FactoryMethodReturnsNullAndAnnotated {
    private FactoryMethodReturnsNullAndAnnotated() {}

    @Nullable public static FactoryMethodReturnsNullAndAnnotated returnsNull() {
      return null;
    }
  }

  static class FactoryMethodAcceptsNull {

    final String name;

    private FactoryMethodAcceptsNull(String name) {
      this.name = name;
    }

    static FactoryMethodAcceptsNull create(@Nullable String name) {
      return new FactoryMethodAcceptsNull(name);
    }
  }

  static class FactoryMethodDoesNotAcceptNull {

    final String name;

    private FactoryMethodDoesNotAcceptNull(String name) {
      this.name = checkNotNull(name);
    }

    public static FactoryMethodDoesNotAcceptNull create(String name) {
      return new FactoryMethodDoesNotAcceptNull(name);
    }
  }

  static class ConstructorAcceptsNull {

    final String name;

    public ConstructorAcceptsNull(@Nullable String name) {
      this.name = name;
    }
  }

  static class ConstructorDoesNotAcceptNull {

    final String name;

    ConstructorDoesNotAcceptNull(String name) {
      this.name = checkNotNull(name);
    }
  }

  static class ConstructorParameterNotInstantiable {
    public ConstructorParameterNotInstantiable(@SuppressWarnings("unused") NotInstantiable x) {}
  }

  static class FactoryMethodParameterNotInstantiable {

    private FactoryMethodParameterNotInstantiable() {}

    static FactoryMethodParameterNotInstantiable create(
        @SuppressWarnings("unused") NotInstantiable x) {
      return new FactoryMethodParameterNotInstantiable();
    }
  }

  static class ConstructorThrows {
    public ConstructorThrows() {
      throw new RuntimeException();
    }
  }

  static class FactoryMethodThrows {
    private FactoryMethodThrows() {}

    public static FactoryMethodThrows create() {
      throw new RuntimeException();
    }
  }

  static class NotInstantiable {
    private NotInstantiable() {}
  }

  private enum NoConstantEnum {}

  private enum OneConstantEnum {
    A
  }

  private enum EnumFailsToCheckNull {
    A;

    @SuppressWarnings("unused") 
    public void failToCheckNull(String s) {}
  }

  private interface AnInterface {}

  private static abstract class AnAbstractClass {
    @SuppressWarnings("unused")
    public AnAbstractClass(String s) {}

    @SuppressWarnings("unused")
    public void failsToCheckNull(String s) {}
  }

  private static class NoPublicStaticMethods {
    static String notPublic() {
      return "";
    }
  }
}