View Javadoc

1   /**
2    * Copyright 2005-2011 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.test.lifecycles;
17  
18  import org.kuali.rice.core.api.lifecycle.Lifecycle;
19  import org.kuali.rice.core.api.lifecycle.Lifecycle;
20  import org.kuali.rice.test.data.PerTestUnitTestData;
21  import org.kuali.rice.test.data.UnitTestData;
22  import org.kuali.rice.test.data.UnitTestDataUtils;
23  
24  import java.lang.reflect.Method;
25  
26  /**
27   * A lifecycle for loading SQL datasets based on the PerSuiteUnitTestData annotation. The individual SQL statements are
28   * loaded first, followed by the statements inside the files (files are loaded sequentially in the order listed in the
29   * annotation).
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class PerTestDataLoaderLifecycle implements Lifecycle {
34  	private boolean started;
35  	private Method method;
36  
37  	public PerTestDataLoaderLifecycle(Method method) {
38  		this.method = method;
39  	}
40  
41  	public boolean isStarted() {
42  		return started;
43  	}
44  
45  	public void start() throws Exception {
46  		if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
47  			UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).value();
48              UnitTestDataUtils.executeDataLoader(data);
49  		}
50  		if (method.isAnnotationPresent(UnitTestData.class)) {
51  			UnitTestData data = method.getAnnotation(UnitTestData.class);
52              UnitTestDataUtils.executeDataLoader(data);
53  		}
54  		started = true;
55  	}
56  
57  	public void stop() throws Exception {
58  	    if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
59              UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).tearDown();
60              UnitTestDataUtils.executeDataLoader(data);
61          }
62  		started = false;
63  	}
64  }