1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  
21  package test.base;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.PrintStream;
28  
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  import javax.xml.transform.Transformer;
33  import javax.xml.transform.TransformerException;
34  import javax.xml.transform.TransformerFactory;
35  import javax.xml.transform.TransformerFactoryConfigurationError;
36  import javax.xml.transform.dom.DOMSource;
37  import javax.xml.transform.stream.StreamResult;
38  
39  import junit.framework.TestCase;
40  
41  import org.w3c.dom.Document;
42  import org.xml.sax.SAXException;
43  
44  import base.ConfigurationManager;
45  
46  public class ConfigurationManagerTest extends TestCase {
47  
48  	/*
49  	 * Test method for 'test.base.ConfigurationManager.fromXml(Document)'
50  	 */
51  	public void testFromXml() throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
52  		File configurationFile = new File(ConfigurationManager.CONFIGURATION_FILE);
53  		if (configurationFile.exists()) {
54  			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
55  			factory.setIgnoringComments(true);
56  			factory.setValidating(false);
57  			factory.setIgnoringElementContentWhitespace(true);
58  			DocumentBuilder docBuilder = factory.newDocumentBuilder();
59  
60  			Document document = docBuilder.parse(new FileInputStream(configurationFile));
61  			ConfigurationManager.getInstance().fromXml(document);
62  			assertTrue(document != null);
63  		}
64  	}
65  
66  	/*
67  	 * Test method for 'test.base.ConfigurationManager.toXml(Document)'
68  	 */
69  	public void testToXml() throws ParserConfigurationException,TransformerFactoryConfigurationError, FileNotFoundException, TransformerException {
70  		File configurationFile = new File(ConfigurationManager.CONFIGURATION_FILE);
71  		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
72  		doc.appendChild(ConfigurationManager.getInstance().toXml(doc));
73  		String fileName = configurationFile.getAbsolutePath();
74  		if (!fileName.endsWith(".xml"))
75  			fileName += ".xml";
76  		Transformer transformer = TransformerFactory.newInstance().newTransformer();
77  		DOMSource source = new DOMSource(doc);
78  		StreamResult streamResult = new StreamResult(new PrintStream(fileName));
79  		transformer.transform(source, streamResult);
80  		assertTrue(configurationFile.exists());
81  	}
82  
83  }