Permalink
Browse files

added more validation of core types

  • Loading branch information...
1 parent 4b38406 commit 3ee453941fe47a457ba1b298d19b43ad6ec602c0 @scottbw committed Nov 25, 2011
@@ -19,7 +19,6 @@
*/
package org.xcri.common;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -0,0 +1,77 @@
+package org.xcri.core.presentation;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
+import java.util.logging.StreamHandler;
+
+import org.jdom.Document;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.junit.Test;
+import org.xcri.Namespaces;
+import org.xcri.core.Catalog;
+import org.xcri.core.Presentation;
+import org.xcri.exceptions.InvalidElementException;
+
+public class EndTest {
+
+ @Test
+ public void endNoText() throws InvalidElementException, JDOMException, IOException{
+ Logger logger = Logger.getLogger(Presentation.class.getName());
+
+ Formatter formatter = new SimpleFormatter();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Handler handler = new StreamHandler(out, formatter);
+ logger.addHandler(handler);
+
+ try {
+ Catalog catalog = new Catalog();
+ SAXBuilder builder = new SAXBuilder();
+ Document document = builder.build(new StringReader("<catalog xmlns=\""+Namespaces.XCRI_NAMESPACE+"\" xmlns:mlo=\""+Namespaces.MLO_NAMESPACE+"\" xmlns:dc=\""+Namespaces.DC_NAMESPACE+"\" xmlns:xsi=\""+Namespaces.XSI_NAMESPACE+"\"><provider><course><presentation><end></end></presentation></course></provider></catalog>"));
+ catalog.fromXml(document);
+
+ handler.flush();
+ String logMsg = out.toString();
+
+ assertNotNull(logMsg);
+ assertTrue(logMsg.contains("WARNING: presentation : skipping invalid end element: end: temporal element has no text content"));
+
+ } finally {
+ logger.removeHandler(handler);
+ }
+ }
+ @Test
+ public void endInvalidDtf() throws InvalidElementException, JDOMException, IOException{
+ Logger logger = Logger.getLogger(Presentation.class.getName());
+
+ Formatter formatter = new SimpleFormatter();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Handler handler = new StreamHandler(out, formatter);
+ logger.addHandler(handler);
+
+ try {
+ Catalog catalog = new Catalog();
+ SAXBuilder builder = new SAXBuilder();
+ Document document = builder.build(new StringReader("<catalog xmlns=\""+Namespaces.XCRI_NAMESPACE+"\" xmlns:mlo=\""+Namespaces.MLO_NAMESPACE+"\" xmlns:dc=\""+Namespaces.DC_NAMESPACE+"\" xmlns:xsi=\""+Namespaces.XSI_NAMESPACE+"\"><provider><course><presentation><end dtf=\"banana\">end</end></presentation></course></provider></catalog>"));
+ catalog.fromXml(document);
+
+ handler.flush();
+ String logMsg = out.toString();
+
+ assertNotNull(logMsg);
+ assertTrue(logMsg.contains("WARNING: presentation : skipping invalid end element: end: temporal element has @dtf attribute with invalid W3C-DTF date"));
+
+ } finally {
+ logger.removeHandler(handler);
+ }
+ }
+
+}
@@ -0,0 +1,77 @@
+package org.xcri.core.presentation;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
+import java.util.logging.StreamHandler;
+
+import org.jdom.Document;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.junit.Test;
+import org.xcri.Namespaces;
+import org.xcri.core.Catalog;
+import org.xcri.core.Presentation;
+import org.xcri.exceptions.InvalidElementException;
+
+public class StartTest {
+
+ @Test
+ public void startNoText() throws InvalidElementException, JDOMException, IOException{
+ Logger logger = Logger.getLogger(Presentation.class.getName());
+
+ Formatter formatter = new SimpleFormatter();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Handler handler = new StreamHandler(out, formatter);
+ logger.addHandler(handler);
+
+ try {
+ Catalog catalog = new Catalog();
+ SAXBuilder builder = new SAXBuilder();
+ Document document = builder.build(new StringReader("<catalog xmlns=\""+Namespaces.XCRI_NAMESPACE+"\" xmlns:mlo=\""+Namespaces.MLO_NAMESPACE+"\" xmlns:dc=\""+Namespaces.DC_NAMESPACE+"\" xmlns:xsi=\""+Namespaces.XSI_NAMESPACE+"\"><provider><course><presentation><mlo:start></mlo:start></presentation></course></provider></catalog>"));
+ catalog.fromXml(document);
+
+ handler.flush();
+ String logMsg = out.toString();
+
+ assertNotNull(logMsg);
+ assertTrue(logMsg.contains("WARNING: presentation : skipping invalid start element: start: temporal element has no text content"));
+
+ } finally {
+ logger.removeHandler(handler);
+ }
+ }
+ @Test
+ public void startInvalidDtf() throws InvalidElementException, JDOMException, IOException{
+ Logger logger = Logger.getLogger(Presentation.class.getName());
+
+ Formatter formatter = new SimpleFormatter();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Handler handler = new StreamHandler(out, formatter);
+ logger.addHandler(handler);
+
+ try {
+ Catalog catalog = new Catalog();
+ SAXBuilder builder = new SAXBuilder();
+ Document document = builder.build(new StringReader("<catalog xmlns=\""+Namespaces.XCRI_NAMESPACE+"\" xmlns:mlo=\""+Namespaces.MLO_NAMESPACE+"\" xmlns:dc=\""+Namespaces.DC_NAMESPACE+"\" xmlns:xsi=\""+Namespaces.XSI_NAMESPACE+"\"><provider><course><presentation><mlo:start dtf=\"banana\">start</mlo:start></presentation></course></provider></catalog>"));
+ catalog.fromXml(document);
+
+ handler.flush();
+ String logMsg = out.toString();
+
+ assertNotNull(logMsg);
+ assertTrue(logMsg.contains("WARNING: presentation : skipping invalid start element: start: temporal element has @dtf attribute with invalid W3C-DTF date"));
+
+ } finally {
+ logger.removeHandler(handler);
+ }
+ }
+
+}
@@ -126,8 +126,7 @@ public void fromXml(Element element) throws InvalidElementException {
presentation.setParent(this);
presentations.add(presentation);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn("course : presentation invalid, skipping");
}
}
this.setPresentations(presentations.toArray(new Presentation[presentations.size()]));
@@ -139,8 +138,7 @@ public void fromXml(Element element) throws InvalidElementException {
qualification.fromXml((Element)obj);
qualifications.add(qualification);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn("course : qualification invalid, skipping");
}
}
this.setQualifications(qualifications.toArray(new Qualification[qualifications.size()]));
@@ -19,6 +19,8 @@
*/
package org.xcri.core;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.jdom.Element;
import org.jdom.Namespace;
import org.xcri.Namespaces;
@@ -31,6 +33,8 @@
public class Presentation extends CommonDescriptiveType {
+ private Log log = LogFactory.getLog(Presentation.class);
+
private Start start;
private End end;
@@ -57,8 +61,7 @@ public void fromXml(Element element) throws InvalidElementException {
start.fromXml(element.getChild("start", Namespaces.MLO_NAMESPACE_NS));
this.setStart(start);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn("presentation : skipping invalid start element: "+e.getMessage());
}
}
if (element.getChild("end", Namespaces.XCRI_NAMESPACE_NS)!=null){
@@ -67,8 +70,7 @@ public void fromXml(Element element) throws InvalidElementException {
end.fromXml(element.getChild("end", Namespaces.XCRI_NAMESPACE_NS));
this.setEnd(end);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn("presentation : skipping invalid end element: "+e.getMessage());
}
}
}
@@ -82,8 +82,7 @@ public void fromXml(Element element) throws InvalidElementException {
}
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid contributor element: "+e.getMessage());
}
}
this.setContributors(contributors.toArray(new Contributor[contributors.size()]));
@@ -95,8 +94,7 @@ public void fromXml(Element element) throws InvalidElementException {
description.fromXml((Element)obj);
descriptions.add(description);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid description element: "+e.getMessage());
}
}
this.setDescriptions(descriptions.toArray(new Description[descriptions.size()]));
@@ -108,8 +106,7 @@ public void fromXml(Element element) throws InvalidElementException {
identifier.fromXml((Element)obj);
identifiers.add(identifier);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid identifier element: "+e.getMessage());
}
}
this.setIdentifiers(identifiers.toArray(new Identifier[identifiers.size()]));
@@ -121,8 +118,7 @@ public void fromXml(Element element) throws InvalidElementException {
title.fromXml((Element)obj);
titles.add(title);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid title element: "+e.getMessage());
}
}
//
@@ -135,7 +131,6 @@ public void fromXml(Element element) throws InvalidElementException {
log.warn("title : there SHOULD NOT be more than one occurrence of title per language tag.");
} else {
languages.add(title.getLang());
- System.out.println(title.getLang());
}
}
this.setTitles(titles.toArray(new Title[titles.size()]));
@@ -147,8 +142,7 @@ public void fromXml(Element element) throws InvalidElementException {
subject.fromXml((Element)obj);
subjects.add(subject);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid subject element: "+e.getMessage());
}
}
this.setSubjects(subjects.toArray(new Subject[subjects.size()]));
@@ -175,8 +169,7 @@ public void fromXml(Element element) throws InvalidElementException {
}
images.add(image);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid image element: "+e.getMessage());
}
}
this.setImages(images.toArray(new Image[images.size()]));
@@ -188,8 +181,7 @@ public void fromXml(Element element) throws InvalidElementException {
type.fromXml((Element)obj);
types.add(type);
} catch (InvalidElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn(this.getName()+" : skipping invalid type element: "+e.getMessage());
}
}
this.setTypes(types.toArray(new Type[types.size()]));
@@ -19,16 +19,12 @@
*/
package org.xcri.types;
-import java.text.DateFormat;
-import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import javax.xml.bind.DatatypeConverter;
-import javax.xml.datatype.XMLGregorianCalendar;
import org.jdom.Element;
-import org.jdom.Namespace;
import org.xcri.exceptions.InvalidElementException;
public class TemporalType extends XcriElement {
@@ -71,7 +67,7 @@ public void fromXml(Element element) throws InvalidElementException {
/*
* If a Temporal Element does not contain text content, an Aggregator MUST treat the element as in error and ignore the element.
*/
- if (this.getValue() == null) throw new InvalidElementException(this.getName()+": temporal element has no text content");
+ if (this.getValue() == null || this.getValue().trim().length() == 0) throw new InvalidElementException(this.getName()+": temporal element has no text content");
if (element.getAttribute("dtf") != null){
try {

0 comments on commit 3ee4539

Please sign in to comment.