-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePlusCommissionEmployeeComposition_test.java
More file actions
193 lines (171 loc) · 6.62 KB
/
BasePlusCommissionEmployeeComposition_test.java
File metadata and controls
193 lines (171 loc) · 6.62 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 java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import junit.framework.TestCase;
public class BasePlusCommissionEmployeeComposition_test extends TestCase {
final String expectedFirst = "John";
final String expectedLast = "Doe";
final String expectedSSN = "111-22-3333";
final double expectedSales = 444.55;
final double expectedRate = 0.1;
final double expectedSalary = 200;
BasePlusCommissionEmployeeComposition emp;
CommissionEmployee commissionEmployeeReference;
protected void setUp() throws Exception {
super.setUp();
emp = new BasePlusCommissionEmployeeComposition(
expectedFirst,
expectedLast,
expectedSSN,
expectedSales,
expectedRate,
expectedSalary);
// Initialize the commissionEmployeeReference to the first Field of type CommissionEmployee encountered (there should only be one... this will be verified in a separate test)
for(Field currentField : emp.getClass().getDeclaredFields()) {
if(currentField.getType() == CommissionEmployee.class) {
currentField.setAccessible(true);
commissionEmployeeReference = (CommissionEmployee)currentField.get(emp);
break;
}
}
}
/**
* Ensure composition
*/
public void testComposition() {
// Ensure that we BasePlusCommissionEmployee holds a single reference to a CommissionEmployee object
int commissionEmployeeReferenceCounter = 0;
for(Field currentField : emp.getClass().getDeclaredFields()) {
if(currentField.getType() == CommissionEmployee.class) {
commissionEmployeeReferenceCounter++;
}
}
assertEquals("BasePlusCommissionEmployee does not hold a SINGLE reference to a CommissionEmployee object in support of composition", 1, commissionEmployeeReferenceCounter);
}
/**
* Ensure delegation
*/
public void testDelegation() {
String[] expectedMethodNames = {
"getFirstName",
"getLastName",
"getSocialSecurityNumber",
"getGrossSales",
"setGrossSales",
"getCommissionRate",
"setCommissionRate",
"getBaseSalary",
"setBaseSalary",
"earnings",
"toString"
};
ArrayList<String> actualMethodNames = new ArrayList<String>();
for (Method currentMethod : emp.getClass().getDeclaredMethods()) {
actualMethodNames.add(currentMethod.getName());
}
for (String currentMethodName : expectedMethodNames) {
assertTrue("Expected method (" + currentMethodName + ") not provided in support of delegation", actualMethodNames.contains(currentMethodName));
}
}
/**
* Ensure that negative base-salary values are rejected by the constructor; in that case the base should be set to 0.0)
*/
public void testBasePlusCommissionEmployeeCompositionNegativeBase() {
try {
emp = new BasePlusCommissionEmployeeComposition(
expectedFirst,
expectedLast,
expectedSSN,
expectedSales,
expectedRate,
-200); // invalid negative salary
// Expecting an IllegalArgumentException
fail ("Negative base-salary values should be rejected by setBaseSalary and result in an IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// This is the nominal case
} catch (RuntimeException ex) {
assertThrowableTestFailure(ex);
}
}
public void testSetBaseSalaryPositive() {
final double altBaseSalary = 999.99;
emp.setBaseSalary(altBaseSalary);
try {
Field fBase;
fBase = emp.getClass().getDeclaredField("baseSalary");
fBase.setAccessible(true);
assertEquals("setBaseSalary did not set the value as expected", altBaseSalary, fBase.getDouble(emp));
} catch (Exception ex) {
assertThrowableTestFailure(ex);
}
}
public void testSetBaseSalaryNegative() {
final double altBaseSalary = -999.99;
try {
emp.setBaseSalary(altBaseSalary); // Expecting an IllegalArgumentException
fail ("Negative base-salary values should be rejected by setBaseSalary and result in an IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// This is the nominal case
} catch (RuntimeException ex) {
assertThrowableTestFailure(ex);
}
}
public void testGetBaseSalary() {
assertEquals("getBaseSalary did not perform as expected", expectedSalary, emp.getBaseSalary());
}
public void testGetFirstName() {
assertEquals("getFirstName did not perform as expected", expectedFirst, emp.getFirstName());
}
public void testGetLastName() {
assertEquals("getLastName did not perform as expected", expectedLast, emp.getLastName());
}
public void testGetSocialSecurityNumber() {
assertEquals("getSocialSecurityNumber did not perform as expected", expectedSSN, emp.getSocialSecurityNumber());
}
public void testGetGrossSales() {
assertEquals("getGrossSales did not perform as expected", expectedSales, emp.getGrossSales());
}
public void testSetGrossSales() {
// Only checking the nominal case (this method should be delegated to an already-tested CommissionEmployee anyway...)
final double altGrossSales = 42;
emp.setGrossSales(altGrossSales);
try {
assertEquals("setGrossSales did not set the value as expected", altGrossSales, commissionEmployeeReference.getGrossSales());
} catch (Exception ex) {
assertThrowableTestFailure(ex);
}
}
public void testGetCommissionRate() {
assertEquals("getCommissionRate did not perform as expected", expectedRate, emp.getCommissionRate());
}
public void testSetCommissionRate() {
// Only checking the nominal case (this method should be delegated to an already-tested CommissionEmployee anyway...)
final double altCommissionRate = 0.5;
emp.setCommissionRate(altCommissionRate);
try {
assertEquals("setCommissionRate did not set the value as expected", altCommissionRate, commissionEmployeeReference.getCommissionRate());
} catch (Exception ex) {
assertThrowableTestFailure(ex);
}
}
public void testEarnings() {
try {
Field fBase;
fBase = emp.getClass().getDeclaredField("baseSalary");
fBase.setAccessible(true);
assertEquals("earnings() should return the value of CommissionEmployee.earnings() + baseSalary", commissionEmployeeReference.earnings() + fBase.getDouble(emp), emp.earnings());
} catch (Exception ex) {
assertThrowableTestFailure(ex);
}
}
public void testToString() {
assertTrue("toString output for BasePlusCommissionEmployee should contain the baseSalary", emp.toString().contains(Double.toString(expectedSalary)));
}
/* 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());
}
}