View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Utility class to allow the injection of services that are fetched via the
30   * {@link GlobalResourceLoader}.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public final class GlobalResourceLoaderTestUtils {
35  
36      private static Map<String, Object> mockServiceMap;
37  
38      private volatile boolean initialized = false;
39  
40      // private constructor, instances are not allowed
41      private GlobalResourceLoaderTestUtils() {
42          throw new UnsupportedOperationException("Don't construct me, I'm all about the static methods.");
43      }
44  
45      /**
46       * Inject a service into the {@link org.kuali.rice.core.api.resourceloader.GlobalResourceLoader} such that anytime
47       * that service is fetched by serviceName, the supplied mockService will be returned.
48       *
49       * @param serviceName the name that the service is known by in the GRL
50       * @param mockService the service to inject
51       */
52      public static void addMockService(final String serviceName, final Object mockService) {
53          getMockServiceMap().put(serviceName, mockService);
54      }
55  
56      /**
57       * Getter for the mockServiceMap, which on first call constructs it and also initializes our hack of the GRL.
58       *
59       * @return the mockServiceMap
60       */
61      private static synchronized Map<String, Object> getMockServiceMap() {
62          // the first time this is called, inject our special resource loader
63          if (mockServiceMap == null) {
64              mockServiceMap = new ConcurrentHashMap<String, Object>();
65  
66              // this little dance is required to prevent issues when
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              // Add to the front of the line our hacked resource loader which will fetch services from the mockServiceMap
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  }