Subversion Repositories javautils

Rev

Rev 5 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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