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 java.io.BufferedReader; |
19 |
|
import java.io.File; |
20 |
|
import java.io.FileNotFoundException; |
21 |
|
import java.io.FileReader; |
22 |
|
import java.io.IOException; |
23 |
|
import java.lang.reflect.Field; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.persistence.EntityManager; |
27 |
|
import javax.persistence.PersistenceContext; |
28 |
|
|
29 |
|
import org.apache.commons.lang.StringUtils; |
30 |
|
import org.apache.log4j.Logger; |
31 |
|
import org.junit.Before; |
32 |
|
import org.junit.runner.RunWith; |
33 |
|
import org.springframework.beans.factory.annotation.Autowired; |
34 |
|
import org.springframework.context.ApplicationContext; |
35 |
|
import org.springframework.context.support.FileSystemXmlApplicationContext; |
36 |
|
import org.springframework.core.io.ClassPathResource; |
37 |
|
import org.springframework.test.context.ContextConfiguration; |
38 |
|
import org.springframework.test.context.TestExecutionListeners; |
39 |
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
40 |
|
import org.springframework.test.context.support.DirtiesContextTestExecutionListener; |
41 |
|
import org.springframework.test.context.transaction.BeforeTransaction; |
42 |
|
import org.springframework.test.context.transaction.TransactionConfiguration; |
43 |
|
import org.springframework.test.context.transaction.TransactionalTestExecutionListener; |
44 |
|
import org.springframework.transaction.TransactionDefinition; |
45 |
|
import org.springframework.transaction.TransactionStatus; |
46 |
|
import org.springframework.transaction.annotation.Transactional; |
47 |
|
import org.springframework.transaction.jta.JtaTransactionManager; |
48 |
|
import org.springframework.transaction.support.DefaultTransactionDefinition; |
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 |
|
|
122 |
|
@RunWith(SpringJUnit4ClassRunner.class) |
123 |
|
@ContextConfiguration(locations = { "classpath:META-INF/default-dao-context-test.xml" }) |
124 |
|
@TestExecutionListeners( { TransactionalTestExecutionListener.class, |
125 |
|
DaoTestDependencyInjectorListener.class, |
126 |
|
DirtiesContextTestExecutionListener.class }) |
127 |
|
@Transactional |
128 |
|
@TransactionConfiguration(transactionManager = "JtaTxManager") |
|
|
| 84.8% |
Uncovered Elements: 10 (66) |
Complexity: 17 |
Complexity Density: 0.39 |
|
129 |
|
public abstract class AbstractTransactionalDaoTest { |
130 |
|
final Logger LOG = Logger.getLogger(AbstractTransactionalDaoTest.class); |
131 |
|
@PersistenceContext |
132 |
|
protected EntityManager em; |
133 |
|
|
134 |
|
@Autowired |
135 |
|
private JtaTransactionManager jtaTxManager; |
136 |
|
|
137 |
|
|
138 |
|
private static boolean preloadedData=false; |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
144 |
197
|
@Before... |
145 |
|
public void preLoadBeans() { |
146 |
197
|
for (Field f : this.getClass().getDeclaredFields()) { |
147 |
215
|
if (f.isAnnotationPresent(Dao.class)) { |
148 |
197
|
Dao dao = f.getAnnotation(Dao.class); |
149 |
197
|
if (dao.testDataFile().length() > 0) { |
150 |
23
|
ApplicationContext ac = new FileSystemXmlApplicationContext( |
151 |
|
dao.testDataFile()); |
152 |
23
|
for (Object o : (List<?>) ac.getBean("persistList")) { |
153 |
55
|
em.persist(o); |
154 |
|
} |
155 |
23
|
em.flush(); |
156 |
|
} |
157 |
|
} |
158 |
|
} |
159 |
|
} |
160 |
|
|
|
|
| 79.2% |
Uncovered Elements: 5 (24) |
Complexity: 5 |
Complexity Density: 0.31 |
|
161 |
197
|
@BeforeTransaction... |
162 |
|
public void preLoadData() throws IOException { |
163 |
197
|
if(!preloadedData){ |
164 |
14
|
preloadedData=true; |
165 |
|
|
166 |
14
|
for (Field f : this.getClass().getDeclaredFields()) { |
167 |
17
|
if (f.isAnnotationPresent(Dao.class)) { |
168 |
14
|
Dao dao = f.getAnnotation(Dao.class); |
169 |
14
|
if (dao.testSqlFile().length() > 0) { |
170 |
11
|
if (dao.testSqlFile().startsWith("classpath:")) { |
171 |
11
|
String file = dao.testSqlFile().substring("classpath:".length()); |
172 |
11
|
String[] files = file.split("\\s*,\\s*"); |
173 |
11
|
for(String testFile : files) { |
174 |
12
|
File sqlFile = new ClassPathResource(testFile).getFile(); |
175 |
12
|
process(sqlFile); |
176 |
|
} |
177 |
|
} else { |
178 |
0
|
String[] files = dao.testSqlFile().split("\\s*,\\s*"); |
179 |
0
|
for(String testFile : files) { |
180 |
0
|
File sqlFile = new File(testFile); |
181 |
0
|
process(sqlFile); |
182 |
|
} |
183 |
|
} |
184 |
|
} |
185 |
|
} |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
|
|
| 73.7% |
Uncovered Elements: 5 (19) |
Complexity: 7 |
Complexity Density: 0.47 |
|
190 |
12
|
private void process(File sqlFile) throws FileNotFoundException {... |
191 |
12
|
BufferedReader in |
192 |
|
= new BufferedReader(new FileReader(sqlFile)); |
193 |
12
|
String ln; |
194 |
|
|
195 |
|
|
196 |
12
|
TransactionDefinition txDefinition = new DefaultTransactionDefinition() ; |
197 |
12
|
TransactionStatus txStatus = jtaTxManager.getTransaction(txDefinition); |
198 |
|
|
199 |
12
|
try { |
200 |
|
|
201 |
?
|
while((ln=in.readLine())!=null){ |
202 |
4401
|
if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){ |
203 |
3115
|
ln=ln.replaceFirst("[;/]\\s*$",""); |
204 |
3115
|
em.createNativeQuery(ln).executeUpdate(); |
205 |
|
} |
206 |
|
} |
207 |
12
|
jtaTxManager.commit(txStatus); |
208 |
|
} catch (Exception e) { |
209 |
0
|
LOG.error(e); |
210 |
0
|
jtaTxManager.rollback(txStatus); |
211 |
|
} |
212 |
|
finally{ |
213 |
12
|
try { |
214 |
12
|
in.close(); |
215 |
|
} catch (IOException e) { |
216 |
0
|
LOG.error("IO Stream closed " + e); |
217 |
|
} |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
224 |
396
|
public AbstractTransactionalDaoTest() {... |
225 |
396
|
super(); |
226 |
|
|
227 |
396
|
if (this.getClass().isAnnotationPresent(PersistenceFileLocation.class)) { |
228 |
198
|
PersistenceFileLocation a = this.getClass().getAnnotation( |
229 |
|
PersistenceFileLocation.class); |
230 |
198
|
System.setProperty("ks.test.persistenceLocation", a.value()); |
231 |
|
} else { |
232 |
198
|
System.setProperty("ks.test.persistenceLocation", |
233 |
|
"classpath:META-INF/persistence.xml"); |
234 |
|
} |
235 |
|
} |
236 |
|
} |