/
Fixtures.java
285 lines (260 loc) · 11.6 KB
/
Fixtures.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package play.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.scanner.ScannerException;
import play.Logger;
import play.Play;
import play.PlayPlugin;
import play.classloading.ApplicationClasses;
import play.data.binding.Binder;
import play.data.binding.types.DateBinder;
import play.db.DB;
import play.db.DBPlugin;
import play.db.Model;
import play.exceptions.UnexpectedException;
import play.exceptions.YAMLException;
import play.vfs.VirtualFile;
public class Fixtures {
static Pattern keyPattern = Pattern.compile("([^(]+)\\(([^)]+)\\)");
public static void delete(Class<? extends Model>... types) {
disableForeignKeyConstraints();
for (Class<? extends Model> type : types) {
Model.Manager.factoryFor(type).deleteAll();
}
enableForeignKeyConstraints();
}
public static void delete(List<Class<? extends Model>> classes) {
@SuppressWarnings("unchecked")
Class<? extends Model>[] types = new Class[classes.size()];
for (int i = 0; i < types.length; i++) {
types[i] = classes.get(i);
}
delete(types);
}
@SuppressWarnings("unchecked")
public static void deleteAllModels() {
List<Class<? extends Model>> classes = new ArrayList<Class<? extends Model>>();
for (ApplicationClasses.ApplicationClass c : Play.classes.getAssignableClasses(Model.class)) {
classes.add((Class<? extends Model>)c.javaClass);
}
Fixtures.delete(classes);
}
private static void disableForeignKeyConstraints() {
if (DBPlugin.url.startsWith("jdbc:oracle:")) {
DB.execute("begin\n" +
"for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'\n" +
"and status = 'ENABLED') LOOP\n" +
"execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';\n" +
"end loop;\n" +
"end;");
return;
}
if (DBPlugin.url.startsWith("jdbc:hsqldb:")) {
DB.execute("SET REFERENTIAL_INTEGRITY FALSE");
return;
}
if (DBPlugin.url.startsWith("jdbc:mysql:")) {
DB.execute("SET foreign_key_checks = 0;");
return;
}
// Maybe Log a WARN for unsupported DB ?
Logger.warn("Fixtures : unable to disable constraints, unsupported database : " + DBPlugin.url);
}
private static void enableForeignKeyConstraints() {
if (DBPlugin.url.startsWith("jdbc:oracle:")) {
DB.execute("begin\n" +
"for i in (select constraint_name, table_name from user_constraints where constraint_type ='R'\n" +
"and status = 'DISABLED') LOOP\n" +
"execute immediate 'alter table '||i.table_name||' enable constraint '||i.constraint_name||'';\n" +
"end loop;\n" +
"end;");
return;
}
if (DBPlugin.url.startsWith("jdbc:hsqldb:")) {
DB.execute("SET REFERENTIAL_INTEGRITY TRUE");
return;
}
if (DBPlugin.url.startsWith("jdbc:mysql:")) {
DB.execute("SET foreign_key_checks = 1;");
return;
}
// Maybe Log a WARN for unsupported DB ?
Logger.warn("Fixtures : unable to enable constraints, unsupported database : " + DBPlugin.url);
}
static String getDeleteTableStmt(String name) {
if (DBPlugin.url.startsWith("jdbc:mysql:") ) {
return "TRUNCATE TABLE " + name;
} else if (DBPlugin.url.startsWith("jdbc:postgresql:")) {
return "TRUNCATE TABLE " + name + " cascade";
} else if (DBPlugin.url.startsWith("jdbc:oracle:")) {
return "TRUNCATE TABLE " + name;
}
return "DELETE FROM " + name;
}
public static void deleteAll() {
try {
List<String> names = new ArrayList<String>();
ResultSet rs = DB.getConnection().getMetaData().getTables(null, null, null, new String[]{"TABLE"});
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
names.add(name);
}
disableForeignKeyConstraints();
for (String name : names) {
Logger.trace("Dropping content of table %s", name);
DB.execute(getDeleteTableStmt(name) + ";");
}
enableForeignKeyConstraints();
for(PlayPlugin plugin : Play.plugins) {
plugin.afterFixtureLoad();
}
} catch (Exception e) {
throw new RuntimeException("Cannot delete all table data : " + e.getMessage(), e);
}
}
public static void load(String name) {
VirtualFile yamlFile = null;
try {
for (VirtualFile vf : Play.javaPath) {
yamlFile = vf.child(name);
if (yamlFile != null && yamlFile.exists()) {
break;
}
}
InputStream is = Play.classloader.getResourceAsStream(name);
if (is == null) {
throw new RuntimeException("Cannot load fixture " + name + ", the file was not found");
}
Yaml yaml = new Yaml();
Object o = yaml.load(is);
if (o instanceof LinkedHashMap<?, ?>) {
@SuppressWarnings("unchecked") LinkedHashMap<Object, Map<?, ?>> objects = (LinkedHashMap<Object, Map<?, ?>>) o;
Map<String, Object> idCache = new HashMap<String, Object>();
for (Object key : objects.keySet()) {
Matcher matcher = keyPattern.matcher(key.toString().trim());
if (matcher.matches()) {
String type = matcher.group(1);
String id = matcher.group(2);
if (!type.startsWith("models.")) {
type = "models." + type;
}
if (idCache.containsKey(type + "-" + id)) {
throw new RuntimeException("Cannot load fixture " + name + ", duplicate id '" + id + "' for type " + type);
}
Map<String, String[]> params = new HashMap<String, String[]>();
if (objects.get(key) == null) {
objects.put(key, new HashMap<Object, Object>());
}
serialize(objects.get(key), "object", params);
@SuppressWarnings("unchecked")
Class<Model> cType = (Class<Model>)Play.classloader.loadClass(type);
resolveDependencies(cType, params, idCache);
Model model = (Model)Binder.bind("object", cType, cType, null, params);
for(Field f : model.getClass().getFields()) {
// TODO: handle something like FileAttachment
if (f.getType().isAssignableFrom(Map.class)) {
f.set(model, objects.get(key).get(f.getName()));
}
}
model._save();
Class<?> tType = cType;
while (!tType.equals(Object.class)) {
idCache.put(tType.getName() + "-" + id, Model.Manager.factoryFor(cType).keyValue(model));
tType = tType.getSuperclass();
}
}
}
}
// Most persistence engine will need to clear their state
for(PlayPlugin plugin : Play.plugins) {
plugin.afterFixtureLoad();
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class " + e.getMessage() + " was not found", e);
} catch (ScannerException e) {
throw new YAMLException(e, yamlFile);
} catch (Throwable e) {
throw new RuntimeException("Cannot load fixture " + name + ": " + e.getMessage(), e);
}
}
static void serialize(Map<?, ?> values, String prefix, Map<String, String[]> serialized) {
for (Object key : values.keySet()) {
Object value = values.get(key);
if (value == null) {
continue;
}
if (value instanceof Map<?, ?>) {
serialize((Map<?, ?>) value, prefix + "." + key, serialized);
} else if (value instanceof Date) {
serialized.put(prefix + "." + key.toString(), new String[]{new SimpleDateFormat(DateBinder.ISO).format(((Date) value))});
} else if (value instanceof List<?>) {
List<?> l = (List<?>) value;
String[] r = new String[l.size()];
int i = 0;
for (Object el : l) {
r[i++] = el.toString();
}
serialized.put(prefix + "." + key.toString(), r);
} else if (value instanceof String && value.toString().matches("<<<\\s*\\{[^}]+}\\s*")) {
Matcher m = Pattern.compile("<<<\\s*\\{([^}]+)}\\s*").matcher(value.toString());
m.find();
String file = m.group(1);
VirtualFile f = Play.getVirtualFile(file);
if (f != null && f.exists()) {
serialized.put(prefix + "." + key.toString(), new String[]{f.contentAsString()});
}
} else {
serialized.put(prefix + "." + key.toString(), new String[]{value.toString()});
}
}
}
static void resolveDependencies(Class<Model> type, Map<String, String[]> serialized, Map<String, Object> idCache) {
Set<Field> fields = new HashSet<Field>();
Class<?> clazz = type;
String keyName = Model.Manager.factoryFor(type).keyName();
while (!clazz.equals(Object.class)) {
Collections.addAll(fields, clazz.getDeclaredFields());
clazz = clazz.getSuperclass();
}
for (Model.Property field : Model.Manager.factoryFor(type).listProperties()) {
if (field.isRelation) {
String[] ids = serialized.get("object." + field.name);
if (ids != null) {
for (int i = 0; i < ids.length; i++) {
String id = ids[i];
id = field.relationType.getName() + "-" + id;
if (!idCache.containsKey(id)) {
throw new RuntimeException("No previous reference found for object of type " + field.name + " with key " + ids[i]);
}
ids[i] = idCache.get(id).toString();
}
}
serialized.remove("object." + field.name);
serialized.put("object." + field.name + "." + keyName, ids);
}
}
}
public static void deleteDirectory(String path) {
try {
FileUtils.deleteDirectory(Play.getFile(path));
} catch (IOException ex) {
throw new UnexpectedException(ex);
}
}
}