Changing the DataGetter and PageDataGetter utuils to not log unnecessary errors. Adding unit tests for DataGetters and PageDataGetters.

This commit is contained in:
briancaruso 2012-02-15 17:57:01 +00:00
parent 3b95705d97
commit c30e883f7e
15 changed files with 478 additions and 151 deletions

View file

@ -0,0 +1,77 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils.dataGetter;
import static org.junit.Assert.fail;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Level;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
public class DataGetterUtilsTest extends AbstractTestClass{
OntModel displayModel;
String testDataGetterURI_1 = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#query1data";
String pageURI_1 = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#SPARQLPage";
String pageX = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#pageX";
String dataGetterX = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#pageDataGetterX";
@Before
public void setUp() throws Exception {
// Suppress error logging.
setLoggerLevel(RDFDefaultErrorHandler.class, Level.OFF);
Model model = ModelFactory.createDefaultModel();
InputStream in = DataGetterUtilsTest.class.getResourceAsStream("resources/dataGetterTest.n3");
model.read(in,"","N3");
displayModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model);
}
@Test
public void testGetJClassForDataGetterURI() throws IllegalAccessException {
String fullJavaClassName = DataGetterUtils.getJClassForDataGetterURI(displayModel, testDataGetterURI_1);
Assert.assertNotNull(fullJavaClassName);
Assert.assertTrue("java class name should not be empty", ! StringUtils.isEmpty(fullJavaClassName));
Assert.assertEquals(SparqlQueryDataGetter.class.getName(), fullJavaClassName);
}
@Test
public void testDataGetterForURI() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException {
DataGetter dg = DataGetterUtils.dataGetterForURI(displayModel, testDataGetterURI_1);
Assert.assertNotNull(dg);
}
@Test
public void testGetDataGettersForPage() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException {
List<DataGetter> dgList =
DataGetterUtils.getDataGettersForPage(displayModel, pageURI_1);
Assert.assertNotNull(dgList);
Assert.assertTrue("List of DataGetters was empty, it should not be.", dgList.size() > 0);
}
@Test
public void testNonPageDataGetter() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException{
DataGetter dg = DataGetterUtils.dataGetterForURI(displayModel,dataGetterX);
Assert.assertNull(dg);
List<DataGetter> dgList =
DataGetterUtils.getDataGettersForPage(displayModel, pageX);
Assert.assertNotNull(dgList);
Assert.assertTrue("List should be, it was not", dgList.size() == 0);
}
}

View file

@ -0,0 +1,33 @@
# $This file is distributed under the terms of the license in /doc/license.txt$
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix core: <http://vivoweb.org/ontology/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
### This file is for the test DataGetterUtilsTest.java
display:SPARQLPage
a display:Page ;
display:title "TestQuery" ;
display:urlMapping "/query1" ;
display:hasDataGetter display:query1data .
display:query1data
a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
display:query "SELECT * WHERE { ?uri a <http://xmlns.com/foaf/0.1/Person> } " ;
display:saveToVar "people" .
### test of what happens with a PageDataGetter instead of a DataGetter ###
display:pageX
a display:Page ;
display:title "A PageDataGetter, not a DataGetter" ;
display:urlMapping "/query2" ;
display:hasDataGetter display:pageDataGetterX .
display:pageDataGetterX
a <java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.ClassGroupPageData> .

View file

@ -0,0 +1,67 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter;
import java.io.InputStream;
import java.util.List;
import org.apache.log4j.Level;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import stubs.javax.servlet.http.HttpServletRequestStub;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector;
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
public class PageDataGetterUtilsTest extends AbstractTestClass{
OntModel displayModel;
WebappDaoFactory wdf;
String pageURI = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#pageX";
String pageURI_2 = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#SPARQLPage";
@Before
public void setUp() throws Exception {
// Suppress error logging.
setLoggerLevel(RDFDefaultErrorHandler.class, Level.OFF);
OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM);
InputStream in = PageDataGetterUtilsTest.class.getResourceAsStream("resources/pageDataGetter.n3");
model.read(in,"","N3");
displayModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model);
SimpleOntModelSelector sos = new SimpleOntModelSelector( ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM));
sos.setDisplayModel(displayModel);
wdf = new WebappDaoFactoryJena(sos);
}
@Test
public void testGetPageDataGetterObjects() throws Exception{
VitroRequest vreq = new VitroRequest( new HttpServletRequestStub() );
vreq.setWebappDaoFactory(wdf);
List<PageDataGetter> pdgList = PageDataGetterUtils.getPageDataGetterObjects(vreq, pageURI);
Assert.assertNotNull(pdgList);
Assert.assertTrue("should have one PageDataGetter", pdgList.size() == 1);
}
@Test
public void testGetNonPageDataGetterObjects() throws Exception{
VitroRequest vreq = new VitroRequest( new HttpServletRequestStub() );
vreq.setWebappDaoFactory(wdf);
List<PageDataGetter> pdgList = PageDataGetterUtils.getPageDataGetterObjects(vreq, pageURI_2);
Assert.assertNotNull(pdgList);
Assert.assertTrue("should have no PageDataGetters", pdgList.size() == 0);
}
}

View file

@ -0,0 +1,20 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class SparqlQueryDataGetterTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testGetData() {
fail("Not yet implemented");
}
}

View file

@ -0,0 +1,30 @@
# $This file is distributed under the terms of the license in /doc/license.txt$
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix core: <http://vivoweb.org/ontology/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
### This file is for the test PageDataGetterUtilsTest.java
display:SPARQLPage
a display:Page ;
display:title "TestQuery" ;
display:urlMapping "/query1" ;
display:hasDataGetter display:query1data .
display:query1data
a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
display:query "SELECT * WHERE { ?uri a <http://xmlns.com/foaf/0.1/Person> } " ;
display:saveToVar "people" .
display:pageX
a display:Page ;
display:title "A PageDataGetter, not a DataGetter" ;
display:urlMapping "/query2" ;
display:hasDataGetter display:pageDataGetterX .
display:pageDataGetterX
a <java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.ClassGroupPageData> .