Skip to contentMethod: TestEnvironment()
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.environment;
16:
17: import cz.cvut.kbss.jopa.Persistence;
18: import cz.cvut.kbss.jopa.model.EntityManager;
19: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
20: import cz.cvut.kbss.jopa.model.JOPAPersistenceProvider;
21:
22: import java.io.File;
23: import java.util.*;
24:
25: public class TestEnvironment {
26:
27: public static final String TEST_RESULTS_DIR = "testResults";
28:
29: public static final String REASONER_FACTORY_CLASS = "com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory";
30: public static final String IRI_BASE = "http://krizik.felk.cvut.cz/ontologies/2013/jopa-tests/";
31:
32: public static final String EXPLICIT_DATATYPE = "http://www.w3.org/ns/csvw#uriTemplate";
33:
34: /**
35: * True if the ontology file should be deleted before access to it is initialized. This effectively means that the
36: * test will create the ontology from scratch. Default value is true.
37: */
38: public static final boolean DELETE_ONTOLOGY_FILE = true;
39:
40: private TestEnvironment() {
41: throw new AssertionError();
42: }
43:
44: public static EntityManager getPersistenceConnector(String name, StorageConfig storage,
45: boolean cache, Map<String, String> properties) {
46: return getPersistenceConnector(name, Collections.singletonList(storage), cache, properties)
47: .get(0);
48: }
49:
50: /**
51: * Creates persistence connector for the specified list of storages.
52: *
53: * @param baseName Base name used for ontology URI and physical storage path/URI
54: * @param storages List of storage configurations
55: * @param cache Whether second level cache should be enabled
56: * @param props Additional properties for the persistence provider
57: * @return Persistence context
58: */
59: public static List<EntityManager> getPersistenceConnector(String baseName,
60: List<StorageConfig> storages, boolean cache,
61: Map<String, String> props) {
62: final Map<String, String> params = initParams(cache);
63: // Can override default params
64: params.putAll(props);
65: int i = 1;
66: final List<EntityManager> managers = new ArrayList<>(storages.size());
67: for (StorageConfig si : storages) {
68: si.setName(baseName);
69: si.setDirectory(TEST_RESULTS_DIR);
70: final Map<String, String> config = si.createStorageConfiguration(i++);
71: config.putAll(params);
72: final EntityManager em = Persistence.createEntityManagerFactory("context-name_" + i, config)
73: .createEntityManager();
74: managers.add(em);
75: }
76: return managers;
77: }
78:
79: private static Map<String, String> initParams(boolean cache) {
80: final Map<String, String> params = new HashMap<>();
81: if (cache) {
82: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "true");
83: } else {
84: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "false");
85: }
86: /* Set location of the entities (package) */
87: params.put(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa.test");
88: params.put(JOPAPersistenceProperties.JPA_PERSISTENCE_PROVIDER, JOPAPersistenceProvider.class.getName());
89: return params;
90: }
91:
92: /**
93: * Removes (recursively) the specified file/directory.
94: * <p>
95: * The removal is executed only if the file exists and {@code deleteOntologyFile} is set to {@code true}.
96: *
97: * @param file The file/directory to remove
98: */
99: public static void removeOldTestFiles(File file) {
100: if (file.exists() && DELETE_ONTOLOGY_FILE) {
101: if (file.isDirectory() && file.listFiles() != null) {
102: for (File c : file.listFiles())
103: removeOldTestFiles(c);
104: assert file.delete();
105: } else {
106: if (!file.delete()) {
107: throw new RuntimeException("Unable to delete file " + file);
108: }
109: }
110: }
111: }
112: }