Skip to content

Method: removeOldTestFiles(File)

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