Package: QueryTestEnvironment
QueryTestEnvironment
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
generate() |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassAInstances() |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassBInstances() |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassDInstances(List) |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassEInstances() |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassJInstances(List) |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassMInstances() |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassTInstances(List) |
|
|
|
|
|
||||||||||||||||||||
generateOwlClassYInstances() |
|
|
|
|
|
||||||||||||||||||||
generateTestData(EntityManager) |
|
|
|
|
|
||||||||||||||||||||
generateTestData(EntityManager, Collection) |
|
|
|
|
|
||||||||||||||||||||
getData() |
|
|
|
|
|
||||||||||||||||||||
getData(Class) |
|
|
|
|
|
||||||||||||||||||||
getDataByContext(URI, Class) |
|
|
|
|
|
||||||||||||||||||||
lambda$generateTestData$0(EntityManager, URI, Map) |
|
|
|
|
|
||||||||||||||||||||
persistIntoContext(EntityManager, Map, URI) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
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.query;
19:
20: import cz.cvut.kbss.jopa.model.EntityManager;
21: import cz.cvut.kbss.jopa.model.MultilingualString;
22: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
23: import cz.cvut.kbss.jopa.test.*;
24: import cz.cvut.kbss.jopa.test.environment.Generators;
25: import cz.cvut.kbss.jopa.test.environment.TestEnvironment;
26: import org.slf4j.Logger;
27: import org.slf4j.LoggerFactory;
28:
29: import java.net.URI;
30: import java.util.*;
31: import java.util.Map.Entry;
32:
33: public final class QueryTestEnvironment {
34:
35: private static final Logger LOG = LoggerFactory.getLogger(QueryTestEnvironment.class);
36:
37: private static final int ITEM_COUNT = 10;
38:
39: private static final String BASE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA_";
40: public static final String COMMON_TYPE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA";
41: private static final String BASE_B = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB_";
42: private static final String BASE_D = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD_";
43:
44: private static final URI NULL_CONTEXT = URI.create("http://NullContext");
45:
46: private static Map<Class<?>, List<?>> data;
47: private static final Map<URI, Map<Class<?>, List<?>>> dataByContext = new HashMap<>();
48:
49: private QueryTestEnvironment() {
50: // Private constructor to prevent instantiation
51: }
52:
53: /**
54: * Generates and persists test data into the default context of the
55: * specified entity manager.
56: *
57: * @param em EntityManager
58: */
59: public static void generateTestData(EntityManager em) {
60:• assert em != null;
61: final Map<Class<?>, List<?>> map = generate();
62: LOG.debug("Persisting test data...");
63: persistIntoContext(em, map, null);
64: data = map;
65: }
66:
67: /**
68: * Generates and persists test data into the specified contexts.
69: * <p>
70: * This method distributes the data approximately uniformly into all the
71: * specified contexts.
72: *
73: * @param em EntityManager
74: * @param contexts A collection of target contexts
75: */
76: public static void generateTestData(EntityManager em, Collection<URI> contexts) {
77:• assert em != null;
78:• assert contexts != null && !contexts.isEmpty();
79: final Map<Class<?>, List<?>> map = generate();
80: LOG.debug("Persisting test data...");
81: final int contextCount = contexts.size();
82: final Map<URI, Map<Class<?>, List<?>>> contextMap = new HashMap<>();
83:• for (Entry<Class<?>, List<?>> e : map.entrySet()) {
84: final List<?> dataLst = e.getValue();
85: final int sublistSize = dataLst.size() / contextCount;
86: int sublistStart = 0;
87:• for (URI ctx : contexts) {
88:• if (!contextMap.containsKey(ctx)) {
89: contextMap.put(ctx, new HashMap<>());
90: }
91: final List<?> sublist = dataLst.subList(sublistStart, sublistStart + sublistSize);
92: contextMap.get(ctx).put(e.getKey(), sublist);
93: sublistStart += sublistSize;
94: }
95: }
96: contextMap.forEach((ctx, lst) -> persistIntoContext(em, lst, ctx));
97: data = map;
98: }
99:
100: private static void persistIntoContext(EntityManager em, Map<Class<?>, List<?>> data, URI context) {
101: final EntityDescriptor desc = new EntityDescriptor(context);
102: try {
103: em.getTransaction().begin();
104:• for (List<?> l : data.values()) {
105:• for (Object o : l) {
106: em.persist(o, desc);
107: }
108: }
109: em.getTransaction().commit();
110:• if (context == null) {
111: context = NULL_CONTEXT;
112: }
113: dataByContext.put(context, data);
114: } catch (RuntimeException e) {
115:• if (em.getTransaction().isActive()) {
116: em.getTransaction().rollback();
117: }
118: throw e;
119: }
120: }
121:
122: /**
123: * Get all current test data.
124: *
125: * @return Map of test data
126: */
127: public static Map<Class<?>, List<?>> getData() {
128: return data;
129: }
130:
131: /**
132: * Get a list of test instances of the specified class.
133: *
134: * @param cls The class
135: * @return List of test data of the specified class
136: */
137: @SuppressWarnings("unchecked")
138: public static <T> List<T> getData(Class<T> cls) {
139:• assert cls != null;
140: return (List<T>) data.get(cls);
141: }
142:
143: /**
144: * Gets data from the specified context and of the specified type
145: *
146: * @param context Context URI, null is permitted
147: * @param cls Data type
148: * @return List of data or an empty list
149: */
150: @SuppressWarnings("unchecked")
151: public static <T> List<T> getDataByContext(URI context, Class<T> cls) {
152:• assert cls != null;
153:• if (context == null) {
154: context = NULL_CONTEXT;
155: }
156:• if (!dataByContext.containsKey(context)) {
157: return Collections.emptyList();
158: }
159: final Map<Class<?>, List<?>> contextData = dataByContext.get(context);
160:• if (!contextData.containsKey(cls)) {
161: return Collections.emptyList();
162: }
163: return (List<T>) contextData.get(cls);
164: }
165:
166: private static Map<Class<?>, List<?>> generate() {
167: LOG.debug("Generating test data...");
168: final Map<Class<?>, List<?>> m = new HashMap<>();
169: final List<OWLClassA> aa = generateOwlClassAInstances();
170: m.put(OWLClassA.class, aa);
171: m.put(OWLClassB.class, generateOwlClassBInstances());
172: m.put(OWLClassD.class, generateOwlClassDInstances(aa));
173: m.put(OWLClassE.class, generateOwlClassEInstances());
174: m.put(OWLClassJ.class, generateOwlClassJInstances(aa));
175: m.put(OWLClassM.class, generateOwlClassMInstances());
176: m.put(OWLClassT.class, generateOwlClassTInstances(aa));
177: m.put(OWLClassY.class, generateOwlClassYInstances());
178: return m;
179: }
180:
181: private static List<OWLClassA> generateOwlClassAInstances() {
182: final List<OWLClassA> lst = new ArrayList<>(ITEM_COUNT);
183: final String typeOne = Generators.generateUri().toString();
184: final String typeTwo = Generators.generateUri().toString();
185: int randomNum = Generators.randomInt(1000);
186:• for (int i = 0; i < ITEM_COUNT; i++) {
187: final OWLClassA a = new OWLClassA();
188: a.setUri(URI.create(BASE_A + randomNum));
189: a.setStringAttribute("stringAttribute" + randomNum);
190: final Set<String> s = new HashSet<>();
191: s.add(COMMON_TYPE);
192:• s.add(Generators.randomBoolean() ? typeOne : typeTwo);
193: a.setTypes(s);
194: lst.add(a);
195: randomNum++;
196: }
197: return lst;
198: }
199:
200: private static List<OWLClassB> generateOwlClassBInstances() {
201: final List<OWLClassB> lst = new ArrayList<>(ITEM_COUNT);
202: int randomNum = Generators.randomInt(1000);
203:• for (int i = 0; i < ITEM_COUNT; i++) {
204: final OWLClassB b = new OWLClassB();
205: b.setUri(URI.create(BASE_B + randomNum));
206: b.setStringAttribute("strAtt" + randomNum);
207: lst.add(b);
208: randomNum++;
209: }
210: return lst;
211: }
212:
213: private static List<OWLClassD> generateOwlClassDInstances(List<OWLClassA> aList) {
214: final List<OWLClassD> lst = new ArrayList<>(ITEM_COUNT);
215: int randomNum = Generators.randomInt(1000);
216:• for (int i = 0; i < ITEM_COUNT; i++) {
217: final OWLClassD d = new OWLClassD();
218: d.setUri(URI.create(BASE_D + randomNum));
219: d.setOwlClassA(aList.get(i));
220: lst.add(d);
221: randomNum++;
222: }
223: return lst;
224: }
225:
226: private static List<OWLClassE> generateOwlClassEInstances() {
227: final List<OWLClassE> lst = new ArrayList<>(ITEM_COUNT);
228:• for (int i = 0; i < ITEM_COUNT; i++) {
229: final OWLClassE e = new OWLClassE();
230: // Auto-generated id
231: e.setStringAttribute("eStr" + i);
232: lst.add(e);
233: }
234: return lst;
235: }
236:
237: private static List<OWLClassT> generateOwlClassTInstances(List<OWLClassA> aList) {
238: final List<OWLClassT> lst = new ArrayList<>(ITEM_COUNT);
239:• for (int i = 0; i < ITEM_COUNT; i++) {
240: final OWLClassT t = new OWLClassT();
241: t.setIntAttribute(i);
242: t.setName("tInstance " + i);
243: t.setDescription("Description of tInstance" + i);
244: t.setOwlClassA(aList.get(Generators.randomInt(aList.size())));
245: lst.add(t);
246: }
247: return lst;
248: }
249:
250: private static List<OWLClassJ> generateOwlClassJInstances(List<OWLClassA> aList) {
251: final List<OWLClassJ> lst = new ArrayList<>(ITEM_COUNT);
252:• for (int i = 0; i < ITEM_COUNT; i++) {
253: final OWLClassJ inst = new OWLClassJ(Generators.generateUri());
254: inst.setOwlClassA(new HashSet<>());
255:• for (int j = 0; j < 3; j++) {
256: inst.getOwlClassA().add(Generators.getRandomItem(aList));
257: }
258: lst.add(inst);
259: }
260: return lst;
261: }
262:
263: private static List<OWLClassM> generateOwlClassMInstances() {
264: final List<OWLClassM> lst = new ArrayList<>();
265:• for (int i = 0; i < ITEM_COUNT; i++) {
266: final OWLClassM m = new OWLClassM();
267: m.setKey(Vocabulary.C_OWL_CLASS_M + Generators.randomPositiveInt(0, Integer.MAX_VALUE));
268: m.setBooleanAttribute(Generators.randomBoolean());
269: m.setDoubleAttribute(Generators.getRandomGenerator().nextDouble() * 100);
270: m.setFloatAttribute(Generators.getRandomGenerator().nextFloat() * 100);
271: m.setIntAttribute(Generators.randomInt());
272: m.setLongAttribute((long) Generators.randomInt());
273: m.setEnumAttribute(Generators.getRandomItem(Arrays.asList(OWLClassM.Severity.values())));
274: m.setEnumSimpleLiteral(m.getEnumAttribute());
275: m.setOrdinalEnumAttribute(m.getEnumAttribute());
276: lst.add(m);
277: }
278: return lst;
279: }
280:
281: private static List<OWLClassY> generateOwlClassYInstances() {
282: final List<OWLClassY> lst = new ArrayList<>();
283:• for (int i = 0; i < ITEM_COUNT; i++) {
284: final OWLClassY y = new OWLClassY();
285: y.setSingularString(MultilingualString.create("Test" + i, TestEnvironment.PERSISTENCE_LANGUAGE));
286:• switch (Generators.randomPositiveInt(0, 3)) {
287: case 0:
288: y.getSingularString().set("de", "Testwert nummer " + i);
289: break;
290: case 1:
291: y.getSingularString().set("cs", "Testovací hodnota číslo " + i);
292: break;
293: case 2:
294: y.getSingularString().set("fr", "nombre de valeurs de test " + i);
295: break;
296: }
297: lst.add(y);
298: }
299: return lst;
300: }
301: }