1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.test.spring;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.junit.Before;
21 import org.junit.runner.RunWith;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.support.FileSystemXmlApplicationContext;
25 import org.springframework.core.io.ClassPathResource;
26 import org.springframework.test.context.ContextConfiguration;
27 import org.springframework.test.context.TestExecutionListeners;
28 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29 import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
30 import org.springframework.test.context.transaction.BeforeTransaction;
31 import org.springframework.test.context.transaction.TransactionConfiguration;
32 import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
33 import org.springframework.transaction.TransactionDefinition;
34 import org.springframework.transaction.TransactionStatus;
35 import org.springframework.transaction.annotation.Transactional;
36 import org.springframework.transaction.jta.JtaTransactionManager;
37 import org.springframework.transaction.support.DefaultTransactionDefinition;
38
39 import javax.persistence.EntityManager;
40 import javax.persistence.PersistenceContext;
41 import java.io.BufferedReader;
42 import java.io.File;
43 import java.io.FileNotFoundException;
44 import java.io.FileReader;
45 import java.io.IOException;
46 import java.lang.reflect.Field;
47 import java.util.List;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @RunWith(SpringJUnit4ClassRunner.class)
122 @ContextConfiguration(locations = { "classpath:META-INF/default-dao-context-test.xml" })
123 @TestExecutionListeners( { TransactionalTestExecutionListener.class,
124 DaoTestDependencyInjectorListener.class,
125 DirtiesContextTestExecutionListener.class })
126 @Transactional
127 @TransactionConfiguration(transactionManager = "JtaTxManager")
128 public abstract class AbstractTransactionalDaoTest {
129 final Logger LOG = Logger.getLogger(AbstractTransactionalDaoTest.class);
130 @PersistenceContext
131 protected EntityManager em;
132
133 @Autowired
134 private JtaTransactionManager jtaTxManager;
135
136 private static boolean preloadedData=false;
137
138
139
140
141
142
143 @Before
144 public void preLoadBeans() {
145 for (Field f : this.getClass().getDeclaredFields()) {
146 if (f.isAnnotationPresent(Dao.class)) {
147 Dao dao = f.getAnnotation(Dao.class);
148 if (dao.testDataFile().length() > 0) {
149 ApplicationContext ac = new FileSystemXmlApplicationContext(
150 dao.testDataFile());
151 for (Object o : (List<?>) ac.getBean("persistList")) {
152 em.persist(o);
153 }
154 em.flush();
155 }
156 }
157 }
158 }
159
160 @BeforeTransaction
161 public void preLoadData() throws IOException {
162 if(!preloadedData){
163 preloadedData=true;
164
165 for (Field f : this.getClass().getDeclaredFields()) {
166 if (f.isAnnotationPresent(Dao.class)) {
167 Dao dao = f.getAnnotation(Dao.class);
168 if (dao.testSqlFile().length() > 0) {
169 if (dao.testSqlFile().startsWith("classpath:")) {
170 String file = dao.testSqlFile().substring("classpath:".length());
171 String[] files = file.split("\\s*,\\s*");
172 for(String testFile : files) {
173 File sqlFile = new ClassPathResource(testFile).getFile();
174 process(sqlFile);
175 }
176 } else {
177 String[] files = dao.testSqlFile().split("\\s*,\\s*");
178 for(String testFile : files) {
179 File sqlFile = new File(testFile);
180 process(sqlFile);
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188
189 private void process(File sqlFile) throws FileNotFoundException {
190 BufferedReader in
191 = new BufferedReader(new FileReader(sqlFile));
192 String ln;
193
194
195 TransactionDefinition txDefinition = new DefaultTransactionDefinition() ;
196 TransactionStatus txStatus = jtaTxManager.getTransaction(txDefinition);
197
198 try {
199
200 while((ln=in.readLine())!=null){
201 if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){
202 ln=ln.replaceFirst("[;/]\\s*$","");
203 System.err.println(ln);
204 em.createNativeQuery(ln).executeUpdate();
205 }
206 }
207 jtaTxManager.commit(txStatus);
208 } catch (Exception e) {
209 LOG.error(e);
210 jtaTxManager.rollback(txStatus);
211 }
212 finally{
213 try {
214 in.close();
215 } catch (IOException e) {
216 LOG.error("IO Stream closed " + e);
217 }
218 }
219 }
220
221
222
223
224 public AbstractTransactionalDaoTest() {
225 super();
226
227 if (this.getClass().isAnnotationPresent(PersistenceFileLocation.class)) {
228 PersistenceFileLocation a = this.getClass().getAnnotation(
229 PersistenceFileLocation.class);
230 System.setProperty("ks.test.persistenceLocation", a.value());
231 } else {
232 System.setProperty("ks.test.persistenceLocation",
233 "classpath:META-INF/persistence.xml");
234 }
235 }
236 }