Author: tn
Date: Sun Jan 6 12:26:59 2013
New Revision: 1429504
URL: http://svn.apache.org/viewvc?rev=1429504&view=rev
Log:
Mock invalid URL tests to prevent test failures due to misbehaving ISPs.
Modified:
commons/proper/email/trunk/pom.xml
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/BaseEmailTestCase.java
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
Modified: commons/proper/email/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/pom.xml?rev=1429504&r1=1429503&r2=1429504&view=diff
==============================================================================
--- commons/proper/email/trunk/pom.xml (original)
+++ commons/proper/email/trunk/pom.xml Sun Jan 6 12:26:59 2013
@@ -225,6 +225,24 @@
<version>3.1.7</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>1.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-easymock</artifactId>
+ <version>1.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>3.1</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<properties>
Modified:
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/BaseEmailTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/BaseEmailTestCase.java?rev=1429504&r1=1429503&r2=1429504&view=diff
==============================================================================
---
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/BaseEmailTestCase.java
(original)
+++
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/BaseEmailTestCase.java
Sun Jan 6 12:26:59 2013
@@ -16,10 +16,15 @@
*/
package org.apache.commons.mail;
+import static org.easymock.EasyMock.expect;
+import static org.powermock.api.easymock.PowerMock.createMock;
+import static org.powermock.api.easymock.PowerMock.replay;
+
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
+import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
@@ -513,4 +518,21 @@ public abstract class BaseEmailTestCase
protected boolean isMailServerStopped(Wiser fakeMailServer) {
return !fakeMailServer.getServer().isRunning();
}
+
+ /**
+ * Create a mocked URL object which always throws an IOException
+ * when the openStream() method is called.
+ * <p>
+ * Several ISPs do resolve invalid URLs like {@code http://example.invalid}
+ * to some error page causing tests to fail otherwise.
+ *
+ * @return an invalid URL
+ */
+ protected URL createInvalidURL() throws Exception {
+ URL url = createMock(URL.class);
+ expect(url.openStream()).andThrow(new IOException());
+ replay(url);
+
+ return url;
+ }
}
Modified:
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java?rev=1429504&r1=1429503&r2=1429504&view=diff
==============================================================================
---
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
(original)
+++
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
Sun Jan 6 12:26:59 2013
@@ -29,6 +29,10 @@ import org.apache.commons.mail.mocks.Moc
import org.apache.commons.mail.settings.EmailConfiguration;
import org.apache.commons.mail.util.MimeMessageParser;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
/**
* JUnit test case for HtmlEmail Class.
*
@@ -36,6 +40,8 @@ import org.apache.commons.mail.util.Mime
* @author <a href="mailto:[email protected]">Corey Scott</a>
* @version $Id$
*/
+@RunWith(PowerMockRunner.class)
+@PrepareForTest( { MockHtmlEmailConcrete.class })
public class HtmlEmailTest extends BaseEmailTestCase
{
/** */
@@ -177,7 +183,7 @@ public class HtmlEmailTest extends BaseE
// Does an invalid URL throw an exception?
try
{
- this.email.embed(new URL("http://example.invalid"), "Bad URL");
+ this.email.embed(createInvalidURL(), "Bad URL");
fail("Should have thrown an exception");
}
catch (EmailException e)
Modified:
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java?rev=1429504&r1=1429503&r2=1429504&view=diff
==============================================================================
---
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
(original)
+++
commons/proper/email/trunk/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
Sun Jan 6 12:26:59 2013
@@ -28,6 +28,9 @@ import javax.activation.URLDataSource;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.mail.mocks.MockMultiPartEmailConcrete;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
/**
* JUnit test case for MultiPartEmail Class
@@ -36,6 +39,8 @@ import org.apache.commons.mail.mocks.Moc
* @author <a href="mailto:[email protected]">Corey Scott</a>
* @version $Id$
*/
+@RunWith(PowerMockRunner.class)
+@PrepareForTest( { MockMultiPartEmailConcrete.class, URLDataSource.class })
public class MultiPartEmailTest extends BaseEmailTestCase
{
/** */
@@ -194,7 +199,7 @@ public class MultiPartEmailTest extends
* @throws MalformedURLException when a bad attachment URL is used
* @throws EmailException when a bad address or attachment is used
*/
- public void testAttach() throws MalformedURLException, EmailException
+ public void testAttach() throws MalformedURLException, EmailException,
Exception
{
EmailAttachment attachment;
@@ -241,7 +246,7 @@ public class MultiPartEmailTest extends
attachment = new EmailAttachment();
try
{
- attachment.setURL(new URL("http://example.invalid"));
+ attachment.setURL(createInvalidURL());
this.email.attach(attachment);
fail("Should have thrown an exception");
}
@@ -289,7 +294,7 @@ public class MultiPartEmailTest extends
* @throws MalformedURLException when a bad attachment URL is used
* @throws EmailException when a bad address or attachment is used
*/
- public void testAttach3() throws MalformedURLException, EmailException
+ public void testAttach3() throws MalformedURLException, EmailException,
Exception
{
// ====================================================================
// Test Success - URL
@@ -317,7 +322,7 @@ public class MultiPartEmailTest extends
// invalid datasource
try
{
- URLDataSource urlDs = new URLDataSource(new
URL("http://example.invalid/"));
+ URLDataSource urlDs = new URLDataSource(createInvalidURL());
this.email.attach(urlDs, "Test Attachment", "Test Attachment
Desc");
fail("Should have thrown an exception");
}