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