📄 htmlemailtest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.mail;import java.io.File;import java.io.IOException;import java.net.URL;import javax.activation.FileDataSource;import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;import org.apache.commons.mail.settings.EmailConfiguration;/** * JUnit test case for HtmlEmail Class * * @since 1.0 * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a> * @version $Id: HtmlEmailTest.java 545815 2007-06-09 23:46:00Z bspeakmon $ */public class HtmlEmailTest extends BaseEmailTestCase{ /** */ private MockHtmlEmailConcrete email; /** * @param name name */ public HtmlEmailTest(String name) { super(name); } /** * @throws Exception */ protected void setUp() throws Exception { super.setUp(); // reusable objects to be used across multiple tests this.email = new MockHtmlEmailConcrete(); } /** * @throws EmailException */ public void testGetSetTextMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== for (int i = 0; i < testCharsValid.length; i++) { this.email.setTextMsg(testCharsValid[i]); assertEquals(testCharsValid[i], this.email.getTextMsg()); } // ==================================================================== // Test Exception // ==================================================================== for (int i = 0; i < this.testCharsNotValid.length; i++) { try { this.email.setTextMsg(this.testCharsNotValid[i]); fail("Should have thrown an exception"); } catch (EmailException e) { assertTrue(true); } } } /** * @throws EmailException if setting the message fails */ public void testGetSetHtmlMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== for (int i = 0; i < testCharsValid.length; i++) { this.email.setHtmlMsg(testCharsValid[i]); assertEquals(testCharsValid[i], this.email.getHtmlMsg()); } // ==================================================================== // Test Exception // ==================================================================== for (int i = 0; i < this.testCharsNotValid.length; i++) { try { this.email.setHtmlMsg(this.testCharsNotValid[i]); fail("Should have thrown an exception"); } catch (EmailException e) { assertTrue(true); } } } /** * @throws EmailException */ public void testGetSetMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== for (int i = 0; i < testCharsValid.length; i++) { this.email.setMsg(testCharsValid[i]); assertEquals(testCharsValid[i], this.email.getTextMsg()); assertTrue( this.email.getHtmlMsg().indexOf(testCharsValid[i]) != -1); } // ==================================================================== // Test Exception // ==================================================================== for (int i = 0; i < this.testCharsNotValid.length; i++) { try { this.email.setMsg(this.testCharsNotValid[i]); fail("Should have thrown an exception"); } catch (EmailException e) { assertTrue(true); } } } /** * * @throws Exception Exception */ public void testEmbedUrl() throws Exception { // ==================================================================== // Test Success // ==================================================================== String strEmbed = this.email.embed(new URL(this.strTestURL), "Test name"); assertNotNull(strEmbed); assertEquals(HtmlEmail.CID_LENGTH, strEmbed.length()); // if we embed the same name again, do we get the same content ID // back? String testCid = this.email.embed(new URL(this.strTestURL), "Test name"); assertEquals(strEmbed, testCid); // if we embed the same URL under a different name, is the content ID // unique? String newCid = this.email.embed(new URL(this.strTestURL), "Test name 2"); assertFalse(strEmbed.equals(newCid)); // ==================================================================== // Test Exceptions // ==================================================================== // Does an invalid URL throw an exception? try { this.email.embed(new URL("http://bad.url"), "Bad URL"); fail("Should have thrown an exception"); } catch (EmailException e) { // expected } // if we try to embed a different URL under a previously used name, // does it complain? try { this.email.embed(new URL("http://www.google.com"), "Test name"); fail("shouldn't be able to use an existing name with a different URL!"); } catch (EmailException e) { // expected } } public void testEmbedFile() throws Exception { // ==================================================================== // Test Success // ==================================================================== File file = File.createTempFile("testEmbedFile", "txt"); file.deleteOnExit(); String strEmbed = this.email.embed(file); assertNotNull(strEmbed); assertEquals("generated CID has wrong length", HtmlEmail.CID_LENGTH, strEmbed.length()); // if we embed the same file again, do we get the same content ID // back? String testCid = this.email.embed(file); assertEquals("didn't get same CID after embedding same file twice", strEmbed, testCid); // if we embed a new file, is the content ID unique? File otherFile = File.createTempFile("testEmbedFile2", "txt"); otherFile.deleteOnExit(); String newCid = this.email.embed(otherFile); assertFalse("didn't get unique CID from embedding new file", strEmbed.equals(newCid)); } public void testEmbedUrlAndFile() throws Exception { File tmpFile = File.createTempFile("testfile", "txt"); tmpFile.deleteOnExit(); String fileCid = this.email.embed(tmpFile); URL fileUrl = tmpFile.toURL(); String urlCid = this.email.embed(fileUrl, "urlName"); assertFalse("file and URL cids should be different even for same resource", fileCid.equals(urlCid)); } public void testEmbedDataSource() throws Exception { File tmpFile = File.createTempFile("testEmbedDataSource", "txt");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -