Subversion Repositories javautils

Rev

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

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