001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.test;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.lifecycle.Lifecycle;
020    import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
021    import org.kuali.rice.krad.datadictionary.DataDictionary;
022    import org.kuali.rice.test.BaselineTestCase;
023    import org.kuali.rice.test.SQLDataLoader;
024    import org.kuali.rice.test.TestHarnessServiceLocator;
025    import org.kuali.rice.test.TestUtilities;
026    import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
027    import org.springframework.context.ConfigurableApplicationContext;
028    import org.springframework.context.support.ClassPathXmlApplicationContext;
029    
030    import javax.xml.namespace.QName;
031    import java.util.HashSet;
032    import java.util.List;
033    
034    /**
035     * Default test base for a full KRAD enabled integration test
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
040    public abstract class KRADTestCase extends BaselineTestCase {
041    
042        private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
043        private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
044        private static final String KRAD_MODULE_NAME = "krad";
045    
046        protected DataDictionary dd;
047    
048        public KRADTestCase() {
049            super(KRAD_MODULE_NAME);
050        }
051    
052        /**
053         * propagate constructor
054         * @param moduleName - the name of the module
055         */
056        public KRADTestCase(String moduleName) {
057            super(moduleName);
058        }
059    
060        @Override
061        protected void setUpInternal() throws Exception {
062            super.setUpInternal();
063    
064            List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
065                    new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
066    
067            // if annotation is present then initialize test data dictionary (setup once per suite)
068            if (!classes.isEmpty()) {
069                ConfigurableApplicationContext context  = new ClassPathXmlApplicationContext("TestDataDictionary.xml");
070                dd = (DataDictionary) context.getBean("testDataDictionary");
071    
072                // add any additional dictionary files required by the test
073                for (Class c : classes) {
074                    if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
075                        TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
076                                TestDictionaryConfig.class);
077    
078                        String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
079                        String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
080                        for (String dictionaryFile : dictionaryFiles) {
081                            dd.addConfigFileLocation(dictionaryFile);
082                        }
083                    }
084                }
085    
086                dd.parseDataDictionaryConfigurationFiles(true);
087            }
088        }
089    
090        /**
091         * Returns an instance of the bean with the given id that has been configured in the test dictionary
092         *
093         * @param id - id of the bean definition
094         * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
095         */
096        protected Object getTestDictionaryObject(String id) {
097            if (dd != null) {
098                return dd.getDictionaryObject(id);
099            }
100    
101            return null;
102        }
103    
104        @Override
105        protected List<Lifecycle> getSuiteLifecycles() {
106            List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
107            suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
108    
109            return suiteLifecycles;
110        }
111    
112        @Override
113        protected void loadSuiteTestData() throws Exception {
114            super.loadSuiteTestData();
115            new SQLDataLoader(SQL_FILE, ";").runSql();
116        }
117    
118        @Override
119        protected Lifecycle getLoadApplicationLifecycle() {
120            SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KRADTestResourceLoader"),
121                    "classpath:KRADTestSpringBeans.xml", null);
122            springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
123            return springResourceLoader;
124        }
125    }