NIHVIVO-3542 Enhance the Stub classes so I can write unit tests for the IndividualController. Adjust AuthenticateTest to use the enhanced HttpServletRequestStub.

This commit is contained in:
j2blake 2012-01-24 17:22:34 +00:00
parent 191d579065
commit ca46511f40
12 changed files with 1007 additions and 191 deletions

View file

@ -37,7 +37,6 @@ import edu.cornell.mannlib.vedit.beans.LoginStatusBean.AuthenticationSource;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.ActiveIdentifierBundleFactories;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.factory.HasPermissionFactory;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.Permission;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.PermissionRegistry;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PermissionsPolicy;
@ -163,7 +162,7 @@ public class AuthenticateTest extends AbstractTestClass {
request = new HttpServletRequestStub();
request.setSession(session);
request.setRequestUrl(new URL("http://this.that/vivo/authenticate"));
request.setRequestUrlByParts("http://this.that", "/vivo", "/authenticate", null);
request.setMethod("POST");
response = new HttpServletResponseStub();

View file

@ -32,8 +32,12 @@ public class IndividualStub implements Individual {
// ----------------------------------------------------------------------
private final String uri;
private final Set<DataPropertyStatement> dpsSet = new HashSet<DataPropertyStatement>();
private final Set<ObjectPropertyStatement> opsSet = new HashSet<ObjectPropertyStatement>();
private final Set<VClass> vClasses = new HashSet<VClass>();
private String name = "NoName";
public IndividualStub(String uri) {
this.uri = uri;
@ -44,7 +48,20 @@ public class IndividualStub implements Individual {
}
public void addObjectPropertyStatement(String predicateUri, String objectUri) {
opsSet.add(new ObjectPropertyStatementImpl(this.uri, predicateUri, objectUri));
opsSet.add(new ObjectPropertyStatementImpl(this.uri, predicateUri,
objectUri));
}
public void addPopulatedObjectPropertyStatement(String predicateUri,
String objectUri, Individual object) {
ObjectPropertyStatementImpl stmt = new ObjectPropertyStatementImpl(
this.uri, predicateUri, objectUri);
stmt.setObject(object);
opsSet.add(stmt);
}
public void addVclass(String namespace, String localname, String vClassName) {
vClasses.add(new VClass(namespace, localname, vClassName));
}
// ----------------------------------------------------------------------
@ -56,6 +73,16 @@ public class IndividualStub implements Individual {
return uri;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public List<DataPropertyStatement> getDataPropertyStatements() {
return new ArrayList<DataPropertyStatement>(dpsSet);
@ -65,7 +92,7 @@ public class IndividualStub implements Individual {
public List<DataPropertyStatement> getDataPropertyStatements(
String propertyUri) {
List<DataPropertyStatement> list = new ArrayList<DataPropertyStatement>();
for (DataPropertyStatement dps: dpsSet) {
for (DataPropertyStatement dps : dpsSet) {
if (dps.getDatapropURI().equals(propertyUri)) {
list.add(dps);
}
@ -73,6 +100,16 @@ public class IndividualStub implements Individual {
return list;
}
@Override
public String getDataValue(String propertyUri) {
for (DataPropertyStatement dps : dpsSet) {
if (dps.getDatapropURI().equals(propertyUri)) {
return dps.getData();
}
}
return null;
}
@Override
public List<ObjectPropertyStatement> getObjectPropertyStatements() {
return new ArrayList<ObjectPropertyStatement>(opsSet);
@ -82,7 +119,7 @@ public class IndividualStub implements Individual {
public List<ObjectPropertyStatement> getObjectPropertyStatements(
String propertyUri) {
List<ObjectPropertyStatement> list = new ArrayList<ObjectPropertyStatement>();
for (ObjectPropertyStatement ops: opsSet) {
for (ObjectPropertyStatement ops : opsSet) {
if (ops.getPropertyURI().equals(propertyUri)) {
list.add(ops);
}
@ -90,6 +127,64 @@ public class IndividualStub implements Individual {
return list;
}
@Override
public Individual getRelatedIndividual(String propertyUri) {
for (ObjectPropertyStatement ops : opsSet) {
if (ops.getPropertyURI().equals(propertyUri)) {
return ops.getObject();
}
}
return null;
}
@Override
public boolean isVClass(String vclassUri) {
for (VClass vc : vClasses) {
if (vc.getURI().equals(vclassUri)) {
return true;
}
}
return false;
}
@Override
public List<VClass> getVClasses(boolean direct) {
return new ArrayList<VClass>(vClasses);
}
@Override
public void sortForDisplay() {
// does nothing.
}
@Override
public String getLocalName() {
// Useless for now.
return "BOGUS Local Name";
}
@Override
public VClass getVClass() {
for (VClass vc : vClasses) {
return vc;
}
return null;
}
@Override
public String getVClassURI() {
for (VClass vc : vClasses) {
return vc.getURI();
}
return null;
}
@Override
public List<VClass> getVClasses() {
return new ArrayList<VClass>(vClasses);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@ -117,12 +212,6 @@ public class IndividualStub implements Individual {
"ResourceBean.setNamespace() not implemented.");
}
@Override
public String getLocalName() {
throw new RuntimeException(
"ResourceBean.getLocalName() not implemented.");
}
@Override
public void setLocalName(String localName) {
throw new RuntimeException(
@ -171,26 +260,11 @@ public class IndividualStub implements Individual {
"Comparable<Individual>.compareTo() not implemented.");
}
@Override
public String getName() {
throw new RuntimeException("Individual.getName() not implemented.");
}
@Override
public void setName(String in) {
throw new RuntimeException("Individual.setName() not implemented.");
}
@Override
public String getRdfsLabel() {
throw new RuntimeException("Individual.getRdfsLabel() not implemented.");
}
@Override
public String getVClassURI() {
throw new RuntimeException("Individual.getVClassURI() not implemented.");
}
@Override
public void setVClassURI(String in) {
throw new RuntimeException("Individual.setVClassURI() not implemented.");
@ -296,41 +370,16 @@ public class IndividualStub implements Individual {
"Individual.getDataValues() not implemented.");
}
@Override
public String getDataValue(String propertyUri) {
throw new RuntimeException("Individual.getDataValue() not implemented.");
}
@Override
public VClass getVClass() {
throw new RuntimeException("Individual.getVClass() not implemented.");
}
@Override
public void setVClass(VClass class1) {
throw new RuntimeException("Individual.setVClass() not implemented.");
}
@Override
public List<VClass> getVClasses() {
throw new RuntimeException("Individual.getVClasses() not implemented.");
}
@Override
public List<VClass> getVClasses(boolean direct) {
throw new RuntimeException("Individual.getVClasses() not implemented.");
}
@Override
public void setVClasses(List<VClass> vClassList, boolean direct) {
throw new RuntimeException("Individual.setVClasses() not implemented.");
}
@Override
public boolean isVClass(String uri) {
throw new RuntimeException("Individual.isVClass() not implemented.");
}
@Override
public boolean isMemberOfClassProhibitedFromSearch(ProhibitedFromSearch pfs) {
throw new RuntimeException(
@ -349,12 +398,6 @@ public class IndividualStub implements Individual {
"Individual.getRelatedIndividuals() not implemented.");
}
@Override
public Individual getRelatedIndividual(String propertyUri) {
throw new RuntimeException(
"Individual.getRelatedIndividual() not implemented.");
}
@Override
public List<DataPropertyStatement> getExternalIds() {
throw new RuntimeException(
@ -394,12 +437,6 @@ public class IndividualStub implements Individual {
throw new RuntimeException("Individual.hasThumb() not implemented.");
}
@Override
public void sortForDisplay() {
throw new RuntimeException(
"Individual.sortForDisplay() not implemented.");
}
@Override
public JSONObject toJSON() throws JSONException {
throw new RuntimeException("Individual.toJSON() not implemented.");
@ -419,11 +456,13 @@ public class IndividualStub implements Individual {
@Override
public String getSearchSnippet() {
throw new RuntimeException("Individual.getSearchSnippet() not implemented.");
throw new RuntimeException(
"Individual.getSearchSnippet() not implemented.");
}
@Override
public void setSearchSnippet(String snippet) {
throw new RuntimeException("Individual.setSearchSnippet() not implemented.");
throw new RuntimeException(
"Individual.setSearchSnippet() not implemented.");
}
}

View file

@ -0,0 +1,90 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.beans;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
/**
* TODO
*/
public class SelfEditingConfigurationStub extends SelfEditingConfiguration {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private Map<String, List<Individual>> associatedIndividuals = new HashMap<String, List<Individual>>();
public SelfEditingConfigurationStub() {
super("bogusMatchingProperty");
}
public void addAssociatedIndividual(String externalAuthId,
Individual individual) {
if (!associatedIndividuals.containsKey(externalAuthId)) {
associatedIndividuals.put(externalAuthId,
new ArrayList<Individual>());
}
associatedIndividuals.get(externalAuthId).add(individual);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public List<Individual> getAssociatedIndividuals(IndividualDao indDao,
String externalAuthId) {
if (associatedIndividuals.containsKey(externalAuthId)) {
return associatedIndividuals.get(externalAuthId);
} else {
return Collections.emptyList();
}
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public boolean isConfigured() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SelfEditingConfigurationStub.isConfigured() not implemented.");
}
@Override
public String getMatchingPropertyUri() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SelfEditingConfigurationStub.getMatchingPropertyUri() not implemented.");
}
@Override
public List<Individual> getAssociatedIndividuals(IndividualDao indDao,
UserAccount user) {
// TODO Auto-generated method stub
throw new RuntimeException(
"SelfEditingConfigurationStub.getAssociatedIndividuals() not implemented.");
}
@Override
public void associateIndividualWithUserAccount(IndividualDao indDao,
DataPropertyStatementDao dpsDao, UserAccount user,
String associatedIndividualUri) {
// TODO Auto-generated method stub
throw new RuntimeException(
"SelfEditingConfigurationStub.associateIndividualWithUserAccount() not implemented.");
}
}

View file

@ -0,0 +1,58 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.List;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.dao.ApplicationDao;
/**
* A minimal implementation of the ApplicationDao.
*
* I have only implemented the methods that I needed. Feel free to implement
* others.
*/
public class ApplicationDaoStub implements ApplicationDao {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private ApplicationBean applicationBean;
public void setApplicationBean(ApplicationBean applicationBean) {
this.applicationBean = applicationBean;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public ApplicationBean getApplicationBean() {
return this.applicationBean;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void updateApplicationBean(ApplicationBean appBean) {
throw new RuntimeException(
"ApplicationDaoStub.updateApplicationBean() not implemented.");
}
@Override
public List<String> getExternallyLinkedNamespaces() {
throw new RuntimeException(
"ApplicationDaoStub.getExternallyLinkedNamespaces() not implemented.");
}
@Override
public boolean isExternallyLinkedNamespace(String namespace) {
throw new RuntimeException(
"ApplicationDaoStub.isExternallyLinkedNamespace() not implemented.");
}
}

View file

@ -0,0 +1,42 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import edu.cornell.mannlib.vitro.webapp.dao.MenuDao;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
/**
* A minimal implementation of the MenuDao.
*
* I have only implemented the methods that I needed. Feel free to implement
* others.
*/
public class MenuDaoStub implements MenuDao {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private MainMenu mainMenu;
public void setMainMenu(MainMenu mainMenu) {
this.mainMenu = mainMenu;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
/**
* For this first implementation, we just ignore the "url" parameter and
* return whatever MainMenu has been loaded.
*/
@Override
public MainMenu getMainMenu(String url) {
return this.mainMenu;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
}

View file

@ -0,0 +1,250 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.PropertyInstance;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
/**
* A minimal implementation of the ObjectPropertyStatementDao
*
* I have only implemented the methods that I needed. Feel free to implement
* others.
*/
public class ObjectPropertyStatementDaoStub implements
ObjectPropertyStatementDao {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private static class CanonicalObjectPropertyStatement implements
ObjectPropertyStatement {
final String s;
final String p;
final String o;
CanonicalObjectPropertyStatement(String s, String p, String o) {
if (s == null) {
throw new NullPointerException(
"statment subject may not be null.");
}
if (p == null) {
throw new NullPointerException(
"statement predicate may not be null.");
}
if (o == null) {
throw new NullPointerException(
"statement object may not be null.");
}
this.s = s;
this.p = p;
this.o = o;
}
CanonicalObjectPropertyStatement(ObjectPropertyStatement stmt) {
this(stmt.getSubjectURI(), stmt.getPropertyURI(), stmt
.getObjectURI());
}
@Override
public boolean isSubjectOriented() {
return false;
}
@Override
public void setSubjectOriented(boolean subjectOriented) {
throw new UnsupportedOperationException();
}
@Override
public String getSubjectURI() {
return s;
}
@Override
public void setSubjectURI(String subjectURI) {
throw new UnsupportedOperationException();
}
@Override
public String getObjectURI() {
return o;
}
@Override
public void setObjectURI(String objectURI) {
throw new UnsupportedOperationException();
}
@Override
public Individual getSubject() {
return null;
}
@Override
public void setSubject(Individual subject) {
throw new UnsupportedOperationException();
}
@Override
public ObjectProperty getProperty() {
return null;
}
@Override
public void setProperty(ObjectProperty property) {
throw new UnsupportedOperationException();
}
@Override
public Individual getObject() {
return null;
}
@Override
public void setObject(Individual object) {
throw new UnsupportedOperationException();
}
@Override
public String getPropertyURI() {
return p;
}
@Override
public void setPropertyURI(String URI) {
throw new UnsupportedOperationException();
}
@Override
public PropertyInstance toPropertyInstance() {
return null;
}
public boolean matches(ObjectPropertyStatement stmt) {
String otherS = stmt.getSubjectURI();
String otherP = stmt.getPropertyURI();
String otherO = stmt.getObjectURI();
return ((otherS == null) || otherS.equals(s))
&& ((otherP == null) || otherP.equals(p))
&& ((otherO == null) || otherO.equals(o));
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null) {
return false;
}
if (!other.getClass().equals(this.getClass())) {
return false;
}
CanonicalObjectPropertyStatement that = (CanonicalObjectPropertyStatement) other;
return this.s.equals(that.s) && this.o.equals(that.o)
&& this.p.equals(that.p);
}
@Override
public int hashCode() {
return this.s.hashCode() ^ this.o.hashCode() ^ this.p.hashCode();
}
}
Set<CanonicalObjectPropertyStatement> statements = new HashSet<CanonicalObjectPropertyStatement>();
public void addObjectPropertyStatement(String s, String p, String o) {
statements.add(new CanonicalObjectPropertyStatement(s, p, o));
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public int insertNewObjectPropertyStatement(ObjectPropertyStatement stmt) {
if (stmt == null) {
throw new NullPointerException("objPropertyStmt may not be null.");
}
statements.add(new CanonicalObjectPropertyStatement(stmt));
return 0;
}
@Override
public List<ObjectPropertyStatement> getObjectPropertyStatements(
ObjectPropertyStatement stmt) {
List<ObjectPropertyStatement> list = new ArrayList<ObjectPropertyStatement>();
for (CanonicalObjectPropertyStatement cStmt : statements) {
if (cStmt.matches(stmt)) {
list.add(cStmt);
}
}
return list;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void deleteObjectPropertyStatement(
ObjectPropertyStatement objPropertyStmt) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.deleteObjectPropertyStatement() not implemented.");
}
@Override
public List<ObjectPropertyStatement> getObjectPropertyStatements(
ObjectProperty objectProperty) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.getObjectPropertyStatements() not implemented.");
}
@Override
public List<ObjectPropertyStatement> getObjectPropertyStatements(
ObjectProperty objectProperty, int startIndex, int endIndex) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.getObjectPropertyStatements() not implemented.");
}
@Override
public Individual fillExistingObjectPropertyStatements(Individual entity) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.fillExistingObjectPropertyStatements() not implemented.");
}
@Override
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String propertyUri, String objectKey,
String query) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.getObjectPropertyStatementsForIndividualByProperty() not implemented.");
}
@Override
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String propertyUri, String objectKey,
String query, Set<String> constructQueries) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.getObjectPropertyStatementsForIndividualByProperty() not implemented.");
}
@Override
public Map<String, String> getMostSpecificTypesInClassgroupsForIndividual(
String subjectUri) {
throw new RuntimeException(
"ObjectPropertyStatementDaoStub.getMostSpecificTypesInClassgroupsForIndividual() not implemented.");
}
}

View file

@ -0,0 +1,63 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
/**
* A minimal implementation of the OntologyDao.
*
* I have only implemented the methods that I needed. Feel free to implement
* others.
*/
public class OntologyDaoStub implements OntologyDao {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private final Map<String, Ontology> ontologies = new HashMap<String, Ontology>();
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public String insertNewOntology(Ontology ontology) {
ontologies.put(ontology.getURI(), ontology);
return ontology.getURI();
}
@Override
public List<Ontology> getAllOntologies() {
return new ArrayList<Ontology>(ontologies.values());
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public Ontology getOntologyByURI(String ontologyURI) {
throw new RuntimeException(
"OntologyDaoStub.getOntologyByURI() not implemented.");
}
@Override
public void updateOntology(Ontology ontology) {
throw new RuntimeException(
"OntologyDaoStub.updateOntology() not implemented.");
}
@Override
public void deleteOntology(Ontology ontology) {
throw new RuntimeException(
"OntologyDaoStub.deleteOntology() not implemented.");
}
}

View file

@ -36,23 +36,48 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
// Stub infrastructure
// ----------------------------------------------------------------------
private IndividualDao individualDao;
private String defaultNamespace;
private ApplicationDao applicationDao;
private DataPropertyDao dataPropertyDao;
private IndividualDao individualDao;
private MenuDao menuDao;
private ObjectPropertyDao objectPropertyDao;
private ObjectPropertyStatementDao objectPropertyStatementDao;
private OntologyDao ontologyDao;
private UserAccountsDao userAccountsDao;
public void setIndividualDao(IndividualDao individualDao) {
this.individualDao = individualDao;
public void setDefaultNamespace(String defaultNamespace) {
this.defaultNamespace = defaultNamespace;
}
public void setApplicationDao(ApplicationDao applicationDao) {
this.applicationDao = applicationDao;
}
public void setDataPropertyDao(DataPropertyDao dataPropertyDao) {
this.dataPropertyDao = dataPropertyDao;
}
public void setIndividualDao(IndividualDao individualDao) {
this.individualDao = individualDao;
}
public void setMenuDao(MenuDao menuDao) {
this.menuDao = menuDao;
}
public void setObjectPropertyDao(ObjectPropertyDao objectPropertyDao) {
this.objectPropertyDao = objectPropertyDao;
}
public void setObjectPropertyStatementDao(ObjectPropertyStatementDao objectPropertyStatementDao) {
this.objectPropertyStatementDao = objectPropertyStatementDao;
}
public void setOntologyDao(OntologyDao ontologyDao) {
this.ontologyDao = ontologyDao;
}
public void setUserAccountsDao(UserAccountsDao userAccountsDao) {
this.userAccountsDao = userAccountsDao;
}
@ -62,8 +87,13 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
// ----------------------------------------------------------------------
@Override
public IndividualDao getIndividualDao() {
return this.individualDao;
public String getDefaultNamespace() {
return this.defaultNamespace;
}
@Override
public ApplicationDao getApplicationDao() {
return this.applicationDao;
}
@Override
@ -71,11 +101,30 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
return this.dataPropertyDao;
}
@Override
public IndividualDao getIndividualDao() {
return this.individualDao;
}
@Override
public MenuDao getMenuDao() {
return this.menuDao;
}
@Override
public ObjectPropertyDao getObjectPropertyDao() {
return this.objectPropertyDao;
}
@Override
public ObjectPropertyStatementDao getObjectPropertyStatementDao() {
return this.objectPropertyStatementDao; }
@Override
public OntologyDao getOntologyDao() {
return this.ontologyDao;
}
@Override
public UserAccountsDao getUserAccountsDao() {
return this.userAccountsDao;
@ -103,12 +152,6 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
"WebappDaoFactory.checkURI() not implemented.");
}
@Override
public String getDefaultNamespace() {
throw new RuntimeException(
"WebappDaoFactory.getDefaultNamespace() not implemented.");
}
@Override
public Set<String> getNonuserNamespaces() {
throw new RuntimeException(
@ -151,12 +194,6 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
"WebappDaoFactory.getDatatypeDao() not implemented.");
}
@Override
public OntologyDao getOntologyDao() {
throw new RuntimeException(
"WebappDaoFactory.getOntologyDao() not implemented.");
}
@Override
public VClassDao getVClassDao() {
throw new RuntimeException(
@ -169,24 +206,12 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
"WebappDaoFactory.getDataPropertyStatementDao() not implemented.");
}
@Override
public ObjectPropertyStatementDao getObjectPropertyStatementDao() {
throw new RuntimeException(
"WebappDaoFactory.getObjectPropertyStatementDao() not implemented.");
}
@Override
public DisplayModelDao getDisplayModelDao() {
throw new RuntimeException(
"WebappDaoFactory.getDisplayModelDao() not implemented.");
}
@Override
public ApplicationDao getApplicationDao() {
throw new RuntimeException(
"WebappDaoFactory.getApplicationDao() not implemented.");
}
@Override
public VClassGroupDao getVClassGroupDao() {
throw new RuntimeException(
@ -211,12 +236,6 @@ public class WebappDaoFactoryStub implements WebappDaoFactory {
"WebappDaoFactory.getPageDao() not implemented.");
}
@Override
public MenuDao getMenuDao() {
throw new RuntimeException(
"WebappDaoFactory.getMenuDao() not implemented.");
}
@Override
public void close() {
throw new RuntimeException("WebappDaoFactory.close() not implemented.");

View file

@ -0,0 +1,123 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
/**
* A minimal implementation of the NamespaceMapper.
*
* I have only implemented the methods that I needed. Feel free to implement
* others.
*/
public class NamespaceMapperStub implements NamespaceMapper {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private final Map<String, String> prefixMap = new HashMap<String, String>();
public void setPrefixForNamespace(String prefix, String namespace) {
prefixMap.put(prefix, namespace);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public String getNamespaceForPrefix(String prefix) {
return prefixMap.get(prefix);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void addedStatement(Statement arg0) {
throw new RuntimeException(
"NamespaceMapperStub.addedStatement() not implemented.");
}
@Override
public void addedStatements(Statement[] arg0) {
throw new RuntimeException(
"NamespaceMapperStub.addedStatements() not implemented.");
}
@Override
public void addedStatements(List<Statement> arg0) {
throw new RuntimeException(
"NamespaceMapperStub.addedStatements() not implemented.");
}
@Override
public void addedStatements(StmtIterator arg0) {
throw new RuntimeException(
"NamespaceMapperStub.addedStatements() not implemented.");
}
@Override
public void addedStatements(Model arg0) {
throw new RuntimeException(
"NamespaceMapperStub.addedStatements() not implemented.");
}
@Override
public void notifyEvent(Model arg0, Object arg1) {
throw new RuntimeException(
"NamespaceMapperStub.notifyEvent() not implemented.");
}
@Override
public void removedStatement(Statement arg0) {
throw new RuntimeException(
"NamespaceMapperStub.removedStatement() not implemented.");
}
@Override
public void removedStatements(Statement[] arg0) {
throw new RuntimeException(
"NamespaceMapperStub.removedStatements() not implemented.");
}
@Override
public void removedStatements(List<Statement> arg0) {
throw new RuntimeException(
"NamespaceMapperStub.removedStatements() not implemented.");
}
@Override
public void removedStatements(StmtIterator arg0) {
throw new RuntimeException(
"NamespaceMapperStub.removedStatements() not implemented.");
}
@Override
public void removedStatements(Model arg0) {
throw new RuntimeException(
"NamespaceMapperStub.removedStatements() not implemented.");
}
@Override
public String getPrefixForNamespace(String namespace) {
throw new RuntimeException(
"NamespaceMapperStub.getPrefixForNamespace() not implemented.");
}
@Override
public List<String> getPrefixesForNamespace(String namespace) {
throw new RuntimeException(
"NamespaceMapperStub.getPrefixesForNamespace() not implemented.");
}
}

View file

@ -27,8 +27,16 @@ public class ServletContextStub implements ServletContext {
// Stub infrastructure
// ----------------------------------------------------------------------
private String contextPath = ""; // root context returns ""
private final Map<String, Object> attributes = new HashMap<String, Object>();
private final Map<String, String> mockResources = new HashMap<String, String>();
private final Map<String, String> realPaths = new HashMap<String, String>();
public void setContextPath(String contextPath) {
if (contextPath == null) {
throw new NullPointerException("contextPath may not be null.");
}
}
public void setMockResource(String path, String contents) {
if (path == null) {
@ -41,10 +49,26 @@ public class ServletContextStub implements ServletContext {
}
}
public void setRealPath(String path, String filepath) {
if (path == null) {
throw new NullPointerException("path may not be null.");
}
if (filepath == null) {
realPaths.remove(path);
} else {
realPaths.put(path, filepath);
}
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public String getContextPath() {
return contextPath;
}
@Override
public Object getAttribute(String name) {
return attributes.get(name);
@ -78,6 +102,11 @@ public class ServletContextStub implements ServletContext {
}
}
@Override
public String getRealPath(String path) {
return realPaths.get(path);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@ -88,12 +117,6 @@ public class ServletContextStub implements ServletContext {
"ServletContextStub.getContext() not implemented.");
}
@Override
public String getContextPath() {
throw new RuntimeException(
"ServletContextStub.getContextPath() not implemented.");
}
@Override
public String getInitParameter(String arg0) {
throw new RuntimeException(
@ -131,12 +154,6 @@ public class ServletContextStub implements ServletContext {
"ServletContextStub.getNamedDispatcher() not implemented.");
}
@Override
public String getRealPath(String arg0) {
throw new RuntimeException(
"ServletContextStub.getRealPath() not implemented.");
}
@Override
public RequestDispatcher getRequestDispatcher(String arg0) {
throw new RuntimeException(

View file

@ -21,16 +21,22 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
/**
* A simple stub for HttpServletRequest
*/
@SuppressWarnings("deprecation")
public class HttpServletRequestStub implements HttpServletRequest {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private URL requestUrl;
private String pathInfo;
private String requestUri;
private String requestUrl;
private String contextPath;
private String servletPath;
private String httpMethodType = "GET";
private String remoteAddr = "127.0.0.1";
@ -52,8 +58,60 @@ public class HttpServletRequestStub implements HttpServletRequest {
this.attributes.putAll(attributes);
}
/**
* Supply the request URL as a single URL. We will parse it on the
* assumption that the contextPath and the pathInfo are empty.
* Don't include a query string. Instead, set parameters.
*/
public void setRequestUrl(URL url) {
this.requestUrl = url;
this.contextPath = "";
this.pathInfo = null;
this.requestUrl = url.toString();
String path = url.getPath();
if (path.isEmpty()) {
this.servletPath = "/";
} else {
this.servletPath = path;
}
this.requestUri = this.servletPath;
}
/**
* Supply the pieces of the request URL, so we can respond correctly when
* asked for a piece.
* Don't include a query string. Instead, set parameters.
*/
public void setRequestUrlByParts(String shemeHostPort, String contextPath,
String servletPath, String pathInfo) {
if (contextPath == null) {
throw new NullPointerException("contextPath may not be null.");
}
this.contextPath = contextPath;
this.pathInfo = pathInfo;
if (servletPath == null) {
throw new NullPointerException("servletPath may not be null.");
}
if (!servletPath.startsWith("/")) {
throw new IllegalArgumentException(
"servletPath must start with a /");
}
this.servletPath = servletPath;
this.requestUri = contextPath + servletPath + ((pathInfo == null) ? "" : pathInfo);
if (shemeHostPort == null) {
throw new NullPointerException("shemeHostPort may not be null.");
}
if (!shemeHostPort.contains("://")) {
throw new IllegalArgumentException(
"schemeHostPort must be sheme://host[:port]");
}
this.requestUrl = shemeHostPort + this.requestUri;
}
/** Set to "GET" or "POST", etc. */
@ -93,10 +151,12 @@ public class HttpServletRequestStub implements HttpServletRequest {
// Stub methods
// ----------------------------------------------------------------------
@Override
public HttpSession getSession() {
return getSession(true);
}
@Override
public HttpSession getSession(boolean create) {
if (create && (session == null)) {
session = new HttpSessionStub();
@ -104,32 +164,63 @@ public class HttpServletRequestStub implements HttpServletRequest {
return session;
}
public String getContextPath() {
String path = requestUrl.getPath();
if (path.isEmpty()) {
return "";
}
int secondSlash = path.indexOf("/", 1);
if (secondSlash == -1) {
return "";
} else {
return path.substring(0, secondSlash);
}
@Override
public StringBuffer getRequestURL() {
return new StringBuffer(requestUrl);
}
@Override
public String getRequestURI() {
return requestUri;
}
@Override
public String getContextPath() {
return contextPath;
}
@Override
public String getServletPath() {
return servletPath;
}
@Override
public String getPathInfo() {
return pathInfo;
}
@Override
public String getQueryString() {
if (parameters.isEmpty()) {
return null;
}
String qs = "";
for (String key:parameters.keySet()) {
for (String value: parameters.get(key)) {
qs += "&" + key + "=" + URLEncoder.encode(value);
}
}
return "?" + qs.substring(1);
}
@Override
public String getMethod() {
return httpMethodType;
}
@Override
public String getRemoteAddr() {
return remoteAddr;
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getParameterNames() {
return Collections.enumeration(parameters.keySet());
}
@Override
@SuppressWarnings("rawtypes")
public Map getParameterMap() {
Map<String, String[]> map = new HashMap<String, String[]>();
@ -139,6 +230,7 @@ public class HttpServletRequestStub implements HttpServletRequest {
return map;
}
@Override
public String getParameter(String name) {
if (!parameters.containsKey(name)) {
return null;
@ -146,6 +238,7 @@ public class HttpServletRequestStub implements HttpServletRequest {
return parameters.get(name).get(0);
}
@Override
public String[] getParameterValues(String name) {
if (!parameters.containsKey(name)) {
return null;
@ -154,19 +247,23 @@ public class HttpServletRequestStub implements HttpServletRequest {
return list.toArray(new String[list.size()]);
}
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getAttributeNames() {
return Collections.enumeration(attributes.keySet());
}
@Override
public void removeAttribute(String name) {
attributes.remove(name);
}
@Override
public void setAttribute(String name, Object value) {
if (value == null) {
removeAttribute(name);
@ -174,11 +271,13 @@ public class HttpServletRequestStub implements HttpServletRequest {
attributes.put(name, value);
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getHeaderNames() {
return Collections.enumeration(headers.keySet());
}
@Override
public String getHeader(String name) {
name = name.toLowerCase();
if (headers.containsKey(name)) {
@ -188,6 +287,7 @@ public class HttpServletRequestStub implements HttpServletRequest {
}
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getHeaders(String name) {
name = name.toLowerCase();
@ -222,46 +322,21 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getIntHeader() not implemented.");
}
public String getPathInfo() {
throw new RuntimeException(
"HttpServletRequestStub.getPathInfo() not implemented.");
}
public String getPathTranslated() {
throw new RuntimeException(
"HttpServletRequestStub.getPathTranslated() not implemented.");
}
public String getQueryString() {
throw new RuntimeException(
"HttpServletRequestStub.getQueryString() not implemented.");
}
public String getRemoteUser() {
throw new RuntimeException(
"HttpServletRequestStub.getRemoteUser() not implemented.");
}
public String getRequestURI() {
throw new RuntimeException(
"HttpServletRequestStub.getRequestURI() not implemented.");
}
public StringBuffer getRequestURL() {
throw new RuntimeException(
"HttpServletRequestStub.getRequestURL() not implemented.");
}
public String getRequestedSessionId() {
throw new RuntimeException(
"HttpServletRequestStub.getRequestedSessionId() not implemented.");
}
public String getServletPath() {
throw new RuntimeException(
"HttpServletRequestStub.getServletPath() not implemented.");
}
public Principal getUserPrincipal() {
throw new RuntimeException(
"HttpServletRequestStub.getUserPrincipal() not implemented.");

View file

@ -2,10 +2,14 @@
package stubs.javax.servlet.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
@ -23,7 +27,11 @@ public class HttpServletResponseStub implements HttpServletResponse {
private String redirectLocation;
private int status = 200;
private String errorMessage;
private StringWriter outputWriter = new StringWriter();
private Map<String, String> headers = new HashMap<String, String>();
private String contentType;
private ByteArrayOutputStream outputStream;
private StringWriter outputWriter;
public String getRedirectLocation() {
return redirectLocation;
@ -37,6 +45,20 @@ public class HttpServletResponseStub implements HttpServletResponse {
return errorMessage;
}
public String getOutput() {
if (outputStream != null) {
return outputStream.toString();
} else if (outputWriter != null) {
return outputWriter.toString();
} else {
return "";
}
}
public String getHeader(String name) {
return headers.get(name);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@ -47,11 +69,18 @@ public class HttpServletResponseStub implements HttpServletResponse {
}
@Override
public void setStatus(int status) {
this.status = status;
}
@Override
@SuppressWarnings("hiding")
public void sendError(int status) throws IOException {
this.status = status;
}
@Override
@SuppressWarnings("hiding")
public void sendError(int status, String message) throws IOException {
this.status = status;
this.errorMessage = message;
@ -59,7 +88,55 @@ public class HttpServletResponseStub implements HttpServletResponse {
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(outputWriter);
if (outputStream != null) {
throw new IllegalStateException(
"Can't get a Writer after getting an OutputStream.");
}
if (outputWriter == null) {
outputWriter = new StringWriter();
}
return new PrintWriter(outputWriter, true);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (outputWriter != null) {
throw new IllegalStateException(
"Can't get an OutputStream after getting a Writer.");
}
if (outputStream == null) {
outputStream = new ByteArrayOutputStream();
}
return new ServletOutputStream() {
@Override
public void write(int thisChar) throws IOException {
outputStream.write(thisChar);
}
};
}
@Override
public void setHeader(String name, String value) {
headers.put(name, value);
}
@Override
public boolean containsHeader(String name) {
return headers.containsKey(name);
}
@Override
public void setContentType(String contentType) {
this.contentType = contentType;
}
@Override
public String getContentType() {
return contentType;
}
// ----------------------------------------------------------------------
@ -84,24 +161,12 @@ public class HttpServletResponseStub implements HttpServletResponse {
"HttpServletResponseStub.getCharacterEncoding() not implemented.");
}
@Override
public String getContentType() {
throw new RuntimeException(
"HttpServletResponseStub.getContentType() not implemented.");
}
@Override
public Locale getLocale() {
throw new RuntimeException(
"HttpServletResponseStub.getLocale() not implemented.");
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.getOutputStream() not implemented.");
}
@Override
public boolean isCommitted() {
throw new RuntimeException(
@ -138,12 +203,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
"HttpServletResponseStub.setContentLength() not implemented.");
}
@Override
public void setContentType(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setContentType() not implemented.");
}
@Override
public void setLocale(Locale arg0) {
throw new RuntimeException(
@ -174,12 +233,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
"HttpServletResponseStub.addIntHeader() not implemented.");
}
@Override
public boolean containsHeader(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.containsHeader() not implemented.");
}
@Override
public String encodeRedirectURL(String arg0) {
throw new RuntimeException(
@ -210,24 +263,12 @@ public class HttpServletResponseStub implements HttpServletResponse {
"HttpServletResponseStub.setDateHeader() not implemented.");
}
@Override
public void setHeader(String arg0, String arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setHeader() not implemented.");
}
@Override
public void setIntHeader(String arg0, int arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setIntHeader() not implemented.");
}
@Override
public void setStatus(int arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setStatus() not implemented.");
}
@Override
public void setStatus(int arg0, String arg1) {
throw new RuntimeException(