1
2
3 package org.virtualmock;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import org.virtualmock.call.CallManager;
8 import org.virtualmock.call.InvokedCallQueue;
9 import org.virtualmock.call.RecordedCallQueue;
10 import org.virtualmock.resource.ResourceManager;
11 import org.virtualmock.rule.AllRecordedCallsMustBeInvokedRule;
12 import org.virtualmock.rule.Rule;
13 import org.virtualmock.rule.RuleManager;
14 import org.virtualmock.util.Toolbox;
15 import org.virtualmock.verify.VerificationManager;
16
17
18 /***
19 * A Factory to create instances of VM. This is the entry point to
20 * VirtualMock. Also performs common initialization for the VirtualMock
21 * environment.
22 *
23 * @author Chad Woolley
24 * @version $Revision: 1.20 $
25 */
26 public class VMFactory {
27 /***
28 * Creates a new instance of the VM class.
29 *
30 * @return a new instance of the VM class.
31 */
32 public static VM createVM() {
33 VMFactory vmFactory = new VMFactory();
34 CallManager callManager = vmFactory.createCallManager();
35
36 ResourceManager resourceManager = vmFactory.createResourceManager();
37 List rules = vmFactory.createRules(callManager, resourceManager);
38 RuleManager ruleManager =
39 vmFactory.createRuleManager(rules, resourceManager);
40
41 VerificationManager verificationManager =
42 new VerificationManager(ruleManager);
43
44 VM vm = new VM(callManager, verificationManager);
45
46 return vm;
47 }
48
49 /***
50 * Create the CallManager.
51 *
52 * @return a new, initialized CallManager
53 */
54 protected CallManager createCallManager() {
55 InvokedCallQueue invokedCallQueue = new InvokedCallQueue();
56 RecordedCallQueue recordedCallQueue = new RecordedCallQueue();
57
58 CallManager callManager =
59 new CallManager(invokedCallQueue, recordedCallQueue);
60
61 Toolbox.registerComponent(Toolbox.CALL_MANAGER, callManager);
62
63 return callManager;
64 }
65
66 /***
67 * Create the ResourceManager.
68 *
69 * @return a new, initialized ResourceManager
70 *
71 * @todo Read Locale settings from config file
72 */
73 protected ResourceManager createResourceManager() {
74 ResourceManager resourceManager =
75 new ResourceManager("virtualmock", "en", "US");
76
77 return resourceManager;
78 }
79
80 /***
81 * Create the RuleManager.
82 *
83 * @param rules the list of rules to be used when creating the RuleManager
84 * @param resourceManager the ResourceManager to be used when creating the
85 * RuleManager
86 *
87 * @return a new, initialized RuleManager
88 */
89 protected RuleManager createRuleManager(List rules,
90 ResourceManager resourceManager) {
91 RuleManager ruleManager = new RuleManager(rules, resourceManager);
92
93 return ruleManager;
94 }
95
96 /***
97 * Create and configure the rules that will apply for this virtualmock
98 * session.
99 *
100 * @param callManager the CallManager that will be used when creating the
101 * rules
102 * @param resourceManager the ResourceManager that will be used when
103 * creating the rules
104 *
105 * @return a list of all rules that were created
106 */
107 protected List createRules(CallManager callManager,
108 ResourceManager resourceManager) {
109 Rule allRecordedCallsMustBeInvokedRule =
110 new AllRecordedCallsMustBeInvokedRule("ALL_RECORDED_CALLS_MUST_BE_INVOKED",
111 Rule.WARN, callManager, resourceManager);
112 ArrayList rules = new ArrayList();
113 rules.add(allRecordedCallsMustBeInvokedRule);
114
115 return rules;
116 }
117 }