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.FileReader; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Map; |
23 |
|
|
24 |
|
import javax.persistence.EntityManager; |
25 |
|
import javax.persistence.PersistenceContext; |
26 |
|
import javax.persistence.PersistenceException; |
27 |
|
|
28 |
|
import org.apache.commons.lang.StringUtils; |
29 |
|
import org.apache.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
import org.springframework.context.ApplicationContext; |
32 |
|
import org.springframework.context.ApplicationContextAware; |
33 |
|
import org.springframework.context.support.FileSystemXmlApplicationContext; |
34 |
|
import org.springframework.core.io.ClassPathResource; |
35 |
|
import org.springframework.transaction.annotation.Transactional; |
36 |
|
|
37 |
|
@Transactional |
|
|
| 82.4% |
Uncovered Elements: 12 (68) |
Complexity: 23 |
Complexity Density: 0.49 |
|
38 |
|
public class LoadDataBean implements ApplicationContextAware{ |
39 |
|
private static final Log LOG = LogFactory.getLog(LoadDataBean.class); |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@PersistenceContext |
44 |
|
EntityManager em; |
45 |
|
private boolean loaded = false; |
46 |
|
private String daoAnnotations; |
47 |
|
|
48 |
|
private ApplicationContext applicationContext; |
49 |
|
|
|
|
| 83.3% |
Uncovered Elements: 8 (48) |
Complexity: 16 |
Complexity Density: 0.47 |
|
50 |
30
|
public void loadData(){... |
51 |
30
|
if (daoAnnotations == null || loaded || daoAnnotations.trim().isEmpty()) { |
52 |
18
|
return; |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
12
|
String[] classes = daoAnnotations.split(","); |
57 |
12
|
for (String line : classes) { |
58 |
14
|
try { |
59 |
14
|
String[] split = line.split("\\|"); |
60 |
|
|
61 |
|
|
62 |
14
|
invokeDataLoader(split[0]); |
63 |
|
|
64 |
|
|
65 |
14
|
if (split.length > 1&& !split[1].isEmpty()) { |
66 |
4
|
String testDataFile = split[1]; |
67 |
|
|
68 |
4
|
ApplicationContext ac = new FileSystemXmlApplicationContext( |
69 |
|
testDataFile); |
70 |
4
|
for (Object bean : (List<?>) ac.getBean("persistList")) { |
71 |
21
|
if(!em.contains(bean)){ |
72 |
21
|
em.persist(bean); |
73 |
|
} |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
14
|
if (split.length > 2&& !split[2].isEmpty()) { |
78 |
|
|
79 |
8
|
String testDataFile = split[2]; |
80 |
8
|
File sqlFile; |
81 |
8
|
if(testDataFile.startsWith("classpath:")){ |
82 |
8
|
sqlFile = new ClassPathResource(testDataFile.substring("classpath:".length())).getFile(); |
83 |
|
}else{ |
84 |
0
|
sqlFile = new File(testDataFile); |
85 |
|
} |
86 |
8
|
BufferedReader in |
87 |
|
= new BufferedReader(new FileReader(sqlFile)); |
88 |
8
|
try { |
89 |
8
|
String ln = ""; |
90 |
8
|
int lnNr = 0; |
91 |
|
|
92 |
8
|
try { |
93 |
?
|
while((ln=in.readLine())!=null){ |
94 |
2495
|
lnNr++; |
95 |
2495
|
if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){ |
96 |
1480
|
ln=ln.replaceFirst("[;/]\\s*$",""); |
97 |
1480
|
em.createNativeQuery(ln).executeUpdate(); |
98 |
|
} |
99 |
|
} |
100 |
|
} catch (PersistenceException e) { |
101 |
0
|
LOG.error("Failed statement at line " + lnNr + ": " + ln); |
102 |
0
|
throw e; |
103 |
|
} |
104 |
|
} finally { |
105 |
8
|
in.close(); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
} catch (Exception e) { |
110 |
0
|
throw new RuntimeException(e); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
12
|
loaded = true; |
115 |
|
|
116 |
|
} |
117 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 4 |
Complexity Density: 0.4 |
|
118 |
14
|
protected void invokeDataLoader(String dao){... |
119 |
14
|
try { |
120 |
|
|
121 |
14
|
Class<?> daoType = Class.forName(dao).getInterfaces()[0]; |
122 |
|
|
123 |
14
|
Class<?> clazz = Class.forName(daoType.getName() + "Loader"); |
124 |
2
|
DaoLoader daoLoader = (DaoLoader)clazz.newInstance(); |
125 |
|
|
126 |
|
|
127 |
2
|
Map<?,?> daoBeans = applicationContext.getBeansOfType(daoType); |
128 |
|
|
129 |
|
|
130 |
2
|
if (daoBeans.size() == 1){ |
131 |
2
|
daoLoader.setDao(daoBeans.values().iterator().next()); |
132 |
2
|
daoLoader.run(); |
133 |
|
} |
134 |
|
|
135 |
|
} catch (ClassNotFoundException cnfe) { |
136 |
12
|
LOG.info(cnfe); |
137 |
|
} catch (Exception e) { |
138 |
0
|
LOG.error(e); |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
@return |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
0
|
public String getDaoAnnotations() {... |
146 |
0
|
return daoAnnotations; |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
@param |
151 |
|
|
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
153 |
15
|
public void setDaoAnnotations(String daoAnnotations) {... |
154 |
15
|
this.daoAnnotations = daoAnnotations; |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
157 |
15
|
public void setApplicationContext(ApplicationContext applicationContext){... |
158 |
15
|
this.applicationContext = applicationContext; |
159 |
|
} |
160 |
|
} |