1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util;
17
18 import org.junit.AfterClass;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.kuali.rice.core.api.CoreConstants;
23 import org.kuali.rice.core.api.config.property.ConfigContext;
24 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
25 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
26 import org.kuali.rice.core.framework.config.property.SimpleConfig;
27 import org.kuali.rice.krad.data.metadata.MetadataRepository;
28 import org.kuali.rice.krad.service.DataDictionaryService;
29 import org.mockito.runners.MockitoJUnitRunner;
30
31 import javax.xml.namespace.QName;
32
33 import java.util.concurrent.Callable;
34
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39
40
41
42
43
44
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class LegacyUtilsTest {
48
49 @BeforeClass
50 public static void initGrl() throws Exception {
51
52 MetadataRepository metadataRepository = mock(MetadataRepository.class);
53 DataDictionaryService dataDictionaryService = mock(DataDictionaryService.class);
54 ResourceLoader resourceLoader = mock(ResourceLoader.class);
55
56 SimpleConfig config = new SimpleConfig();
57 config.putProperty(CoreConstants.Config.APPLICATION_ID, LegacyUtilsTest.class.getName());
58 config.putProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK, "true");
59 ConfigContext.init(config);
60
61 when(resourceLoader.getName()).thenReturn(QName.valueOf(LegacyUtilsTest.class.getName()));
62 when(resourceLoader.getService(QName.valueOf("metadataRepository"))).thenReturn(metadataRepository);
63 when(resourceLoader.getService(QName.valueOf("dataDictionaryService"))).thenReturn(dataDictionaryService);
64
65 GlobalResourceLoader.addResourceLoader(resourceLoader);
66 GlobalResourceLoader.start();
67 }
68
69 @AfterClass
70 public static void stopGrl() throws Exception {
71 GlobalResourceLoader.stop();
72 }
73
74 @Test
75 public void testIsKnsDocumentEntry() {
76 assertTrue(LegacyUtils.isKnsDocumentEntry(new org.kuali.rice.kns.datadictionary.TransactionalDocumentEntry()));
77 assertTrue(LegacyUtils.isKnsDocumentEntry(new org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry()));
78 assertFalse(LegacyUtils.isKnsDocumentEntry(new org.kuali.rice.krad.datadictionary.TransactionalDocumentEntry()));
79 assertFalse(LegacyUtils.isKnsDocumentEntry(new org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry()));
80 }
81
82 @Test
83 public void testDoInLegacyContext() throws Exception {
84 assertFalse(LegacyUtils.isInLegacyContext());
85 Boolean evaluated = LegacyUtils.doInLegacyContext(new Callable<Boolean>() {
86 @Override
87 public Boolean call() throws Exception {
88 assertTrue(LegacyUtils.isInLegacyContext());
89 return true;
90 }
91 });
92 assertTrue(evaluated);
93 assertFalse(LegacyUtils.isInLegacyContext());
94 }
95
96 @Test
97 public void testDoInLegacyContext_Exception() {
98 final String exceptionMessage = "I failed! Oh the humanity!!!";
99 assertFalse(LegacyUtils.isInLegacyContext());
100 try {
101 Boolean evaluated = LegacyUtils.doInLegacyContext(new Callable<Boolean>() {
102 @Override
103 public Boolean call() throws Exception {
104 assertTrue(LegacyUtils.isInLegacyContext());
105 throw new Exception(exceptionMessage);
106 }
107 });
108 fail("An exception should have been thrown.");
109 } catch (Exception e) {
110 assertEquals(exceptionMessage, e.getMessage());
111 }
112 assertFalse(LegacyUtils.isInLegacyContext());
113 }
114 }