001 package fr.isae.data;
002
003 import java.io.*;
004 import java.util.ArrayList;
005 import java.util.Iterator;
006 import javax.xml.parsers.DocumentBuilder;
007 import javax.xml.parsers.DocumentBuilderFactory;
008 import javax.xml.parsers.ParserConfigurationException;
009 import org.junit.*;
010 import static org.junit.Assert.*;
011 import org.w3c.dom.Document;
012 import org.w3c.dom.Element;
013 import org.w3c.dom.Node;
014 import org.w3c.dom.NodeList;
015 import org.xml.sax.SAXException;
016 import static org.junit.Assert.*;
017
018 /**
019 * Unit Test for class Acquisition.
020 *
021 *
022 * Created: Sat Nov 3 22:36:50 2013
023 *
024 * @author <a href="mailto:garion@isae.fr">Christophe Garion</a>
025 * @version 1.0
026 */
027 public class AcquisitionTest {
028
029 private Acquisition acqNormale;
030 private Acquisition acqChaine;
031 private Acquisition acqNegatif;
032 private Acquisition acqXML;
033
034 /**
035 * Setup for the tests.
036 */
037 @Before public void setUp() {
038
039 }
040
041 /**
042 * Cleanup for the tests.
043 */
044 @After public void tearDown() {
045
046 }
047
048 /**
049 * Test for a normal acquisition. The file is
050 * data/data1.xml
051 */
052 @Test public void testAcquisitionNormale() throws ElementXMLException,
053 ResponsableException, FormatDonneesException,
054 ParserConfigurationException, SAXException, IOException {
055 this.acqNormale = new Acquisition("data/data1.xml");
056
057 assertEquals("Allan Bonnet", this.acqNormale.getResponsable());
058
059 Iterator<Double> it = this.acqNormale.iterator();
060 int nb = 0;
061
062 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
063 DocumentBuilder db = null;
064 Document doc = null;
065
066 db = dbf.newDocumentBuilder();
067 doc = db.parse(new File("data/data1.xml"));
068
069 Element root = doc.getDocumentElement();
070
071 assertEquals("Allan Bonnet", root.getAttribute("resp-name"));
072
073 NodeList childNodes = root.getChildNodes();
074
075 for(int i = 0; i < childNodes.getLength(); i++) {
076 Node n = childNodes.item(i);
077 if (n.getNodeType() == Node.ELEMENT_NODE) {
078 assertEquals(it.next(), Double.parseDouble(n.getTextContent()), 1E-9);
079 nb++;
080 }
081 }
082
083 assertEquals(5, nb);
084 assertFalse(it.hasNext());
085 }
086
087 /**
088 * Test for an acquisition with a file containing a
089 * string. The file is data/data2.xml.
090 */
091 @Test(expected=fr.isae.data.FormatDonneesException.class)
092 public void testAcquisitionChaine() throws ElementXMLException,
093 ResponsableException, FormatDonneesException,
094 ParserConfigurationException, SAXException, IOException {
095 try {
096 this.acqChaine = new Acquisition("data/data2.xml");
097 } catch (FormatDonneesException e ) {
098 ArrayList<String> errors = e.getErrors();
099 assertTrue(errors.contains("reel ne represente pas un nombre"));
100
101 throw new FormatDonneesException("Erreur de format", null);
102 }
103 }
104
105 /**
106 * Test for an acquisition with a file containing a
107 * negative double. The file is data/data3.xml.
108 */
109 @Test(expected=fr.isae.data.FormatDonneesException.class)
110 public void testAcquisitionNegatif() throws ElementXMLException,
111 ResponsableException, FormatDonneesException,
112 ParserConfigurationException, SAXException, IOException {
113 try {
114 this.acqChaine = new Acquisition("data/data3.xml");
115 } catch (FormatDonneesException e ) {
116 ArrayList<String> errors = e.getErrors();
117 assertTrue(errors.contains("-5.43 n'est pas positif"));
118
119 throw new FormatDonneesException("Erreur de format", null);
120 }
121 }
122
123 /**
124 * Test for an acquisition with a file containing a
125 * root element that is not <code>experience</code>.
126 * The file is data/data4.xml.
127 */
128 @Test(expected=fr.isae.data.ElementXMLException.class)
129 public void testAcquisitionPressions() throws ElementXMLException,
130 ResponsableException, FormatDonneesException,
131 ParserConfigurationException, SAXException, IOException {
132 this.acqXML = new Acquisition("data/data4.xml");
133 }
134
135 /**
136 * Test for an acquisition with a file containing a
137 * element that is not <code>pression</code>.
138 * The file is data/data5.xml.
139 */
140 @Test(expected=fr.isae.data.ElementXMLException.class)
141 public void testAcquisitionPression() throws ElementXMLException,
142 ResponsableException, FormatDonneesException,
143 ParserConfigurationException, SAXException, IOException {
144 this.acqXML = new Acquisition("data/data5.xml");
145 }
146 }
|