View Javadoc

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