Skip to content

Method: initParams(boolean)

1: /**
2: * Copyright (C) 2020 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: /**
33: * True if the ontology file should be deleted before access to it is initialized. This effectively means that the
34: * test will create the ontology from scratch. Default value is true.
35: */
36: public static final boolean DELETE_ONTOLOGY_FILE = true;
37:
38: private TestEnvironment() {
39: throw new AssertionError();
40: }
41:
42: /**
43: * Creates persistence connector, with enabled second level cache, for OWLAPI accessed ontology stored in a file.
44: *
45: * @param name Base name used for ontology URI and physical storage path/URI
46: * @return Persistence context
47: */
48: public static EntityManager getPersistenceConnector(String name) {
49: return getOwlapiPersistenceConnector(name, true);
50: }
51:
52: /**
53: * Creates persistence connector for OWLAPI accessed ontology stored in a file.
54: *
55: * @param name Base name used for ontology URI and physical storage path/URI
56: * @param cache Whether second level cache should be enabled
57: * @return Persistence context
58: */
59: public static EntityManager getPersistenceConnector(String name, boolean cache) {
60: return getOwlapiPersistenceConnector(name, cache);
61: }
62:
63: private static EntityManager getOwlapiPersistenceConnector(String name, boolean cache) {
64: return getPersistenceConnector(name,
65: Collections.singletonList(new OwlapiStorageConfig()), cache).get(0);
66: }
67:
68: public static EntityManager getPersistenceConnector(String name, StorageConfig storage,
69: boolean cache, Map<String, String> properties) {
70: return getPersistenceConnector(name, Collections.singletonList(storage), cache, properties)
71: .get(0);
72: }
73:
74: /**
75: * Creates persistence connector for the specified list of storages.
76: *
77: * @param baseName Base name used for ontology URI and physical storage path/URI
78: * @param storages List of storage configurations
79: * @param cache Whether second level cache should be enabled
80: * @return Persistence context
81: */
82: public static List<EntityManager> getPersistenceConnector(String baseName,
83: List<StorageConfig> storages, boolean cache) {
84: return getPersistenceConnector(baseName, storages, cache,
85: Collections.emptyMap());
86: }
87:
88: /**
89: * Creates persistence connector for the specified list of storages.
90: *
91: * @param baseName Base name used for ontology URI and physical storage path/URI
92: * @param storages List of storage configurations
93: * @param cache Whether second level cache should be enabled
94: * @param props Additional properties for the persistence provider
95: * @return Persistence context
96: */
97: public static List<EntityManager> getPersistenceConnector(String baseName,
98: List<StorageConfig> storages, boolean cache,
99: Map<String, String> props) {
100: final Map<String, String> params = initParams(cache);
101: // Can override default params
102: params.putAll(props);
103: int i = 1;
104: final List<EntityManager> managers = new ArrayList<>(storages.size());
105: for (StorageConfig si : storages) {
106: si.setName(baseName);
107: si.setDirectory(TEST_RESULTS_DIR);
108: final Map<String, String> config = si.createStorageConfiguration(i++);
109: config.putAll(params);
110: final EntityManager em = Persistence.createEntityManagerFactory("context-name_" + i, config)
111: .createEntityManager();
112: managers.add(em);
113: }
114: return managers;
115: }
116:
117: private static Map<String, String> initParams(boolean cache) {
118: final Map<String, String> params = new HashMap<>();
119:• if (cache) {
120: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "true");
121: } else {
122: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "false");
123: }
124: /* Set location of the entities (package) */
125: params.put(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa.test");
126: params.put(JOPAPersistenceProperties.JPA_PERSISTENCE_PROVIDER, JOPAPersistenceProvider.class.getName());
127: return params;
128: }
129:
130: /**
131: * Removes (recursively) the specified file/directory.
132: * <p>
133: * The removal is executed only if the file exists and {@code deleteOntologyFile} is set to {@code true}.
134: *
135: * @param file The file/directory to remove
136: */
137: public static void removeOldTestFiles(File file) {
138: if (file.exists() && DELETE_ONTOLOGY_FILE) {
139: if (file.isDirectory() && file.listFiles() != null) {
140: for (File c : file.listFiles())
141: removeOldTestFiles(c);
142: assert file.delete();
143: } else {
144: if (!file.delete()) {
145: throw new RuntimeException("Unable to delete file " + file);
146: }
147: }
148: }
149: }
150: }