-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestEnvironmentUtils.java
183 lines (164 loc) · 6.77 KB
/
TestEnvironmentUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p/>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p/>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.environment.utils;
import cz.cvut.kbss.jopa.loaders.EntityLoader;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
import cz.cvut.kbss.jopa.sessions.change.ObjectChangeSetImpl;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Value;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
import static org.junit.Assert.assertEquals;
public final class TestEnvironmentUtils {
private static Random random;
private static Set<Class<?>> managedTypes;
private TestEnvironmentUtils() {
throw new AssertionError();
}
static {
random = new Random();
}
public static ObjectChangeSet createObjectChangeSet(Object original, Object clone, URI context) {
return new ObjectChangeSetImpl(original, clone, context);
}
public static int randomInt(int max) {
return random.nextInt(max);
}
/**
* Returns true if the specified assertions correspond to all properties in the {@code properties} parameter.
* <p/>
* Works for both string-based and typed properties.
*
* @param properties Expected properties
* @param assertions Actual assertions
* @return Equality status.
*/
public static boolean assertionsCorrespondToProperties(Map properties, Map<Assertion, Set<Value<?>>> assertions) {
if (properties.size() != assertions.size()) {
return false;
}
for (Object entry : properties.entrySet()) {
@SuppressWarnings("unchecked")
Entry<?, Set<?>> e = (Entry<?, Set<?>>) entry;
final Set<Value<?>> values = assertions
.get(Assertion.createPropertyAssertion(URI.create(e.getKey().toString()), false));
if (values == null) {
return false;
}
assertEquals(e.getValue().size(), values.size());
for (Value<?> val : values) {
if (!e.getValue().contains(val.getValue())) {
return false;
}
}
}
return true;
}
/**
* Sets value of field {@code field} on the {@code target} object to the specified {@code mock}. </p>
* <p/>
* This method also works for final fields. Note that in case static fields are set, it is the responsibility of the
* client to reset the field to the original value in test cleanup.
*
* @param target Target instance, can be {@code null} for static fields
* @param field Field to set
* @param mock The new value
*/
public static void setMock(Object target, Field field, Object mock) throws Exception {
assert field != null;
field.setAccessible(true);
removeFinalModifier(field);
field.set(target, mock);
}
private static void removeFinalModifier(Field field) throws Exception {
// remove final modifier from field
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
public static Set<String> generateTypes(int count) {
final Set<String> types = new HashSet<>(count);
for (int i = 0; i < count; i++) {
types.add("http://krizik.felk.cvut.cz/ontologies/jopa#type_" + i);
}
return types;
}
public static Map<String, Set<String>> generateStringProperties(int propCount, int valCount) {
int valueCounter = 0;
final Map<String, Set<String>> properties = new HashMap<>(propCount);
for (int i = 0; i < propCount; i++) {
final Set<String> values = new HashSet<>(valCount);
properties.put("http://krizik.felk.cvut.cz/ontologies/jopa#property_" + i, values);
boolean objectProperty = i % 2 != 0;
for (int j = 0; j < valCount; j++) {
if (objectProperty) {
values.add("http://krizik.felk.cvut.cz/ontologies/jopa#value_" + valueCounter++);
} else {
values.add(j % 2 != 0 ? "value" + valueCounter++ : Integer.toString(valueCounter++));
}
}
}
return properties;
}
public static Map<URI, Set<Object>> generateTypedProperties(int propCount, int valCount) {
int valueCounter = 0;
final Map<URI, Set<Object>> properties = new HashMap<>(propCount);
for (int i = 0; i < propCount; i++) {
final Set<Object> values = new HashSet<>(valCount);
properties.put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#property_" + i), values);
boolean objectProperty = i % 2 != 0;
for (int j = 0; j < valCount; j++) {
if (objectProperty) {
values.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value_" + valueCounter++));
} else {
values.add(generateDataPropertyValue(j));
}
}
}
return properties;
}
private static Object generateDataPropertyValue(int counter) {
final int index = counter % 5;
switch (index) {
case 0:
return "StringValue_" + counter;
case 1:
return true;
case 2:
return counter;
case 3:
return (double) counter;
case 4:
return new Date();
default:
return null;
}
}
public static Set<Class<?>> getManagedTypes() {
if (managedTypes == null) {
initManagedTypes();
}
return Collections.unmodifiableSet(managedTypes);
}
private static void initManagedTypes() {
managedTypes = new EntityLoader().discoverEntityClasses(new Configuration(
Collections.singletonMap(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa")));
}
}