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.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   * Tests the {@link LegacyUtils} class. For the most part, functionality of this is tested alredy by the
41   * {@link LegacyDetectorTest} since LegacyUtils delegates most of it's calls to the detector. This tests will test
42   * code internal to LegacyUtils.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
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 }