From 6732a3a408b96766f9a37553e494f900bcd2c537 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Thu, 15 Nov 2018 12:53:03 +1100 Subject: [PATCH 1/9] Lighthouse#2273: Stop Fixtures attempting to set static Map fields --- framework/src/play/test/Fixtures.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/framework/src/play/test/Fixtures.java b/framework/src/play/test/Fixtures.java index 9334792da1..c333799331 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,12 +266,14 @@ 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(); From 05206b2c29c740246b46f15c972cb2b27c29a633 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Thu, 15 Nov 2018 12:58:18 +1100 Subject: [PATCH 2/9] lighthouse#2273: Fix code formatting --- framework/src/play/test/Fixtures.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/play/test/Fixtures.java b/framework/src/play/test/Fixtures.java index c333799331..e115c4b9b2 100644 --- a/framework/src/play/test/Fixtures.java +++ b/framework/src/play/test/Fixtures.java @@ -266,14 +266,14 @@ 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 (!Modifier.isStatic(f.getModifiers())) { + 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(); From 2e9e66ac670056583452757d2518e150d5a43d25 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Tue, 20 Nov 2018 17:34:04 +1100 Subject: [PATCH 3/9] Add initial attempt at unit test --- .../test-src/play/test/FixturesTest.java | 59 +++++++++++++++++++ framework/test-src/play/test/FixturesTest.yml | 3 + .../play/test/models/ClassWithStaticMap.java | 14 +++++ 3 files changed, 76 insertions(+) create mode 100755 framework/test-src/play/test/FixturesTest.java create mode 100644 framework/test-src/play/test/FixturesTest.yml create mode 100644 framework/test-src/play/test/models/ClassWithStaticMap.java diff --git a/framework/test-src/play/test/FixturesTest.java b/framework/test-src/play/test/FixturesTest.java new file mode 100755 index 0000000000..70a7c689d0 --- /dev/null +++ b/framework/test-src/play/test/FixturesTest.java @@ -0,0 +1,59 @@ +package play.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.net.URL; +import java.nio.file.Paths; + +import static org.fest.assertions.Assertions.assertThat; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import play.Play; +import play.PlayBuilder; +import play.vfs.VirtualFile; + + +public class FixturesTest { + + @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.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 { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testStaticMap() { + Fixtures.loadModels(false, "FixturesTest.yml"); + } +} diff --git a/framework/test-src/play/test/FixturesTest.yml b/framework/test-src/play/test/FixturesTest.yml new file mode 100644 index 0000000000..179bdb8bd3 --- /dev/null +++ b/framework/test-src/play/test/FixturesTest.yml @@ -0,0 +1,3 @@ + +ClassWithStaticMap(instanceA): + name: hello diff --git a/framework/test-src/play/test/models/ClassWithStaticMap.java b/framework/test-src/play/test/models/ClassWithStaticMap.java new file mode 100644 index 0000000000..02dbc2e8bd --- /dev/null +++ b/framework/test-src/play/test/models/ClassWithStaticMap.java @@ -0,0 +1,14 @@ +package play.test.models; + +import java.util.HashMap; +import java.util.Map; + +public class ClassWithStaticMap { + public static final Map map = new HashMap<>(); + + public String name; + + public ClassWithStaticMap() { + super(); + } +} From ea765888027356b0f3a418799f5e13e75742be73 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Thu, 22 Nov 2018 23:56:08 +1100 Subject: [PATCH 4/9] lighthouse#2273: Added unit test --- .../test/ClassWithStaticFinalMap.java.src | 20 ++++++ .../test-src/play/test/FixturesTest.java | 63 ++++++++++++++++--- framework/test-src/play/test/FixturesTest.yml | 3 - .../test/models/ClassWithStaticFinalMap.java | 20 ++++++ .../play/test/models/ClassWithStaticMap.java | 14 ----- .../testModelClassStaticFinalMapField.yml | 3 + 6 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 framework/test-src/play/test/ClassWithStaticFinalMap.java.src delete mode 100644 framework/test-src/play/test/FixturesTest.yml create mode 100644 framework/test-src/play/test/models/ClassWithStaticFinalMap.java delete mode 100644 framework/test-src/play/test/models/ClassWithStaticMap.java create mode 100644 framework/test-src/play/test/testModelClassStaticFinalMapField.yml diff --git a/framework/test-src/play/test/ClassWithStaticFinalMap.java.src b/framework/test-src/play/test/ClassWithStaticFinalMap.java.src new file mode 100644 index 0000000000..4dadca4b27 --- /dev/null +++ b/framework/test-src/play/test/ClassWithStaticFinalMap.java.src @@ -0,0 +1,20 @@ +package models; + +/* + * Import below from play/test/models/ClassWithStaticFinalMap.java + */ + +import java.util.HashMap; +import java.util.Map; + +import play.test.FixturesTest.MockModel; + +public class ClassWithStaticFinalMap extends MockModel { + public static final Map 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 index 70a7c689d0..07a49fbb63 100755 --- a/framework/test-src/play/test/FixturesTest.java +++ b/framework/test-src/play/test/FixturesTest.java @@ -1,12 +1,9 @@ package play.test; -import static org.junit.Assert.*; - import java.io.File; import java.net.URL; import java.nio.file.Paths; - -import static org.fest.assertions.Assertions.assertThat; +import java.util.List; import org.junit.After; import org.junit.AfterClass; @@ -16,10 +13,47 @@ import play.Play; import play.PlayBuilder; +import play.classloading.ApplicationClasses; +import play.classloading.ApplicationClasses.ApplicationClass; +import play.db.Model; +import play.db.Model.Property; +import play.plugins.PluginCollection; import play.vfs.VirtualFile; public class FixturesTest { + + public static class MockModel implements Model { + @Override + public void _save() { /* Do nothing */ } + @Override + public void _delete() { /* Do nothing */ } + @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 Long.valueOf(1); } + @Override + public play.db.Model findById(Object id) { return null; } + @Override + public List fetch(int offset, int length, String orderBy, String orderDirection, + List properties, String keywords, String where) { return null; } + @Override + public Long count(List properties, String keywords, String where) { return null; } + @Override + public void deleteAll() { /* Do nothing */ } + @Override + public List listProperties() { return null; } + }; + + private static final String CLASS_WITH_STATIC_FINAL_MAP_NAME = "models.ClassWithStaticFinalMap"; + @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -30,9 +64,22 @@ public static void setUpBeforeClass() throws Exception { File file = Paths.get(url.toURI()).toFile().getParentFile(); Play.applicationPath = file; - VirtualFile appRoot = VirtualFile.open(file); + ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.src")); + + Play.classes = new ApplicationClasses() { + public ApplicationClass getApplicationClass(String name) { + return CLASS_WITH_STATIC_FINAL_MAP_NAME.equals(name) ? testClass : super.getApplicationClass(name); + } + }; + + Play.pluginCollection = new PluginCollection() { + public Model.Factory modelFactory(Class modelClass) { + return MockModel.class.isAssignableFrom(modelClass) ? mockModelFactory : null; + } + }; + Play.roots.clear(); Play.roots.add(appRoot); @@ -53,7 +100,9 @@ public void tearDown() throws Exception { } @Test - public void testStaticMap() { - Fixtures.loadModels(false, "FixturesTest.yml"); + 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"); } } diff --git a/framework/test-src/play/test/FixturesTest.yml b/framework/test-src/play/test/FixturesTest.yml deleted file mode 100644 index 179bdb8bd3..0000000000 --- a/framework/test-src/play/test/FixturesTest.yml +++ /dev/null @@ -1,3 +0,0 @@ - -ClassWithStaticMap(instanceA): - name: hello diff --git a/framework/test-src/play/test/models/ClassWithStaticFinalMap.java b/framework/test-src/play/test/models/ClassWithStaticFinalMap.java new file mode 100644 index 0000000000..7b57d6308c --- /dev/null +++ b/framework/test-src/play/test/models/ClassWithStaticFinalMap.java @@ -0,0 +1,20 @@ +package play.test.models; + +/* + * Copy below to play/test/ClassWithStaticFinalMap.java.src + */ + +import java.util.HashMap; +import java.util.Map; + +import play.test.FixturesTest.MockModel; + +public class ClassWithStaticFinalMap extends MockModel { + public static final Map map = new HashMap<>(); + + public String name; + + public ClassWithStaticFinalMap() { + super(); + } +} diff --git a/framework/test-src/play/test/models/ClassWithStaticMap.java b/framework/test-src/play/test/models/ClassWithStaticMap.java deleted file mode 100644 index 02dbc2e8bd..0000000000 --- a/framework/test-src/play/test/models/ClassWithStaticMap.java +++ /dev/null @@ -1,14 +0,0 @@ -package play.test.models; - -import java.util.HashMap; -import java.util.Map; - -public class ClassWithStaticMap { - public static final Map map = new HashMap<>(); - - public String name; - - public ClassWithStaticMap() { - super(); - } -} 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 From db0cf65a72d844b1987ffb7cd76e65176719d375 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 23 Nov 2018 00:13:53 +1100 Subject: [PATCH 5/9] lighthouse#2273: Renamed java src file to ensure it's copied to classes --- ...StaticFinalMap.java.src => ClassWithStaticFinalMap.java.xml} | 0 framework/test-src/play/test/FixturesTest.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename framework/test-src/play/test/{ClassWithStaticFinalMap.java.src => ClassWithStaticFinalMap.java.xml} (100%) diff --git a/framework/test-src/play/test/ClassWithStaticFinalMap.java.src b/framework/test-src/play/test/ClassWithStaticFinalMap.java.xml similarity index 100% rename from framework/test-src/play/test/ClassWithStaticFinalMap.java.src rename to framework/test-src/play/test/ClassWithStaticFinalMap.java.xml diff --git a/framework/test-src/play/test/FixturesTest.java b/framework/test-src/play/test/FixturesTest.java index 07a49fbb63..3b8a935d3d 100755 --- a/framework/test-src/play/test/FixturesTest.java +++ b/framework/test-src/play/test/FixturesTest.java @@ -66,7 +66,7 @@ public static void setUpBeforeClass() throws Exception { Play.applicationPath = file; VirtualFile appRoot = VirtualFile.open(file); - ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.src")); + ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.xml")); Play.classes = new ApplicationClasses() { public ApplicationClass getApplicationClass(String name) { From 8f6be661e49fe8e825029c4e80b2a543f1af55c0 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 23 Nov 2018 00:23:05 +1100 Subject: [PATCH 6/9] lighthouse#2273: Fix source formatting --- .../test-src/play/test/FixturesTest.java | 127 +++++++++--------- 1 file changed, 63 insertions(+), 64 deletions(-) diff --git a/framework/test-src/play/test/FixturesTest.java b/framework/test-src/play/test/FixturesTest.java index 3b8a935d3d..9e87e4e353 100755 --- a/framework/test-src/play/test/FixturesTest.java +++ b/framework/test-src/play/test/FixturesTest.java @@ -22,69 +22,68 @@ public class FixturesTest { - - public static class MockModel implements Model { - @Override - public void _save() { /* Do nothing */ } - @Override - public void _delete() { /* Do nothing */ } - @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 Long.valueOf(1); } - @Override - public play.db.Model findById(Object id) { return null; } - @Override - public List fetch(int offset, int length, String orderBy, String orderDirection, - List properties, String keywords, String where) { return null; } - @Override - public Long count(List properties, String keywords, String where) { return null; } - @Override - public void deleteAll() { /* Do nothing */ } - @Override - public List listProperties() { return null; } - }; - - private static final String CLASS_WITH_STATIC_FINAL_MAP_NAME = "models.ClassWithStaticFinalMap"; - - + + public static class MockModel implements Model { + @Override + public void _save() { /* Do nothing */ } + @Override + public void _delete() { /* Do nothing */ } + @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 Long.valueOf(1); } + @Override + public play.db.Model findById(Object id) { return null; } + @Override + public List fetch(int offset, int length, String orderBy, String orderDirection, + List properties, String keywords, String where) { return null; } + @Override + public Long count(List properties, String keywords, String where) { return null; } + @Override + public void deleteAll() { /* Do nothing */ } + @Override + public List listProperties() { return null; } + }; + + private static final String CLASS_WITH_STATIC_FINAL_MAP_NAME = "models.ClassWithStaticFinalMap"; + @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); - - ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.xml")); - - Play.classes = new ApplicationClasses() { - public ApplicationClass getApplicationClass(String name) { - return CLASS_WITH_STATIC_FINAL_MAP_NAME.equals(name) ? testClass : super.getApplicationClass(name); - } - }; - - Play.pluginCollection = new PluginCollection() { - public Model.Factory modelFactory(Class modelClass) { - return MockModel.class.isAssignableFrom(modelClass) ? mockModelFactory : null; - } - }; - - Play.roots.clear(); - Play.roots.add(appRoot); - - Play.javaPath.clear(); - Play.javaPath.add(appRoot); + 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); + + ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.xml")); + + Play.classes = new ApplicationClasses() { + public ApplicationClass getApplicationClass(String name) { + return CLASS_WITH_STATIC_FINAL_MAP_NAME.equals(name) ? testClass : super.getApplicationClass(name); + } + }; + + Play.pluginCollection = new PluginCollection() { + public Model.Factory modelFactory(Class modelClass) { + return MockModel.class.isAssignableFrom(modelClass) ? mockModelFactory : null; + } + }; + + Play.roots.clear(); + Play.roots.add(appRoot); + + Play.javaPath.clear(); + Play.javaPath.add(appRoot); } @AfterClass @@ -101,8 +100,8 @@ 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"); + // Fixtures should not attempt to set a static final field + // on a Model object otherwise an exception would occur. + Fixtures.loadModels(false, "testModelClassStaticFinalMapField.yml"); } } From 818a3d40357fa8040729683086d591b0430ee0d8 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 23 Nov 2018 00:27:40 +1100 Subject: [PATCH 7/9] lighthouse#2273: Fixed source file reference --- .../test-src/play/test/models/ClassWithStaticFinalMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/test-src/play/test/models/ClassWithStaticFinalMap.java b/framework/test-src/play/test/models/ClassWithStaticFinalMap.java index 7b57d6308c..40ea072531 100644 --- a/framework/test-src/play/test/models/ClassWithStaticFinalMap.java +++ b/framework/test-src/play/test/models/ClassWithStaticFinalMap.java @@ -1,7 +1,7 @@ package play.test.models; /* - * Copy below to play/test/ClassWithStaticFinalMap.java.src + * Copy below to play/test/ClassWithStaticFinalMap.java.xml */ import java.util.HashMap; From 0e40208ac18b240222fe3d9f88d6b5310fa73873 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 23 Nov 2018 08:30:06 +1100 Subject: [PATCH 8/9] lighthouse#2273: Moved Model test class to models package under test-src --- .../models/ClassWithStaticFinalMap.java | 2 +- .../test/ClassWithStaticFinalMap.java.xml | 20 ------------------- .../test-src/play/test/FixturesTest.java | 12 ----------- 3 files changed, 1 insertion(+), 33 deletions(-) rename framework/test-src/{play/test => }/models/ClassWithStaticFinalMap.java (93%) delete mode 100644 framework/test-src/play/test/ClassWithStaticFinalMap.java.xml diff --git a/framework/test-src/play/test/models/ClassWithStaticFinalMap.java b/framework/test-src/models/ClassWithStaticFinalMap.java similarity index 93% rename from framework/test-src/play/test/models/ClassWithStaticFinalMap.java rename to framework/test-src/models/ClassWithStaticFinalMap.java index 40ea072531..f6c0e71b92 100644 --- a/framework/test-src/play/test/models/ClassWithStaticFinalMap.java +++ b/framework/test-src/models/ClassWithStaticFinalMap.java @@ -1,4 +1,4 @@ -package play.test.models; +package models; /* * Copy below to play/test/ClassWithStaticFinalMap.java.xml diff --git a/framework/test-src/play/test/ClassWithStaticFinalMap.java.xml b/framework/test-src/play/test/ClassWithStaticFinalMap.java.xml deleted file mode 100644 index 4dadca4b27..0000000000 --- a/framework/test-src/play/test/ClassWithStaticFinalMap.java.xml +++ /dev/null @@ -1,20 +0,0 @@ -package models; - -/* - * Import below from play/test/models/ClassWithStaticFinalMap.java - */ - -import java.util.HashMap; -import java.util.Map; - -import play.test.FixturesTest.MockModel; - -public class ClassWithStaticFinalMap extends MockModel { - public static final Map 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 index 9e87e4e353..af877d2071 100755 --- a/framework/test-src/play/test/FixturesTest.java +++ b/framework/test-src/play/test/FixturesTest.java @@ -13,8 +13,6 @@ import play.Play; import play.PlayBuilder; -import play.classloading.ApplicationClasses; -import play.classloading.ApplicationClasses.ApplicationClass; import play.db.Model; import play.db.Model.Property; import play.plugins.PluginCollection; @@ -52,8 +50,6 @@ public void deleteAll() { /* Do nothing */ } public List listProperties() { return null; } }; - private static final String CLASS_WITH_STATIC_FINAL_MAP_NAME = "models.ClassWithStaticFinalMap"; - @BeforeClass public static void setUpBeforeClass() throws Exception { new PlayBuilder().build(); @@ -65,14 +61,6 @@ public static void setUpBeforeClass() throws Exception { Play.applicationPath = file; VirtualFile appRoot = VirtualFile.open(file); - ApplicationClass testClass = new ApplicationClass(CLASS_WITH_STATIC_FINAL_MAP_NAME, appRoot.child("ClassWithStaticFinalMap.java.xml")); - - Play.classes = new ApplicationClasses() { - public ApplicationClass getApplicationClass(String name) { - return CLASS_WITH_STATIC_FINAL_MAP_NAME.equals(name) ? testClass : super.getApplicationClass(name); - } - }; - Play.pluginCollection = new PluginCollection() { public Model.Factory modelFactory(Class modelClass) { return MockModel.class.isAssignableFrom(modelClass) ? mockModelFactory : null; From 1cb225df314edac3a0444e853bc498012f716325 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 23 Nov 2018 10:40:04 +1100 Subject: [PATCH 9/9] lighthouse#2273: Added asserts that the model was loaded correctly --- .../test-src/play/test/FixturesTest.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/framework/test-src/play/test/FixturesTest.java b/framework/test-src/play/test/FixturesTest.java index af877d2071..a940638228 100755 --- a/framework/test-src/play/test/FixturesTest.java +++ b/framework/test-src/play/test/FixturesTest.java @@ -1,8 +1,13 @@ 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; @@ -11,6 +16,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import models.ClassWithStaticFinalMap; import play.Play; import play.PlayBuilder; import play.db.Model; @@ -20,12 +26,16 @@ public class FixturesTest { + + public static List store; public static class MockModel implements Model { + public Integer id; + @Override - public void _save() { /* Do nothing */ } + public void _save() { this.id = store.size(); store.add(this); } @Override - public void _delete() { /* Do nothing */ } + public void _delete() { store.remove(this); } @Override public Object _key() { return Model.Manager.factoryFor(this.getClass()).keyValue(this); } } @@ -36,7 +46,7 @@ public void _delete() { /* Do nothing */ } @Override public Class keyType() { return null; } @Override - public Object keyValue(play.db.Model m) { return Long.valueOf(1); } + public Object keyValue(play.db.Model m) { return ((MockModel)m).id; } @Override public play.db.Model findById(Object id) { return null; } @Override @@ -80,6 +90,8 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { + // Initialise the model store. + store = new LinkedList<>(); } @After @@ -91,5 +103,12 @@ 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"); } }