Skip to content

Commit

Permalink
Lighthouse#2273: Stop Fixtures attempting to set static (Map) fields (#…
Browse files Browse the repository at this point in the history
…1281)

Lighthouse#2273: Stop Fixtures attempting to set static Map fields
  • Loading branch information
tazmaniax authored and asolntsev committed Dec 25, 2018
1 parent 8a06f34 commit bf22d43
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
13 changes: 8 additions & 5 deletions framework/src/play/test/Fixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions framework/test-src/models/ClassWithStaticFinalMap.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
114 changes: 114 additions & 0 deletions framework/test-src/play/test/FixturesTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

ClassWithStaticFinalMap(instanceA):
name: hello

0 comments on commit bf22d43

Please sign in to comment.