Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5 | daniel-mar | 1 | package com.dominicsayers.isemail; |
2 | |||
3 | import static org.junit.Assert.*; |
||
4 | |||
5 | import java.io.File; |
||
6 | import java.io.IOException; |
||
7 | |||
8 | import javax.xml.parsers.DocumentBuilder; |
||
9 | import javax.xml.parsers.DocumentBuilderFactory; |
||
10 | import javax.xml.parsers.ParserConfigurationException; |
||
11 | |||
12 | import org.junit.Test; |
||
13 | import org.w3c.dom.Document; |
||
14 | import org.w3c.dom.Element; |
||
15 | import org.w3c.dom.Node; |
||
16 | import org.w3c.dom.NodeList; |
||
17 | import org.xml.sax.SAXException; |
||
18 | |||
19 | public class IsEMailTest { |
||
20 | |||
21 | static int errorCount; |
||
22 | |||
23 | private void checkXML(String xmlFile) throws ParserConfigurationException, SAXException, IOException { |
||
24 | File file = new File(xmlFile); |
||
25 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
||
26 | DocumentBuilder db = dbf.newDocumentBuilder(); |
||
27 | Document doc = db.parse(file); |
||
28 | doc.getDocumentElement().normalize(); |
||
29 | NodeList nodeLst = doc.getElementsByTagName("test"); |
||
30 | |||
31 | for (int s = 0; s < nodeLst.getLength(); s++) { |
||
32 | |||
33 | Node fstNode = nodeLst.item(s); |
||
34 | |||
35 | if (fstNode.getNodeType() == Node.ELEMENT_NODE) { |
||
36 | |||
37 | Element fstElmnt = (Element) fstNode; |
||
38 | |||
39 | String id; |
||
40 | String address; |
||
41 | boolean expected; |
||
42 | |||
43 | NodeList fstNmElmntLst; |
||
44 | Element fstNmElmnt; |
||
45 | NodeList fstNm; |
||
46 | String cont; |
||
47 | |||
48 | fstNmElmntLst = fstElmnt.getElementsByTagName("address"); |
||
49 | fstNmElmnt = (Element) fstNmElmntLst.item(0); |
||
50 | fstNm = fstNmElmnt.getChildNodes(); |
||
51 | try { |
||
52 | cont = ((Node) fstNm.item(0)).getNodeValue(); |
||
53 | } catch (NullPointerException e) { |
||
54 | cont = ""; |
||
55 | } |
||
56 | address = cont; |
||
57 | |||
58 | fstNmElmntLst = fstElmnt.getElementsByTagName("valid"); |
||
59 | fstNmElmnt = (Element) fstNmElmntLst.item(0); |
||
60 | fstNm = fstNmElmnt.getChildNodes(); |
||
61 | cont = ((Node) fstNm.item(0)).getNodeValue(); |
||
62 | expected = Boolean.parseBoolean(cont); |
||
63 | |||
64 | fstNmElmntLst = fstElmnt.getElementsByTagName("id"); |
||
65 | fstNmElmnt = (Element) fstNmElmntLst.item(0); |
||
66 | fstNm = fstNmElmnt.getChildNodes(); |
||
67 | cont = ((Node) fstNm.item(0)).getNodeValue(); |
||
68 | id = cont; |
||
69 | |||
70 | boolean actual = IsEMail.is_email(address); |
||
71 | EMailSyntaxDiagnosis diagnosis = IsEMail.is_email_diagnosis(address); |
||
72 | |||
73 | // assertEquals(expected, actual); |
||
74 | if (expected != actual) { |
||
75 | System.err.println("Mail Test #" + id + " FAILED! '" |
||
76 | + address + "' is '" + actual + "' ('" + diagnosis + "') instead of '" |
||
77 | + expected + "'!"); |
||
78 | errorCount++; |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | @Test |
||
85 | public void performXMLTests() throws SAXException, IOException, |
||
86 | ParserConfigurationException { |
||
87 | |||
88 | // First: Null-Pointer Test |
||
89 | |||
90 | IsEMail.is_email(null); |
||
91 | |||
92 | // Now check the XML testcases |
||
93 | |||
9 | daniel-mar | 94 | checkXML("test/eMailTests/SayersTests.xml"); |
95 | checkXML("test/eMailTests/ExperimentalTests.xml"); |
||
5 | daniel-mar | 96 | |
97 | if (errorCount > 0) { |
||
98 | System.err.println("==> " + errorCount + " ERRORS OCCOURED! <=="); |
||
99 | fail(); |
||
100 | } |
||
101 | } |
||
102 | } |