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:
j2blake 2012-04-20 19:26:59 +00:00
parent e3d2a8fb47
commit 68f88fe257
3 changed files with 42 additions and 13 deletions

View file

@ -40,6 +40,7 @@ public class PropertyListConfig {
// TODO Lump these together into the PropertyListConfigContext // TODO Lump these together into the PropertyListConfigContext
private final ObjectPropertyTemplateModel optm; private final ObjectPropertyTemplateModel optm;
private final VitroRequest vreq; private final VitroRequest vreq;
private final TemplateLoader templateLoader;
private boolean isDefaultConfig; private boolean isDefaultConfig;
private Set<String> constructQueries; private Set<String> constructQueries;
@ -47,12 +48,14 @@ public class PropertyListConfig {
private String templateName; private String templateName;
private ObjectPropertyDataPostProcessor postprocessor; // never null 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 { throws InvalidConfigurationException {
this.optm = optm; this.optm = optm;
this.vreq = vreq; this.vreq = vreq;
WebappDaoFactory wadf = vreq.getWebappDaoFactory(); WebappDaoFactory wadf = vreq.getWebappDaoFactory();
this.templateLoader = templateLoader;
// Get the custom config filename // Get the custom config filename
String configFileName = wadf.getObjectPropertyDao().getCustomListViewConfigFileName(op); String configFileName = wadf.getObjectPropertyDao().getCustomListViewConfigFileName(op);
@ -117,10 +120,8 @@ public class PropertyListConfig {
return ConfigError.NO_TEMPLATE; return ConfigError.NO_TEMPLATE;
} }
Configuration fmConfig = (Configuration) vreq.getAttribute("freemarkerConfig");
TemplateLoader tl = fmConfig.getTemplateLoader();
try { try {
if ( tl.findTemplateSource(templateName) == null ) { if ( templateLoader.findTemplateSource(templateName) == null ) {
return ConfigError.TEMPLATE_NOT_FOUND; return ConfigError.TEMPLATE_NOT_FOUND;
} }
} catch (IOException e) { } catch (IOException e) {

View file

@ -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.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.Property; import edu.cornell.mannlib.vitro.webapp.beans.Property;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; 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;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route; 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.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig;
import freemarker.cache.TemplateLoader;
public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel { public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel {
@ -87,7 +89,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
// Get the config for this object property // Get the config for this object property
try { try {
config = new PropertyListConfig(this, vreq, op, editing); config = new PropertyListConfig(this, getFreemarkerTemplateLoader(), vreq, op, editing);
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
throw e; throw e;
} catch (Exception 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() { protected List<Map<String, String>> getStatementData() {
ObjectPropertyStatementDao opDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao(); ObjectPropertyStatementDao opDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao();
return opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, objectKey, getSelectQuery(), getConstructQueries()); return opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, objectKey, getSelectQuery(), getConstructQueries());

View file

@ -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.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; 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.InvalidConfigurationException;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig;
import freemarker.template.Configuration; import freemarker.cache.TemplateLoader;
public class ObjectPropertyTemplateModel_PropertyListConfigTest extends public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
AbstractTestClass { AbstractTestClass {
@ -135,11 +134,11 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
subject = new IndividualImpl(); subject = new IndividualImpl();
Configuration fmConfig = new Configuration(); // We need a stub TemplateLoader because PropertyListConfig will check
vreq.setAttribute("freemarkerConfig", fmConfig); // to see whether the template name is recognized. How can we get around
// that? This will do for now.
tl = new TemplateLoaderStub(); tl = new TemplateLoaderStub();
tl.createTemplate("propStatement-default.ftl", ""); tl.createTemplate("propStatement-default.ftl", "");
fmConfig.setTemplateLoader(tl);
} }
@AfterClass @AfterClass
@ -564,7 +563,7 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
// Supporting classes // Supporting classes
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private static class NonCollatingOPTM extends ObjectPropertyTemplateModel { private class NonCollatingOPTM extends ObjectPropertyTemplateModel {
NonCollatingOPTM(ObjectProperty op, Individual subject, NonCollatingOPTM(ObjectProperty op, Individual subject,
VitroRequest vreq, boolean editing) VitroRequest vreq, boolean editing)
throws InvalidConfigurationException { throws InvalidConfigurationException {
@ -581,13 +580,18 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
return false; return false;
} }
@Override
protected TemplateLoader getFreemarkerTemplateLoader() {
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
}
} }
/* /*
* No populated properties and we don't do syntax checking on the select * No populated properties and we don't do syntax checking on the select
* query. * query.
*/ */
private static class SimpleCollatingOPTM extends private class SimpleCollatingOPTM extends
CollatedObjectPropertyTemplateModel { CollatedObjectPropertyTemplateModel {
SimpleCollatingOPTM(ObjectProperty op, Individual subject, SimpleCollatingOPTM(ObjectProperty op, Individual subject,
VitroRequest vreq, boolean editing) VitroRequest vreq, boolean editing)
@ -601,10 +605,15 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
return null; return null;
} }
@Override
protected TemplateLoader getFreemarkerTemplateLoader() {
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
}
} }
/** No populated properties but we do check the syntax of the select query. */ /** No populated properties but we do check the syntax of the select query. */
private static class CheckingCollatingOPTM extends private class CheckingCollatingOPTM extends
CollatedObjectPropertyTemplateModel { CollatedObjectPropertyTemplateModel {
CheckingCollatingOPTM(ObjectProperty op, Individual subject, CheckingCollatingOPTM(ObjectProperty op, Individual subject,
VitroRequest vreq, boolean editing) VitroRequest vreq, boolean editing)
@ -613,6 +622,11 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
.<ObjectProperty> emptyList()); .<ObjectProperty> emptyList());
} }
@Override
protected TemplateLoader getFreemarkerTemplateLoader() {
return ObjectPropertyTemplateModel_PropertyListConfigTest.this.tl;
}
} }
/** Does not implement the required interface. */ /** Does not implement the required interface. */