Author: sgoeschl
Date: Thu Jul 29 20:35:54 2010
New Revision: 980563
URL: http://svn.apache.org/viewvc?rev=980563&view=rev
Log:
[EMAIL-92] Cleaning up the test code
Modified:
commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
commons/proper/email/trunk/src/java/org/apache/commons/mail/impl/URLFactory.java
commons/proper/email/trunk/src/test/attachments/download_email.cgi.html
commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
(original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java Thu
Jul 29 20:35:54 2010
@@ -1097,13 +1097,13 @@ public abstract class Email
*/
public void buildMimeMessage() throws EmailException
{
- if(this.message != null)
+ if (this.message != null)
{
// EMAIL-95 we assume that an email is not reused therefore
invoking
// buildMimeMessage() more than once is illegal.
throw new IllegalStateException("The MimeMessage is already
built.");
}
-
+
try
{
this.getMailSession();
Modified:
commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
(original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
Thu Jul 29 20:35:54 2010
@@ -502,7 +502,7 @@ public class HtmlEmail extends MultiPart
*
* @exception EmailException if there was an error.
* @since 1.0
- */
+ */
public void buildMimeMessage() throws EmailException
{
try
Modified:
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
---
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
(original)
+++
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
Thu Jul 29 20:35:54 2010
@@ -46,23 +46,25 @@ import java.util.regex.Pattern;
*/
public class ImageHtmlEmail extends HtmlEmail
{
- /**
- * Regular Expression to find all <IMG SRC="..."> entries in an HTML
- * document.
- *
- * It needs to cater for various things, like more whitespaces including
- * newlines on any place, HTML is not case sensitive and there can be
- * arbitrary text between "IMG" and "SRC" like IDs and other things.
- */
+ // Regular Expression to find all <IMG SRC="..."> entries in an HTML
+ // document.It needs to cater for various things, like more whitespaces
+ // including newlines on any place, HTML is not case sensitive and there
+ // can be arbitrary text between "IMG" and "SRC" like IDs and other things.
+
+ /** regexp for extracting <img> tags */
public static final String REGEX_IMG_SRC =
"(<[Ii][Mm][Gg]\\s*[^>]*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])";
+ /** regexp for extracting <script> tags */
public static final String REGEX_SCRIPT_SRC =
"(<[Ss][Cc][Rr][Ii][Pp][Tt]\\s*.*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])";
// this pattern looks for the HTML image tag which indicates embedded
images,
// the grouping is necessary to allow to replace the element with the CID
- protected static final Pattern pattern = Pattern.compile(REGEX_IMG_SRC);
- protected static final Pattern scriptPattern =
Pattern.compile(REGEX_SCRIPT_SRC);
+ /** pattern for extracting <img> tags */
+ private static final Pattern IMG_PATTERN = Pattern.compile(REGEX_IMG_SRC);
+
+ /** pattern for extracting <script> tags */
+ private static final Pattern SCRIPT_PATTERN =
Pattern.compile(REGEX_SCRIPT_SRC);
/**
* Set the HTML message and try to add any image that is linked in the HTML
@@ -110,10 +112,10 @@ public class ImageHtmlEmail extends Html
}
// replace images
- String temp = replacePattern(htmlMessage, pattern, baseUrl, isLenient);
+ String temp = replacePattern(htmlMessage, IMG_PATTERN, baseUrl,
isLenient);
// replace scripts
- temp = replacePattern(temp, scriptPattern, baseUrl, isLenient);
+ temp = replacePattern(temp, SCRIPT_PATTERN, baseUrl, isLenient);
// finally set the resulting HTML with all images replaced if possible
return super.setHtmlMsg(temp);
@@ -134,30 +136,30 @@ public class ImageHtmlEmail extends Html
{
DataSource imageDataSource;
StringBuffer stringBuffer = new StringBuffer();
-
+
// maps "cid" --> name
Map cidCache = new HashMap();
-
- // maps "name" --> dataSource
+
+ // maps "name" --> dataSource
Map dataSourceCache = new HashMap();
-
+
// in the String, replace all "img src" with a CID and embed the
related
// image file if we find it.
Matcher matcher = pattern.matcher(htmlMessage);
// the matcher returns all instances one by one
while (matcher.find())
- {
+ {
// in the RegEx we have the <src> element as second "group"
String image = matcher.group(2);
// avoid loading the same data source more than once
- if(dataSourceCache.get(image) == null)
+ if (dataSourceCache.get(image) == null)
{
// in lenient mode we might get a 'null' data source if the
resource was not found
imageDataSource = resolve(baseUrl, image, isLenient);
-
- if(imageDataSource != null)
+
+ if (imageDataSource != null)
{
dataSourceCache.put(image, imageDataSource);
}
@@ -165,29 +167,29 @@ public class ImageHtmlEmail extends Html
else
{
imageDataSource = (DataSource) dataSourceCache.get(image);
- }
+ }
if (imageDataSource != null)
{
String name = imageDataSource.getName();
String cid = (String) cidCache.get(name);
- if(cid == null)
+ if (cid == null)
{
cid = embed(imageDataSource, imageDataSource.getName());
cidCache.put(name, cid);
}
-
+
// if we embedded something, then we need to replace the URL
with
// the CID, otherwise the Matcher takes care of adding the
- // non-replaced text afterwards, so no else is necessary here!
+ // non-replaced text afterwards, so no else is necessary here!
matcher.appendReplacement(stringBuffer, matcher.group(1) +
"cid:" + cid + matcher.group(3));
}
}
// append the remaining items...
matcher.appendTail(stringBuffer);
-
+
cidCache.clear();
dataSourceCache.clear();
@@ -207,7 +209,7 @@ public class ImageHtmlEmail extends Html
* @throws EmailException resolving the resource failed
*/
protected DataSource resolve(final URL baseUrl, final String
resourceLocation, final boolean isLenient)
- throws EmailException
+ throws EmailException
{
DataSource result = null;
@@ -224,7 +226,7 @@ public class ImageHtmlEmail extends Html
}
catch (IOException e)
{
- if(!isLenient)
+ if (!isLenient)
{
throw new EmailException("Resolving the resource failed : " +
resourceLocation, e);
}
Modified:
commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
---
commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
(original)
+++
commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
Thu Jul 29 20:35:54 2010
@@ -223,7 +223,7 @@ public class MultiPartEmail extends Emai
*
* @exception EmailException if there was an error.
* @since 1.0
- */
+ */
public void buildMimeMessage() throws EmailException
{
try
Modified:
commons/proper/email/trunk/src/java/org/apache/commons/mail/impl/URLFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/impl/URLFactory.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
---
commons/proper/email/trunk/src/java/org/apache/commons/mail/impl/URLFactory.java
(original)
+++
commons/proper/email/trunk/src/java/org/apache/commons/mail/impl/URLFactory.java
Thu Jul 29 20:35:54 2010
@@ -45,40 +45,52 @@ public final class URLFactory
{
// if we get an non-existing base url than the resource can
// be directly used to create an URL
- if(baseUrl == null)
+ if (baseUrl == null)
{
return new URL(resource);
}
// if we get an non-existing location than use the base url alone
- if(resource == null)
+ if (resource == null)
{
return baseUrl;
}
// if we get a stand-alone resource than ignore the base url
- if(isFileUrl(resource) || isHttpUrl(resource))
+ if (isFileUrl(resource) || isHttpUrl(resource))
{
return new URL(resource);
}
// crate a file URL
- String baseUrlString = baseUrl.toExternalForm();
- if(isFileUrl(baseUrlString))
+ String baseUrlString = baseUrl.toExternalForm();
+ if (isFileUrl(baseUrlString))
{
return new File(toFile(baseUrl), resource).toURI().toURL();
}
else
{
- return new URL(baseUrl, resource.replaceAll("&", "&")) ;
- }
+ return new URL(baseUrl, resource.replaceAll("&", "&"));
+ }
}
+ /**
+ * Is this a file URL?
+ *
+ * @param urlString the URL string
+ * @return true if it is a file URL
+ */
private static boolean isFileUrl(String urlString)
{
return urlString.startsWith("file://");
}
+ /**
+ * Is this a HTTP/HTTPS URL?
+ *
+ * @param urlString the URL string
+ * @return true if it is a HTTP/HTTPS URL
+ */
private static boolean isHttpUrl(String urlString)
{
return urlString.startsWith("http://") ||
urlString.startsWith("https://");
@@ -86,10 +98,6 @@ public final class URLFactory
/**
* Convert from a <code>URL</code> to a <code>File</code>.
- * <p>
- * From version 1.1 this method will decode the URL.
- * Syntax such as <code>file:///my%20docs/file.txt</code> will be
- * correctly decoded to <code>/my docs/file.txt</code>.
*
* @param url the file URL to convert, <code>null</code> returns
<code>null</code>
* @return the equivalent <code>File</code> object, or <code>null</code>
@@ -105,7 +113,7 @@ public final class URLFactory
else
{
String filename = url.getFile().replace('/', File.separatorChar);
- int pos =0;
+ int pos = 0;
while ((pos = filename.indexOf('%', pos)) >= 0)
{
if (pos + 2 < filename.length())
Modified:
commons/proper/email/trunk/src/test/attachments/download_email.cgi.html
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/attachments/download_email.cgi.html?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/attachments/download_email.cgi.html
(original)
+++ commons/proper/email/trunk/src/test/attachments/download_email.cgi.html Thu
Jul 29 20:35:54 2010
@@ -1,4 +1,23 @@
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!--
+ 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.
+-->
<html>
<head>
Modified:
commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
---
commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java
(original)
+++
commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java
Thu Jul 29 20:35:54 2010
@@ -59,7 +59,7 @@ public abstract class BaseEmailTestCase
private static final String LINE_SEPARATOR = "\r\n";
/** default port */
- private static int mailServerPort = EmailConfiguration.MAIL_SERVER_PORT;
+ private static int mailServerPort = 2500;
/** The fake Wiser email server */
protected Wiser fakeMailServer;
Modified:
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java?rev=980563&r1=980562&r2=980563&view=diff
==============================================================================
---
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
(original)
+++
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
Thu Jul 29 20:35:54 2010
@@ -253,39 +253,4 @@ public class EmailLiveTest extends BaseE
EmailUtils.writeMimeMessage( new
File("./target/test-emails/testImageHtmlEmailRemote.eml"),
send(email).getMimeMessage());
}
-
- /**
- public void testGmail() throws Exception {
-
- Email email = new SimpleEmail();
- email.setSmtpPort(587);
- email.setAuthenticator(new
DefaultAuthenticator(EmailConfiguration.TEST_USER,
EmailConfiguration.TEST_PASSWD));
- email.setDebug(true);
- email.setHostName("smtp.gmail.com");
- email.setFrom("[email protected]", "SenderName");
- email.setSubject("TestMail");
- email.setMsg("This is a test mail?");
- email.addTo("[email protected]", "ToName");
- email.setTLS(true);
- email.send();
- }
- */
-
- /**
- public void testGmx() throws Exception {
-
- Email email = new SimpleEmail();
- email.setSmtpPort(465);
- email.setAuthenticator(new DefaultAuthenticator("[email protected]",
"pratsch"));
- email.setDebug(true);
- email.setHostName("mail.gmx.net");
- email.setFrom("[email protected]");
- email.setSubject("TestMail");
- email.setMsg("This is a test mail?");
- email.addTo("[email protected]", "ToName");
- // email.setTLS(true);
- email.setSSL(true);
- email.send();
- }
- */
}
\ No newline at end of file