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