-
Notifications
You must be signed in to change notification settings - Fork 0
/
Employee_test.java
530 lines (458 loc) · 19.3 KB
/
Employee_test.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
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Scanner;
import junit.framework.TestCase;
public class Employee_test extends TestCase {
public static String newline = System.getProperty("line.separator");
public void testMemberPresence() throws NoSuchFieldException {
try {
Employee.class.getDeclaredField("firstName");
Employee.class.getDeclaredField("lastName");
Employee.class.getDeclaredField("monthlySalary");
} catch (SecurityException e) {
fail("One of the required methods does not exist or is improperly defined. Please refer to the sample solution for more information.");
}
}
public void testMemberAccessibility() {
try {
assertTrue("firstName should be private", Modifier.isPrivate(Employee.class.getDeclaredField("firstName").getModifiers()));
assertTrue("lastName should be private", Modifier.isPrivate(Employee.class.getDeclaredField("lastName").getModifiers()));
assertTrue("monthlySalary should be private", Modifier.isPrivate(Employee.class.getDeclaredField("monthlySalary").getModifiers()));
} catch (SecurityException e) {
assertThrowableTestFailure(e);
} catch (NoSuchFieldException e) {
fail("One of the required fields does not exist or is improperly defined. Please refer to the sample solution for more information.");
}
}
public void testMethodAccessibility() {
try {
assertTrue("getFirstName should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("getFirstName").getModifiers()));
assertTrue("getLastName should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("getLastName").getModifiers()));
assertTrue("getMonthlySalary should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("getMonthlySalary").getModifiers()));
assertTrue("setFirstName should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("setFirstName", String.class).getModifiers()));
assertTrue("setLastName should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("setLastName", String.class).getModifiers()));
assertTrue("setMonthlySalary should be public", Modifier.isPublic(Employee.class.getDeclaredMethod("setMonthlySalary", Double.TYPE).getModifiers()));
} catch (SecurityException e) {
assertThrowableTestFailure(e);
} catch (NoSuchMethodException e) {
fail("One of the required methods does not exist or is improperly defined. Please refer to the sample solution for more information.");
}
}
public void testConstructorBaseCase() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
try {
Field fFirst = Employee.class.getDeclaredField("firstName");
fFirst.setAccessible(true);
Field fLast = Employee.class.getDeclaredField("lastName");
fLast.setAccessible(true);
Field fSalary = Employee.class.getDeclaredField("monthlySalary");
fSalary.setAccessible(true);
assertEquals(fFirst.get(employee), first);
assertEquals(fLast.get(employee), last);
assertEquals(fSalary.get(employee), salary);
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testConstructorNegativeSalary() {
final String first = "John";
final String last = "Doe";
final double salary = -1000;
Employee employee = new Employee(first, last, salary);
try {
Field fSalary = Employee.class.getDeclaredField("monthlySalary");
fSalary.setAccessible(true);
assertEquals(0.0, fSalary.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testGetFirstName() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
assertEquals("getFirstName did not return the expected value", first, employee.getFirstName());
}
public void testGetLastName() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
assertEquals("getLastName did not return the expected value", last, employee.getLastName());
}
public void testGetMonthlySalary() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
assertEquals("getMonthlySalary did not return the expected value", salary, employee.getMonthlySalary());
}
public void testSetFirstName() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
final String first2 = "Jane";
employee.setFirstName(first2);
Field fFirst;
try {
fFirst = Employee.class.getDeclaredField("firstName");
fFirst.setAccessible(true);
assertEquals("setFirstName did not return the expected value", first2, fFirst.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testSetLastName() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
final String last2 = "Darling";
employee.setLastName(last2);
Field fLast;
try {
fLast = Employee.class.getDeclaredField("lastName");
fLast.setAccessible(true);
assertEquals("setLastName did not return the expected value", last2, fLast.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testSetMonthlySalaryBase() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
final double salary2 = 8675309;
employee.setMonthlySalary(salary2);
Field fSalary;
try {
fSalary = Employee.class.getDeclaredField("monthlySalary");
fSalary.setAccessible(true);
assertEquals("setMonthlySalary did not return the expected value", salary2, fSalary.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testSetMonthlySalaryNegative() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
final double salary2 = -1000;
employee.setMonthlySalary(salary2);
Field fSalary;
try {
fSalary = Employee.class.getDeclaredField("monthlySalary");
fSalary.setAccessible(true);
assertEquals("setMonthlySalary did not return the expected value: " + salary, salary, fSalary.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testSetMonthlySalaryZero() {
final String first = "John";
final String last = "Doe";
final double salary = 1000;
Employee employee = new Employee(first, last, salary);
final double salary2 = 0;
employee.setMonthlySalary(salary2);
Field fSalary;
try {
fSalary = Employee.class.getDeclaredField("monthlySalary");
fSalary.setAccessible(true);
assertEquals("setMonthlySalary did not return the expected value: " + salary, salary, fSalary.get(employee));
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testYearlySalaryOutputBen() {
final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOut));
final double expectedInitialSalaryBen = 14814.72;
String[] arr = {};
EmployeeTest.main(arr);
Scanner sc = new Scanner(capturedOut.toString());
try {
/* FIRST LINE
*/
try { // Skip to Ben's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's salary not present as expected");
}
final double bensSalary = sc.nextDouble();
final double delta = expectedInitialSalaryBen/12 * .01;
if (expectedInitialSalaryBen/12 - delta <= bensSalary && bensSalary <= expectedInitialSalaryBen/12 + delta) {
fail ("The program's output is non-conformant: monthly salaries are being displayed (yearly salaries are required by the assignment prompt)");
}
assertEquals("The program's output is non-conformant: Ben's salary not as specified", expectedInitialSalaryBen, bensSalary, expectedInitialSalaryBen * .01);
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testRaiseOutput() {
final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOut));
String[] arr = {};
EmployeeTest.main(arr);
Scanner sc = new Scanner(capturedOut.toString());
try {
/* FIRST LINE
*/
assertEquals("The program's output is non-conformant: no output was recorded", sc.hasNext(), true);
sc.next();
try { // Skip to Ben's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: last name and descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's salary not present as expected");
}
final double bensSalary = sc.nextDouble();
/* SECOND LINE
*/
try { // Skip to Paul's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Paul's salary not present as expected");
}
final double paulsSalary = sc.nextDouble();
/* FOURTH LINE
*/
try { // Skip to Ben's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's raised salary not present as expected");
}
final double bensRaisedSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Ben's raised salary not as specified", bensSalary * 1.1, bensRaisedSalary, bensSalary * 1.1 * .01);
/* FIFTH LINE
*/
try { // Skip to Paul's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Pauls's raised salary not present as expected");
}
final double paulsRaisedSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Paul's raised salary not as specified", paulsSalary * 1.1, paulsRaisedSalary, paulsSalary * 1.1 * .01);
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testAttemptNegativeSalaryOutput() {
final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOut));
String[] arr = {};
EmployeeTest.main(arr);
Scanner sc = new Scanner(capturedOut.toString());
try {
/* FIRST LINE
*/
assertEquals("The program's output is non-conformant: no output was recorded", sc.hasNext(), true);
sc.next();
try { // Skip to Ben's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: last name and descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's salary not present as expected");
}
sc.nextDouble(); // Read past Ben's salary
/* SECOND LINE
*/
try { // Skip to Paul's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Paul's salary not present as expected");
}
sc.nextDouble(); // Read past Paul's salary
/* FOURTH LINE
*/
try { // Skip to Ben's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's raised salary not present as expected");
}
final double bensRaisedSalary = sc.nextDouble();
/* FIFTH LINE
*/
try { // Skip to Paul's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Pauls's raised salary not present as expected");
}
sc.nextDouble(); // Read past Paul's raised salary
/* SEVENTH LINE
*/
try { // Skip to Ben's still non-negative salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's raised salary not present as expected");
}
final double bensFinalSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Ben's decidedly non-negative salary not as specified", bensRaisedSalary, bensFinalSalary, bensRaisedSalary * .01);
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
public void testEmployeeOutputFormat() {
final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOut));
String[] arr = {};
EmployeeTest.main(arr);
Scanner sc = new Scanner(capturedOut.toString());
try {
/* FIRST LINE
*/
assertEquals("The program's output is non-conformant: no output was recorded", sc.hasNext(), true);
String firstLine = sc.nextLine().toLowerCase();
assertEquals("The program's output is non-conformant: first line does not contain \"Benjamin\"", true, firstLine.contains("benjamin"));
assertEquals("The program's output is non-conformant: first line does not contain \"Bernanke\"", true, firstLine.contains("bernanke"));
assertEquals("The program's output is non-conformant: first line does not contain \"Bernanke\"", true, firstLine.contains("salary"));
assertEquals("The program's output is non-conformant: first line does not contain a numeric value (necessary to represent the yearly salary)", true, firstLine.contains("salary"));
Scanner digitScan = new Scanner(firstLine);
try { // Skip to Ben's salary
digitScan.skip("\\D+");
} catch (Exception e) {
// Squelched... we'll already have failed on one of the asserts above if the above line would throw an exception
}
if (!digitScan.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's salary not present as expected");
}
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
// /* Personally I'd prefer to validate the output using this omnibus method... it'd make things more simple for me, but it wouldn't provide
// * as much granularity for partial credit. Still, your code should pass this test method; I just commented it out to avoid "double jeopardy"
// */
public void testNominalOutput() {
final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedOut));
final double expectedInitialSalaryBen = 14814.72;
final double expectedInitialSalaryPaul = 28148.04;
final double expectedPostRaiseSalaryBen = expectedInitialSalaryBen * 1.1;
final double expectedPostRaiseSalaryPaul = expectedInitialSalaryPaul * 1.1;
String[] arr = {};
EmployeeTest.main(arr);
Scanner sc = new Scanner(capturedOut.toString());
try {
/* FIRST LINE
*/
assertEquals("The program's output is non-conformant: no output was recorded", sc.hasNext(), true);
assertEquals("The program's output is non-conformant: first token is not \"Benjamin\"", "Benjamin", sc.next());
try { // Skip to Ben's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: last name and descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's salary not present as expected");
}
final double bensSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Ben's salary not as specified", expectedInitialSalaryBen, bensSalary, expectedInitialSalaryBen * .01);
try { // Skip to Paul's first name
sc.skip("\\W+");
} catch (Exception e) {
fail("The program's output is non-conformant: line-breaks not present as expected");
}
/* SECOND LINE
*/
try { // Skip to Paul's last name
sc.next();
assertEquals("The program's output is non-conformant: \"Volcker's\" last name not present where expected on line 2", "Volcker's", sc.next());
} catch (Exception e) {
fail("The program's output is non-conformant: \"Volcker's\" last name not present where expected on line 2");
}
try { // Skip to Paul's salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Paul's salary not present as expected");
}
final double paulsSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Paul's salary not as specified", expectedInitialSalaryPaul, paulsSalary, expectedInitialSalaryPaul * .01);
/* FOURTH LINE
*/
try { // Skip to Ben's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's raised salary not present as expected");
}
final double bensRaisedSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Ben's raised salary not as specified", expectedPostRaiseSalaryBen, bensRaisedSalary, expectedPostRaiseSalaryBen * .01);
/* FIFTH LINE
*/
try { // Skip to Paul's raised salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Pauls's raised salary not present as expected");
}
final double paulsRaisedSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Paul's raised salary not as specified", expectedPostRaiseSalaryPaul, paulsRaisedSalary, expectedPostRaiseSalaryPaul * .01);
/* SEVENTH LINE
*/
try { // Skip to Ben's still non-negative salary
sc.skip("\\D+");
} catch (Exception e) {
fail("The program's output is non-conformant: descriptive text absent");
}
if (!sc.hasNextDouble()) {
fail("The program's output is non-conformant: Ben's raised salary not present as expected");
}
final double bensFinalSalary = sc.nextDouble();
assertEquals("The program's output is non-conformant: Ben's decidedly non-negative salary not as specified", expectedPostRaiseSalaryBen, bensFinalSalary, expectedPostRaiseSalaryBen * .01);
} catch (Exception e) {
assertThrowableTestFailure(e);
}
}
/* method name retrieval code courtesy of: http://dev.kanngard.net/Permalinks/ID_20030114224837.html
*/
private void assertThrowableTestFailure(Throwable thrown) {
StackTraceElement stackTraceElements[] =
(new Throwable()).getStackTrace();
fail(thrown.getClass().getName() + " encountered! Unable to successfully execute test: " + stackTraceElements[1].toString());
}
}