Subversion Repositories javautils

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. package com.iamcal.rfc3696;
  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 RFC3696EmailParserTest {
  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 = RFC3696EmailParser.isValidEmailAddress(address);
  71.  
  72.                                 // assertEquals(expected, actual);
  73.                                 if (expected != actual) {
  74.                                         System.err.println("Mail Test #" + id + " FAILED! '"
  75.                                                         + address + "' is '" + actual + "' instead of '"
  76.                                                         + expected + "'!");
  77.                                         errorCount++;
  78.                                 }
  79.                         }
  80.                 }
  81.         }
  82.  
  83.         @Test
  84.         public void performXMLTests() throws SAXException, IOException,
  85.                         ParserConfigurationException {
  86.  
  87.                 // First: Null-Pointer Test
  88.                
  89.                 RFC3696EmailParser.isValidEmailAddress(null);
  90.                
  91.                 // Now check the XML testcases
  92.                
  93.                 checkXML("test/eMailTests/SayersTests.xml");
  94.                 checkXML("test/eMailTests/ExperimentalTests.xml");
  95.  
  96.                 if (errorCount > 0) {
  97.                         System.err.println("==> " + errorCount + " ERRORS OCCOURED! <==");
  98.                         fail();
  99.                 }
  100.         }
  101. }
  102.