1 package fr.ifremer.quadrige3.core.test;
2
3 /*-
4 * #%L
5 * Quadrige3 Core :: Quadrige3 Core Shared
6 * $Id:$
7 * $HeadURL:$
8 * %%
9 * Copyright (C) 2017 Ifremer
10 * %%
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * #L%
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 * Useful method around tests.
41 *
42 * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
43 * @since 3.3.1
44 */
45 @Ignore
46 public final class Tests {
47
48 /**
49 * <p>Constructor for Tests.</p>
50 */
51 protected Tests() {
52 }
53
54 /** Logger. */
55 private static final Log log = LogFactory.getLog(Tests.class);
56
57 /**
58 * <p>checkDbExists.</p>
59 *
60 * @param testClass a {@link Class} object.
61 * @param dbDirectory a {@link String} object.
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 * <p>getBean.</p>
77 *
78 * @param name a {@link String} object.
79 * @param serviceType a {@link Class} object.
80 * @param <S> a S object.
81 * @return a S object.
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 * <p>createStatelessQuery.</p>
91 *
92 * @param hqlQuery a {@link String} object.
93 * @return a {@link org.hibernate.Query} object.
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 * <p>countEntities.</p>
103 *
104 * @param entityClass a {@link Class} object.
105 * @return a long.
106 */
107 public static long countEntities(Class<?> entityClass) {
108 return countEntities(entityClass, null, "");
109 }
110
111 /**
112 * <p>countEntities.</p>
113 *
114 * @param entityClass a {@link Class} object.
115 * @param alias a {@link String} object.
116 * @param whereClause a {@link String} object.
117 * @return a long.
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 }