📄 baseemailtestcase.java
字号:
//test from address assertEquals("got wrong From: address from mail", fromAdd.toString(), mimeMessage.getHeader("From", null)); //test to address assertTrue("got wrong To: address from mail", toAdd.toString().indexOf(mimeMessage.getHeader("To", null)) != -1); //test cc address if (ccAdd.size() > 0) { assertTrue("got wrong Cc: address from mail", ccAdd.toString().indexOf(mimeMessage.getHeader("Cc", null)) != -1); } //test bcc address if (bccAdd.size() > 0) { assertTrue("got wrong Bcc: address from mail", bccAdd.toString().indexOf(mimeMessage.getHeader("Bcc", null)) != -1); } } catch (MessagingException me) { IllegalStateException ise = new IllegalStateException("caught MessagingException in validateSend()"); ise.initCause(me); throw ise; } return emailMessage; } /** * Validate the message was sent properly * @param mailServer reference to the fake mail server * @param strSubject expected subject * @param content the expected message content * @param fromAdd expected from address * @param toAdd list of expected to addresses * @param ccAdd list of expected cc addresses * @param bccAdd list of expected bcc addresses * @param boolSaveToFile true will output to file, false doesnt * @throws IOException Exception */ protected void validateSend( Wiser mailServer, String strSubject, Multipart content, InternetAddress fromAdd, List toAdd, List ccAdd, List bccAdd, boolean boolSaveToFile) throws IOException { // test other properties WiserMessage emailMessage = this.validateSend( mailServer, strSubject, fromAdd, toAdd, ccAdd, bccAdd, boolSaveToFile); // test message content // get sent email content String strSentContent = content.getContentType(); // get received email content (chop off the auto-added \n // and -- (front and end) String emailMessageBody = getMessageBody(emailMessage); String strMessageBody = emailMessageBody.substring(BaseEmailTestCase.BODY_START_PAD, emailMessageBody.length() - BaseEmailTestCase.BODY_END_PAD); assertTrue("didn't find expected content type in message body", strMessageBody.indexOf(strSentContent) != -1); } /** * Validate the message was sent properly * @param mailServer reference to the fake mail server * @param strSubject expected subject * @param strMessage the expected message as a string * @param fromAdd expected from address * @param toAdd list of expected to addresses * @param ccAdd list of expected cc addresses * @param bccAdd list of expected bcc addresses * @param boolSaveToFile true will output to file, false doesnt * @throws IOException Exception */ protected void validateSend( Wiser mailServer, String strSubject, String strMessage, InternetAddress fromAdd, List toAdd, List ccAdd, List bccAdd, boolean boolSaveToFile) throws IOException { // test other properties WiserMessage emailMessage = this.validateSend( mailServer, strSubject, fromAdd, toAdd, ccAdd, bccAdd, true); // test message content assertTrue("didn't find expected message content in message body", getMessageBody(emailMessage).indexOf(strMessage) != -1); } /** * Serializes the {@link MimeMessage} from the <code>WiserMessage</code> * passed in. The headers are serialized first followed by the message * body. * * @param email The <code>WiserMessage</code> to serialize. * @return The string format of the message. * @throws MessagingException * @throws IOException * Thrown while serializing the body from * {@link DataHandler#writeTo(java.io.OutputStream)}. * @throws MessagingException * Thrown while getting the body content from * {@link MimeMessage#getDataHandler()} * @since 1.1 */ private String serializeEmailMessage(WiserMessage wiserMessage) throws MessagingException, IOException { if (wiserMessage == null) { return ""; } StringBuffer serializedEmail = new StringBuffer(); MimeMessage message = wiserMessage.getMimeMessage(); // Serialize the headers for (Enumeration headers = message.getAllHeaders(); headers .hasMoreElements();) { Header header = (Header) headers.nextElement(); serializedEmail.append(header.getName()); serializedEmail.append(": "); serializedEmail.append(header.getValue()); serializedEmail.append(LINE_SEPARATOR); } // Serialize the body byte[] messageBody = getMessageBodyBytes(message); serializedEmail.append(LINE_SEPARATOR); serializedEmail.append(messageBody); serializedEmail.append(LINE_SEPARATOR); return serializedEmail.toString(); } /** * Returns a string representation of the message body. If the message body * cannot be read, an empty string is returned. * * @param wiserMessage The wiser message from which to extract the message body * @return The string representation of the message body * @throws IOException * Thrown while serializing the body from * {@link DataHandler#writeTo(java.io.OutputStream)}. * @since 1.1 */ private String getMessageBody(WiserMessage wiserMessage) throws IOException { if (wiserMessage == null) { return ""; } byte[] messageBody = null; try { MimeMessage message = wiserMessage.getMimeMessage(); messageBody = getMessageBodyBytes(message); } catch (MessagingException me) { // Thrown while getting the body content from // {@link MimeMessage#getDataHandler()} IllegalStateException ise = new IllegalStateException("couldn't process MimeMessage from WiserMessage in getMessageBody()"); ise.initCause(me); throw ise; } return (messageBody != null) ? (new String(messageBody).intern()) : ""; } /** * Gets the byte making up the body of the message. * * @param mimeMessage * The mime message from which to extract the body. * @return A byte array representing the message body * @throws IOException * Thrown while serializing the body from * {@link DataHandler#writeTo(java.io.OutputStream)}. * @throws MessagingException * Thrown while getting the body content from * {@link MimeMessage#getDataHandler()} * @since 1.1 */ private byte[] getMessageBodyBytes(MimeMessage mimeMessage) throws IOException, MessagingException { DataHandler dataHandler = mimeMessage.getDataHandler(); ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream(); BufferedOutputStream buffOs = new BufferedOutputStream( byteArrayOutStream); dataHandler.writeTo(buffOs); buffOs.flush(); return byteArrayOutStream.toByteArray(); } /** * Checks if an email server is running at the address stored in the * <code>fakeMailServer</code>. * * @param fakeMailServer * The server from which the address is picked up. * @return <code>true</code> if the server claims to be running * @since 1.1 */ protected boolean isMailServerStopped(Wiser fakeMailServer) { return !fakeMailServer.getServer().isRunning(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -