001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestEmail.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.mail;
009:
010: import com.uwyn.rife.mail.Email;
011: import com.uwyn.rife.site.ValidationError;
012: import java.util.Iterator;
013: import junit.framework.TestCase;
014:
015: public class TestEmail extends TestCase {
016: public TestEmail(String name) {
017: super (name);
018: }
019:
020: public void testInstantiation() {
021: Email email = new Email();
022: assertEquals(-1, email.getId());
023: assertNull(email.getFromAddress());
024: assertNull(email.getToAddresses());
025: assertNull(email.getSubject());
026: assertNull(email.getBody());
027: assertNull(email.getCcAddresses());
028: assertNull(email.getBccAddresses());
029: assertFalse(email.getQueueFlag());
030: assertEquals(0, email.hashCode());
031:
032: assertEquals(email, new Email());
033: }
034:
035: public void testPopulation() {
036: Email email1 = new Email();
037: email1.setId(2);
038: email1.setFromAddress("info@uwyn.com");
039: email1.setToAddresses("test@uwyn.com,test2@uwyn.com");
040: email1
041: .setCcAddresses("test3@uwyn.com,test4@uwyn.com,test5@uwyn.com");
042: email1.setBccAddresses("test6@uwyn.com,test7@uwyn.com");
043: email1.setSubject("subject");
044: email1.setBody("body");
045:
046: assertEquals(2, email1.getId());
047: assertEquals("info@uwyn.com", email1.getFromAddress());
048: assertEquals("test@uwyn.com,test2@uwyn.com", email1
049: .getToAddresses());
050: assertEquals("test3@uwyn.com,test4@uwyn.com,test5@uwyn.com",
051: email1.getCcAddresses());
052: assertEquals("test6@uwyn.com,test7@uwyn.com", email1
053: .getBccAddresses());
054: assertEquals("subject", email1.getSubject());
055: assertEquals("body", email1.getBody());
056:
057: Email email2 = new Email();
058: email2.id(27).from("infob@uwyn.com").toAddresses(
059: "testb@uwyn.com").ccAddresses(
060: "test2b@uwyn.com,test3b@uwyn.com").bccAddresses(
061: "test4b@uwyn.com").subject("subjectb").body("bodyb");
062:
063: assertEquals(27, email2.getId());
064: assertEquals("infob@uwyn.com", email2.getFromAddress());
065: assertEquals("testb@uwyn.com", email2.getToAddresses());
066: assertEquals("test2b@uwyn.com,test3b@uwyn.com", email2
067: .getCcAddresses());
068: assertEquals("test4b@uwyn.com", email2.getBccAddresses());
069: assertEquals("subjectb", email2.getSubject());
070: assertEquals("bodyb", email2.getBody());
071:
072: email2.to("info2b@uwyn.com").cc("test5b@uwyn.com").bcc(
073: "test6b@uwyn.com");
074: email2.addTo("info3b@uwyn.net");
075: email2.addCc("info5b@uwyn.net");
076: email2.addBcc("info6b@uwyn.net");
077:
078: assertEquals(27, email2.getId());
079: assertEquals("infob@uwyn.com", email2.getFromAddress());
080: assertEquals("testb@uwyn.com,info2b@uwyn.com,info3b@uwyn.net",
081: email2.getToAddresses());
082: assertEquals(
083: "test2b@uwyn.com,test3b@uwyn.com,test5b@uwyn.com,info5b@uwyn.net",
084: email2.getCcAddresses());
085: assertEquals("test4b@uwyn.com,test6b@uwyn.com,info6b@uwyn.net",
086: email2.getBccAddresses());
087: assertEquals("subjectb", email2.getSubject());
088: assertEquals("bodyb", email2.getBody());
089: }
090:
091: public void testValidation() {
092: Email email = new Email();
093:
094: Iterator<ValidationError> validationerrors_it = null;
095: ValidationError validationerror = null;
096:
097: assertTrue(false == email.validate());
098:
099: validationerrors_it = email.getValidationErrors().iterator();
100: assertTrue(validationerrors_it.hasNext());
101: validationerror = validationerrors_it.next();
102: assertEquals("MANDATORY", validationerror.getIdentifier());
103: assertEquals("fromAddress", validationerror.getSubject());
104: assertTrue(validationerrors_it.hasNext());
105: validationerror = validationerrors_it.next();
106: assertEquals("MANDATORY", validationerror.getIdentifier());
107: assertEquals("toAddresses", validationerror.getSubject());
108: assertTrue(validationerrors_it.hasNext());
109: validationerror = validationerrors_it.next();
110: assertEquals("MANDATORY", validationerror.getIdentifier());
111: assertEquals("subject", validationerror.getSubject());
112: assertTrue(validationerrors_it.hasNext());
113: validationerror = validationerrors_it.next();
114: assertEquals("MANDATORY", validationerror.getIdentifier());
115: assertEquals("body", validationerror.getSubject());
116: assertTrue(false == validationerrors_it.hasNext());
117:
118: email.resetValidation();
119:
120: email.from("").toAddresses("").subject("").body("");
121:
122: assertTrue(false == email.validate());
123:
124: validationerrors_it = email.getValidationErrors().iterator();
125: assertTrue(validationerrors_it.hasNext());
126: validationerror = validationerrors_it.next();
127: assertEquals("MANDATORY", validationerror.getIdentifier());
128: assertEquals("fromAddress", validationerror.getSubject());
129: assertTrue(validationerrors_it.hasNext());
130: validationerror = validationerrors_it.next();
131: assertEquals("MANDATORY", validationerror.getIdentifier());
132: assertEquals("toAddresses", validationerror.getSubject());
133: assertTrue(validationerrors_it.hasNext());
134: validationerror = validationerrors_it.next();
135: assertEquals("MANDATORY", validationerror.getIdentifier());
136: assertEquals("subject", validationerror.getSubject());
137: assertTrue(validationerrors_it.hasNext());
138: validationerror = validationerrors_it.next();
139: assertEquals("MANDATORY", validationerror.getIdentifier());
140: assertEquals("body", validationerror.getSubject());
141: assertTrue(false == validationerrors_it.hasNext());
142:
143: email.resetValidation();
144:
145: email
146: .from(
147: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
148: .toAddresses(
149: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
150: .ccAddresses(
151: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
152: .bccAddresses(
153: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
154: .subject(
155: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
156: .body(
157: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345");
158:
159: assertTrue(false == email.validate());
160:
161: validationerrors_it = email.getValidationErrors().iterator();
162: assertTrue(validationerrors_it.hasNext());
163: validationerror = validationerrors_it.next();
164: assertEquals("WRONGLENGTH", validationerror.getIdentifier());
165: assertEquals("fromAddress", validationerror.getSubject());
166: assertTrue(validationerrors_it.hasNext());
167: validationerror = validationerrors_it.next();
168: assertEquals("WRONGLENGTH", validationerror.getIdentifier());
169: assertEquals("toAddresses", validationerror.getSubject());
170: assertTrue(validationerrors_it.hasNext());
171: validationerror = validationerrors_it.next();
172: assertEquals("WRONGLENGTH", validationerror.getIdentifier());
173: assertEquals("subject", validationerror.getSubject());
174: assertTrue(validationerrors_it.hasNext());
175: validationerror = validationerrors_it.next();
176: assertEquals("WRONGLENGTH", validationerror.getIdentifier());
177: assertEquals("ccAddresses", validationerror.getSubject());
178: assertTrue(validationerrors_it.hasNext());
179: validationerror = validationerrors_it.next();
180: assertEquals("WRONGLENGTH", validationerror.getIdentifier());
181: assertEquals("bccAddresses", validationerror.getSubject());
182: assertTrue(false == validationerrors_it.hasNext());
183: }
184:
185: public void testEquals() {
186: Email email1 = new Email();
187: email1.setId(2);
188: email1.setFromAddress("info@uwyn.com");
189: email1.setToAddresses("test@uwyn.com");
190: email1.setCcAddresses("test2@uwyn.com");
191: email1.setBccAddresses("test3@uwyn.com");
192: email1.setSubject("subject");
193: email1.setBody("body");
194:
195: Email email2 = new Email();
196: email2.setId(2);
197: email2.setFromAddress("info@uwyn.com");
198: email2.setToAddresses("test@uwyn.com");
199: email2.setCcAddresses("test2@uwyn.com");
200: email2.setBccAddresses("test3@uwyn.com");
201: email2.setSubject("subject");
202: email2.setBody("body");
203:
204: assertEquals(email1, email2);
205:
206: email1.setId(3);
207: assertTrue(email1.equals(email2));
208:
209: email1.setQueueFlag(true);
210: assertTrue(email1.equals(email2));
211:
212: email1.setFromAddress("infob@uwyn.com");
213: assertFalse(email1.equals(email2));
214: email2.setFromAddress("infob@uwyn.com");
215: assertTrue(email1.equals(email2));
216:
217: email1.setToAddresses("testb@uwyn.com");
218: assertFalse(email1.equals(email2));
219: email2.setToAddresses("testb@uwyn.com");
220: assertTrue(email1.equals(email2));
221:
222: email1.setCcAddresses("test2b@uwyn.com");
223: assertFalse(email1.equals(email2));
224: email2.setCcAddresses("test2b@uwyn.com");
225: assertTrue(email1.equals(email2));
226:
227: email1.setBccAddresses("test3b@uwyn.com");
228: assertFalse(email1.equals(email2));
229: email2.setBccAddresses("test3b@uwyn.com");
230: assertTrue(email1.equals(email2));
231:
232: email1.setSubject("subject1");
233: assertFalse(email1.equals(email2));
234: email2.setSubject("subject1");
235: assertTrue(email1.equals(email2));
236:
237: email1.setBody("body1");
238: assertFalse(email1.equals(email2));
239: email2.setBody("body1");
240: assertTrue(email1.equals(email2));
241: }
242:
243: public void testHashcode() {
244: Email email1 = new Email();
245: email1.setId(2);
246: email1.setFromAddress("info@uwyn.com");
247: email1.setToAddresses("test@uwyn.com");
248: email1.setCcAddresses("test2@uwyn.com");
249: email1.setBccAddresses("test3@uwyn.com");
250: email1.setSubject("subject");
251: email1.setBody("body");
252:
253: Email email2 = new Email();
254: email2.setId(2);
255: email2.setFromAddress("info@uwyn.com");
256: email2.setToAddresses("test@uwyn.com");
257: email2.setCcAddresses("test2@uwyn.com");
258: email2.setBccAddresses("test3@uwyn.com");
259: email2.setSubject("subject");
260: email2.setBody("body");
261:
262: assertTrue(email1.hashCode() == email2.hashCode());
263:
264: email1.setId(3);
265: assertTrue(email1.hashCode() == email2.hashCode());
266:
267: email1.setQueueFlag(true);
268: assertTrue(email1.hashCode() == email2.hashCode());
269:
270: email1.setFromAddress("infob@uwyn.com");
271: assertFalse(email1.hashCode() == email2.hashCode());
272: email2.setFromAddress("infob@uwyn.com");
273: assertTrue(email1.hashCode() == email2.hashCode());
274:
275: email1.setToAddresses("testb@uwyn.com");
276: assertFalse(email1.hashCode() == email2.hashCode());
277: email2.setToAddresses("testb@uwyn.com");
278: assertTrue(email1.hashCode() == email2.hashCode());
279:
280: email1.setCcAddresses("test2b@uwyn.com");
281: assertFalse(email1.hashCode() == email2.hashCode());
282: email2.setCcAddresses("test2b@uwyn.com");
283: assertTrue(email1.hashCode() == email2.hashCode());
284:
285: email1.setBccAddresses("test3b@uwyn.com");
286: assertFalse(email1.hashCode() == email2.hashCode());
287: email2.setBccAddresses("test3b@uwyn.com");
288: assertTrue(email1.hashCode() == email2.hashCode());
289:
290: email1.setSubject("subject1");
291: assertFalse(email1.hashCode() == email2.hashCode());
292: email2.setSubject("subject1");
293: assertTrue(email1.hashCode() == email2.hashCode());
294:
295: email1.setBody("body1");
296: assertFalse(email1.hashCode() == email2.hashCode());
297: email2.setBody("body1");
298: assertTrue(email1.hashCode() == email2.hashCode());
299: }
300: }
|