1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
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
93 ClearDatabaseLifecycle clearDatabaseLifeCycle = new ClearDatabaseLifecycle();
94 clearDatabaseLifeCycle.start();
95
96
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 }