From 9205b621a07f5dd13218c66a5f926ebd6e59f754 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 9 Jun 2018 12:55:33 +0100
Subject: [PATCH v2 27/28] ExtendedDialogMocker, HelpAwareOptionPaneMocker,
JOptionPaneSimpleMocker: add invocations raising AssertionError to invocation
log
---
.../testutils/mockers/ExtendedDialogMocker.java | 32 ++--
.../mockers/HelpAwareOptionPaneMocker.java | 104 +++++------
.../testutils/mockers/JOptionPaneSimpleMocker.java | 190 ++++++++++++---------
3 files changed, 183 insertions(+), 143 deletions(-)
diff --git a/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java b/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
index f0fb62f29..4401f64fa 100644
a
|
b
|
import mockit.Mock;
|
39 | 39 | * only the parts necessary for a particular case. |
40 | 40 | * |
41 | 41 | * The default {@link #getMockResult(ExtendedDialog)} will raise an |
42 | | * {@link junit.framework.AssertionFailedError} on an {@link ExtendedDialog} activation without a |
| 42 | * {@link AssertionError} on an {@link ExtendedDialog} activation without a |
43 | 43 | * matching mapping entry or if the named button doesn't exist. |
44 | 44 | * |
45 | 45 | * The public {@link #getMockResultMap()} method returns the modifiable result map to allow for situations |
… |
… |
public class ExtendedDialogMocker extends BaseDialogMockUp<ExtendedDialog> {
|
127 | 127 | @Mock |
128 | 128 | private void setVisible(final Invocation invocation, final boolean value) { |
129 | 129 | if (value == true) { |
130 | | final ExtendedDialog instance = invocation.getInvokedInstance(); |
131 | | final int mockResult = this.getMockResult(instance); |
132 | | // TODO check validity of mockResult? |
133 | | Deencapsulation.setField(instance, "result", mockResult); |
134 | | Logging.info( |
135 | | "{0} answering {1} to ExtendedDialog with content {2}", |
136 | | this.getClass().getName(), |
137 | | mockResult, |
138 | | this.getString(instance) |
139 | | ); |
140 | | this.getInvocationLogInternal().add(this.getInvocationLogEntry(instance, mockResult)); |
| 130 | try { |
| 131 | final ExtendedDialog instance = invocation.getInvokedInstance(); |
| 132 | final int mockResult = this.getMockResult(instance); |
| 133 | // TODO check validity of mockResult? |
| 134 | Deencapsulation.setField(instance, "result", mockResult); |
| 135 | Logging.info( |
| 136 | "{0} answering {1} to ExtendedDialog with content {2}", |
| 137 | this.getClass().getName(), |
| 138 | mockResult, |
| 139 | this.getString(instance) |
| 140 | ); |
| 141 | this.getInvocationLogInternal().add(this.getInvocationLogEntry(instance, mockResult)); |
| 142 | } catch (AssertionError e) { |
| 143 | // in case this exception gets ignored by the calling thread we want to signify this failure |
| 144 | // in the invocation log. it's hard to know what to add to the log in these cases as it's |
| 145 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own. |
| 146 | this.getInvocationLogInternal().add(new Object[] {e}); |
| 147 | throw e; |
| 148 | } |
141 | 149 | } |
142 | 150 | } |
143 | 151 | |
diff --git a/test/unit/org/openstreetmap/josm/testutils/mockers/HelpAwareOptionPaneMocker.java b/test/unit/org/openstreetmap/josm/testutils/mockers/HelpAwareOptionPaneMocker.java
index a157df4e7..54fd3eba7 100644
a
|
b
|
import mockit.Mock;
|
40 | 40 | * of only the parts necessary for a particular case. |
41 | 41 | * |
42 | 42 | * The default {@link #getMockResultForMessage(Object)} will raise an |
43 | | * {@link junit.framework.AssertionFailedError} on an {@link #showOptionDialog(Component, Object, String, |
| 43 | * {@link AssertionError} on an {@link #showOptionDialog(Component, Object, String, |
44 | 44 | * int, Icon, HelpAwareOptionPane.ButtonSpec[], HelpAwareOptionPane.ButtonSpec, String)} |
45 | 45 | * activation without a matching mapping entry or if the named button doesn't exist. |
46 | 46 | * |
… |
… |
public class HelpAwareOptionPaneMocker extends BaseDialogMockUp<HelpAwareOptionP
|
134 | 134 | final HelpAwareOptionPane.ButtonSpec defaultOption, |
135 | 135 | final String helpTopic |
136 | 136 | ) { |
137 | | final Object result = this.getMockResultForMessage(msg); |
| 137 | try { |
| 138 | final Object result = this.getMockResultForMessage(msg); |
| 139 | |
| 140 | if (result == null) { |
| 141 | fail( |
| 142 | "Invalid result for HelpAwareOptionPane: null (HelpAwareOptionPane returns" |
| 143 | + "JOptionPane.OK_OPTION for closed windows if that was the intent)" |
| 144 | ); |
| 145 | } |
138 | 146 | |
139 | | if (result == null) { |
140 | | fail( |
141 | | "Invalid result for HelpAwareOptionPane: null (HelpAwareOptionPane returns" |
142 | | + "JOptionPane.OK_OPTION for closed windows if that was the intent)" |
143 | | ); |
144 | | } |
| 147 | Integer retval = null; |
| 148 | if (result instanceof String) { |
| 149 | retval = this.getButtonPositionFromLabel(options, (String) result); |
| 150 | } else if (result instanceof Integer) { |
| 151 | retval = (Integer) result; |
| 152 | } else { |
| 153 | throw new IllegalArgumentException( |
| 154 | "HelpAwareOptionPane message mapped to unsupported type of Object: " + result |
| 155 | ); |
| 156 | } |
145 | 157 | |
146 | | Integer retval = null; |
147 | | if (result instanceof String) { |
148 | | retval = this.getButtonPositionFromLabel(options, (String) result); |
149 | | } else if (result instanceof Integer) { |
150 | | retval = (Integer) result; |
151 | | } else { |
152 | | throw new IllegalArgumentException( |
153 | | "HelpAwareOptionPane message mapped to unsupported type of Object: " + result |
| 158 | // check the returned integer for validity |
| 159 | if (retval < 0) { |
| 160 | fail(String.format( |
| 161 | "Invalid result for HelpAwareOptionPane: %s (HelpAwareOptionPane returns " |
| 162 | + "JOptionPane.OK_OPTION for closed windows if that was the intent)", |
| 163 | retval |
| 164 | )); |
| 165 | } else if (retval > (options == null ? 0 : options.length)) { // NOTE 1-based indexing |
| 166 | fail(String.format( |
| 167 | "Invalid result for HelpAwareOptionPane: %s (in call with options = %s)", |
| 168 | retval, |
| 169 | options |
| 170 | )); |
| 171 | } |
| 172 | |
| 173 | Logging.info( |
| 174 | "{0} answering {1} to HelpAwareOptionPane with message {2}", |
| 175 | this.getClass().getName(), |
| 176 | retval, |
| 177 | this.getStringFromMessage(msg) |
154 | 178 | ); |
155 | | } |
156 | 179 | |
157 | | // check the returned integer for validity |
158 | | if (retval < 0) { |
159 | | fail(String.format( |
160 | | "Invalid result for HelpAwareOptionPane: %s (HelpAwareOptionPane returns " |
161 | | + "JOptionPane.OK_OPTION for closed windows if that was the intent)", |
| 180 | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
| 181 | msg, |
| 182 | title, |
| 183 | messageType, |
| 184 | icon, |
| 185 | options, |
| 186 | defaultOption, |
| 187 | helpTopic, |
162 | 188 | retval |
163 | 189 | )); |
164 | | } else if (retval > (options == null ? 0 : options.length)) { // NOTE 1-based indexing |
165 | | fail(String.format( |
166 | | "Invalid result for HelpAwareOptionPane: %s (in call with options = %s)", |
167 | | retval, |
168 | | options |
169 | | )); |
170 | | } |
171 | 190 | |
172 | | Logging.info( |
173 | | "{0} answering {1} to HelpAwareOptionPane with message {2}", |
174 | | this.getClass().getName(), |
175 | | retval, |
176 | | this.getStringFromMessage(msg) |
177 | | ); |
178 | | |
179 | | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
180 | | msg, |
181 | | title, |
182 | | messageType, |
183 | | icon, |
184 | | options, |
185 | | defaultOption, |
186 | | helpTopic, |
187 | | retval |
188 | | )); |
189 | | |
190 | | return retval; |
| 191 | return retval; |
| 192 | } catch (AssertionError e) { |
| 193 | // in case this exception gets ignored by the calling thread we want to signify this failure |
| 194 | // in the invocation log. it's hard to know what to add to the log in these cases as it's |
| 195 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own. |
| 196 | this.getInvocationLogInternal().add(new Object[] {e}); |
| 197 | throw e; |
| 198 | } |
191 | 199 | } |
192 | 200 | } |
diff --git a/test/unit/org/openstreetmap/josm/testutils/mockers/JOptionPaneSimpleMocker.java b/test/unit/org/openstreetmap/josm/testutils/mockers/JOptionPaneSimpleMocker.java
index 256ee8ba0..8dae51174 100644
a
|
b
|
import mockit.MockUp;
|
46 | 46 | * only the parts necessary for a particular case. |
47 | 47 | * |
48 | 48 | * The default {@link #getMockResultForMessage(Object)} will raise an |
49 | | * {@link junit.framework.AssertionFailedError} on an activation without a matching mapping entry or if |
| 49 | * {@link junit.framework.AssertionError} on an activation without a matching mapping entry or if |
50 | 50 | * the mapped result value is invalid for the call. |
51 | 51 | * |
52 | 52 | * The public {@link #getMockResultMap()} method returns the modifiable result map to allow for situations |
… |
… |
public class JOptionPaneSimpleMocker extends BaseDialogMockUp<JOptionPane> {
|
161 | 161 | final Object[] selectionValues, |
162 | 162 | final Object initialSelectionValue |
163 | 163 | ) { |
164 | | final Object result = this.getMockResultForMessage(message); |
165 | | if (selectionValues == null) { |
166 | | if (!(result instanceof String)) { |
167 | | fail(String.format( |
168 | | "Only valid result type for showInputDialog with null selectionValues is String: received %s", |
169 | | result |
170 | | )); |
| 164 | try { |
| 165 | final Object result = this.getMockResultForMessage(message); |
| 166 | if (selectionValues == null) { |
| 167 | if (!(result instanceof String)) { |
| 168 | fail(String.format( |
| 169 | "Only valid result type for showInputDialog with null selectionValues is String: received %s", |
| 170 | result |
| 171 | )); |
| 172 | } |
| 173 | } else { |
| 174 | if (!Arrays.asList(selectionValues).contains(result)) { |
| 175 | fail(String.format( |
| 176 | "Result for showInputDialog not present in selectionValues: %s", |
| 177 | result |
| 178 | )); |
| 179 | } |
171 | 180 | } |
172 | | } else { |
173 | | if (!Arrays.asList(selectionValues).contains(result)) { |
174 | | fail(String.format( |
175 | | "Result for showInputDialog not present in selectionValues: %s", |
176 | | result |
177 | | )); |
178 | | } |
179 | | } |
180 | 181 | |
181 | | Logging.info( |
182 | | "{0} answering {1} to showInputDialog with message {2}", |
183 | | this.getClass().getName(), |
184 | | result, |
185 | | this.getStringFromMessage(message) |
186 | | ); |
| 182 | Logging.info( |
| 183 | "{0} answering {1} to showInputDialog with message {2}", |
| 184 | this.getClass().getName(), |
| 185 | result, |
| 186 | this.getStringFromMessage(message) |
| 187 | ); |
187 | 188 | |
188 | | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
189 | | message, |
190 | | title, |
191 | | null, |
192 | | messageType, |
193 | | icon, |
194 | | selectionValues, |
195 | | initialSelectionValue, |
196 | | result |
197 | | )); |
| 189 | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
| 190 | message, |
| 191 | title, |
| 192 | null, |
| 193 | messageType, |
| 194 | icon, |
| 195 | selectionValues, |
| 196 | initialSelectionValue, |
| 197 | result |
| 198 | )); |
198 | 199 | |
199 | | return result; |
| 200 | return result; |
| 201 | } catch (AssertionError e) { |
| 202 | // in case this exception gets ignored by the calling thread we want to signify this failure |
| 203 | // in the invocation log. it's hard to know what to add to the log in these cases as it's |
| 204 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own. |
| 205 | this.getInvocationLogInternal().add(new Object[] {e}); |
| 206 | throw e; |
| 207 | } |
200 | 208 | } |
201 | 209 | |
202 | 210 | @Mock |
… |
… |
public class JOptionPaneSimpleMocker extends BaseDialogMockUp<JOptionPane> {
|
207 | 215 | final int messageType, |
208 | 216 | final Icon icon |
209 | 217 | ) { |
210 | | // why look up a "result" for a message dialog which can only have one possible result? it's |
211 | | // a good opportunity to assert its contents |
212 | | final Object result = this.getMockResultForMessage(message); |
213 | | if (!(result instanceof Integer && (int) result == JOptionPane.OK_OPTION)) { |
214 | | fail(String.format( |
215 | | "Only valid result for showMessageDialog is %d: received %s", |
216 | | JOptionPane.OK_OPTION, |
217 | | result |
218 | | )); |
219 | | } |
| 218 | try { |
| 219 | // why look up a "result" for a message dialog which can only have one possible result? it's |
| 220 | // a good opportunity to assert its contents |
| 221 | final Object result = this.getMockResultForMessage(message); |
| 222 | if (!(result instanceof Integer && (int) result == JOptionPane.OK_OPTION)) { |
| 223 | fail(String.format( |
| 224 | "Only valid result for showMessageDialog is %d: received %s", |
| 225 | JOptionPane.OK_OPTION, |
| 226 | result |
| 227 | )); |
| 228 | } |
220 | 229 | |
221 | | Logging.info( |
222 | | "{0} answering {1} to showMessageDialog with message {2}", |
223 | | this.getClass().getName(), |
224 | | result, |
225 | | this.getStringFromMessage(message) |
226 | | ); |
| 230 | Logging.info( |
| 231 | "{0} answering {1} to showMessageDialog with message {2}", |
| 232 | this.getClass().getName(), |
| 233 | result, |
| 234 | this.getStringFromMessage(message) |
| 235 | ); |
227 | 236 | |
228 | | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
229 | | message, |
230 | | title, |
231 | | null, |
232 | | messageType, |
233 | | icon, |
234 | | null, |
235 | | null, |
236 | | JOptionPane.OK_OPTION |
237 | | )); |
| 237 | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
| 238 | message, |
| 239 | title, |
| 240 | null, |
| 241 | messageType, |
| 242 | icon, |
| 243 | null, |
| 244 | null, |
| 245 | JOptionPane.OK_OPTION |
| 246 | )); |
| 247 | } catch (AssertionError e) { |
| 248 | // in case this exception gets ignored by the calling thread we want to signify this failure |
| 249 | // in the invocation log. it's hard to know what to add to the log in these cases as it's |
| 250 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own. |
| 251 | this.getInvocationLogInternal().add(new Object[] {e}); |
| 252 | throw e; |
| 253 | } |
238 | 254 | } |
239 | 255 | |
240 | 256 | @Mock |
… |
… |
public class JOptionPaneSimpleMocker extends BaseDialogMockUp<JOptionPane> {
|
246 | 262 | final int messageType, |
247 | 263 | final Icon icon |
248 | 264 | ) { |
249 | | final Object result = this.getMockResultForMessage(message); |
250 | | if (!(result instanceof Integer && Ints.contains(optionTypePermittedResults.get(optionType), (int) result))) { |
251 | | fail(String.format( |
252 | | "Invalid result for showConfirmDialog with optionType %d: %s", |
| 265 | try { |
| 266 | final Object result = this.getMockResultForMessage(message); |
| 267 | if (!(result instanceof Integer && Ints.contains(optionTypePermittedResults.get(optionType), (int) result))) { |
| 268 | fail(String.format( |
| 269 | "Invalid result for showConfirmDialog with optionType %d: %s", |
| 270 | optionType, |
| 271 | result |
| 272 | )); |
| 273 | } |
| 274 | |
| 275 | Logging.info( |
| 276 | "{0} answering {1} to showConfirmDialog with message {2}", |
| 277 | this.getClass().getName(), |
| 278 | result, |
| 279 | this.getStringFromMessage(message) |
| 280 | ); |
| 281 | |
| 282 | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
| 283 | message, |
| 284 | title, |
253 | 285 | optionType, |
| 286 | messageType, |
| 287 | icon, |
| 288 | null, |
| 289 | null, |
254 | 290 | result |
255 | 291 | )); |
256 | | } |
257 | | |
258 | | Logging.info( |
259 | | "{0} answering {1} to showConfirmDialog with message {2}", |
260 | | this.getClass().getName(), |
261 | | result, |
262 | | this.getStringFromMessage(message) |
263 | | ); |
264 | 292 | |
265 | | this.getInvocationLogInternal().add(this.getInvocationLogEntry( |
266 | | message, |
267 | | title, |
268 | | optionType, |
269 | | messageType, |
270 | | icon, |
271 | | null, |
272 | | null, |
273 | | result |
274 | | )); |
275 | | |
276 | | return (int) result; |
| 293 | return (int) result; |
| 294 | } catch (AssertionError e) { |
| 295 | // in case this exception gets ignored by the calling thread we want to signify this failure |
| 296 | // in the invocation log. it's hard to know what to add to the log in these cases as it's |
| 297 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own. |
| 298 | this.getInvocationLogInternal().add(new Object[] {e}); |
| 299 | throw e; |
| 300 | } |
277 | 301 | } |
278 | 302 | |
279 | 303 | /** |