Skip to content

Package: QueryTestEnvironment

QueryTestEnvironment

nameinstructionbranchcomplexitylinemethod
generate()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
generateOwlClassAInstances(int)
M: 0 C: 55
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
generateOwlClassBInstances(int)
M: 0 C: 44
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
generateOwlClassDInstances(int, List)
M: 0 C: 41
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
generateOwlClassEInstances(int)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
generateOwlClassTInstances(int, List)
M: 0 C: 51
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
generateTestData(EntityManager)
M: 4 C: 15
79%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
generateTestData(EntityManager, Collection)
M: 102 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
getData()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getData(Class)
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getDataByContext(URI)
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDataByContext(URI, Class)
M: 8 C: 25
76%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 2 C: 7
78%
M: 0 C: 1
100%
lambda$0(EntityManager, URI, Map)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
persistIntoContext(EntityManager, Map, URI)
M: 10 C: 48
83%
M: 3 C: 5
63%
M: 2 C: 3
60%
M: 3 C: 11
79%
M: 0 C: 1
100%
static {...}
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

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.query;
16:
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.test.*;
20: import cz.cvut.kbss.jopa.test.environment.Generators;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.net.URI;
25: import java.util.*;
26: import java.util.Map.Entry;
27:
28: public final class QueryTestEnvironment {
29:
30: private static final Logger LOG = LoggerFactory.getLogger(QueryTestEnvironment.class);
31:
32: private static final String BASE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA_";
33: private static final String TYPE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA";
34: private static final String BASE_B = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB_";
35: private static final String BASE_D = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD_";
36:
37: private static final URI NULL_CONTEXT = URI.create("http://NullContext");
38:
39: private static Map<Class<?>, List<?>> data;
40: private static final Map<URI, Map<Class<?>, List<?>>> dataByContext = new HashMap<>();
41:
42: private QueryTestEnvironment() {
43: // Private constructor to prevent instantiation
44: }
45:
46: /**
47: * Generates and persists test data into the default context of the
48: * specified entity manager.
49: *
50: * @param em EntityManager
51: */
52: public static void generateTestData(EntityManager em) {
53:• assert em != null;
54: final Map<Class<?>, List<?>> map = generate();
55: LOG.debug("Persisting test data...");
56: persistIntoContext(em, map, null);
57: data = map;
58: }
59:
60: /**
61: * Generates and persists test data into the specified contexts.
62: * <p>
63: * This method distributes the data approximately uniformly into all the
64: * specified contexts.
65: *
66: * @param em EntityManager
67: * @param contexts A collection of target contexts
68: */
69: public static void generateTestData(EntityManager em, Collection<URI> contexts) {
70:• assert em != null;
71:• assert contexts != null && !contexts.isEmpty();
72: final Map<Class<?>, List<?>> map = generate();
73: LOG.debug("Persisting test data...");
74: final int contextCount = contexts.size();
75: final Map<URI, Map<Class<?>, List<?>>> contextMap = new HashMap<>();
76:• for (Entry<Class<?>, List<?>> e : map.entrySet()) {
77: final List<?> dataLst = e.getValue();
78: final int sublistSize = dataLst.size() / contextCount;
79: int sublistStart = 0;
80:• for (URI ctx : contexts) {
81:• if (!contextMap.containsKey(ctx)) {
82: contextMap.put(ctx, new HashMap<>());
83: }
84: final List<?> sublist = dataLst.subList(sublistStart, sublistStart + sublistSize);
85: contextMap.get(ctx).put(e.getKey(), sublist);
86: sublistStart += sublistSize;
87: }
88: }
89: contextMap.forEach((ctx, lst) -> persistIntoContext(em, lst, ctx));
90: data = map;
91: }
92:
93: private static void persistIntoContext(EntityManager em, Map<Class<?>, List<?>> data, URI context) {
94: final EntityDescriptor desc = new EntityDescriptor(context);
95: try {
96: em.getTransaction().begin();
97:• for (List<?> l : data.values()) {
98:• for (Object o : l) {
99: em.persist(o, desc);
100: }
101: }
102: em.getTransaction().commit();
103:• if (context == null) {
104: context = NULL_CONTEXT;
105: }
106: dataByContext.put(context, data);
107: } catch (RuntimeException e) {
108:• if (em.getTransaction().isActive()) {
109: em.getTransaction().rollback();
110: }
111: throw e;
112: }
113: }
114:
115: /**
116: * Get all current test data.
117: *
118: * @return Map of test data
119: */
120: public static Map<Class<?>, List<?>> getData() {
121: return data;
122: }
123:
124: /**
125: * Get a list of test instances of the specified class.
126: *
127: * @param cls The class
128: * @return List of test data of the specified class
129: */
130: @SuppressWarnings("unchecked")
131: public static <T> List<T> getData(Class<T> cls) {
132:• assert cls != null;
133: return (List<T>) data.get(cls);
134: }
135:
136: /**
137: * Gets all data from the specified context.
138: *
139: * @param context Context URI, null is permitted
140: * @return Map of all data or an empty map
141: */
142: public static Map<Class<?>, List<?>> getDataByContext(URI context) {
143:• if (context == null) {
144: context = NULL_CONTEXT;
145: }
146:• if (!dataByContext.containsKey(context)) {
147: return Collections.emptyMap();
148: }
149: return dataByContext.get(context);
150: }
151:
152: /**
153: * Gets data from the specified context and of the specified type
154: *
155: * @param context Context URI, null is permitted
156: * @param cls Data type
157: * @return List of data or an empty list
158: */
159: @SuppressWarnings("unchecked")
160: public static <T> List<T> getDataByContext(URI context, Class<T> cls) {
161:• assert cls != null;
162:• if (context == null) {
163: context = NULL_CONTEXT;
164: }
165:• if (!dataByContext.containsKey(context)) {
166: return Collections.emptyList();
167: }
168: final Map<Class<?>, List<?>> contextData = dataByContext.get(context);
169:• if (!contextData.containsKey(cls)) {
170: return Collections.emptyList();
171: }
172: return (List<T>) contextData.get(cls);
173: }
174:
175: private static Map<Class<?>, List<?>> generate() {
176: LOG.debug("Generating test data...");
177: final Map<Class<?>, List<?>> m = new HashMap<>();
178: final int count = 10;
179: final List<OWLClassA> aa = generateOwlClassAInstances(count);
180: m.put(OWLClassA.class, aa);
181: m.put(OWLClassB.class, generateOwlClassBInstances(count));
182: m.put(OWLClassD.class, generateOwlClassDInstances(count, aa));
183: m.put(OWLClassE.class, generateOwlClassEInstances(count));
184: m.put(OWLClassT.class, generateOwlClassTInstances(count, aa));
185: return m;
186: }
187:
188: private static List<OWLClassA> generateOwlClassAInstances(int count) {
189: final List<OWLClassA> lst = new ArrayList<>();
190: int randomNum = Generators.randomInt(1000);
191:• for (int i = 0; i < count; i++) {
192: final OWLClassA a = new OWLClassA();
193: a.setUri(URI.create(BASE_A + randomNum));
194: a.setStringAttribute("stringAttribute" + randomNum);
195: final Set<String> s = new HashSet<>();
196: s.add(TYPE_A);
197: a.setTypes(s);
198: lst.add(a);
199: randomNum++;
200: }
201: return lst;
202: }
203:
204: private static List<OWLClassB> generateOwlClassBInstances(int count) {
205: final List<OWLClassB> lst = new ArrayList<>();
206: int randomNum = Generators.randomInt(1000);
207:• for (int i = 0; i < count; i++) {
208: final OWLClassB b = new OWLClassB();
209: b.setUri(URI.create(BASE_B + randomNum));
210: b.setStringAttribute("strAtt" + randomNum);
211: lst.add(b);
212: randomNum++;
213: }
214: return lst;
215: }
216:
217: private static List<OWLClassD> generateOwlClassDInstances(int count, List<OWLClassA> aList) {
218: final List<OWLClassD> lst = new ArrayList<>();
219: int randomNum = Generators.randomInt(1000);
220:• for (int i = 0; i < count; i++) {
221: final OWLClassD d = new OWLClassD();
222: d.setUri(URI.create(BASE_D + randomNum));
223: d.setOwlClassA(aList.get(i));
224: lst.add(d);
225: randomNum++;
226: }
227: return lst;
228: }
229:
230: private static List<OWLClassE> generateOwlClassEInstances(int count) {
231: final List<OWLClassE> lst = new ArrayList<>();
232:• for (int i = 0; i < count; i++) {
233: final OWLClassE e = new OWLClassE();
234: // Auto-generated id
235: e.setStringAttribute("eStr" + i);
236: lst.add(e);
237: }
238: return lst;
239: }
240:
241: private static List<OWLClassT> generateOwlClassTInstances(int count, List<OWLClassA> aList) {
242: final List<OWLClassT> lst = new ArrayList<>();
243:• for (int i = 0; i < count; i++) {
244: final OWLClassT t = new OWLClassT();
245: t.setIntAttribute(i);
246: t.setName("tInstance " + i);
247: t.setDescription("Description of tInstance" + i);
248: t.setOwlClassA(aList.get(Generators.randomInt(aList.size())));
249: lst.add(t);
250: }
251: return lst;
252: }
253: }