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.user;
22  
23  import java.util.GregorianCalendar;
24  
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.ParserConfigurationException;
27  import javax.xml.transform.Transformer;
28  import javax.xml.transform.TransformerException;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.TransformerFactoryConfigurationError;
31  import javax.xml.transform.dom.DOMSource;
32  import javax.xml.transform.stream.StreamResult;
33  
34  import junit.framework.TestCase;
35  
36  import org.w3c.dom.Document;
37  
38  import base.user.EAddress;
39  import base.user.NAddress;
40  import base.user.PhoneNumber;
41  import base.user.User;
42  import base.user.UserFactory;
43  
44  public class UserTest extends TestCase {
45  
46  	User user = null;
47  
48  	NAddress nAddress = null;
49  
50  	EAddress eAddress = null;
51  
52  	PhoneNumber phoneNumber = null;
53  
54  	@Override
55  	protected void setUp() throws Exception {
56  		super.setUp();
57  		/*
58  		 * String name, String surname, String gender, Date birthday, String
59  		 * nickname, String password, String credential, Document document,
60  		 * String imagePath)
61  		 */
62  		user = UserFactory.newUser("Guido Angelo", "Ingenito", "Male",
63  				new GregorianCalendar(1981, 12, 24).getTime(), "skunk",
64  				"password", "Administrator", null, "");
65  		/*
66  		 * User user, String type, String number, String authority, String
67  		 * description, Date release, Date expiration, String imagePath
68  		 */
69  		user.setDocument(new base.user.Document("Identity Card", "0123456789",
70  				"City Council", "A description", new GregorianCalendar(2004,
71  						05, 01).getTime(), new GregorianCalendar(2014, 05, 01)
72  						.getTime(), ""));
73  	}
74  
75  	/*
76  	 * Test method for 'base.user.User.buildUserPassword(String)'
77  	 */
78  	public void testBuildUserPassword() {
79  		String passwordDigest = User.buildUserPassword("password");
80  		assertTrue(passwordDigest != null && !passwordDigest.equals(""));
81  		assertEquals(user.getPassword(), passwordDigest);
82  	}
83  
84  	/*
85  	 * Test method for 'base.user.User.newNAddress(String, String, String,
86  	 * String, String, String)'
87  	 */
88  	public void testNewNAddress() {
89  		NAddress nAddress = user.newNAddress("Salerno", "Italy", "A Street",
90  				"A Region", "A Postal Code", "A Description");
91  		assertEquals(nAddress.getId(), 0);
92  		user.addNAddress(nAddress);
93  		assertTrue(nAddress.getId() >= 0);
94  		assertEquals(user.getNAddress().length, 1);
95  	}
96  
97  	/*
98  	 * Test method for 'base.user.User.newEAddress(String)'
99  	 */
100 	public void testNewEAddress() {
101 		eAddress = user.newEAddress("userName@hostName.domainName");
102 		eAddress.setDescription("This is a test description...");
103 		assertEquals(eAddress.getId(), 0);
104 		user.addEAddress(eAddress);
105 		assertTrue(eAddress.getId() >= 0);
106 		assertEquals(user.getEAddress().length, 1);
107 
108 	}
109 
110 	/*
111 	 * Test method for 'base.user.User.newPhoneNumber(String, String, String,
112 	 * String)'
113 	 */
114 	public void testNewPhoneNumber() {
115 		phoneNumber = user.newPhoneNumber("0039", "51", "307602",
116 				"My House's Phone Number");
117 		assertEquals(phoneNumber.getId(), 0);
118 		user.addPhoneNumber(phoneNumber);
119 		assertTrue(phoneNumber.getId() >= 0);
120 		assertEquals(user.getPhoneNumber().length, 1);
121 
122 	}
123 
124 	/*
125 	 * Test method for 'base.user.User.deleteNAddress(NAddress)'
126 	 */
127 	public void testDeleteNAddress() {
128 		user.deleteNAddress(nAddress);
129 		assertEquals(user.getNAddress().length, 0);
130 	}
131 
132 	/*
133 	 * Test method for 'base.user.User.deleteEAddress(NAddress)'
134 	 */
135 	public void testDeleteEAddress() {
136 		user.deleteEAddress(eAddress);
137 		assertEquals(user.getEAddress().length, 0);
138 	}
139 
140 	/*
141 	 * Test method for 'base.user.User.deletePhoneNumber(PhoneNumber)'
142 	 */
143 	public void testDeletePhoneNumber() {
144 		user.deletePhoneNumber(phoneNumber);
145 		assertEquals(user.getPhoneNumber().length, 0);
146 	}
147 
148 	/*
149 	 * Test method for 'base.user.User.toXml(org.w3c.dom.Document)'
150 	 */
151 	public void testToXml() throws ParserConfigurationException,
152 			TransformerFactoryConfigurationError, TransformerException {
153 		testNewNAddress();
154 		testNewEAddress();
155 		testNewPhoneNumber();
156 		Document document = DocumentBuilderFactory.newInstance()
157 				.newDocumentBuilder().newDocument();
158 		document.appendChild(user.toXml(document));
159 		Transformer transformer = TransformerFactory.newInstance()
160 				.newTransformer();
161 		DOMSource source = new DOMSource(document);
162 		StreamResult streamResult = new StreamResult(System.out);
163 		transformer.transform(source, streamResult);
164 	}
165 }