forked from akrotov-education/practice-autumn-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask06Test.java
104 lines (87 loc) · 3.08 KB
/
Task06Test.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
package lesson02.part02;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import util.ReadFileUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
@RunWith(JUnit4.class)
public class Task06Test {
@Test
public void checkCatFields() {
Field[] fields = Task06.Cat.class.getDeclaredFields();
Assert.assertTrue("Class Cat must contain only one field 'fullName'",
fields.length == 1 && fields[0].getName().contentEquals("fullName")
);
}
@Test
public void checkFullNameField() {
try {
Field field = Task06.Cat.class.getDeclaredField("fullName");
String modifier = Modifier.toString(field.getModifiers());
String type = field.getType().toString();
Assert.assertEquals("Field fullName must be private",
"private",
modifier
);
Assert.assertTrue("Field fullName must have String type",
type.contains("String")
);
} catch (NoSuchFieldException e) {
Assert.fail("Class Cat doesn't contain fullName field");
}
}
@Test
public void checkClassMethods() {
Method[] methods = Task06.Cat.class.getDeclaredMethods();
Assert.assertTrue("Class Cat must contain only one method setName",
methods.length == 1 && methods[0].getName().contentEquals("setName")
);
}
@Test
public void checkLocalVar() {
List<String> lines = ReadFileUtil.readFileInList("./src/main/java/lesson02/part02/Task06.java");
int startLine = 0;
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).contains("public void setName")) {
startLine = i;
break;
}
}
Boolean haveLocalVar = false;
for (int i = startLine; i < lines.size(); i++) {
String line = lines.get(i);
if (line.contains("}")) {
break;
}
if (line.contains("String fullName")) {
haveLocalVar = true;
break;
}
}
Assert.assertTrue("Method setName should contain local variable fullName",
haveLocalVar
);
}
@Test
public void checkSetNameWork() {
Task06.Cat cat = new Task06.Cat();
cat.setName("Kek", "Kekovich");
try {
Field field = cat.getClass().getDeclaredField("fullName");
field.setAccessible(true);
String val = (String) field.get(cat);
Assert.assertEquals("Method setName should set value of local variable fullName to field fullName",
"Kek Kekovich",
val
);
} catch (NoSuchFieldException e) {
Assert.fail("Class Cat doesn't contain fullName field");
} catch (IllegalAccessException e) {
Assert.fail("Can't get access to field");
}
}
}