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.TestEBOAbstract;
28 import org.kuali.rice.krad.bo.test.TestEBOConcrete;
29 import org.kuali.rice.krad.bo.test.TestEBOInterface;
30 import org.kuali.rice.krad.bo.test.TestEBOInterfaceSub;
31 import org.kuali.rice.krad.data.provider.Provider;
32 import org.kuali.rice.krad.data.provider.ProviderRegistry;
33 import org.kuali.rice.krad.service.DataDictionaryService;
34 import org.kuali.rice.krad.service.KRADServiceLocator;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.springframework.beans.factory.support.StaticListableBeanFactory;
38
39 import javax.xml.namespace.QName;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import static org.mockito.Mockito.*;
46
47
48
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class ModuleConfigurationTest {
52
53 private ModuleConfiguration config = new ModuleConfiguration();
54 @Mock
55 private DataDictionaryService ddsMock;
56 @Mock
57 private ProviderRegistry prMock;
58
59 private List<String> ddPackages = Arrays.asList(new String[]{"package1", "package2"});
60 private List<Provider> providers = Arrays.asList(new Provider[] { mock(Provider.class), mock(Provider.class) });
61
62
63 @Before
64 public void initGRL() throws Exception {
65 GlobalResourceLoader.stop();
66 SimpleConfig config = new SimpleConfig();
67 config.putProperty(CoreConstants.Config.APPLICATION_ID, "APPID");
68 ConfigContext.init(config);
69 StaticListableBeanFactory testBf = new StaticListableBeanFactory();
70 testBf.addBean(KRADServiceLocator.PROVIDER_REGISTRY, prMock);
71 ResourceLoader rl = new BeanFactoryResourceLoader(new QName("moduleconfiguration-unittest"), testBf);
72 GlobalResourceLoader.addResourceLoader(rl);
73 GlobalResourceLoader.start();
74 }
75
76 @Before
77 public void setup() {
78 config.setDataDictionaryService(ddsMock);
79 }
80
81 protected void initModuleConfiguration() {
82 config.setNamespaceCode("moduleconfiguration-unittest");
83 config.setDataDictionaryPackages(ddPackages);
84 config.setProviders(providers);
85 }
86
87 @Test
88 public void testInitializationWithoutDD() throws Exception {
89 initModuleConfiguration();
90 config.afterPropertiesSet();
91 verify(ddsMock, never()).addDataDictionaryLocations(config.getNamespaceCode(), ddPackages);
92 }
93
94 @Test
95 public void testInitialization() throws Exception {
96 initModuleConfiguration();
97 config.setInitializeDataDictionary(true);
98 config.afterPropertiesSet();
99 verify(ddsMock).addDataDictionaryLocations(config.getNamespaceCode(), ddPackages);
100 verify(prMock).registerProvider(providers.get(0));
101 verify(prMock).registerProvider(providers.get(1));
102 }
103
104 @Test
105 public void testConcreteEBOIsValid() {
106 Map<Class, Class> ebos = new HashMap<Class, Class>();
107 ebos.put(TestEBOInterface.class, TestEBOConcrete.class);
108
109 config.setExternalizableBusinessObjectImplementations(ebos);
110 }
111
112 @Test(expected=RuntimeException.class)
113 public void testAbstractEBOIsInvalid() {
114 Map<Class, Class> ebos = new HashMap<Class, Class>();
115 ebos.put(TestEBOInterface.class, TestEBOAbstract.class);
116 config.setExternalizableBusinessObjectImplementations(ebos);
117 }
118
119 @Test(expected=RuntimeException.class)
120 public void testInterfaceEBOIsInvalid() {
121 Map<Class, Class> ebos = new HashMap<Class, Class>();
122 ebos.put(TestEBOInterface.class, TestEBOInterfaceSub.class);
123 config.setExternalizableBusinessObjectImplementations(ebos);
124 }
125
126 @Test(expected=UnsupportedOperationException.class)
127 public void testGetEBOsImmutability() {
128 Map<Class, Class> ebos = new HashMap<Class, Class>();
129 ebos.put(TestEBOInterface.class, TestEBOConcrete.class);
130
131 config.setExternalizableBusinessObjectImplementations(ebos);
132
133 Map<Class, Class> returnedEbos = config.getExternalizableBusinessObjectImplementations();
134
135 returnedEbos.put(TestEBOInterface.class, TestEBOConcrete.class);
136 }
137
138 @Test(expected=UnsupportedOperationException.class)
139 public void testGetProvidersImmutability() {
140 initModuleConfiguration();
141 List<Provider> providers = config.getProviders();
142
143 providers.add(mock(Provider.class));
144 }
145 }