1
2
3 package org.virtualmock.rule;
4
5 import java.util.ArrayList;
6 import org.virtualmock.call.CallManager;
7 import org.virtualmock.call.RecordedCall;
8 import org.virtualmock.resource.ResourceKeys;
9 import org.virtualmock.resource.ResourceManager;
10
11
12 /***
13 * Implementation of the Rule: ALL_RECORDED_CALLS_MUST_BE_INVOKED.
14 *
15 * @author Chad Woolley
16 * @version $Revision: 1.15 $
17 */
18 public class AllRecordedCallsMustBeInvokedRule implements Rule {
19 private CallManager callManager = null;
20 private ResourceManager resourceManager = null;
21 private String key;
22 private int enforcementLevel = -1;
23
24 /***
25 * Constructor.
26 *
27 * @param key the key for this rule
28 * @param enforcementLevel the enforcementLevel for this rule
29 * @param callManager the CallManager, which will be used internally by
30 * this rule
31 * @param resourceManager the ResourceManager, which will be used
32 * internally by this rule
33 */
34 public AllRecordedCallsMustBeInvokedRule(String key, int enforcementLevel,
35 CallManager callManager, ResourceManager resourceManager) {
36 this.key = key;
37 this.enforcementLevel = enforcementLevel;
38 this.callManager = callManager;
39 this.resourceManager = resourceManager;
40 }
41
42 /***
43 * Accessor.
44 *
45 * @return the enforcementLevel for this rule
46 */
47 public int getEnforcementLevel() {
48 return enforcementLevel;
49 }
50
51 /***
52 * Accessor.
53 *
54 * @return the key
55 */
56 public String getKey() {
57 return key;
58 }
59
60 /***
61 * Gets violation messages for this rule.
62 *
63 * @return a string array of violation messages, if any exist
64 */
65 public String[] getViolationMessages() {
66 ArrayList violationMessagesArrayList = new ArrayList();
67 RecordedCall[] uninvokedCalls = callManager.getUninvokedCalls();
68
69 for (int i = 0; i < uninvokedCalls.length; i++) {
70 violationMessagesArrayList.add(getViolationMessage(
71 uninvokedCalls[i]));
72 }
73
74 return (String[]) violationMessagesArrayList.toArray(new String[0]);
75 }
76
77 /***
78 * Get a single violation message.
79 *
80 * @param recordedCall the recorded call to which the message applies
81 *
82 * @return the formatted violation message
83 *
84 * @todo make this message cleaner and more descriptive
85 */
86 protected String getViolationMessage(RecordedCall recordedCall) {
87 String resourceKey =
88 ResourceKeys.RULE_FAIL_ALL_RECORDED_CALLS_MUST_BE_INVOKED;
89 String message =
90 resourceManager.getString(resourceKey, recordedCall.toString());
91
92 return message;
93 }
94 }