Skip to content

Package: QueryTestEnvironment

QueryTestEnvironment

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