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:12 UTC

[commons-configuration] branch master updated (9a828da1 -> c5c6227e)

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

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


    from 9a828da1 Use System.lineSeparator()
     new 4e76fd8c CONFIGURATION-813: simplify mailapi conversions
     new c5c6227e use fake data in tests

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/configuration2/DataConfiguration.java  |  2 +-
 .../configuration2/convert/PropertyConverter.java  | 40 +++++++++-------------
 .../configuration2/TestDataConfiguration.java      | 27 +++++----------
 .../configuration2/TestXMLConfiguration.java       |  2 +-
 4 files changed, 27 insertions(+), 44 deletions(-)


[commons-configuration] 02/02: use fake data in tests

Posted by ma...@apache.org.
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 c5c6227e1dc8fcc463c1b021fa49b942c54891fc
Author: Matt Juntunen <ma...@apache.org>
AuthorDate: Wed Jun 15 23:45:24 2022 -0400

    use fake data in tests
---
 .../org/apache/commons/configuration2/DataConfiguration.java |  2 +-
 .../apache/commons/configuration2/TestDataConfiguration.java | 12 ++++++------
 .../apache/commons/configuration2/TestXMLConfiguration.java  |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/configuration2/DataConfiguration.java b/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
index 0b532dad..997c4db0 100644
--- a/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
@@ -65,7 +65,7 @@ import org.apache.commons.lang3.StringUtils;
  * title.color = #0000FF
  * remote.host = 192.168.0.53
  * default.locales = fr,en,de
- * email.contact = ebourg@apache.org, tester@test.org
+ * email.contact = dev@test.org, tester@test.org
  * </pre>
  *
  * Usage:
diff --git a/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
index f9099493..de0300a4 100644
--- a/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
@@ -345,9 +345,9 @@ public class TestDataConfiguration {
         conf.addProperty("ip.object", InetAddress.getByName("127.0.0.1"));
 
         // email address
-        conf.addProperty("email.string", "ebourg@apache.org");
+        conf.addProperty("email.string", "dev@test.org");
         conf.addProperty("email.string.interpolated", "${email.string}");
-        conf.addProperty("email.object", new InternetAddress("ebourg@apache.org"));
+        conf.addProperty("email.object", new InternetAddress("dev@test.org"));
     }
 
     /**
@@ -1818,7 +1818,7 @@ public class TestDataConfiguration {
 
     @Test
     public void testGetInternetAddress() throws Exception {
-        final Object expected = new InternetAddress("ebourg@apache.org");
+        final Object expected = new InternetAddress("dev@test.org");
 
         // address as string
         assertEquals(expected, conf.get(expected.getClass(), "email.string"));
@@ -1829,7 +1829,7 @@ public class TestDataConfiguration {
         // interpolated value
         assertEquals(expected, conf.get(expected.getClass(), "email.string.interpolated"));
 
-        conf.setProperty("email.invalid", "ebourg@apache@org");
+        conf.setProperty("email.invalid", "dev@test@org");
         try {
             conf.get(expected.getClass(), "email.invalid");
             fail("ConversionException should be thrown for invalid emails");
@@ -1840,8 +1840,8 @@ public class TestDataConfiguration {
 
     @Test(expected = ConversionException.class)
     public void testGetInternetAddressInvalidType() throws Exception {
-        final Object expected = new InternetAddress("ebourg@apache.org");
-        conf.setProperty("email.invalid", "ebourg@apache@org");
+        final Object expected = new InternetAddress("dev@test.org");
+        conf.setProperty("email.invalid", "dev@test@org");
         conf.get(expected.getClass(), "email.invalid");
     }
 
diff --git a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
index 9985dfe9..2e7510fb 100644
--- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
@@ -1371,7 +1371,7 @@ public class TestXMLConfiguration {
         conf.setEntityResolver(resolver);
         conf.setSchemaValidation(true);
         load(conf, testFile2);
-        conf.setProperty("Employee.Email", "JohnDoe@apache.org");
+        conf.setProperty("Employee.Email", "JohnDoe@test.org");
         try {
             conf.validate();
             fail("No validation failure on save");


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

Posted by ma...@apache.org.
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");
     }