Author: henning Date: Wed Sep 7 03:56:41 2005 New Revision: 279295 URL: http://svn.apache.org/viewcvs?rev=279295&view=rev Log: Applied Niall's patch from 36536. Thanks.
Added: jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java (with props) Modified: jakarta/commons/proper/email/trunk/project.xml jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java jakarta/commons/proper/email/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/email/trunk/project.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/email/trunk/project.xml?rev=279295&r1=279294&r2=279295&view=diff ============================================================================== --- jakarta/commons/proper/email/trunk/project.xml (original) +++ jakarta/commons/proper/email/trunk/project.xml Wed Sep 7 03:56:41 2005 @@ -152,6 +152,10 @@ <roles/> </contributor> <contributor> + <name>Niall Pemberton</name> + <roles/> + </contributor> + <contributor> <name>Corey Scott</name> <email>[EMAIL PROTECTED]</email> <roles/> @@ -170,8 +174,8 @@ <dependencies> <dependency> <groupId>javamail</groupId> - <artifactId>javamail</artifactId> - <version>1.3.3</version> + <artifactId>mail</artifactId> + <version>1.2</version> <url>http://java.sun.com/products/javamail/</url> </dependency> <dependency> Modified: jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java?rev=279295&r1=279294&r2=279295&view=diff ============================================================================== --- jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java (original) +++ jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java Wed Sep 7 03:56:41 2005 @@ -409,16 +409,18 @@ name = email; } + // Using this instead of new InternetAddress(email, name, [charset]) makes + // commons-email usable with javamail 1.2 / J2EE 1.3 + address = new InternetAddress(email); + if (EmailUtils.isNotEmpty(this.charset)) { - address = new InternetAddress(email, name, this.charset); + address.setPersonal(name, this.charset); } else { - address = new InternetAddress(email, name); + address.setPersonal(name); } - - address.validate(); } catch (Exception e) { Added: jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java?rev=279295&view=auto ============================================================================== --- jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java (added) +++ jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java Wed Sep 7 03:56:41 2005 @@ -0,0 +1,250 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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.lang.reflect.Method; + +import javax.mail.internet.InternetAddress; + +/** + * JUnit test case demonstrating InternetAddress validation. + * + * @since 1.0 + * @author Niall Pemberton + * @version $Id$ + */ + +public class InvalidInternetAddressTest extends BaseEmailTestCase +{ + + /** */ + private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com"; + + /** JavaMail 1.2. does not know about this */ + private static Method validateMethod = null; + + /** */ + private static final String[] ARR_INVALID_EMAILS = + { + "local [EMAIL PROTECTED]", + "local([EMAIL PROTECTED]", + "local)[EMAIL PROTECTED]", + "local<[EMAIL PROTECTED]", + "local>[EMAIL PROTECTED]", + "local,[EMAIL PROTECTED]", + "local;[EMAIL PROTECTED]", + "local:[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "local\"[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED] com", + "[EMAIL PROTECTED](com", + "[EMAIL PROTECTED])com", + "[EMAIL PROTECTED]<com", + "[EMAIL PROTECTED]>com", + "[EMAIL PROTECTED],com", + "[EMAIL PROTECTED];com", + "[EMAIL PROTECTED]:com", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "local.name@", + "@domain.com" }; + /** + * @param name name + */ + public InvalidInternetAddressTest(String name) + { + super(name); + } + + protected void setUp() + { + super.setUp(); + + try + { + validateMethod = InternetAddress.class.getMethod("validate", new Class [0]); + } + catch (Exception e) + { + assertEquals("Got wrong Exception when looking for validate()", NoSuchMethodException.class, e.getClass()); + } + } + + /** + * + * @throws Exception Exception + */ + public void testStrictConstructor() throws Exception + { + // ==================================================================== + // Prove InternetAddress constructor is throwing exception. + // ==================================================================== + + + // test Invalid Email addresses + for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) + { + + try { + + // Create Internet Address using "strict" constructor + new InternetAddress(ARR_INVALID_EMAILS[i]); + + // Expected an exception to be thrown + fail("Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]); + + } catch (Exception ex) { + // Expected Result + } + + } + + // test valid 'quoted' Email addresses + try { + + // Create Internet Address using "strict" constructor + new InternetAddress(VALID_QUOTED_EMAIL); + + } catch (Exception ex) { + fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + + " - " + ex.getMessage()); + } + } + + /** + * + * @throws Exception Exception + */ + public void testValidateMethod() throws Exception + { + if (validateMethod == null) + { + return; + } + + // ==================================================================== + // Prove InternetAddress constructor isn't throwing exception and + // the validate() method is + // ==================================================================== + + for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) + { + + InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe"); + + // N.B. validate() doesn't check addresses containing quotes or '[' + boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0); + int atIndex = ARR_INVALID_EMAILS[i].indexOf("@"); + boolean domainBracket = (atIndex >= 0) && + (ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0); + try { + + validateMethod.invoke(address, null); + + if (!(quoted || domainBracket)) { + fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]); + } + + } catch (Exception ex) { + + if (quoted || domainBracket) { + fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] + + " - " + ex.getMessage()); + } + + } + + } + + // test valid 'quoted' Email addresses + try { + + validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe"), null); + + } catch (Exception ex) { + fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + + " - " + ex.getMessage()); + } + + } + + /** + * + * @throws Exception Exception + */ + public void testValidateMethodCharset() throws Exception + { + if (validateMethod == null) + { + return; + } + + // ==================================================================== + // Prove InternetAddress constructor isn't throwing exception and + // the validate() method is + // ==================================================================== + + for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) + { + + InternetAddress address = new InternetAddress(ARR_INVALID_EMAILS[i], "Joe", "UTF-8"); + + // N.B. validate() doesn't check addresses containing quotes or '[' + boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0); + int atIndex = ARR_INVALID_EMAILS[i].indexOf("@"); + boolean domainBracket = (atIndex >= 0) && + (ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0); + + try { + + validateMethod.invoke(address, null); + + if (!(quoted || domainBracket)) { + fail("Validate " + i + " passed: " + ARR_INVALID_EMAILS[i]); + } + + } catch (Exception ex) { + + if (quoted || domainBracket) { + fail("Validate " + i + " failed: " + ARR_INVALID_EMAILS[i] + + " - " + ex.getMessage()); + } + + } + + } + + // test valid 'quoted' Email addresses + try { + + validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, "Joe", "UTF-8"), null); + + } catch (Exception ex) { + fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + + " - " + ex.getMessage()); + } + } + +} Propchange: jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/InvalidInternetAddressTest.java ------------------------------------------------------------------------------ svn:keywords = Id Author Modified: jakarta/commons/proper/email/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/email/trunk/xdocs/changes.xml?rev=279295&r1=279294&r2=279295&view=diff ============================================================================== --- jakarta/commons/proper/email/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/email/trunk/xdocs/changes.xml Wed Sep 7 03:56:41 2005 @@ -22,6 +22,11 @@ <body> <release version="1.0-rc8-SNAPSHOT" date="in Subversion"> + <action dev="henning" type="update" due-to="Niall Pemberton" issue="36536"> + Replace our usage of address.validate() with simpler code that allows + commons-email to work with javamail 1.2 / J2EE 1.3 environment. Added an + unit test which verifies the behaviour. + </action> <action dev="henning" type="update" due-to="Niall Pemberton" issue="36535"> Add an unit test to check for invalid addresses. </action> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]