-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRefactoringTest.java
362 lines (306 loc) · 9.03 KB
/
RefactoringTest.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
package com.example.refactorings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.*;
public class RefactoringTest {
@Test
@DisplayName("change signature")
void changeSignature() {
// use height, width, length when calculating volume
Cage cage = new Cage(1.0, 2.0);
Assertions.assertEquals(2.0, cage.getVolume());
}
interface Inf {
int publicMethod();
}
class Class {
public Inf method() {
final int i = 0;
return new Inf() {
public int publicMethod() {
return i;
}
};
}
}
@Test
@DisplayName("convert anonymous to inner class")
void convertAnonymousToInnerClass() {
}
@Test
@DisplayName("Convert to instance method")
void convertToInstanceMethod() {
new ConvertToInstanceMethod();
}
@Test
@DisplayName("encapsulate fields")
void encapsulateFields() {
class YourClass {
public String aString;
}
}
@Test
@DisplayName("introduce constants")
void introduceConstants() {
List<String> strings = new ArrayList<>();
strings.add("string");
strings.add("string");
}
@Test
@DisplayName("introduce field")
void introduceField() {
class AnotherClass {
public int intValue() {
throw new UnsupportedOperationException();
}
}
class Class {
AnotherClass anotherClass;
public void method1() {
int a = 1;
// anotherClass.intValue() is duplicated
int b = a + anotherClass.intValue();
int c = b + anotherClass.intValue();
}
}
}
@Test
@DisplayName("extract interface")
void extractInterface() {
class AClass {
public static final double CONSTANT = 3.14;
public void publicMethod() {//some code here
}
public void secretMethod() {//some code here}
}
}
}
@Test
@DisplayName("extract method")
void extractMethod() {
new ExtractMethodSample();
}
@Test
@DisplayName("extract superclass")
void extractSuperclass() {
class MyClass {
public int varInt;
private double varDouble;
public static final int CONSTANT = 0;
// should be abstract in super class
public void publicMethod() {
}
public void hiddenMethod() {
}
public void setVarDouble(double var) {
this.varDouble = var;
}
public double getVarDouble() {
return varDouble;
}
}
}
@Test
@DisplayName("introduce variable")
void extractIntroduceVariable() {
Long anotherClass = 1l;
int a = 1;
// duplicated
int b = a + anotherClass.intValue();
int c = b + anotherClass.intValue();
}
@Test
@DisplayName("introduce parameter")
void extractParameter() {
class HelloWorldPrinter {
public void print() {
System.out.println(generateText());
}
private String generateText() {
// message
return "Hello, World!".toUpperCase();
}
}
}
@Test
@DisplayName("extract delegate")
void extractDelegate() {
new ExtractDelegateSample();
}
@Test
@DisplayName("extract method object")
void extractMethodObject() {
sort(Arrays.asList(1, 9, 3, 5));
}
private List<Integer> sort(List<Integer> list) {
List<Integer> sorted = new ArrayList<>();
if (list.size() == 0)
return list;
else {
// move declaraton of X closer to usages
List<Integer> l = new ArrayList<>();
int m = list.get(0);
List<Integer> h = new ArrayList<>();
for (int i : list.subList(1, list.size())) {
if (i < m)
l.add(i);
else
h.add(i);
}
if (l != null) sorted.addAll(sort(l));
sorted.add(m);
if (h != null) sorted.addAll(sort(h));
}
return sorted;
}
@Test
@DisplayName("replace conditional logic with strategy pattern")
void replaceConditionalLogicWithStrategyPattern() {
ConditionalLogicExistingClass calculator = new ConditionalLogicExistingClass();
Assertions.assertEquals(1_825, calculator.calculateInsurance(5_000), "low");
Assertions.assertEquals(38_600, calculator.calculateInsurance(25_000), "medium");
Assertions.assertEquals(78_500, calculator.calculateInsurance(50_000), "high");
Assertions.assertEquals(106_400, calculator.calculateInsurance(100_000), "very high");
/**
* 1. Extract Method Object: class로 추출. InsuranceCalculator
* 2. Extract Methods: getAdjustment, getWeight, getConstants
* - template method primitive operations
* 3. factory method of 제공
* - 서브 클래스들을 필요하게 만듦
* 4. push members down(primitive operations)
* 5. coverage를 보면서 unused code 삭제
*/
}
@Test
@DisplayName("generify refactoring transform existing code that does not use Generics, into the Generics-aware code ")
void generifyRefactoringTransformExistingCodeThatDoesNotUseGenericsIntoTheGenericsAwareCode() {
List list = new LinkedList();
list.add("string");
}
@Test
@DisplayName("inline constructor, superclass, anonymous class")
void inlineVariableMethodConstructorSuperclassAnonymousClass() {
class Class {
public int varInt;
// inline constructor
public Class() {
this(0);
}
public Class(int i) {
varInt = i;
}
public void method() {
Class aClass = new Class();
}
}
class Bar {
int calculations1() {
return 0;
}
int calculations2() {
return 0;
}
}
// inline superclass(refactor at Bar)
class Foo extends Bar {
int someMethod() {
int something = 0;
if (something > calculations1()) {
return calculations2();
}
return -1;
}
}
class MyComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return 0;
}
}
List<String> scores = new ArrayList();
// inline anonymous class
scores.sort(new MyComparator());
}
@Test
@DisplayName("invert boolean")
void invertBoolean() {
System.out.println(method(0));
}
// invert boolean
boolean method(int a) {
if (a > 15 && a < 100) {
return true;
}
return false;
}
@Test
@DisplayName("make static")
void makeStatic() {
new MakeStaticSample();
}
@Test
@DisplayName("migrate")
void migrate() {
// migrate / edit
}
@Test
@DisplayName("copy class and move method")
void copyAndMove() {
new CopyClassSample();
}
@Test
@DisplayName("pull members up")
void pullMembersUp() {
new PullMembersUpSample();
}
@Test
@DisplayName("remove middleman")
void removeMiddleman() {
new RemoveMiddlemanSample();
}
@Test
@DisplayName("replace constructor with builder")
void replaceConstructorWithBuilder() {
new ReplaceConstructorWithBuilderSample("Red Delicious").saying();
}
@Test
@DisplayName("replace constructor with factory method")
void replaceConstructorWithFactoryMethod() {
new ReplaceConstructorWithBuilderSample("Red Delicious").saying();
}
@Test
@DisplayName("replace inheritance with delegation")
void replaceInheritanceWithDelegation() {
new ReplaceInheritanceWithDelegationSample();
}
@Test
@DisplayName("replace temp with query")
void replaceTempWithQuery() {
printConcatedString("str");
}
void printConcatedString(String str) {
String originalString = "Original String ";
String result = originalString.concat(str);
System.out.println(result);
}
@Test
@DisplayName("type migration")
void typeMigration() {
someMethodToTypeMigrate(0);
}
void someMethodToTypeMigrate(int i) {
}
@Test
@DisplayName("wrap return value")
void wrapReturnValue() {
}
class AbstractOrder {
String customer;
}
class Order extends AbstractOrder {
String getCustomer() {
return customer;
}
}
}