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.test.data;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.test.SQLDataLoader;
20  
21  /**
22   * Utilities for unit test data annotations. 
23   * 
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   *
26   */
27  public final class UnitTestDataUtils {
28  
29  	private UnitTestDataUtils() {
30  		throw new UnsupportedOperationException("do not call");
31  	}
32  	
33      public static void executeDataLoader(UnitTestData[] data) throws Exception {
34          for (UnitTestData d: data) {
35              executeDataLoader(d);
36          }
37      }
38  
39      public static void executeDataLoader(UnitTestData data) throws Exception {
40          SQLDataLoader sqlDataLoader;
41          for (UnitTestData.Type type : data.order()) {
42              switch (type) {
43                  case SQL_FILES : 
44                      for (UnitTestFile file : data.sqlFiles()) {
45                          sqlDataLoader = new SQLDataLoader(file.filename(), file.delimiter());
46                          sqlDataLoader.runSql();
47                      }
48                      break;
49                  case SQL_STATEMENTS : 
50                      for (UnitTestSql statement : data.sqlStatements()) {
51                          sqlDataLoader = new SQLDataLoader(statement.value());
52                          sqlDataLoader.runSql();
53                      }
54                      break;
55                  default: break;
56              }
57          }
58          
59          if (!StringUtils.isEmpty(data.filename())) {
60              if (!StringUtils.isEmpty(data.value()))
61                  throw new RuntimeException("UnitTestDataArtifact may not specify both SQL file and content");
62              sqlDataLoader = new SQLDataLoader(data.filename(), data.delimiter());
63              sqlDataLoader.runSql();
64          } else if (!StringUtils.isEmpty(data.value())) {
65              sqlDataLoader = new SQLDataLoader(data.value());
66              sqlDataLoader.runSql();
67          }
68      }
69  }