1 package fr.ifremer.quadrige3.core.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.quadrige3.core.service.ServiceLocator;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.hibernate.Query;
32 import org.hibernate.SessionFactory;
33 import org.hibernate.StatelessSession;
34 import org.junit.Assume;
35 import org.junit.Ignore;
36
37 import java.io.File;
38
39
40
41
42
43
44
45 @Ignore
46 public final class Tests {
47
48
49
50
51 protected Tests() {
52 }
53
54
55 private static final Log log = LogFactory.getLog(Tests.class);
56
57
58
59
60
61
62
63 public static void checkDbExists(Class<?> testClass, String dbDirectory) {
64 File db = new File(dbDirectory);
65 if (!db.exists()) {
66
67 if (log.isWarnEnabled()) {
68 log.warn("Could not find db at " + db + ", test [" +
69 testClass + "] is skipped.");
70 }
71 Assume.assumeTrue(false);
72 }
73 }
74
75
76
77
78
79
80
81
82
83 public static <S> S getBean(String name, Class<S> serviceType) {
84 S result = ServiceLocator.instance().getService(name, serviceType);
85 Assume.assumeNotNull(result);
86 return result;
87 }
88
89
90
91
92
93
94
95 public static Query createStatelessQuery(String hqlQuery) {
96 SessionFactory sessionFactory = getBean("sessionFactory", SessionFactory.class);
97 StatelessSession session = sessionFactory.openStatelessSession();
98 return session.createQuery(hqlQuery);
99 }
100
101
102
103
104
105
106
107 public static long countEntities(Class<?> entityClass) {
108 return countEntities(entityClass, null, "");
109 }
110
111
112
113
114
115
116
117
118
119 public static long countEntities(Class<?> entityClass, String alias, String whereClause) {
120 if (StringUtils.isNotBlank(whereClause)) {
121 whereClause = " " + alias + " where " + whereClause;
122 }
123 Query query = createStatelessQuery("select count(*) from " + entityClass.getName() + whereClause);
124 return (Long)query.uniqueResult();
125 }
126 }