001    /*
002     * Copyright 2007 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.rice.test.lifecycles;
014    
015    import java.lang.reflect.Method;
016    
017    import org.kuali.rice.core.api.lifecycle.Lifecycle;
018    import org.kuali.rice.test.data.PerTestUnitTestData;
019    import org.kuali.rice.test.data.UnitTestData;
020    import org.kuali.rice.test.data.UnitTestDataUtils;
021    
022    /**
023     * A lifecycle for loading SQL datasets based on the PerSuiteUnitTestData annotation. The individual SQL statements are
024     * loaded first, followed by the statements inside the files (files are loaded sequentially in the order listed in the
025     * annotation).
026     * 
027     * @author Kuali Rice Team (rice.collab@kuali.org)
028     */
029    public class PerTestDataLoaderLifecycle implements Lifecycle {
030            private boolean started;
031            private Method method;
032    
033            public PerTestDataLoaderLifecycle(Method method) {
034                    this.method = method;
035            }
036    
037            public boolean isStarted() {
038                    return started;
039            }
040    
041            public void start() throws Exception {
042                    if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
043                            UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).value();
044                UnitTestDataUtils.executeDataLoader(data);
045                    }
046                    if (method.isAnnotationPresent(UnitTestData.class)) {
047                            UnitTestData data = method.getAnnotation(UnitTestData.class);
048                UnitTestDataUtils.executeDataLoader(data);
049                    }
050                    started = true;
051            }
052    
053            public void stop() throws Exception {
054                if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
055                UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).tearDown();
056                UnitTestDataUtils.executeDataLoader(data);
057            }
058                    started = false;
059            }
060    }