View Javadoc
1   package net.sumaris.core.test;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Test shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  
28  import net.sumaris.core.service.ServiceLocator;
29  import org.apache.commons.lang3.StringUtils;
30  import org.junit.Assume;
31  import org.junit.Ignore;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import javax.persistence.EntityManager;
36  import javax.persistence.Query;
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 Logger log = LoggerFactory.getLogger(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 Query} object.
94  	 */
95  	public static Query createStatelessQuery(String hqlQuery) {
96  		EntityManager entityManager = getBean("entityManager", EntityManager.class);
97  		return entityManager.createQuery(hqlQuery);
98      }
99  
100 	/**
101 	 * <p>countEntities.</p>
102 	 *
103 	 * @param entityClass a {@link Class} object.
104 	 * @return a long.
105 	 */
106 	public static long countEntities(Class<?> entityClass) {
107         return countEntities(entityClass, null, "");
108     }
109 
110 	/**
111 	 * <p>countEntities.</p>
112 	 *
113 	 * @param entityClass a {@link Class} object.
114 	 * @param alias a {@link String} object.
115 	 * @param whereClause a {@link String} object.
116 	 * @return a long.
117 	 */
118 	public static long countEntities(Class<?> entityClass, String alias, String whereClause) {
119         if (StringUtils.isNotBlank(whereClause)) {
120             whereClause = " " + alias + " where " + whereClause;
121         }
122         Query query = createStatelessQuery("select count(*) from " + entityClass.getName() + whereClause);
123         return (Long)query.getSingleResult();
124     }
125 }