Browse files

Implement variant of Fixtures.load() that optionally does not prefix …

…models. for entities.
  • Loading branch information...
1 parent 7d2697d commit 7efa5f04fc7a0c4192091d64bf1652cf8bf4f363 @optilude committed Jan 23, 2011
View
23 framework/src/play/test/Fixtures.java
@@ -163,7 +163,13 @@ public static void deleteAll() {
}
}
- public static void load(String name) {
+ /**
+ * Load entities from a yaml file. If implicitModelsPrefix is true,
+ * entities that do not start with models.* will have the models package
+ * name prefixed. Set to false for models not in the top level models.*
+ * package.
+ */
+ public static void load(String name, boolean implicitModelsPrefix) {
VirtualFile yamlFile = null;
try {
for (VirtualFile vf : Play.javaPath) {
@@ -185,8 +191,11 @@ public static void load(String name) {
if (matcher.matches()) {
String type = matcher.group(1);
String id = matcher.group(2);
- if (!type.startsWith("models.")) {
- type = "models." + type;
+
+ if(implicitModelsPrefix) {
+ 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);
@@ -228,6 +237,14 @@ public static void load(String name) {
throw new RuntimeException("Cannot load fixture " + name + ": " + e.getMessage(), e);
}
}
+
+ /**
+ * Default load - uses the implicit models.* package prefix
+ * for entities in the yaml file.
+ */
+ public static void load(String name) {
+ load(name, true);
+ }
public static void load(String... names) {
for (String name : names) {
View
15 samples-and-tests/just-test-cases/app/org/example/models/NotInModels.java
@@ -0,0 +1,15 @@
+package org.example.models;
+
+import play.*;
+import play.db.jpa.*;
+
+import javax.persistence.*;
+import java.util.*;
+
+@Entity
+public class NotInModels extends Model {
+
+ public String name;
+
+}
+
View
9 samples-and-tests/just-test-cases/test/FixturesTest.java
@@ -12,6 +12,8 @@
import models.Base;
import models.*;
+import org.example.models.NotInModels;
+
import org.junit.Before;
import org.junit.Test;
@@ -85,4 +87,11 @@ public void withGenericModel() {
assertFalse(parent.children.isEmpty());
}
+ @Test
+ public void loadDataNotInModels() {
+ Fixtures.load("not-in-models.yml", false);
+ assertEquals(1, NotInModels.findAll().size());
+ NotInModels nim = NotInModels.all().first();
+ assertEquals("Foo", nim.name);
+ }
}
View
6 samples-and-tests/just-test-cases/test/not-in-models.yml
@@ -0,0 +1,6 @@
+#==============================================================================
+# Test for loading an entity not in the models top level package with
+# Fixtures.load(<filename>, false)
+#==============================================================================
+org.example.models.NotInModels(1):
+ name: Foo

0 comments on commit 7efa5f0

Please sign in to comment.