001    /*
002     * Copyright 2007-2008 The Kuali Foundation
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
005     * compliance with the License. You may obtain a copy of the License at
006     * 
007     * http://www.opensource.org/licenses/ecl2.php
008     * 
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
010     * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
011     * language governing permissions and limitations under the License.
012     */
013    package org.kuali.test;
014    
015    import java.util.List;
016    
017    import org.kuali.rice.core.lifecycle.BaseLifecycle;
018    import org.kuali.rice.core.lifecycle.Lifecycle;
019    import org.kuali.rice.kew.batch.KEWXmlDataLoaderLifecycle;
020    import org.kuali.rice.kns.service.KNSServiceLocator;
021    import org.kuali.rice.kns.util.GlobalVariables;
022    import org.kuali.rice.kns.util.MessageMap;
023    import org.kuali.rice.test.RiceInternalSuiteDataTestCase;
024    import org.kuali.rice.test.TransactionalLifecycle;
025    import org.kuali.rice.test.lifecycles.JettyServerLifecycle;
026    import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
027    import org.kuali.rice.test.web.HtmlUnitUtil;
028    
029    
030    /**
031     * Default test base for a full KNS enabled unit test.
032     * 
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public abstract class KNSTestCase extends RiceInternalSuiteDataTestCase {
036    
037            private static final String SQL_FILE = "classpath:org/kuali/rice/kns/test/DefaultSuiteTestData.sql";
038            private static final String XML_FILE = "classpath:org/kuali/rice/kns/test/DefaultSuiteTestData.xml";
039            
040            private String contextName = "/knstest";
041            private String relativeWebappRoot = "/../kns/src/test/webapp";
042        
043            
044            private TransactionalLifecycle transactionalLifecycle;
045            
046            @Override
047            protected void loadSuiteTestData() throws Exception {
048                    super.loadSuiteTestData();
049                    new SQLDataLoaderLifecycle(SQL_FILE, ";").start();
050            }
051    
052            @Override
053            protected List<Lifecycle> getSuiteLifecycles() {
054                    List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
055                    suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
056                    return suiteLifecycles;
057            }
058    
059            @Override
060            protected Lifecycle getLoadApplicationLifecycle() {
061                    return new BaseLifecycle() {
062                            public void start() throws Exception {
063                                    new JettyServerLifecycle(getPort(), getContextName(), getRelativeWebappRoot()).start();
064                                    super.start();
065                            }
066                    };      
067            }
068    
069            public void setUp() throws Exception {
070                    super.setUp();
071                    final boolean needsSpring = getClass().isAnnotationPresent(KNSWithTestSpringContext.class);
072                    GlobalVariables.setMessageMap(new MessageMap());
073                    if (needsSpring) {
074                            transactionalLifecycle = new TransactionalLifecycle();
075                            transactionalLifecycle.setTransactionManager(KNSServiceLocator.getTransactionManager());
076                            transactionalLifecycle.start();
077                    }
078            }
079            
080            public void tearDown() throws Exception {
081                    final boolean needsSpring = getClass().isAnnotationPresent(KNSWithTestSpringContext.class);
082                    if (needsSpring) {
083                        if ( (transactionalLifecycle != null) && (transactionalLifecycle.isStarted()) ) {
084                            transactionalLifecycle.stop();
085                        }
086                    }
087                    GlobalVariables.setMessageMap(new MessageMap());
088                    super.tearDown();
089            }
090    
091    
092            @Override
093            protected String getModuleName() {
094                    return "kns";
095            }
096    
097            protected String getContextName() {
098                    return contextName;
099            }
100    
101            protected void setContextName(String contextName) {
102                    this.contextName = contextName;
103            }
104    
105            protected int getPort() {
106                    return HtmlUnitUtil.getPort();
107            }
108    
109            protected String getRelativeWebappRoot() {
110                    return relativeWebappRoot;
111            }
112    
113            protected void setRelativeWebappRoot(String relativeWebappRoot) {
114                    this.relativeWebappRoot = relativeWebappRoot;
115            }
116    
117    }