1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.api.resourceloader;
17
18 import org.kuali.rice.core.api.CoreConstants;
19 import org.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.core.api.exception.RiceRuntimeException;
21 import org.kuali.rice.core.framework.config.property.SimpleConfig;
22 import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
23
24 import javax.xml.namespace.QName;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28
29
30
31
32
33
34 public final class GlobalResourceLoaderTestUtils {
35
36 private static Map<String, Object> mockServiceMap;
37
38 private volatile boolean initialized = false;
39
40
41 private GlobalResourceLoaderTestUtils() {
42 throw new UnsupportedOperationException("Don't construct me, I'm all about the static methods.");
43 }
44
45
46
47
48
49
50
51
52 public static void addMockService(final String serviceName, final Object mockService) {
53 getMockServiceMap().put(serviceName, mockService);
54 }
55
56
57
58
59
60
61 private static synchronized Map<String, Object> getMockServiceMap() {
62
63 if (mockServiceMap == null) {
64 mockServiceMap = new ConcurrentHashMap<String, Object>();
65
66
67
68 SimpleConfig config = new SimpleConfig();
69 config.putProperty(CoreConstants.Config.APPLICATION_ID, "TEST");
70 ConfigContext.init(config);
71
72 try {
73 GlobalResourceLoader.stop();
74 } catch (Exception e) {
75 throw new RiceRuntimeException(e);
76 }
77
78
79 GlobalResourceLoader.addResourceLoaderFirst(new BaseResourceLoader(new QName("TEST", "TEST")) {
80 @Override
81 public Object getService(QName serviceQName) {
82
83 String localPart = serviceQName.getLocalPart();
84 return mockServiceMap.get(localPart);
85 }
86 });
87 }
88
89 return mockServiceMap;
90 }
91 }