You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2022/06/16 04:01:13 UTC

[commons-configuration] 01/02: CONFIGURATION-813: simplify mailapi conversions

This is an automated email from the ASF dual-hosted git repository.

mattjuntunen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git

commit 4e76fd8c9e9139314ae83faa21c9da2aee786031
Author: Matt Juntunen <ma...@apache.org>
AuthorDate: Tue Jun 14 23:34:00 2022 -0400

    CONFIGURATION-813: simplify mailapi conversions
---
 .../configuration2/convert/PropertyConverter.java  | 40 +++++++++-------------
 .../configuration2/TestDataConfiguration.java      | 21 +++---------
 2 files changed, 22 insertions(+), 39 deletions(-)

diff --git a/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java b/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
index 2c2b451e..255bedab 100644
--- a/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
+++ b/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
@@ -67,10 +67,10 @@ public final class PropertyConverter {
     /** Constant for the argument classes of the Number constructor that takes a String. */
     private static final Class<?>[] CONSTR_ARGS = {String.class};
 
-    /** The fully qualified name of {@code javax.mail.internet.InternetAddress} */
-    private static final String INTERNET_ADDRESS_CLASSNAME = "javax.mail.internet.InternetAddress";
+    /** The fully qualified name of {@code javax.mail.internet.InternetAddress}, as used in the javamail-1.* API.  */
+    private static final String INTERNET_ADDRESS_CLASSNAME_JAVAX = "javax.mail.internet.InternetAddress";
 
-    /** The fully qualified name of {@code jakarta.mail.internet.InternetAddress} */
+    /** The fully qualified name of {@code jakarta.mail.internet.InternetAddress}, as used in the javamail-2.0+ API. */
     private static final String INTERNET_ADDRESS_CLASSNAME_JAKARTA = "jakarta.mail.internet.InternetAddress";
 
     /**
@@ -151,10 +151,12 @@ public final class PropertyConverter {
             return convertToEnum(cls, value);
         } else if (Color.class.equals(cls)) {
             return toColor(value);
-        } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
-            return toInternetAddress(value);
+        } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME_JAVAX)) {
+            // javamail-1.* With javax.mail.* namespace.
+            return toInternetAddress(value, INTERNET_ADDRESS_CLASSNAME_JAVAX);
         } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME_JAKARTA)) {
-            return toInternetAddress(value);
+            // javamail-2.0+, with jakarta.mail.* namespace.
+            return toInternetAddress(value, INTERNET_ADDRESS_CLASSNAME_JAKARTA);
         } else if (InetAddress.class.isAssignableFrom(cls)) {
             return toInetAddress(value);
         } else if (Duration.class.equals(cls)) {
@@ -600,36 +602,28 @@ public final class PropertyConverter {
     }
 
     /**
-     * Convert the specified value into an email address.
+     * Convert the specified value into an email address with the given class name.
      *
      * @param value the value to convert
+     * @param targetClassName the fully qualified name of the {@code InternetAddress} class to convert to, e.g.,
+     *      {@value #INTERNET_ADDRESS_CLASSNAME_JAVAX} or {@value #INTERNET_ADDRESS_CLASSNAME_JAKARTA}
      * @return the converted value
      * @throws ConversionException thrown if the value cannot be converted to an email address
      *
      * @since 1.5
      */
-    static Object toInternetAddress(final Object value) throws ConversionException {
-        if (value.getClass().getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
-            return value;
-        }
-        if (value.getClass().getName().equals(INTERNET_ADDRESS_CLASSNAME_JAKARTA)) {
+    static Object toInternetAddress(final Object value, final String targetClassName) throws ConversionException {
+        if (value.getClass().getName().equals(targetClassName)) {
             return value;
         }
         if (!(value instanceof String)) {
-            throw new ConversionException("The value " + value + " can't be converted to a InternetAddress");
+            throw new ConversionException("The value " + value + " can't be converted to an InternetAddress");
         }
         try {
-            try {
-                // javamail-2.0+, with jakarta.mail.* namespace.
-                final Constructor<?> ctor = Class.forName(INTERNET_ADDRESS_CLASSNAME_JAKARTA).getConstructor(String.class);
-                return ctor.newInstance(value);
-            } catch (ClassNotFoundException e) {
-                // maybe javamail-1.*? With javax.mail.* namespace.
-                final Constructor<?> ctor = Class.forName(INTERNET_ADDRESS_CLASSNAME).getConstructor(String.class);
-                return ctor.newInstance(value);
-            }
+            final Constructor<?> ctor = Class.forName(targetClassName).getConstructor(String.class);
+            return ctor.newInstance(value);
         } catch (final Exception e) {
-            throw new ConversionException("The value " + value + " can't be converted to a InternetAddress", e);
+            throw new ConversionException("The value " + value + " can't be converted to an InternetAddress", e);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
index af393b0a..f9099493 100644
--- a/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
@@ -48,6 +48,7 @@ import org.easymock.EasyMock;
 import org.junit.Before;
 import org.junit.Test;
 
+import jakarta.mail.internet.InternetAddress;
 import junitx.framework.ArrayAssert;
 import junitx.framework.ListAssert;
 
@@ -71,18 +72,6 @@ public class TestDataConfiguration {
     /** The test instance. */
     private DataConfiguration conf;
 
-    /**
-     * Create an instance of InternetAddress. This trick is necessary to compile and run the test with Java 1.3 and the
-     * javamail-1.4 which is not compatible with Java 1.3.
-     *
-     * <p>javamail-2.0 had a namespace change, moving javax.mail.* to jakarta.mail.*. This test verifies if we have
-     * javax.mail.* in the classpath before trying the Jakarta classes.</p>
-     */
-    private Object createInternetAddress(final String email) throws Exception {
-        final Class<?> cls = Class.forName("jakarta.mail.internet.InternetAddress");
-        return cls.getConstructor(String.class).newInstance(email);
-    }
-
     @Before
     public void setUp() throws Exception {
         final BaseConfiguration baseConfig = new BaseConfiguration();
@@ -358,7 +347,7 @@ public class TestDataConfiguration {
         // email address
         conf.addProperty("email.string", "ebourg@apache.org");
         conf.addProperty("email.string.interpolated", "${email.string}");
-        conf.addProperty("email.object", createInternetAddress("ebourg@apache.org"));
+        conf.addProperty("email.object", new InternetAddress("ebourg@apache.org"));
     }
 
     /**
@@ -842,7 +831,7 @@ public class TestDataConfiguration {
         }
 
         try {
-            conf.get(Class.forName("jakarta.mail.internet.InternetAddress"), "key1");
+            conf.get(InternetAddress.class, "key1");
             fail("getInternetAddress didn't throw a ConversionException");
         } catch (final ConversionException e) {
             // expected
@@ -1829,7 +1818,7 @@ public class TestDataConfiguration {
 
     @Test
     public void testGetInternetAddress() throws Exception {
-        final Object expected = createInternetAddress("ebourg@apache.org");
+        final Object expected = new InternetAddress("ebourg@apache.org");
 
         // address as string
         assertEquals(expected, conf.get(expected.getClass(), "email.string"));
@@ -1851,7 +1840,7 @@ public class TestDataConfiguration {
 
     @Test(expected = ConversionException.class)
     public void testGetInternetAddressInvalidType() throws Exception {
-        final Object expected = createInternetAddress("ebourg@apache.org");
+        final Object expected = new InternetAddress("ebourg@apache.org");
         conf.setProperty("email.invalid", "ebourg@apache@org");
         conf.get(expected.getClass(), "email.invalid");
     }