Adding test for DateTimeWithPrecision.

This commit is contained in:
bdc34 2010-11-11 19:12:08 +00:00
parent b8ec635ac0
commit 30e1269c3e
4 changed files with 333 additions and 63 deletions

View file

@ -9,8 +9,3 @@ hour: <input name="${fieldName}.hour" type="text" value="${hour}"/> <br/>
minute: <input name=${fieldName}.minute" type="text" value="${minute}"/> <br/>
second: <input name=${fieldName}.second" type="text" value="${second}"/> <br/>
<br/>
<select name="${fieldName}.precision" >
<#list possiblePrecisions as uri>
<option value="${uri}"<#if uri == precision>selected="selected"</#if>>${uri}</option>
</#list>
</select>

View file

@ -2,22 +2,24 @@
package edu.cornell.mannlib.vitro.webapp.edit.elements;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission;
@ -28,17 +30,24 @@ import freemarker.template.Configuration;
* datetime with precision and to convert he submitted parameters into
* varname -> Literal and varname -> URI maps.
*/
public class DateTimeWithPrecision implements EditElement {
public class DateTimeWithPrecision extends BaseEditElement {
private static final Log log = LogFactory.getLog(DateTimeWithPrecision.class);
private String TEMPATE_NAME = "DateTimeWithPrecision.ftl";
protected String[] PRECISIONS = {
"http://bogus.com/precision/none",
"http://bogus.com/precision/year",
"http://bogus.com/precision/month",
"http://bogus.com/precision/day",
"http://bogus.com/precision/hour",
"http://bogus.com/precision/minute",
"http://bogus.com/precision/second" };
@Override
public String draw(String fieldName, EditConfiguration editConfig,
EditSubmission editSub, Configuration fmConfig) {
Map map = getMapForTemplate(fieldName, editConfig, editSub);
map.putAll( FreemarkerHttpServlet.getDirectives());
// return (new FreemarkerHelper( fmConfig )).mergeMapToTemplate(TEMPATE_NAME, map);
return "problem with DateTimeWithPrecision"; // TODO - just so this will compile.
return merge( fmConfig, TEMPATE_NAME, map);
}
/**
@ -59,13 +68,6 @@ public class DateTimeWithPrecision implements EditElement {
map.put("precision", getPrecision(fieldName,editConfig,editSub));
Collection<String> possiblePrecisions = new ArrayList<String>();
possiblePrecisions.add("http://bogus.com/yearonly");
possiblePrecisions.add("http://bogus.com/yearMonth");
possiblePrecisions.add("http://bogus.com/yearMonthDay");
possiblePrecisions.add("http://bogus.com/YearMonthDayTime");
map.put("possiblePrecisions", possiblePrecisions);
//maybe we should put in empty validation errors to show what they would be?
//ex: map.put("year.error","");
@ -99,46 +101,110 @@ public class DateTimeWithPrecision implements EditElement {
return literalMap;
}
protected Literal getDateTime(String fieldName,
Map<String, String[]> queryParameters) {
Integer year = parseToInt(fieldName+".year", queryParameters);
Integer month = parseToInt(fieldName+".month", queryParameters);
if( month == null || month == 0 )
month = 1;
Integer day = parseToInt(fieldName+".day", queryParameters);
if( day == null || day == 0 )
day = 1;
Integer hour = parseToInt(fieldName+".hour", queryParameters);
if( hour == null )
hour = 0;
Integer minute = parseToInt(fieldName+".minute", queryParameters);
if( minute == null )
minute = 0;
Integer second = parseToInt(fieldName+".second", queryParameters);
if( second == null )
second = 0;
int mills = 0;
DateTime value = new DateTime(
year.intValue(),month.intValue(),day.intValue(),
hour.intValue(),minute.intValue(),second.intValue(),mills);
Date dValue = value.toDate();
/*This isn't doing what I want it to do. It is recording the correct instance of timeb
* but it is recording it with the timezone UTC/zulu */
//return ResourceFactory.createTypedLiteral(ISODateTimeFormat.dateTimeNoMillis().print(value),XSDDatatype.XSDdateTime);
Calendar c = Calendar.getInstance();
c.setTime(value.toDate());
Model m = ModelFactory.createDefaultModel();
Literal lit = m.createTypedLiteral( c );
return lit;
}
/**
* This gets the URIs for a submitted form from the queryParmeters.
* It will only be called if getValidationErrors() doesn't return any errors.
*/
@Override
public Map<String, String> getURIs(String fieldName,
EditConfiguration editConfig, Map<String, String[]> queryParameters) {
String precisionUri;
try {
precisionUri = getSubmittedPrecision( fieldName, queryParameters);
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("getURIS() should only be called on input that passed getValidationErrors()");
return Collections.emptyMap();
}
Map<String,String> uriMap = new HashMap<String,String>();
String precisionUri = getSubmittedPrecision( fieldName, queryParameters);
uriMap.put(fieldName+".precision", precisionUri);
return uriMap;
}
private Literal getDateTime(String fieldName,
Map<String, String[]> queryParameters) {
/**
* Precision is based on the values returned by the form. Throws an exception with
* the error message if the queryParameters cannot make a valid date/precision because
* there are values missing.
*/
protected String getSubmittedPrecision(String fieldName,
Map<String, String[]> queryParameters) throws Exception {
Integer year = parseToInt(fieldName+".year",queryParameters);
Integer month = parseToInt(fieldName+".month",queryParameters);
Integer day = parseToInt(fieldName+".day",queryParameters);
Integer hour = parseToInt(fieldName+".hour",queryParameters);
Integer minute = parseToInt(fieldName+".minute",queryParameters);
Integer second = parseToInt(fieldName+".second",queryParameters);
int mills = 0;
Integer[] values = { year, month, day, hour, minute, second };
DateTime value = new DateTime(
year.intValue(),month.intValue(),day.intValue(),
hour.intValue(),minute.intValue(),second.intValue(),mills);
return ResourceFactory.createTypedLiteral(value.toDate());
/* find the most significant date field that is null. */
int indexOfFirstNull= -1;
for(int i=0; i < values.length ; i++){
if( values[i] == null ){
indexOfFirstNull = i;
break;
}
}
private String getSubmittedPrecision(String fieldName,
Map<String, String[]> queryParameters) {
String rv= null;
String[] precisionUri = queryParameters.get(fieldName+".precision");
if( precisionUri != null && precisionUri.length > 0)
rv = precisionUri[0];
else
rv = null;
return rv;
/* if they all had values then we have seconds precision */
if( indexOfFirstNull == -1 )
return PRECISIONS[6];
/* check that there are no values after the most significant null field
* that are non-null. */
boolean nonNullAfterFirstNull=false;
for(int i=0; i < values.length ; i++){
if( i > indexOfFirstNull && values[i] != null ){
nonNullAfterFirstNull = true;
break;
}
}
if( nonNullAfterFirstNull )
throw new Exception("cannot determine precision, there were filledout values after the first un-filledout value, ");
else{
return PRECISIONS[ indexOfFirstNull ];
}
}
@Override
@ -162,6 +228,7 @@ public class DateTimeWithPrecision implements EditElement {
* This checks for invalid date times like "2010-02-31" or "2010-02-01T99:99:99".
*/
private Map<String,String> checkDate(String fieldName, Map<String, String[]> queryParameters){
//TODO
//see EditSubmission.getDateTime() for an example of checking for valid dates.
// Integer year,month,day,hour,minute,second;
@ -180,28 +247,25 @@ public class DateTimeWithPrecision implements EditElement {
return Collections.emptyMap();
}
private boolean hasNoneOrSingle(String key, Map<String, String[]> queryParameters){
String[] vt = queryParameters.get(key);
return vt == null || vt.length == 0 || vt.length==1;
}
private Integer parseToInt(String key,Map<String, String[]> queryParameters){
Integer out = null;
try{
String[] vt = queryParameters.get(key);
if( vt == null || vt.length ==0 || vt[0] == null)
out = null;
else
out = Integer.parseInt(vt[0]);
}catch(IndexOutOfBoundsException iex){
out = new Integer(0);
out = null;
}catch(NumberFormatException nfe){
out = new Integer(0);
out = null;
}catch(NullPointerException npe){
out = new Integer(0);
out = null;
}
if( out == null )
out = new Integer(0);
return out;
}
//
// /**
// * Create the var->value map for the datetimeprec.ftl template.

View file

@ -0,0 +1,201 @@
package edu.cornell.mannlib.vitro.webapp.edit.elements;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration;
public class DateTimeWithPrecisionTest extends DateTimeWithPrecision {
@Test
public void precisionSecondsValidationTest() throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
queryParameters.put(FIELDNAME+".month", new String[]{"12"});
queryParameters.put(FIELDNAME+".day", new String[]{"01"});
queryParameters.put(FIELDNAME+".hour", new String[]{"12"});
queryParameters.put(FIELDNAME+".minute", new String[]{"00"});
queryParameters.put(FIELDNAME+".second", new String[]{"00"});
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[6], precisionURI);
}
@Test
public void precisionMinutesValidationTest() throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
queryParameters.put(FIELDNAME+".month", new String[]{"12"});
queryParameters.put(FIELDNAME+".day", new String[]{"01"});
queryParameters.put(FIELDNAME+".hour", new String[]{"12"});
queryParameters.put(FIELDNAME+".minute", new String[]{"00"});
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[5], precisionURI);
}
@Test
public void precisionHourssValidationTest() throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
queryParameters.put(FIELDNAME+".month", new String[]{"12"});
queryParameters.put(FIELDNAME+".day", new String[]{"01"});
queryParameters.put(FIELDNAME+".hour", new String[]{"12"});
//no minutes
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[4], precisionURI);
}
@Test
public void precisionDaysValidationTest() throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
queryParameters.put(FIELDNAME+".month", new String[]{"12"});
queryParameters.put(FIELDNAME+".day", new String[]{"01"});
//no hours
//no minutes
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[3], precisionURI);
}
@Test
public void precisionMonthsValidationTest()throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
queryParameters.put(FIELDNAME+".month", new String[]{"12"});
//no days
//no hours
//no minutes
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[2], precisionURI);
}
@Test
public void precisionYearValidationTest() throws Exception{
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
//no months
//no days
//no hours
//no minutes
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
String precisionURI = null;
precisionURI = dtwp.getSubmittedPrecision(FIELDNAME, queryParameters);
Assert.assertNotNull(precisionURI);
Assert.assertEquals(dtwp.PRECISIONS[1], precisionURI);
}
@Test
public void getDateLiteralTest(){
String FIELDNAME = "testfield";
DateTimeWithPrecision dtwp = new DateTimeWithPrecision();
Map<String,String[]> queryParameters = new HashMap<String, String[]>();
queryParameters.put(FIELDNAME+".year", new String[]{"1999" });
//no months
//no days
//no hours
//no minutes
//no seconds
EditConfiguration editConfig=null;
Map<String,String> validationMsgs = dtwp.getValidationMessages("testfield", editConfig, queryParameters);
Assert.assertNotNull(validationMsgs);
Assert.assertTrue(validationMsgs.size() == 0 );
Literal date = dtwp.getDateTime(FIELDNAME, queryParameters);
Assert.assertNotNull(date);
Assert.assertEquals( XSDDatatype.XSDdateTime.getURI() ,date.getDatatypeURI() );
DateTime result = new DateTime( date.getLexicalForm() );
DateTime expected = new DateTime(1999,1,1,0,0,0,0);
Assert.assertEquals(expected, result);
Object obj = date.getValue();
Assert.assertNotNull(obj);
Assert.assertEquals(XSDDateTime.class, obj.getClass());
}
}

10
test/log4j.properties Normal file
View file

@ -0,0 +1,10 @@
#
# A simple Log4J configuration for unit tests.
#
# It's not very important, because the tests themselves will override this
# configuration in AbstractTestClass.initializeLogging().
#
log4j.rootLogger=WARN, AllAppender
log4j.appender.AllAppender=org.apache.log4j.ConsoleAppender
log4j.appender.AllAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AllAppender.layout.ConversionPattern=%p %t %c - %m%n