validate in 'relaxed folding' mode
[ical4j.git] / test / net / fortuna / ical4j / validation / ValidationBySchemaTest.java
blobc774493583fe768228351c7f1e2dc4bc4f3fde24
1 package net.fortuna.ical4j.validation;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.util.zip.ZipEntry;
7 import java.util.zip.ZipInputStream;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
13 import net.fortuna.ical4j.data.CalendarBuilder;
14 import net.fortuna.ical4j.data.ParserException;
15 import net.fortuna.ical4j.model.ValidationException;
16 import net.fortuna.ical4j.util.CompatibilityHints;
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24 import org.xml.sax.SAXException;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
29 /**
30 * Checks whether the ical4j validation correctly catches the problems and
31 * passes the valid calendars from the icalvalidSchema collection from
32 * http://icalvalid.wikidot.com/
34 * @author arnouten
36 public class ValidationBySchemaTest extends TestCase {
37 private static final Log LOG = LogFactory
38 .getLog(ValidationBySchemaTest.class);
39 private static final int BUFFER = 2048;
43 /* (non-Javadoc)
44 * @see junit.framework.TestCase#setUp()
46 protected void setUp() throws Exception {
47 CompatibilityHints.setHintEnabled(
48 CompatibilityHints.KEY_RELAXED_UNFOLDING, true);
49 super.setUp();
52 /* (non-Javadoc)
53 * @see junit.framework.TestCase#tearDown()
55 protected void tearDown() throws Exception {
56 CompatibilityHints.clearHintEnabled(
57 CompatibilityHints.KEY_RELAXED_UNFOLDING);
58 super.tearDown();
61 private String name;
62 private String textContent;
63 private boolean shouldPass;
65 public ValidationBySchemaTest(String testMethod, String name, boolean shouldPass,
66 String textContent) {
67 super(testMethod);
68 this.name = name;
69 this.shouldPass = shouldPass;
70 this.textContent = textContent;
73 private static void addToSuite(TestSuite suite, ZipEntry entry, ZipInputStream zip)
74 throws IOException, SAXException, ParserConfigurationException
76 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
78 DocumentBuilder db = dbf.newDocumentBuilder();
80 ByteArrayOutputStream dest = new ByteArrayOutputStream();
81 int count;
82 byte data[] = new byte[BUFFER];
84 while ((count = zip.read(data, 0, BUFFER)) != -1) {
85 dest.write(data, 0, count);
87 dest.flush();
88 dest.close();
90 Document dom = db.parse(new ByteArrayInputStream(dest.toByteArray()));
92 addToSuite(suite, entry.getName(), dom);
95 private static void addToSuite(TestSuite suite, String name, Document dom) throws IOException {
96 LOG.info("Looking at " + name);
98 Node rule = dom.getFirstChild();
99 NodeList nodes = rule.getChildNodes();
100 for (int i = 0; i < nodes.getLength(); i++)
102 Node node = nodes.item(i);
103 String textContent = StringUtils.trim(node.getTextContent());
104 if ("pass".equals(node.getNodeName()) || "fail".equals(node.getNodeName()))
106 suite.addTest(new ValidationBySchemaTest("testValidateBySchema", name, "pass".equals(node.getNodeName()), textContent));
111 public void testValidateBySchema() throws IOException
113 boolean passed;
116 new CalendarBuilder().build(new ByteArrayInputStream(textContent.getBytes())).validate(true);
117 passed = true;
119 catch (ParserException e)
121 passed = false;
122 } catch (ValidationException e) {
123 passed = false;
125 assertEquals("Test result inconsistent for rule " + name, shouldPass, !passed);
128 public static TestSuite suite() throws IOException, SAXException, ParserConfigurationException {
129 TestSuite suite = new TestSuite();
131 ZipInputStream zip = new ZipInputStream(ValidationBySchemaTest.class
132 .getResourceAsStream("/icalvalidSchema_0_4.zip"));
134 ZipEntry entry = zip.getNextEntry();
136 while (entry != null) {
137 if (!entry.isDirectory() && entry.getName().startsWith("icalvalidSchema/rules/"))
139 addToSuite(suite, entry, zip);
141 entry = zip.getNextEntry();
144 return suite;