Skip to content

Method: initParams(boolean)

1: /**
2: * Copyright (C) 2023 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 PERSISTENCE_LANGUAGE = "en";
28:
29: public static final String TEST_RESULTS_DIR = "testResults";
30:
31: public static final String REASONER_FACTORY_CLASS = "com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory";
32: public static final String IRI_BASE = "http://krizik.felk.cvut.cz/ontologies/2013/jopa-tests/";
33:
34: public static final String EXPLICIT_DATATYPE = "http://www.w3.org/ns/csvw#uriTemplate";
35:
36: /**
37: * True if the ontology file should be deleted before access to it is initialized. This effectively means that the
38: * test will create the ontology from scratch. Default value is true.
39: */
40: public static final boolean DELETE_ONTOLOGY_FILE = true;
41:
42: private TestEnvironment() {
43: throw new AssertionError();
44: }
45:
46: public static EntityManager getPersistenceConnector(String name, StorageConfig storage,
47: boolean cache, Map<String, String> properties) {
48: return getPersistenceConnector(name, Collections.singletonList(storage), cache, properties)
49: .get(0);
50: }
51:
52: /**
53: * Creates persistence connector for the specified list of storages.
54: *
55: * @param baseName Base name used for ontology URI and physical storage path/URI
56: * @param storages List of storage configurations
57: * @param cache Whether second level cache should be enabled
58: * @param props Additional properties for the persistence provider
59: * @return Persistence context
60: */
61: public static List<EntityManager> getPersistenceConnector(String baseName,
62: List<StorageConfig> storages, boolean cache,
63: Map<String, String> props) {
64: final Map<String, String> params = initParams(cache);
65: // Can override default params
66: params.putAll(props);
67: int i = 1;
68: final List<EntityManager> managers = new ArrayList<>(storages.size());
69: for (StorageConfig si : storages) {
70: si.setName(baseName);
71: si.setDirectory(TEST_RESULTS_DIR);
72: final Map<String, String> config = si.createStorageConfiguration(i++);
73: config.putAll(params);
74: final EntityManager em = Persistence.createEntityManagerFactory("context-name_" + i, config)
75: .createEntityManager();
76: managers.add(em);
77: }
78: return managers;
79: }
80:
81: private static Map<String, String> initParams(boolean cache) {
82: final Map<String, String> params = new HashMap<>();
83:• if (cache) {
84: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "true");
85: } else {
86: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "false");
87: }
88: /* Set location of the entities (package) */
89: params.put(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa.test");
90: params.put(JOPAPersistenceProperties.JPA_PERSISTENCE_PROVIDER, JOPAPersistenceProvider.class.getName());
91: return params;
92: }
93:
94: /**
95: * Removes (recursively) the specified file/directory.
96: * <p>
97: * The removal is executed only if the file exists and {@code deleteOntologyFile} is set to {@code true}.
98: *
99: * @param file The file/directory to remove
100: */
101: public static void removeOldTestFiles(File file) {
102: if (file.exists() && DELETE_ONTOLOGY_FILE) {
103: if (file.isDirectory() && file.listFiles() != null) {
104: for (File c : file.listFiles())
105: removeOldTestFiles(c);
106: assert file.delete();
107: } else {
108: if (!file.delete()) {
109: throw new RuntimeException("Unable to delete file " + file);
110: }
111: }
112: }
113: }
114: }