|
21 | 21 | import java.lang.reflect.Field;
|
22 | 22 | import java.text.DateFormat;
|
23 | 23 | import java.text.SimpleDateFormat;
|
| 24 | +import java.util.ArrayList; |
24 | 25 | import java.util.Arrays;
|
25 |
| -import java.util.Collection; |
26 |
| -import java.util.Collections; |
27 | 26 | import java.util.List;
|
28 | 27 |
|
| 28 | +import org.apache.commons.beanutils.MethodUtils; |
29 | 29 | import org.junit.Test;
|
30 | 30 | import org.openmrs.util.Reflect;
|
31 | 31 |
|
|
37 | 37 | */
|
38 | 38 | public class OrderTest {
|
39 | 39 |
|
| 40 | + protected static void assertThatAllFieldsAreCopied(Order original, String methodName, String... otherfieldsToExclude) |
| 41 | + throws Exception { |
| 42 | + if (methodName == null) { |
| 43 | + methodName = "copy"; |
| 44 | + } |
| 45 | + List<String> fieldsToExclude = new ArrayList<String>(); |
| 46 | + fieldsToExclude.addAll(Arrays.asList("log", "serialVersionUID", "orderId", "uuid")); |
| 47 | + if (otherfieldsToExclude != null) { |
| 48 | + fieldsToExclude.addAll(Arrays.asList(otherfieldsToExclude)); |
| 49 | + } |
| 50 | + List<Field> fields = Reflect.getAllFields(original.getClass()); |
| 51 | + for (Field field : fields) { |
| 52 | + if (fieldsToExclude.contains(field.getName())) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + field.setAccessible(true); |
| 56 | + Object fieldValue = null; |
| 57 | + |
| 58 | + if (field.getType().isEnum()) { |
| 59 | + fieldValue = field.getType().getEnumConstants()[0]; |
| 60 | + } else if (field.getType().equals(Boolean.class)) { |
| 61 | + fieldValue = true; |
| 62 | + } else if (field.getType().equals(Integer.class)) { |
| 63 | + fieldValue = 10; |
| 64 | + } else if (field.getType().equals(Double.class)) { |
| 65 | + fieldValue = 5.0; |
| 66 | + } else { |
| 67 | + fieldValue = field.getType().newInstance(); |
| 68 | + } |
| 69 | + field.set(original, fieldValue); |
| 70 | + } |
| 71 | + |
| 72 | + Order copy = (Order) MethodUtils.invokeExactMethod(original, methodName, null); |
| 73 | + for (Field field : fields) { |
| 74 | + Object copyValue = field.get(copy); |
| 75 | + if (fieldsToExclude.contains(field.getName())) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + assertNotNull("Order." + methodName + " should set " + field.getName() + " on the new Order", copyValue); |
| 79 | + assertEquals("Order." + methodName + " should set " + field.getName() + " on the new Order", |
| 80 | + field.get(original), copyValue); |
| 81 | + } |
| 82 | + } |
| 83 | + |
40 | 84 | /**
|
41 | 85 | * Tests the {@link Order#isDiscontinuedRightNow()} method TODO this should be split into many
|
42 | 86 | * different tests
|
@@ -131,43 +175,18 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti
|
131 | 175 | */
|
132 | 176 | @Test
|
133 | 177 | public void copy_shouldCopyAllFields() throws Exception {
|
134 |
| - assertThatAllFieldsAreCopied(new Order()); |
135 |
| - assertThatAllFieldsAreCopied(new TestOrder()); |
| 178 | + assertThatAllFieldsAreCopied(new Order(), null); |
| 179 | + assertThatAllFieldsAreCopied(new TestOrder(), null); |
136 | 180 | }
|
137 | 181 |
|
138 |
| - protected static void assertThatAllFieldsAreCopied(Order original) throws Exception { |
139 |
| - Collection<?> fieldToExclude = Collections.unmodifiableCollection(Arrays.asList("log", "serialVersionUID", |
140 |
| - "orderId", "uuid")); |
141 |
| - List<Field> fields = Reflect.getAllFields(original.getClass()); |
142 |
| - for (Field field : fields) { |
143 |
| - if (fieldToExclude.contains(field.getName())) { |
144 |
| - continue; |
145 |
| - } |
146 |
| - field.setAccessible(true); |
147 |
| - Object fieldValue = null; |
148 |
| - |
149 |
| - if (field.getType().isEnum()) { |
150 |
| - fieldValue = field.getType().getEnumConstants()[0]; |
151 |
| - } else if (field.getType().equals(Boolean.class)) { |
152 |
| - fieldValue = true; |
153 |
| - } else if (field.getType().equals(Integer.class)) { |
154 |
| - fieldValue = 10; |
155 |
| - } else if (field.getType().equals(Double.class)) { |
156 |
| - fieldValue = 5.0; |
157 |
| - } else { |
158 |
| - fieldValue = field.getType().newInstance(); |
159 |
| - } |
160 |
| - field.set(original, fieldValue); |
161 |
| - } |
162 |
| - |
163 |
| - Order copy = original.copy(); |
164 |
| - for (Field field : fields) { |
165 |
| - Object copyValue = field.get(copy); |
166 |
| - if (fieldToExclude.contains(field.getName())) { |
167 |
| - continue; |
168 |
| - } |
169 |
| - assertNotNull("Order.copy should set " + field.getName() + " on the new Order", copyValue); |
170 |
| - assertEquals("Order.copy should set " + field.getName() + " on the new Order", field.get(original), copyValue); |
171 |
| - } |
| 182 | + /** |
| 183 | + * @verifies set all the relevant fields |
| 184 | + * @see Order#cloneForRevision() |
| 185 | + */ |
| 186 | + @Test |
| 187 | + public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception { |
| 188 | + assertThatAllFieldsAreCopied(new Order(), "cloneForRevision", "creator", "dateCreated", "action", "changedBy", |
| 189 | + "dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber", "orderer", |
| 190 | + "previousOrder", "startDate", "dateStopped"); |
172 | 191 | }
|
173 | 192 | }
|
0 commit comments