NIHVIVO-3729 Modify ProptyListConfig so it doesn't rely on the FreemarkerConfiguration being stored as a request attribute, but so it can still be stubbed out for unit tests.
This commit is contained in:
parent
e3d2a8fb47
commit
68f88fe257
3 changed files with 42 additions and 13 deletions
|
@ -40,6 +40,7 @@ public class PropertyListConfig {
|
|||
// TODO Lump these together into the PropertyListConfigContext
|
||||
private final ObjectPropertyTemplateModel optm;
|
||||
private final VitroRequest vreq;
|
||||
private final TemplateLoader templateLoader;
|
||||
|
||||
private boolean isDefaultConfig;
|
||||
private Set<String> constructQueries;
|
||||
|
@ -47,12 +48,14 @@ public class PropertyListConfig {
|
|||
private String templateName;
|
||||
private ObjectPropertyDataPostProcessor postprocessor; // never null
|
||||
|
||||
public PropertyListConfig(ObjectPropertyTemplateModel optm, VitroRequest vreq, ObjectProperty op, boolean editing)
|
||||
public PropertyListConfig(ObjectPropertyTemplateModel optm, TemplateLoader templateLoader, VitroRequest vreq,
|
||||
ObjectProperty op, boolean editing)
|
||||
throws InvalidConfigurationException {
|
||||
|
||||
this.optm = optm;
|
||||
this.vreq = vreq;
|
||||
WebappDaoFactory wadf = vreq.getWebappDaoFactory();
|
||||
this.templateLoader = templateLoader;
|
||||
|
||||
// Get the custom config filename
|
||||
String configFileName = wadf.getObjectPropertyDao().getCustomListViewConfigFileName(op);
|
||||
|
@ -117,10 +120,8 @@ public class PropertyListConfig {
|
|||
return ConfigError.NO_TEMPLATE;
|
||||
}
|
||||
|
||||
Configuration fmConfig = (Configuration) vreq.getAttribute("freemarkerConfig");
|
||||
TemplateLoader tl = fmConfig.getTemplateLoader();
|
||||
try {
|
||||
if ( tl.findTemplateSource(templateName) == null ) {
|
||||
if ( templateLoader.findTemplateSource(templateName) == null ) {
|
||||
return ConfigError.TEMPLATE_NOT_FOUND;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -22,6 +22,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Property;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerConfigurationLoader;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
|
||||
|
@ -29,6 +30,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
|
|||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
|
||||
public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel {
|
||||
|
||||
|
@ -87,7 +89,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
|
||||
// Get the config for this object property
|
||||
try {
|
||||
config = new PropertyListConfig(this, vreq, op, editing);
|
||||
config = new PropertyListConfig(this, getFreemarkerTemplateLoader(), vreq, op, editing);
|
||||
} catch (InvalidConfigurationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
|
@ -128,6 +130,18 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull this into a protected method so we can stub it out in the unit tests.
|
||||
* Other options:
|
||||
* 1) receive a TemplateLoader into the constructor of ObjectPropertyTemplateModel,
|
||||
* 2) provide a service that will check to see whether a given template name is valid,
|
||||
* 3) skip the test for valid template name until we try to use the thing.
|
||||
* This will do for now.
|
||||
*/
|
||||
protected TemplateLoader getFreemarkerTemplateLoader() {
|
||||
return FreemarkerConfigurationLoader.getConfig(vreq).getTemplateLoader();
|
||||
}
|
||||
|
||||
protected List<Map<String, String>> getStatementData() {
|
||||
ObjectPropertyStatementDao opDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao();
|
||||
return opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, objectKey, getSelectQuery(), getConstructQueries());
|
||||
|
|
|
@ -37,10 +37,9 @@ import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
|
||||
public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||
AbstractTestClass {
|
||||
|
@ -135,11 +134,11 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
|||
|
||||
subject = new IndividualImpl();
|
||||
|
||||
Configuration fmConfig = new Configuration();
|
||||
vreq.setAttribute("freemarkerConfig", fmConfig);
|
||||
// We need a stub TemplateLoader because PropertyListConfig will check
|
||||
// to see whether the template name is recognized. How can we get around
|
||||
// that? This will do for now.
|
||||
tl = new TemplateLoaderStub();
|
||||
tl.createTemplate("propStatement-default.ftl", "");
|
||||
fmConfig.setTemplateLoader(tl);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
@ -564,7 +563,7 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
|||
// Supporting classes
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private static class NonCollatingOPTM extends ObjectPropertyTemplateModel {
|
||||
private class NonCollatingOPTM extends ObjectPropertyTemplateModel {
|
||||
NonCollatingOPTM(ObjectProperty op, Individual subject,
|
||||
VitroRequest vreq, boolean editing)
|
||||
throws InvalidConfigurationException {
|
||||
|
@ -581,13 +580,18 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TemplateLoader getFreemarkerTemplateLoader() {
|
||||
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* No populated properties and we don't do syntax checking on the select
|
||||
* query.
|
||||
*/
|
||||
private static class SimpleCollatingOPTM extends
|
||||
private class SimpleCollatingOPTM extends
|
||||
CollatedObjectPropertyTemplateModel {
|
||||
SimpleCollatingOPTM(ObjectProperty op, Individual subject,
|
||||
VitroRequest vreq, boolean editing)
|
||||
|
@ -601,10 +605,15 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TemplateLoader getFreemarkerTemplateLoader() {
|
||||
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** No populated properties but we do check the syntax of the select query. */
|
||||
private static class CheckingCollatingOPTM extends
|
||||
private class CheckingCollatingOPTM extends
|
||||
CollatedObjectPropertyTemplateModel {
|
||||
CheckingCollatingOPTM(ObjectProperty op, Individual subject,
|
||||
VitroRequest vreq, boolean editing)
|
||||
|
@ -613,6 +622,11 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
|||
.<ObjectProperty> emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TemplateLoader getFreemarkerTemplateLoader() {
|
||||
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Does not implement the required interface. */
|
||||
|
|
Loading…
Add table
Reference in a new issue