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