-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDTest.java
More file actions
193 lines (171 loc) · 5.53 KB
/
Copy pathDTest.java
File metadata and controls
193 lines (171 loc) · 5.53 KB
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
import mypackage.A;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Comprehensive test class for D
* Tests inheritance from A and access modifiers behavior
*/
public class DTest {
private D testInstance;
@Before
public void setUp() {
testInstance = new D();
}
/**
* Test that D can be instantiated
*/
@Test
public void testInstantiation() {
assertNotNull("D instance should not be null", testInstance);
}
/**
* Test that D is an instance of A (inheritance)
*/
@Test
public void testInheritanceFromA() {
assertTrue("D should be an instance of A", testInstance instanceof A);
}
/**
* Test that D inherits public fields from A
*/
@Test
public void testPublicFieldAccess() {
testInstance.publicField = 100;
assertEquals("publicField should be accessible and modifiable",
100, testInstance.publicField);
}
/**
* Test that public field has correct default value from A
*/
@Test
public void testPublicFieldDefaultValue() {
assertEquals("publicField should have default value from A",
4, testInstance.publicField);
}
/**
* Test md() method exists and can be called without exception
*/
@Test
public void testMdMethodExecution() {
try {
testInstance.md();
} catch (Exception e) {
fail("md() method should execute without throwing exception");
}
}
/**
* Test that inherited ma() method from A is accessible
*/
@Test
public void testInheritedMaMethod() {
try {
testInstance.ma();
} catch (Exception e) {
fail("Inherited ma() method should be accessible");
}
}
/**
* Test that toString() is inherited from A
*/
@Test
public void testToStringMethod() {
String result = testInstance.toString();
assertNotNull("toString() should return a non-null value", result);
assertTrue("toString() should return a string representation",
result.length() > 0);
}
/**
* Test multiple instances are independent
*/
@Test
public void testMultipleInstances() {
D instance1 = new D();
D instance2 = new D();
instance1.publicField = 100;
instance2.publicField = 200;
assertEquals("instance1 publicField should be 100", 100, instance1.publicField);
assertEquals("instance2 publicField should be 200", 200, instance2.publicField);
}
/**
* Test that D can be assigned to A reference (polymorphism)
*/
@Test
public void testPolymorphism() {
A aReference = new D();
assertNotNull("D instance should be assignable to A reference", aReference);
assertTrue("A reference should actually hold D instance", aReference instanceof D);
}
/**
* Test field modification through parent reference
*/
@Test
public void testFieldModificationThroughParentReference() {
A aReference = new D();
aReference.publicField = 500;
assertEquals("publicField should be modifiable through A reference",
500, aReference.publicField);
}
/**
* Test that package field is NOT accessible from different package
* This test verifies the comment in md() method
*/
@Test
public void testPackageFieldNotAccessibleFromDifferentPackage() {
// This test documents that packageField is not accessible
// from D class because D is in a different package than A
// We can't directly test the inaccessibility, but we verify
// the instance is valid
assertNotNull("Instance should be valid even though packageField is inaccessible",
testInstance);
}
/**
* Test that multiple calls to md() don't cause issues
*/
@Test
public void testMultipleMdCalls() {
testInstance.md();
testInstance.md();
testInstance.md();
// If no exception is thrown, test passes
assertTrue("Multiple md() calls should complete successfully", true);
}
/**
* Test equality of default values across instances
*/
@Test
public void testDefaultValuesConsistency() {
D instance1 = new D();
D instance2 = new D();
assertEquals("Default publicField should be same across instances",
instance1.publicField, instance2.publicField);
}
/**
* Test that D instance is not null after calling md()
*/
@Test
public void testInstanceIntegrityAfterMethodCall() {
testInstance.md();
assertNotNull("Instance should remain valid after md() call", testInstance);
assertEquals("publicField should retain default value after md()",
4, testInstance.publicField);
}
/**
* Test edge case: very large value for publicField
*/
@Test
public void testPublicFieldLargeValue() {
testInstance.publicField = Integer.MAX_VALUE;
assertEquals("publicField should handle Integer.MAX_VALUE",
Integer.MAX_VALUE, testInstance.publicField);
}
/**
* Test edge case: very small value for publicField
*/
@Test
public void testPublicFieldSmallValue() {
testInstance.publicField = Integer.MIN_VALUE;
assertEquals("publicField should handle Integer.MIN_VALUE",
Integer.MIN_VALUE, testInstance.publicField);
}
}