📄 emailtest.java
字号:
htBad.put("X-Mailer", ""); htBad.put("X-Priority", ""); htBad.put("", "me@home.com"); Map arrExpected = new Hashtable(); for (Iterator items = htBad.entrySet().iterator(); items.hasNext();) { Map.Entry element = (Map.Entry) items.next(); try { String strName = (String) element.getKey(); String strValue = (String) element.getValue(); this.email.addHeader(strName, strValue); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { assertTrue(true); } } assertEquals(arrExpected.size(), this.email.getHeaders().size()); assertEquals( arrExpected.toString(), this.email.getHeaders().toString()); } /** */ public void testSetHeaders() { // ==================================================================== // Test Success // ==================================================================== Map ht = new Hashtable(); ht.put("X-Priority", "1"); ht.put("Disposition-Notification-To", "me@home.com"); ht.put("X-Mailer", "Sendmail"); this.email.setHeaders(ht); assertEquals(ht.size(), this.email.getHeaders().size()); assertEquals(ht, this.email.getHeaders()); } /** */ public void testSetHeadersEx() { // ==================================================================== // Test Exceptions // ==================================================================== // first test Map htBad = new Hashtable(); htBad.put("X-Mailer", ""); Map arrExpected = new Hashtable(); try { this.email.setHeaders(htBad); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { assertTrue(true); } assertEquals(arrExpected.size(), this.email.getHeaders().size()); assertEquals( arrExpected.toString(), this.email.getHeaders().toString()); // ==================================================================== // second test htBad = new Hashtable(); htBad.put("", "me@home.com"); try { this.email.setHeaders(htBad); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { assertTrue(true); } assertEquals(arrExpected.size(), this.email.getHeaders().size()); assertEquals( arrExpected.toString(), this.email.getHeaders().toString()); } /** */ public void testSetSubject() { for (int i = 0; i < testCharsValid.length; i++) { this.email.setSubject(testCharsValid[i]); assertEquals(testCharsValid[i], this.email.getSubject()); } } /** */ public void testSendEx() { // ==================================================================== // Test Exceptions (in getMailSession) // ==================================================================== // hostname not set try { this.getMailServer(); this.email = new MockEmailConcrete(); this.email.send(); fail("Should have thrown an exception"); } catch (EmailException e) { this.fakeMailServer.stop(); assertTrue(true); } // bad hostname try { this.getMailServer(); this.email = new MockEmailConcrete(); this.email.setSubject("Test Email #1 Subject"); this.email.setHostName("bad.host.com"); this.email.setFrom("me@home.com"); this.email.addTo("me@home.com"); this.email.addCc("me@home.com"); this.email.addBcc("me@home.com"); this.email.addReplyTo("me@home.com"); this.email.setContent( "test string object", " ; charset=" + Email.US_ASCII); this.email.send(); fail("Should have thrown an exception"); } catch (EmailException e) { assertTrue(e.getCause() instanceof ParseException); this.fakeMailServer.stop(); assertTrue(true); } // ==================================================================== // Test Exceptions (in send) // ==================================================================== // from add not set try { this.getMailServer(); this.email = new MockEmailConcrete(); this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.send(); fail("Should have thrown an exception"); } catch (EmailException e) { this.fakeMailServer.stop(); assertTrue(true); } // destination (to/cc/bcc) dd not set try { this.getMailServer(); this.email = new MockEmailConcrete(); this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.setFrom("me@home.com"); this.email.send(); fail("Should have thrown an exception"); } catch (EmailException e) { this.fakeMailServer.stop(); assertTrue(true); } // bad auth set try { this.getMailServer(); this.email = new MockEmailConcrete(); this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.setFrom(this.strTestMailFrom); this.email.addTo(this.strTestMailTo); this.email.setAuthentication(null, null); this.email.send(); fail("Should have thrown an exception"); } catch (EmailException e) { this.fakeMailServer.stop(); assertTrue(true); } } /** */ public void testGetSetSentDate() { // with input date Date dtTest = Calendar.getInstance().getTime(); this.email.setSentDate(dtTest); assertEquals(dtTest, this.email.getSentDate()); // with null input (this is a fudge :D) this.email.setSentDate(null); Date sentDate = this.email.getSentDate(); // Date objects are millisecond specific. If you have a slow processor, // time passes between the generation of dtTest and the new Date() in // getSentDate() and this test fails. Make sure that the difference // is less than a second... assertTrue(Math.abs(sentDate.getTime() - dtTest.getTime()) < 1000); } /** * @throws EmailException when there are problems adding an address * @throws UnsupportedEncodingException on bad email addresses */ public void testToInternetAddressArray() throws EmailException, UnsupportedEncodingException { List testInetEmailValid = new ArrayList(); testInetEmailValid.add(new InternetAddress("me@home.com", "Name1")); testInetEmailValid.add( new InternetAddress( "joe.doe@apache.org", "joe.doe@apache.org")); testInetEmailValid.add( new InternetAddress( "someone_here@work-address.com.au", "someone_here@work-address.com.au")); this.email.setBcc(testInetEmailValid); assertEquals( testInetEmailValid.size(), this.email.toInternetAddressArray( this.email.getBccList()).length); } /** */ public void testSetPopBeforeSmtp() { // simple test (can be improved) boolean boolPopBeforeSmtp = true; String strHost = "mail.home.com"; String strUsername = "user.name"; String strPassword = "user.passwd"; this.email.setPopBeforeSmtp( boolPopBeforeSmtp, strHost, strUsername, strPassword); // retrieve and verify assertEquals(boolPopBeforeSmtp, this.email.isPopBeforeSmtp()); assertEquals(strHost, this.email.getPopHost()); assertEquals(strUsername, this.email.getPopUsername()); assertEquals(strPassword, this.email.getPopPassword()); } /** * Test: When Email.setCharset() is called, a subsequent setContent() * should use that charset for text content types unless overridden * by the contentType parameter. * See https://issues.apache.org/jira/browse/EMAIL-1. * * * Case 1: * Setting a default charset results in adding that charset info to * to the content type of a text/based content object. * @throws Exception on any error */ public void testDefaultCharsetAppliesToTextContent() throws Exception { this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.setFrom("a@b.com"); this.email.addTo("c@d.com"); this.email.setSubject("test mail"); this.email.setCharset("ISO-8859-1"); this.email.setContent("test content", "text/plain"); this.email.buildMimeMessage(); MimeMessage msg = this.email.getMimeMessage(); msg.saveChanges(); assertEquals("text/plain; charset=ISO-8859-1", msg.getContentType()); } /** * Case 2: * A default charset is overridden by an explicitly specified * charset in setContent(). * @throws Exception on any error */ public void testDefaultCharsetCanBeOverriddenByContentType() throws Exception { this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.setFrom("a@b.com"); this.email.addTo("c@d.com"); this.email.setSubject("test mail"); this.email.setCharset("ISO-8859-1"); this.email.setContent("test content", "text/plain; charset=US-ASCII"); this.email.buildMimeMessage(); MimeMessage msg = this.email.getMimeMessage(); msg.saveChanges(); assertEquals("text/plain; charset=US-ASCII", msg.getContentType()); } /** * Case 3: * A non-text content object ignores a default charset entirely. * @throws Exception on any error */ public void testDefaultCharsetIgnoredByNonTextContent() throws Exception { this.email.setHostName(this.strTestMailServer); this.email.setSmtpPort(this.getMailServerPort()); this.email.setFrom("a@b.com"); this.email.addTo("c@d.com"); this.email.setSubject("test mail"); this.email.setCharset("ISO-8859-1"); this.email.setContent("test content", "application/octet-stream"); this.email.buildMimeMessage(); MimeMessage msg = this.email.getMimeMessage(); msg.saveChanges(); assertEquals("application/octet-stream", msg.getContentType()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -