1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.bo;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.kuali.rice.core.api.CoreConstants;
22 import org.kuali.rice.core.api.config.property.ConfigContext;
23 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
25 import org.kuali.rice.core.framework.config.property.SimpleConfig;
26 import org.kuali.rice.core.framework.resourceloader.BeanFactoryResourceLoader;
27 import org.kuali.rice.krad.bo.test.TestEBOInterface;
28 import org.kuali.rice.krad.data.provider.PersistenceProvider;
29 import org.kuali.rice.krad.data.provider.Provider;
30 import org.kuali.rice.krad.data.provider.ProviderRegistry;
31 import org.kuali.rice.krad.service.KRADServiceLocator;
32 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33 import org.kuali.rice.krad.service.KualiModuleService;
34 import org.kuali.rice.krad.service.ModuleService;
35 import org.kuali.rice.krad.service.impl.ModuleServiceBase;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.springframework.beans.factory.support.StaticListableBeanFactory;
39
40 import javax.xml.namespace.QName;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49
50
51
52
53 @RunWith(MockitoJUnitRunner.class)
54 public class ModuleServiceBaseTest {
55
56
57 private static class ModuleServiceBaseTestEBO implements TestEBOInterface {
58 @Override public void refresh() {}
59 }
60
61
62 private static interface ModuleServiceBaseTestEBOInterface extends TestEBOInterface {
63 }
64
65 private static class DataObject {}
66
67 private ModuleServiceBase module = new ModuleServiceBase();
68 private ModuleConfiguration config = new ModuleConfiguration();
69 @Mock
70 private KualiModuleService kualiModuleService;
71 private List<ModuleService> installedModuleServices = new ArrayList<ModuleService>();
72
73 @Before
74 public void initGRL() throws Exception {
75 GlobalResourceLoader.stop();
76 SimpleConfig config = new SimpleConfig();
77 config.putProperty(CoreConstants.Config.APPLICATION_ID, "APPID");
78 ConfigContext.init(config);
79
80 StaticListableBeanFactory testBf = new StaticListableBeanFactory();
81 when(kualiModuleService.getInstalledModuleServices()).thenReturn(installedModuleServices);
82
83 testBf.addBean(KRADServiceLocator.PROVIDER_REGISTRY, mock(ProviderRegistry.class));
84 testBf.addBean(KRADServiceLocatorWeb.KUALI_MODULE_SERVICE, kualiModuleService);
85
86 ResourceLoader rl = new BeanFactoryResourceLoader(new QName("moduleservicebase-unittest"), testBf);
87 GlobalResourceLoader.addResourceLoader(rl);
88 GlobalResourceLoader.start();
89 }
90
91 @Before
92 public void setup() {
93 config.setNamespaceCode("moduleconfiguration-unittest");
94 config.setDatabaseRepositoryFilePaths(Arrays.asList(new String[]{"path1", "path2"}));
95 config.setDataDictionaryPackages(Arrays.asList(new String[]{"ddpackage1", "ddpackage2"}));
96
97
98 config.setPackagePrefixes(Arrays.asList(new String[]{ TestEBOInterface.class.getPackage().getName(), getClass().getPackage().getName() }));
99
100 PersistenceProvider pp = mock(PersistenceProvider.class);
101 when(pp.handles(DataObject.class)).thenReturn(true);
102 config.setProviders(Arrays.asList(new Provider[] { pp }));
103
104 module.setModuleConfiguration(config);
105 }
106
107 @Test
108 public void testRegistersWithKualiModuleService() throws Exception {
109 module.afterPropertiesSet();
110 assertTrue(installedModuleServices.contains(module));
111 }
112
113 @Test
114 public void testNotResponsibleForNullClass() {
115 assertFalse(module.isResponsibleFor(null));
116 }
117
118 @Test
119 public void testIsResponsibleForClassByPackagePrefix() {
120 assertTrue(module.isResponsibleFor(TestEBOInterface.class));
121 }
122
123 @Test
124 public void testIsResponsibleForClassByProvider() {
125 assertTrue(module.isResponsibleFor(DataObject.class));
126 }
127
128 @Test
129 public void testIsResponsibleForEBOSubclass() {
130 assertTrue(module.isResponsibleFor(ModuleServiceBaseTestEBO.class));
131 }
132
133 @Test
134 public void testIsResponsibleForEBOInterface() {
135 assertTrue(module.isResponsibleFor(ModuleServiceBaseTestEBOInterface.class));
136 }
137 }