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.core.test;
17  
18  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
19  import org.kuali.rice.test.ClearDatabaseLifecycle;
20  import org.kuali.rice.test.TestHarnessServiceLocator;
21  import org.kuali.rice.test.data.PerSuiteUnitTestData;
22  import org.kuali.rice.test.data.UnitTestData;
23  import org.springframework.jdbc.core.ConnectionCallback;
24  import org.springframework.jdbc.core.JdbcTemplate;
25  
26  import javax.sql.DataSource;
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Statement;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * AnnotationTestParent is used by the {@link DataLoaderAnnotationTest} and {@link DataLoaderAnnotationOverrideTest} classes to verify parent class annotation usage
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @PerSuiteUnitTestData({
40          @UnitTestData("insert into " + AnnotationTestParent.TEST_TABLE_NAME + " (COL) values ('1')"),
41          @UnitTestData(filename = "classpath:org/kuali/rice/test/AnnotationTestParentData.sql")
42  })
43  public abstract class AnnotationTestParent extends CORETestCase {
44  
45      protected static final String TEST_TABLE_NAME = "EN_UNITTEST_T";
46      
47      protected void verifyCount(String valueToVerify, int count) throws SQLException {
48          verifyCount(valueToVerify, count, "");
49      }
50  
51      protected void verifyCount(String valueToVerify, int count, String message) throws SQLException {
52          assertEquals(count + " value(s) should be found for id " + valueToVerify + " " + message, count, countTableResults(valueToVerify));
53      }
54  
55      protected void verifyExistence(String valueToVerify) throws SQLException {
56          assertTrue("Value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
57      }
58  
59      protected void verifyNonExistent(String valueToVerify) throws SQLException {
60          assertFalse("No value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
61      }
62  
63      protected boolean hasTableResults(String valueToVerify) {
64          return countTableResults(valueToVerify) > 0;
65      }
66  
67      protected int countTableResults(String valueToVerify) {
68          final String valueToCheck = valueToVerify;
69          final DataSource dataSource = TestHarnessServiceLocator.getDataSource();
70          return (Integer) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
71              public Object doInConnection(final Connection connection) throws SQLException {
72                  Statement statement = null;
73                  try {
74                      statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
75                      final ResultSet resultSet = statement.executeQuery("Select * from " + TEST_TABLE_NAME + " where COL = '" + valueToCheck + "'");
76                      assertNotNull("ResultSet should not be null",resultSet);
77                      int count = 0;
78                      while (resultSet.next()) {
79                          count++;
80                      }
81                      return count;
82                  } finally {
83                      if (statement != null) {
84                          statement.close();
85                      }
86                  }
87              }
88          });
89      }
90  
91      protected void resetDb() throws Exception {
92          // cleanup database from previous @PerSuiteUnitTestData
93          ClearDatabaseLifecycle clearDatabaseLifeCycle = new ClearDatabaseLifecycle();
94          clearDatabaseLifeCycle.start();
95  
96          // Re-Loads Suite Test Data - Needed after adhoc cleanout
97          BaseLifecycle baseLifecycle = new BaseLifecycle() {
98              @Override
99              public void start() throws Exception {
100                 loadSuiteTestData();
101                 super.start();
102             }
103         };
104         baseLifecycle.start();
105     }
106 
107 }