Subversion Repositories javautils

Rev

Rev 9 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 daniel-mar 1
package de.viathinksoft.utils.mail.syntaxchecker;
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
 
17 daniel-mar 19
import de.viathinksoft.utils.mail.address.EMailAddress;
9 daniel-mar 20
 
21
public class MailSyntaxCheckerTest {
22
 
23
        static int errorCount;
24
 
25
        private void checkXML(String xmlFile) throws ParserConfigurationException, SAXException, IOException {
26
                File file = new File(xmlFile);
27
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
28
                DocumentBuilder db = dbf.newDocumentBuilder();
29
                Document doc = db.parse(file);
30
                doc.getDocumentElement().normalize();
31
                NodeList nodeLst = doc.getElementsByTagName("test");
32
 
33
                for (int s = 0; s < nodeLst.getLength(); s++) {
34
 
35
                        Node fstNode = nodeLst.item(s);
36
 
37
                        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
38
 
39
                                Element fstElmnt = (Element) fstNode;
40
 
41
                                String id;
42
                                String address;
43
                                boolean expected;
44
 
45
                                NodeList fstNmElmntLst;
46
                                Element fstNmElmnt;
47
                                NodeList fstNm;
48
                                String cont;
49
 
50
                                fstNmElmntLst = fstElmnt.getElementsByTagName("address");
51
                                fstNmElmnt = (Element) fstNmElmntLst.item(0);
52
                                fstNm = fstNmElmnt.getChildNodes();
53
                                try {
54
                                        cont = ((Node) fstNm.item(0)).getNodeValue();
55
                                } catch (NullPointerException e) {
56
                                        cont = "";
57
                                }
58
                                address = cont;
59
 
60
                                fstNmElmntLst = fstElmnt.getElementsByTagName("valid");
61
                                fstNmElmnt = (Element) fstNmElmntLst.item(0);
62
                                fstNm = fstNmElmnt.getChildNodes();
63
                                cont = ((Node) fstNm.item(0)).getNodeValue();
64
                                expected = Boolean.parseBoolean(cont);
65
 
66
                                fstNmElmntLst = fstElmnt.getElementsByTagName("id");
67
                                fstNmElmnt = (Element) fstNmElmntLst.item(0);
68
                                fstNm = fstNmElmnt.getChildNodes();
69
                                cont = ((Node) fstNm.item(0)).getNodeValue();
70
                                id = cont;
71
 
72
                                boolean actual = MailSyntaxChecker.isMailValid(address);
73
 
74
                                // assertEquals(expected, actual);
75
                                if (expected != actual) {
76
                                        System.err.println("Mail Test #" + id + " FAILED! '"
77
                                                        + address + "' is '" + actual + "' instead of '"
78
                                                        + expected + "'!");
79
                                        errorCount++;
80
                                }
81
                        }
82
                }
83
        }
84
 
85
        @Test
86
        public void performXMLTests() throws SAXException, IOException,
87
                        ParserConfigurationException {
88
 
89
                // First: Null-Pointer Test 
90
 
91
                try {
92
                        MailSyntaxChecker.isMailValid((String)null);
93
                        fail();
94
                } catch (NullPointerException e) {
95
                }
96
 
97
                try {
98
                        MailSyntaxChecker.isMailValid((EMailAddress)null);
99
                        fail();
100
                } catch (NullPointerException e) {
101
                }
102
 
103
                // Now check the XML testcases
104
 
105
                checkXML("test/eMailTests/SayersTests.xml");
106
                checkXML("test/eMailTests/emailExperimentalTests.xml");
107
 
108
                if (errorCount > 0) {
109
                        System.err.println("==> " + errorCount + " ERRORS OCCOURED! <==");
110
                        fail();
111
                }
112
        }
113
}