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