-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAnyXExceptionTest.java
More file actions
242 lines (214 loc) · 6.94 KB
/
Copy pathAnyXExceptionTest.java
File metadata and controls
242 lines (214 loc) · 6.94 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
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
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Comprehensive test class for AnyXException
* Tests the custom RuntimeException for indicating "any X" solution scenarios
*/
public class AnyXExceptionTest {
/**
* Test that AnyXException can be instantiated
*/
@Test
public void testInstantiation() {
AnyXException exception = new AnyXException();
assertNotNull("AnyXException instance should not be null", exception);
}
/**
* Test that AnyXException is an instance of RuntimeException
*/
@Test
public void testIsRuntimeException() {
AnyXException exception = new AnyXException();
assertTrue("AnyXException should be an instance of RuntimeException",
exception instanceof RuntimeException);
}
/**
* Test that AnyXException is an instance of Exception
*/
@Test
public void testIsException() {
AnyXException exception = new AnyXException();
assertTrue("AnyXException should be an instance of Exception",
exception instanceof Exception);
}
/**
* Test that AnyXException is an instance of Throwable
*/
@Test
public void testIsThrowable() {
AnyXException exception = new AnyXException();
assertTrue("AnyXException should be an instance of Throwable",
exception instanceof Throwable);
}
/**
* Test that AnyXException can be thrown
*/
@Test(expected = AnyXException.class)
public void testCanBeThrown() {
throw new AnyXException();
}
/**
* Test that AnyXException can be caught as RuntimeException
*/
@Test
public void testCatchAsRuntimeException() {
try {
throw new AnyXException();
} catch (RuntimeException e) {
assertTrue("Caught exception should be AnyXException",
e instanceof AnyXException);
}
}
/**
* Test that AnyXException can be caught as Exception
*/
@Test
public void testCatchAsException() {
try {
throw new AnyXException();
} catch (Exception e) {
assertTrue("Caught exception should be AnyXException",
e instanceof AnyXException);
}
}
/**
* Test that AnyXException doesn't require throws declaration (RuntimeException property)
*/
@Test
public void testNoThrowsRequired() {
// This method doesn't declare "throws AnyXException" but can still throw it
// This is valid because AnyXException extends RuntimeException
methodThatThrowsAnyXException();
}
private void methodThatThrowsAnyXException() {
// Method that can throw AnyXException without declaring it
// This demonstrates that it's a RuntimeException
}
/**
* Test getMessage() returns null when no message is provided
*/
@Test
public void testGetMessageWithNoMessage() {
AnyXException exception = new AnyXException();
assertNull("getMessage() should return null when no message provided",
exception.getMessage());
}
/**
* Test that exception can be re-thrown
*/
@Test(expected = AnyXException.class)
public void testReThrow() {
try {
throw new AnyXException();
} catch (AnyXException e) {
throw e; // Re-throw
}
}
/**
* Test multiple instances are independent
*/
@Test
public void testMultipleInstances() {
AnyXException exception1 = new AnyXException();
AnyXException exception2 = new AnyXException();
assertNotNull(exception1);
assertNotNull(exception2);
assertNotSame("Two instances should be different objects",
exception1, exception2);
}
/**
* Test that exception class name is correct
*/
@Test
public void testClassName() {
AnyXException exception = new AnyXException();
assertEquals("Class name should be AnyXException",
"AnyXException", exception.getClass().getSimpleName());
}
/**
* Test that toString() contains class name
*/
@Test
public void testToString() {
AnyXException exception = new AnyXException();
String toString = exception.toString();
assertNotNull("toString() should not be null", toString);
assertTrue("toString() should contain class name",
toString.contains("AnyXException"));
}
/**
* Test exception in try-catch-finally
*/
@Test
public void testTryCatchFinally() {
boolean finallyCalled = false;
boolean catchCalled = false;
try {
throw new AnyXException();
} catch (AnyXException e) {
catchCalled = true;
} finally {
finallyCalled = true;
}
assertTrue("Catch block should be executed", catchCalled);
assertTrue("Finally block should be executed", finallyCalled);
}
/**
* Test stack trace is available
*/
@Test
public void testStackTrace() {
AnyXException exception = new AnyXException();
StackTraceElement[] stackTrace = exception.getStackTrace();
assertNotNull("Stack trace should not be null", stackTrace);
assertTrue("Stack trace should contain elements",
stackTrace.length > 0);
}
/**
* Test getCause() returns null when no cause is set
*/
@Test
public void testGetCauseWithNoCause() {
AnyXException exception = new AnyXException();
assertNull("getCause() should return null when no cause is set",
exception.getCause());
}
/**
* Test exception is serializable (inherits from Throwable)
*/
@Test
public void testIsSerializable() {
AnyXException exception = new AnyXException();
assertTrue("AnyXException should be Throwable",
exception instanceof Throwable);
// Throwable implements Serializable
}
/**
* Test that throwing AnyXException interrupts normal flow
*/
@Test
public void testInterruptsNormalFlow() {
boolean normalFlowCompleted = false;
try {
throw new AnyXException();
normalFlowCompleted = true; // This line should never be reached
} catch (AnyXException e) {
// Exception caught
}
assertFalse("Normal flow should be interrupted",
normalFlowCompleted);
}
/**
* Test usage context: equation with any X as solution (0 = 0)
*/
@Test(expected = AnyXException.class)
public void testUsageContextAnyXSolution() {
// This exception is thrown when equation is 0 = 0 (any X is a solution)
double a = 0.0;
double b = 0.0;
double c = 0.0;
if (a == 0.0 && b == 0.0 && c == 0.0) {
throw new AnyXException(); // Any X is a solution
}
}
}