/
SuggesterTest.java
832 lines (796 loc) · 26.9 KB
/
SuggesterTest.java
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
/*
* Copyright 2021 The Error Prone 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.errorprone.bugpatterns.inlineme;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for the {@link Suggester}. */
@RunWith(JUnit4.class)
public class SuggesterTest {
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(Suggester.class, getClass());
@Test
public void testBuildAnnotation_withImports() {
assertThat(
InlineMeData.buildAnnotation(
"REPLACEMENT",
ImmutableSet.of("java.time.Duration", "java.time.Instant"),
ImmutableSet.of()))
.isEqualTo(
"@InlineMe(replacement = \"REPLACEMENT\", "
+ "imports = {\"java.time.Duration\", \"java.time.Instant\"})\n");
}
@Test
public void testBuildAnnotation_withSingleImport() {
assertThat(
InlineMeData.buildAnnotation(
"REPLACEMENT", ImmutableSet.of("java.time.Duration"), ImmutableSet.of()))
.isEqualTo(
"@InlineMe(replacement = \"REPLACEMENT\", " + "imports = \"java.time.Duration\")\n");
}
@Test
public void testInstanceMethodNewImport() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" private Duration deadline = Duration.ofSeconds(5);",
" @Deprecated",
" public void setDeadline(long millis) {",
" setDeadline(Duration.ofMillis(millis));",
" }",
" public void setDeadline(Duration deadline) {",
" this.deadline = deadline;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" private Duration deadline = Duration.ofSeconds(5);",
" @InlineMe(replacement = \"this.setDeadline(Duration.ofMillis(millis))\","
+ " imports = \"java.time.Duration\")",
" @Deprecated",
" public void setDeadline(long millis) {",
" setDeadline(Duration.ofMillis(millis));",
" }",
" public void setDeadline(Duration deadline) {",
" this.deadline = deadline;",
" }",
"}")
.doTest();
}
@Test
public void testStaticMethodInNewClass() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" @Deprecated",
" public Duration fromMillis(long millis) {",
" return Duration.ofMillis(millis);",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" @InlineMe(",
" replacement = \"Duration.ofMillis(millis)\", ",
" imports = \"java.time.Duration\")",
" @Deprecated",
" public Duration fromMillis(long millis) {",
" return Duration.ofMillis(millis);",
" }",
"}")
.doTest();
}
@Test
public void testProtectedConstructor() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" protected Client() {}",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testReturnField() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" @Deprecated",
" public Duration getZero() {",
" return Duration.ZERO;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" @InlineMe(replacement = \"Duration.ZERO\", imports = \"java.time.Duration\")",
" @Deprecated",
" public Duration getZero() {",
" return Duration.ZERO;",
" }",
"}")
.doTest();
}
@Test
public void testImplementationSplitOverMultipleLines() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"import java.time.Instant;",
"public final class Client {",
" @Deprecated",
" public Duration getElapsed() {",
" return Duration.between(",
" Instant.ofEpochMilli(42),",
" Instant.now());",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"import java.time.Instant;",
"public final class Client {",
" @InlineMe(",
" replacement = \"Duration.between(Instant.ofEpochMilli(42), Instant.now())\", ",
" imports = {\"java.time.Duration\", \"java.time.Instant\"})",
" @Deprecated",
" public Duration getElapsed() {",
" return Duration.between(",
" Instant.ofEpochMilli(42),",
" Instant.now());",
" }",
"}")
.doTest();
}
@Test
public void testAnonymousClass() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" public Object getUselessObject() {",
" return new Object() {",
" @Override",
" public int hashCode() {",
" return 42;",
" }",
" };",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testMethodReference() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"import java.util.Optional;",
"public final class Client {",
" @Deprecated",
" public Optional<Duration> silly(Optional<Long> input) {",
" return input.map(Duration::ofMillis);",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"import java.util.Optional;",
"public final class Client {",
" @InlineMe(replacement = \"input.map(Duration::ofMillis)\", ",
" imports = \"java.time.Duration\")",
" @Deprecated",
" public Optional<Duration> silly(Optional<Long> input) {",
" return input.map(Duration::ofMillis);",
" }",
"}")
.doTest();
}
@Test
public void testNewClass() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import org.joda.time.Instant;",
"public final class Client {",
" @Deprecated",
" public Instant silly() {",
" return new Instant();",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import org.joda.time.Instant;",
"public final class Client {",
" @InlineMe(replacement = \"new Instant()\", imports = \"org.joda.time.Instant\")",
" @Deprecated",
" public Instant silly() {",
" return new Instant();",
" }",
"}")
.doTest();
}
@Test
public void testNewArray() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import org.joda.time.Instant;",
"public final class Client {",
" @Deprecated",
" public Instant[] silly() {",
" return new Instant[42];",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import org.joda.time.Instant;",
"public final class Client {",
" @InlineMe(replacement = \"new Instant[42]\", imports = \"org.joda.time.Instant\")",
" @Deprecated",
" public Instant[] silly() {",
" return new Instant[42];",
" }",
"}")
.doTest();
}
@Test
public void testNewNestedClass() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" public NestedClass silly() {",
" return new NestedClass();",
" }",
" public static class NestedClass {}",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"public final class Client {",
" @InlineMe(replacement = \"new NestedClass()\", ",
" imports = \"com.google.frobber.Client.NestedClass\")",
" @Deprecated",
" public NestedClass silly() {",
" return new NestedClass();",
" }",
" public static class NestedClass {}",
"}")
.doTest();
}
@Test
public void testReturnStringLiteral() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" public String getName() {",
" return \"kurt\";",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"public final class Client {",
" @InlineMe(replacement = \"\\\"kurt\\\"\")",
" @Deprecated",
" public String getName() {",
" return \"kurt\";",
" }",
"}")
.doTest();
}
@Test
public void testCallMethodWithStringLiteral() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" public String getName() {",
" return getName(\"kurt\");",
" }",
" public String getName(String defaultValue) {",
" return \"test\";",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"public final class Client {",
" @InlineMe(replacement = \"this.getName(\\\"kurt\\\")\")",
" @Deprecated",
" public String getName() {",
" return getName(\"kurt\");",
" }",
" public String getName(String defaultValue) {",
" return \"test\";",
" }",
"}")
.doTest();
}
@Test
public void testReturnPrivateVariable() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" private final Duration myDuration = Duration.ZERO;",
" @Deprecated",
" public Duration getMyDuration() {",
" return myDuration;",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testReturnPrivateVariable_qualifiedWithThis() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" private final Duration myDuration = Duration.ZERO;",
" @Deprecated",
" public Duration getMyDuration() {",
" return this.myDuration;",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testSettingPrivateVariable() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" private Duration duration = Duration.ZERO;",
" @Deprecated",
" public void setDuration(Duration duration) {",
" this.duration = duration;",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testDelegateToParentClass() {
refactoringTestHelper
.addInputLines(
"Parent.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public class Parent {",
" private Duration duration = Duration.ZERO;",
" public final Duration after() {",
" return duration;",
" }",
"}")
.expectUnchanged()
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client extends Parent {",
" private Duration duration = Duration.ZERO;",
" @Deprecated",
" public final Duration before() {",
" return after();",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client extends Parent {",
" private Duration duration = Duration.ZERO;",
" @InlineMe(replacement = \"this.after()\")",
" @Deprecated",
" public final Duration before() {",
" return after();",
" }",
"}")
.doTest();
}
@Test
public void testWithCast() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" @Deprecated",
" public void setDuration(Object duration) {",
" foo((Duration) duration);",
" }",
" public void foo(Duration duration) {",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" @InlineMe(replacement = \"this.foo((Duration) duration)\", imports ="
+ " \"java.time.Duration\")",
" @Deprecated",
" public void setDuration(Object duration) {",
" foo((Duration) duration);",
" }",
" public void foo(Duration duration) {",
" }",
"}")
.doTest();
}
@Test
public void testAccessPrivateVariable() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" private final Duration myDuration = Duration.ZERO;",
" @Deprecated",
" public boolean silly() {",
" return myDuration.isZero();",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testAccessPrivateMethod() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
" @Deprecated",
" public boolean silly() {",
" return privateDelegate();",
" }",
" private boolean privateDelegate() {",
" return false;",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testTryWithResources() {
refactoringTestHelper
.addInputLines(
"Client.java",
"import java.io.BufferedReader;",
"import java.io.FileReader;",
"import java.io.IOException;",
"public class Client {",
" @Deprecated",
" public String readLine(String path) throws IOException {",
" try (BufferedReader br = new BufferedReader(new FileReader(path))) {",
" return br.readLine();",
" }",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testIfStatement() {
refactoringTestHelper
.addInputLines(
"Client.java",
"public class Client {",
" @Deprecated",
" public void foo(String input) {",
" if (input.equals(\"hi\")) {",
" return;",
" }",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testNestedBlock() {
refactoringTestHelper
.addInputLines(
"Client.java",
"public class Client {",
" @Deprecated",
" public String foo(String input) {",
" {",
" return input.toLowerCase();",
" }",
" }",
"}")
.expectUnchanged()
.doTest();
}
@Test
public void testTernaryOverMultipleLines() {
// This is suggested/works only if we allow users to use the argument more than once
refactoringTestHelper
.setArgs("-XepOpt:" + InlinabilityResult.DISALLOW_ARGUMENT_REUSE + "=false")
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" @Deprecated",
" public Duration getDeadline(Duration deadline) {",
" return deadline.compareTo(Duration.ZERO) > 0",
" ? deadline",
" : Duration.ZERO;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" @InlineMe(replacement = \""
+ "deadline.compareTo(Duration.ZERO) > 0 ? deadline : Duration.ZERO\", ",
"imports = \"java.time.Duration\")",
" @Deprecated",
" public Duration getDeadline(Duration deadline) {",
" return deadline.compareTo(Duration.ZERO) > 0",
" ? deadline",
" : Duration.ZERO;",
" }",
"}")
.doTest();
}
@Test
public void testStaticCallingAnotherQualifiedStatic() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.time.Duration;",
"public final class Client {",
" @Deprecated",
" public static Duration getDeadline() {",
" return Client.getDeadline2();",
" }",
" public static Duration getDeadline2() {",
" return Duration.ZERO;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.time.Duration;",
"public final class Client {",
" @InlineMe(replacement = \"Client.getDeadline2()\", ",
" imports = \"com.google.frobber.Client\")",
" @Deprecated",
" public static Duration getDeadline() {",
" return Client.getDeadline2();",
" }",
" public static Duration getDeadline2() {",
" return Duration.ZERO;",
" }",
"}")
.doTest();
}
@Test
public void staticReferenceToJavaLang() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import static java.lang.String.format;",
"public final class Client {",
" @Deprecated",
" public static String myFormat(String template, String arg) {",
" return format(template, arg);",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import static java.lang.String.format;",
"import com.google.errorprone.annotations.InlineMe;",
"public final class Client {",
" @InlineMe(replacement = \"format(template, arg)\", staticImports ="
+ " \"java.lang.String.format\")",
" @Deprecated",
" public static String myFormat(String template, String arg) {",
" return format(template, arg);",
" }",
"}")
.doTest();
}
@Test
public void replacementContainsGenericInvocation() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"import java.util.ArrayList;",
"import java.util.List;",
"public final class Client {",
" @Deprecated",
" public static List<Void> newArrayList() {",
" return new ArrayList<Void>();",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"import java.util.ArrayList;",
"import java.util.List;",
"public final class Client {",
" @InlineMe(replacement = \"new ArrayList<Void>()\", imports ="
+ " \"java.util.ArrayList\")",
" @Deprecated",
" public static List<Void> newArrayList() {",
" return new ArrayList<Void>();",
" }",
"}")
.doTest();
}
@Test
public void suggestedFinalOnOtherwiseGoodMethod() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public class Client {",
" @Deprecated",
" public int method() {",
" return 42;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"public class Client {",
" @InlineMe(replacement = \"42\")",
" @Deprecated",
" public final int method() {",
" return 42;",
" }",
"}")
.doTest();
}
@Test
public void dontSuggestOnDefaultMethods() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public interface Client {",
" @Deprecated",
" public default int method() {",
" return 42;",
" }",
"}")
.expectUnchanged()
.doTest();
}
// Since constructors can't be "overridden" in the same way as other non-final methods, it's
// OK to inline them even if there could be a subclass of the surrounding class.
@Test
public void deprecatedConstructorInNonFinalClass() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public class Client {",
" @Deprecated",
" public Client() {",
" this(42);",
" }",
" public Client(int value) {}",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.InlineMe;",
"public class Client {",
" @InlineMe(replacement = \"this(42)\")",
" @Deprecated",
" public Client() {",
" this(42);",
" }",
" public Client(int value) {}",
"}")
.doTest();
}
@Test
public void publicStaticFactoryCallsPrivateConstructor() {
refactoringTestHelper
.addInputLines(
"Client.java",
"package com.google.frobber;",
"public class Client {",
" @Deprecated",
" public static Client create() {",
" return new Client();",
" }",
" private Client() {}",
"}")
.expectUnchanged()
.doTest();
}
}