Author: gk
Date: Tue Jul 30 15:14:39 2019
New Revision: 1864012
URL: http://svn.apache.org/viewvc?rev=1864012&view=rev
Log:
Cleanup CommonsEmail Component
- update with log4j2, junit5, parent 6
- replace corba inputstream
- add test
Added:
turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml (with props)
Modified:
turbine/fulcrum/trunk/commonsemail/pom.xml
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/CommonsEmailService.java
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailServiceImpl.java
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailUtils.java
turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
Modified: turbine/fulcrum/trunk/commonsemail/pom.xml
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/pom.xml?rev=1864012&r1=1864011&r2=1864012&view=diff
==============================================================================
--- turbine/fulcrum/trunk/commonsemail/pom.xml (original)
+++ turbine/fulcrum/trunk/commonsemail/pom.xml Tue Jul 30 15:14:39 2019
@@ -76,11 +76,26 @@
<version>1.0.8</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>${turbine.log4j2.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
+ <testResources>
+ <testResource>
+ <directory>${basedir}/src/test</directory>
+ <filtering>false</filtering>
+ <includes>
+ <include>log4j2*.xml</include>
+ </includes>
+ </testResource>
+ </testResources>
</build>
</project>
Modified:
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/CommonsEmailService.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/CommonsEmailService.java?rev=1864012&r1=1864011&r2=1864012&view=diff
==============================================================================
---
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/CommonsEmailService.java
(original)
+++
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/CommonsEmailService.java
Tue Jul 30 15:14:39 2019
@@ -1,5 +1,7 @@
package org.apache.fulcrum.commonsemail;
+import java.io.File;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -31,6 +33,7 @@ import org.apache.commons.mail.EmailExce
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
+import org.apache.fulcrum.commonsemail.impl.CommonsEmailDomainEntry;
/**
* A service taking care of most of the commons-email configuration such as
@@ -231,4 +234,16 @@ public interface CommonsEmailService
*/
SendDeliveryStatus getSendDeliveryStatus(MimeMessage mimeMessage)
throws MessagingException;
+
+ /**
+ * Get the current temporary directory for debugging purposes mainly.
+ */
+ public File getServiceTempDir();
+
+ /**
+ * Get the current domain, for debugging purposes mainly.
+ * @param name of the domain
+ * @return the domain entry
+ */
+ public CommonsEmailDomainEntry getDomain( String name );
}
\ No newline at end of file
Modified:
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailServiceImpl.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailServiceImpl.java?rev=1864012&r1=1864011&r2=1864012&view=diff
==============================================================================
---
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailServiceImpl.java
(original)
+++
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailServiceImpl.java
Tue Jul 30 15:14:39 2019
@@ -580,7 +580,7 @@ public class CommonsEmailServiceImpl
/**
* @return Returns the serviceHomeDir.
*/
- protected File getServiceHomeDir()
+ public File getServiceHomeDir()
{
return serviceHomeDir;
}
@@ -588,7 +588,7 @@ public class CommonsEmailServiceImpl
/**
* @return Returns the serviceTempDir.
*/
- protected File getServiceTempDir()
+ public File getServiceTempDir()
{
return serviceTempDir;
}
@@ -919,10 +919,10 @@ public class CommonsEmailServiceImpl
* <li>revert to the default domain</li>
* </ul>
*
- * @param name the user-suplplied name of the domain
+ * @param name the user-supplied name of the domain
* @return the corresponding domain configuration
*/
- protected CommonsEmailDomainEntry getDomain( String name )
+ public CommonsEmailDomainEntry getDomain( String name )
{
CommonsEmailDomainEntry result;
Modified:
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailUtils.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailUtils.java?rev=1864012&r1=1864011&r2=1864012&view=diff
==============================================================================
---
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailUtils.java
(original)
+++
turbine/fulcrum/trunk/commonsemail/src/java/org/apache/fulcrum/commonsemail/impl/CommonsEmailUtils.java
Tue Jul 30 15:14:39 2019
@@ -19,10 +19,9 @@ package org.apache.fulcrum.commonsemail.
* under the License.
*/
-import org.omg.CORBA_2_3.portable.InputStream;
-
import java.io.File;
import java.io.FileOutputStream;
+import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
@@ -80,7 +79,7 @@ public class CommonsEmailUtils
}
/**
- * Convinience method to read a MimeMessage from a source.
+ * Convenience method to read a MimeMessage from a source.
*
* @param session the mail session
* @param source the source of the MimeMessage
Added: turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml?rev=1864012&view=auto
==============================================================================
--- turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml (added)
+++ turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml Tue Jul 30 15:14:39
2019
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<Configuration status="info" verbose="false">
+ <Appenders>
+ <Console name="console" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
+ </Console>
+ <File name="logfile" fileName="target/fulcrum-test.log">
+ <PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
+ </File>
+ <File name="avalon" fileName="target/avalon-test.log">
+ <PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
+ </File>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.fulcrum" level="info" additivity="false">
+ <AppenderRef ref="console"/>
+ <AppenderRef ref="logfile"/>
+ </Logger>
+ <Logger name="org.apache.fulcrum.yaafi" level="info" additivity="false">
+ <AppenderRef ref="avalon"/>
+ </Logger>
+ <Logger name="avalon" level="warn" additivity="false">
+ <AppenderRef ref="avalon"/>
+ </Logger>
+ <Logger name="org.apache.logging.log4j" level="warn" additivity="false">
+ <AppenderRef ref="logfile"/>
+ </Logger>
+ <Root level="error">
+ <AppenderRef ref="console"/>
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
Propchange: turbine/fulcrum/trunk/commonsemail/src/test/log4j2.xml
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java?rev=1864012&r1=1864011&r2=1864012&view=diff
==============================================================================
---
turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
(original)
+++
turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
Tue Jul 30 15:14:39 2019
@@ -1,5 +1,11 @@
package org.apache.fulcrum.commonsemail;
+import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -29,16 +35,20 @@ import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
-import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
+import org.apache.commons.mail.EmailConstants;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
-import org.apache.fulcrum.testcontainer.BaseUnitTest;
+import org.apache.fulcrum.commonsemail.impl.CommonsEmailUtils;
+import org.apache.fulcrum.testcontainer.BaseUnit5Test;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
/**
@@ -46,7 +56,8 @@ import org.apache.fulcrum.testcontainer.
*
* @author <a href="mailto:[email protected]">Siegfried Goeschl</a>
*/
-public class CommonsEmailServiceTest extends BaseUnitTest
+@DisplayName("CommonsEmailTest-Test")
+public class CommonsEmailServiceTest extends BaseUnit5Test
{
/** the service to test */
private CommonsEmailService service;
@@ -71,26 +82,28 @@ public class CommonsEmailServiceTest ext
/** default HTML text content */
private static final String HTML_CONTENT = "<h1>Hello World</h1>";
+
+ public static enum EmlType { original, overwrite, post, send };
+
+ Logger log = LogManager.getLogger(getClass().getName());
/**
* Defines the testcase name for JUnit.
- *
- * @param name the testcase's name.
*/
- public CommonsEmailServiceTest(String name)
+ public CommonsEmailServiceTest()
{
- super(name);
+
}
/**
* Test setup
*/
- protected void setUp() throws Exception
+ @BeforeEach
+ public void setUp() throws Exception
{
- super.setUp();
this.domain = "test";
- this.subject = this.getName();
+ this.subject = "CommonsEmailTest-Test";
this.mailFrom = "[email protected]";
this.mailTo = "[email protected]";
@@ -108,7 +121,8 @@ public class CommonsEmailServiceTest ext
/**
* @see junit.framework.TestCase#tearDown()
*/
- protected void tearDown()
+ @AfterEach
+ public void tearDown()
{
if( this.result != null )
{
@@ -116,7 +130,7 @@ public class CommonsEmailServiceTest ext
{
File resultDirectory = new File("./target/temp");
resultDirectory.mkdirs();
- File resultFile = new File( resultDirectory,
this.getName()+".eml" );
+ File resultFile = new File( resultDirectory,
"CommonsEmailTest-Test"+".eml" );
FileOutputStream fos = new FileOutputStream(resultFile);
this.result.writeTo(fos);
fos.flush();
@@ -133,30 +147,6 @@ public class CommonsEmailServiceTest ext
}
/**
- * Add all of our test suites.
- *
- * @return the tests to execute
- */
- public static Test suite()
- {
- TestSuite suite= new TestSuite();
-
- suite.addTest( new CommonsEmailServiceTest("testDefaultDomain") );
- suite.addTest( new CommonsEmailServiceTest("testDerivedDomain") );
- suite.addTest( new CommonsEmailServiceTest("testHtmlEmail") );
- suite.addTest( new
CommonsEmailServiceTest("testHtmlEmailWithHashtable") );
- suite.addTest( new CommonsEmailServiceTest("testMultiPartEmail") );
- suite.addTest( new
CommonsEmailServiceTest("testSendEmailToUnknownServer") );
- suite.addTest( new CommonsEmailServiceTest("testSendMimeMessage") );
- suite.addTest( new CommonsEmailServiceTest("testSimpleEmail") );
- suite.addTest( new
CommonsEmailServiceTest("testSimpleEmailWithHashtable") );
- suite.addTest( new
CommonsEmailServiceTest("testCreateMimeMessageWithSession") );
- suite.addTest( new
CommonsEmailServiceTest("testGetSendDeliveryStatus") );
-
- return suite;
- }
-
- /**
* @return the CommonsEmailService service to be used
*/
protected CommonsEmailService getService()
@@ -218,6 +208,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testSimpleEmail() throws Exception
{
SimpleEmail email =
this.getService().createSimpleEmail(this.getDomain());
@@ -234,6 +225,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testHtmlEmail() throws Exception
{
HtmlEmail email = this.getService().createHtmlEmail(this.getDomain());
@@ -251,6 +243,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testMultiPartEmail() throws Exception
{
MultiPartEmail email =
this.getService().createMultiPartEmail(this.getDomain());
@@ -269,6 +262,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testDefaultDomain() throws Exception
{
SimpleEmail email = this.getService().createSimpleEmail("grmpff");
@@ -285,6 +279,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testDerivedDomain() throws Exception
{
SimpleEmail email =
this.getService().createSimpleEmail("[email protected]");
@@ -302,6 +297,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testHtmlEmailWithHashtable() throws Exception
{
Vector attachments = new Vector();
@@ -310,11 +306,11 @@ public class CommonsEmailServiceTest ext
attachments.add(attachment);
Hashtable content = new Hashtable();
- content.put(Email.EMAIL_SUBJECT, this.getSubject());
- content.put(Email.EMAIL_BODY, HTML_CONTENT);
- content.put(Email.SENDER_EMAIL, this.getMailFrom());
- content.put(Email.RECEIVER_EMAIL, this.getMailTo());
- content.put(Email.ATTACHMENTS, attachments);
+ content.put(EmailConstants.EMAIL_SUBJECT, this.getSubject());
+ content.put(EmailConstants.EMAIL_BODY, HTML_CONTENT);
+ content.put(EmailConstants.SENDER_EMAIL, this.getMailFrom());
+ content.put(EmailConstants.RECEIVER_EMAIL, this.getMailTo());
+ content.put(EmailConstants.ATTACHMENTS, attachments);
HtmlEmail email = this.getService().createHtmlEmail(
this.getMailFrom(),
@@ -329,13 +325,14 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testSimpleEmailWithHashtable() throws Exception
{
Hashtable content = new Hashtable();
- content.put(Email.EMAIL_SUBJECT, this.getSubject());
- content.put(Email.EMAIL_BODY, PLAIN_CONTENT);
- content.put(Email.SENDER_EMAIL, this.getMailFrom());
- content.put(Email.RECEIVER_EMAIL, this.getMailTo());
+ content.put(EmailConstants.EMAIL_SUBJECT, this.getSubject());
+ content.put(EmailConstants.EMAIL_BODY, PLAIN_CONTENT);
+ content.put(EmailConstants.SENDER_EMAIL, this.getMailFrom());
+ content.put(EmailConstants.RECEIVER_EMAIL, this.getMailTo());
SimpleEmail email = this.getService().createSimpleEmail(
this.getMailFrom(),
@@ -353,15 +350,16 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testSendEmailToUnknownServer() throws Exception
{
- Hashtable content = new Hashtable();
- content.put(Email.EMAIL_SUBJECT, this.getSubject());
- content.put(Email.EMAIL_BODY, PLAIN_CONTENT);
- content.put(Email.SENDER_EMAIL, this.getMailFrom());
- content.put(Email.RECEIVER_EMAIL, this.getMailTo());
- content.put(Email.MAIL_HOST, "localhost");
- content.put(Email.MAIL_PORT, "63178");
+ Hashtable<String, String> content = new Hashtable<String, String>();
+ content.put(EmailConstants.EMAIL_SUBJECT, this.getSubject());
+ content.put(EmailConstants.EMAIL_BODY, PLAIN_CONTENT);
+ content.put(EmailConstants.SENDER_EMAIL, this.getMailFrom());
+ content.put(EmailConstants.RECEIVER_EMAIL, this.getMailTo());
+ content.put(EmailConstants.MAIL_HOST, "localhost");
+ content.put(EmailConstants.MAIL_PORT, "63178");
SimpleEmail email = this.getService().createSimpleEmail(
this.getMailFrom(),
@@ -388,6 +386,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testCreateMimeMessageWithSession() throws Exception
{
MimeMessage mimeMessage;
@@ -403,8 +402,52 @@ public class CommonsEmailServiceTest ext
session,
mimeMessage
);
-
+
assertNotNull( this.result.getMessageID() );
+
+ }
+
+ /**
+ * Create a mail session and simple MimeMessage and sent it.
+ *
+ * @throws Exception the test failed
+ */
+ @Test
+ public void testReadMimeMessageWithSession() throws Exception
+ {
+ MimeMessage mimeMessage;
+ // domain to read from global
+ Session session =
this.getService().createSmtpSession("test","foo","bar");
+
+ mimeMessage = new MimeMessage(session);
+ mimeMessage.setFrom(new InternetAddress(this.mailFrom));
+ mimeMessage.setSubject(this.getSubject());
+ mimeMessage.setText(PLAIN_CONTENT);
+ mimeMessage.addRecipient(Message.RecipientType.TO, new
InternetAddress(this.mailTo));
+
+ // domain is derived from javax.mail.internet.MimeMessage.getFrom()
+ this.result = this.getService().send(
+ session,
+ mimeMessage
+ );
+
+ if (this.getService().getDomain("test").isMailDump()) {
+
+ for (EmlType et :EmlType.values()) {
+ String name = "CommonsEmailService_" + et + ".eml";
+ File dumpFile = new File(
this.getService().getServiceTempDir(), name );
+ if (dumpFile.exists()) {
+ log.trace("reading from dump file {}",
dumpFile.getAbsoluteFile().toString());
+ MimeMessage tempMessage =
CommonsEmailUtils.readMimeMessage(session, dumpFile);
+
assertEquals("true",tempMessage.getSession().getProperty("mail.smtp.auth"));
+ // messageIds are not equal as result has a fake id with
timestamp
+ System.out.println(tempMessage.getMessageID());
+ assertNotNull( tempMessage.getMessageID() );
+ }
+ }
+
+ }
+
}
/**
@@ -412,6 +455,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testSendMimeMessage() throws Exception
{
MimeMessage mimeMessage;
@@ -438,6 +482,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testSendMimeMessageWithoutDomain() throws Exception
{
SimpleEmail email =
this.getService().createSimpleEmail("[email protected]");
@@ -458,6 +503,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testGetSendDeliveryStatus() throws Exception
{
SimpleEmail email =
this.getService().createSimpleEmail(this.getDomain());
@@ -482,6 +528,7 @@ public class CommonsEmailServiceTest ext
*
* @throws Exception the test failed
*/
+ @Test
public void testGetSendDeliveryStatusFailure() throws Exception
{
MimeMessage mimeMessage;