diff --git a/framework/src/play/test/Fixtures.java b/framework/src/play/test/Fixtures.java index 9334792da1..e115c4b9b2 100644 --- a/framework/src/play/test/Fixtures.java +++ b/framework/src/play/test/Fixtures.java @@ -5,6 +5,7 @@ import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -265,11 +266,13 @@ public static void loadModels(boolean loadAsTemplate, String name) { Model model = (Model) Binder.bind(rootParamNode, "object", cType, cType, annotations); for (Field f : model.getClass().getFields()) { - if (f.getType().isAssignableFrom(Map.class)) { - f.set(model, objects.get(key).get(f.getName())); - } - if (f.getType().equals(byte[].class)) { - f.set(model, objects.get(key).get(f.getName())); + if (!Modifier.isStatic(f.getModifiers())) { + if (f.getType().isAssignableFrom(Map.class)) { + f.set(model, objects.get(key).get(f.getName())); + } + if (f.getType().equals(byte[].class)) { + f.set(model, objects.get(key).get(f.getName())); + } } } model._save(); diff --git a/framework/test-src/models/ClassWithStaticFinalMap.java b/framework/test-src/models/ClassWithStaticFinalMap.java new file mode 100644 index 0000000000..f6c0e71b92 --- /dev/null +++ b/framework/test-src/models/ClassWithStaticFinalMap.java @@ -0,0 +1,20 @@ +package models; + +/* + * Copy below to play/test/ClassWithStaticFinalMap.java.xml + */ + +import java.util.HashMap; +import java.util.Map; + +import play.test.FixturesTest.MockModel; + +public class ClassWithStaticFinalMap extends MockModel { + public static final Map<String, Object> map = new HashMap<>(); + + public String name; + + public ClassWithStaticFinalMap() { + super(); + } +} diff --git a/framework/test-src/play/test/FixturesTest.java b/framework/test-src/play/test/FixturesTest.java new file mode 100755 index 0000000000..a940638228 --- /dev/null +++ b/framework/test-src/play/test/FixturesTest.java @@ -0,0 +1,114 @@ +package play.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.net.URL; +import java.nio.file.Paths; +import java.util.LinkedList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import models.ClassWithStaticFinalMap; +import play.Play; +import play.PlayBuilder; +import play.db.Model; +import play.db.Model.Property; +import play.plugins.PluginCollection; +import play.vfs.VirtualFile; + + +public class FixturesTest { + + public static List<MockModel> store; + + public static class MockModel implements Model { + public Integer id; + + @Override + public void _save() { this.id = store.size(); store.add(this); } + @Override + public void _delete() { store.remove(this); } + @Override + public Object _key() { return Model.Manager.factoryFor(this.getClass()).keyValue(this); } + } + + public static Model.Factory mockModelFactory = new Model.Factory() { + @Override + public String keyName() { return null; } + @Override + public Class<?> keyType() { return null; } + @Override + public Object keyValue(play.db.Model m) { return ((MockModel)m).id; } + @Override + public play.db.Model findById(Object id) { return null; } + @Override + public List<play.db.Model> fetch(int offset, int length, String orderBy, String orderDirection, + List<String> properties, String keywords, String where) { return null; } + @Override + public Long count(List<String> properties, String keywords, String where) { return null; } + @Override + public void deleteAll() { /* Do nothing */ } + @Override + public List<Property> listProperties() { return null; } + }; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + new PlayBuilder().build(); + + String className = FixturesTest.class.getSimpleName() + ".class"; + URL url = FixturesTest.class.getResource(className); + File file = Paths.get(url.toURI()).toFile().getParentFile(); + + Play.applicationPath = file; + VirtualFile appRoot = VirtualFile.open(file); + + Play.pluginCollection = new PluginCollection() { + public Model.Factory modelFactory(Class<? extends play.db.Model> modelClass) { + return MockModel.class.isAssignableFrom(modelClass) ? mockModelFactory : null; + } + }; + + Play.roots.clear(); + Play.roots.add(appRoot); + + Play.javaPath.clear(); + Play.javaPath.add(appRoot); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + // Initialise the model store. + store = new LinkedList<>(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testModelClassStaticFinalMapField() { + // Fixtures should not attempt to set a static final field + // on a Model object otherwise an exception would occur. + Fixtures.loadModels(false, "testModelClassStaticFinalMapField.yml"); + + // Ensure the model was loaded correctly. + assertEquals(store.size(), 1); + MockModel model = store.get(0); + assertNotNull(model); + assertTrue(model instanceof ClassWithStaticFinalMap); + assertEquals(((ClassWithStaticFinalMap)model).name, "hello"); + } +} diff --git a/framework/test-src/play/test/testModelClassStaticFinalMapField.yml b/framework/test-src/play/test/testModelClassStaticFinalMapField.yml new file mode 100644 index 0000000000..5eeb048a35 --- /dev/null +++ b/framework/test-src/play/test/testModelClassStaticFinalMapField.yml @@ -0,0 +1,3 @@ + +ClassWithStaticFinalMap(instanceA): + name: hello