Skip to content

Commit 486c109

Browse files
committed
#168: explicitly require and possibly convert property types
1 parent a386f0d commit 486c109

File tree

6 files changed

+83
-37
lines changed

6 files changed

+83
-37
lines changed

src/main/java/org/simplejavamail/internal/util/Preconditions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ public static <T> T checkNonEmptyArgument(@Nullable final T address, @Nonnull fi
1616
}
1717
return address;
1818
}
19+
20+
public static void assumeTrue(boolean expression, String msg) {
21+
if (!expression) {
22+
throw new AssertionError("Wrong assumption:\n\t" + msg);
23+
}
24+
}
1925
}

src/main/java/org/simplejavamail/mailer/MailerBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.simplejavamail.mailer;
22

33
import org.simplejavamail.mailer.config.TransportStrategy;
4+
import org.simplejavamail.util.ConfigLoader;
45

56
import javax.annotation.Nonnull;
67
import javax.annotation.Nullable;
@@ -11,7 +12,6 @@
1112
import static org.simplejavamail.util.ConfigLoader.Property.SMTP_PORT;
1213
import static org.simplejavamail.util.ConfigLoader.Property.SMTP_USERNAME;
1314
import static org.simplejavamail.util.ConfigLoader.Property.TRANSPORT_STRATEGY;
14-
import static org.simplejavamail.util.ConfigLoader.getProperty;
1515
import static org.simplejavamail.util.ConfigLoader.hasProperty;
1616

1717
/**
@@ -135,20 +135,20 @@ public static class MailerRegularBuilder extends MailerGenericBuilder<MailerRegu
135135
*/
136136
MailerRegularBuilder() {
137137
if (hasProperty(SMTP_HOST)) {
138-
withSMTPServerHost((String) getProperty(SMTP_HOST));
138+
withSMTPServerHost(ConfigLoader.getStringProperty(SMTP_HOST));
139139
}
140140
if (hasProperty(SMTP_PORT)) {
141-
withSMTPServerPort((Integer) getProperty(SMTP_PORT));
141+
withSMTPServerPort(ConfigLoader.getIntegerProperty(SMTP_PORT));
142142
}
143143
if (hasProperty(SMTP_USERNAME)) {
144-
withSMTPServerUsername((String) getProperty(SMTP_USERNAME));
144+
withSMTPServerUsername(ConfigLoader.getStringProperty(SMTP_USERNAME));
145145
}
146146
if (hasProperty(SMTP_PASSWORD)) {
147-
withSMTPServerPassword((String) getProperty(SMTP_PASSWORD));
147+
withSMTPServerPassword(ConfigLoader.getStringProperty(SMTP_PASSWORD));
148148
}
149149
withTransportStrategy(TransportStrategy.SMTP);
150150
if (hasProperty(TRANSPORT_STRATEGY)) {
151-
withTransportStrategy((TransportStrategy) getProperty(TRANSPORT_STRATEGY));
151+
withTransportStrategy(ConfigLoader.<TransportStrategy>getProperty(TRANSPORT_STRATEGY));
152152
}
153153
}
154154

src/main/java/org/simplejavamail/mailer/MailerGenericBuilder.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.simplejavamail.util.ConfigLoader.Property.PROXY_PASSWORD;
2323
import static org.simplejavamail.util.ConfigLoader.Property.PROXY_PORT;
2424
import static org.simplejavamail.util.ConfigLoader.Property.PROXY_USERNAME;
25-
import static org.simplejavamail.util.ConfigLoader.getProperty;
2625
import static org.simplejavamail.util.ConfigLoader.hasProperty;
2726

2827
/**
@@ -131,23 +130,23 @@ public abstract class MailerGenericBuilder<T extends MailerGenericBuilder> {
131130
*/
132131
MailerGenericBuilder() {
133132
if (hasProperty(PROXY_HOST)) {
134-
withProxyHost((String) getProperty(PROXY_HOST));
133+
withProxyHost(ConfigLoader.getStringProperty(PROXY_HOST));
135134
}
136135
if (hasProperty(PROXY_PORT)) {
137-
withProxyPort((Integer) getProperty(PROXY_PORT));
136+
withProxyPort(ConfigLoader.getIntegerProperty(PROXY_PORT));
138137
}
139138
if (hasProperty(PROXY_USERNAME)) {
140-
withProxyUsername((String) getProperty(PROXY_USERNAME));
139+
withProxyUsername(ConfigLoader.getStringProperty(PROXY_USERNAME));
141140
}
142141
if (hasProperty(PROXY_PASSWORD)) {
143-
withProxyPassword((String) getProperty(PROXY_PASSWORD));
142+
withProxyPassword(ConfigLoader.getStringProperty(PROXY_PASSWORD));
144143
}
145144

146-
withProxyBridgePort(ConfigLoader.valueOrProperty(null, Property.PROXY_SOCKS5BRIDGE_PORT, DEFAULT_PROXY_BRIDGE_PORT));
147-
withDebugLogging(ConfigLoader.valueOrProperty(null, Property.JAVAXMAIL_DEBUG, false));
148-
withSessionTimeout(ConfigLoader.valueOrProperty(null, Property.DEFAULT_SESSION_TIMEOUT_MILLIS, DEFAULT_SESSION_TIMEOUT_MILLIS));
149-
withThreadPoolSize(ConfigLoader.valueOrProperty(null, Property.DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE));
150-
withTransportModeLoggingOnly(ConfigLoader.valueOrProperty(null, Property.TRANSPORT_MODE_LOGGING_ONLY, DEFAULT_TRANSPORT_MODE_LOGGING_ONLY));
145+
withProxyBridgePort(ConfigLoader.valueOrPropertyAsInteger(null, Property.PROXY_SOCKS5BRIDGE_PORT, DEFAULT_PROXY_BRIDGE_PORT));
146+
withDebugLogging(ConfigLoader.valueOrPropertyAsBoolean(null, Property.JAVAXMAIL_DEBUG, false));
147+
withSessionTimeout(ConfigLoader.valueOrPropertyAsInteger(null, Property.DEFAULT_SESSION_TIMEOUT_MILLIS, DEFAULT_SESSION_TIMEOUT_MILLIS));
148+
withThreadPoolSize(ConfigLoader.valueOrPropertyAsInteger(null, Property.DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE));
149+
withTransportModeLoggingOnly(ConfigLoader.valueOrPropertyAsBoolean(null, Property.TRANSPORT_MODE_LOGGING_ONLY, DEFAULT_TRANSPORT_MODE_LOGGING_ONLY));
151150

152151
withEmailAddressCriteria(EmailAddressCriteria.RFC_COMPLIANT);
153152
trustingAllHosts(true);

src/main/java/org/simplejavamail/mailer/config/TransportStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public enum TransportStrategy {
6767
public Properties generateProperties() {
6868
final Properties props = super.generateProperties();
6969
props.put("mail.transport.protocol", "smtp");
70-
if (ConfigLoader.valueOrProperty(opportunisticTLS, OPPORTUNISTIC_TLS, DEFAULT_OPPORTUNISTIC_TLS)) {
70+
if (ConfigLoader.valueOrPropertyAsBoolean(opportunisticTLS, OPPORTUNISTIC_TLS, DEFAULT_OPPORTUNISTIC_TLS)) {
7171
LOGGER.debug("Opportunistic TLS mode enabled for SMTP plain protocol.");
7272
props.put("mail.smtp.starttls.enable", "true");
7373
props.put("mail.smtp.starttls.required", "false");

src/main/java/org/simplejavamail/util/ConfigLoader.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.simplejavamail.util;
22

3+
import org.simplejavamail.internal.util.SimpleConversions;
34
import org.simplejavamail.mailer.config.TransportStrategy;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67

8+
import javax.annotation.Nonnull;
9+
import javax.annotation.Nullable;
710
import java.io.File;
811
import java.io.FileInputStream;
912
import java.io.FileNotFoundException;
@@ -127,10 +130,38 @@ private ConfigLoader() {
127130
/**
128131
* @return The value if not null or else the value from config file if provided or else <code>null</code>.
129132
*/
133+
@SuppressWarnings("WeakerAccess")
130134
public static <T> T valueOrProperty(final T value, final Property property) {
131135
return valueOrProperty(value, property, null);
132136
}
133-
137+
138+
/**
139+
* See {@link #valueOrProperty(Object, Property, Object)}.
140+
*/
141+
@SuppressWarnings("unchecked")
142+
@Nullable
143+
public static String valueOrPropertyAsString(@Nullable final String value, @Nonnull final Property property, @Nullable final String defaultValue) {
144+
return SimpleConversions.convertToString(valueOrProperty(value, property, defaultValue));
145+
}
146+
147+
/**
148+
* See {@link #valueOrProperty(Object, Property, Object)}.
149+
*/
150+
@SuppressWarnings("unchecked")
151+
@Nullable
152+
public static Boolean valueOrPropertyAsBoolean(@Nullable final Boolean value, @Nonnull final Property property, @Nullable final Boolean defaultValue) {
153+
return SimpleConversions.convertToBoolean(valueOrProperty(value, property, defaultValue));
154+
}
155+
156+
/**
157+
* See {@link #valueOrProperty(Object, Property, Object)}.
158+
*/
159+
@SuppressWarnings("unchecked")
160+
@Nullable
161+
public static Integer valueOrPropertyAsInteger(@Nullable final Integer value, @Nonnull final Property property, @Nullable final Integer defaultValue) {
162+
return SimpleConversions.convertToInteger(valueOrProperty(value, property, defaultValue));
163+
}
164+
134165
/**
135166
* Returns the given value if not null and not empty, otherwise tries to resolve the given property and if still not found resort to the default value if
136167
* provided.
@@ -139,7 +170,8 @@ public static <T> T valueOrProperty(final T value, final Property property) {
139170
*
140171
* @return The value if not null or else the value from config file if provided or else <code>defaultValue</code>.
141172
*/
142-
public static <T> T valueOrProperty(final T value, final Property property, final T defaultValue) {
173+
@Nullable
174+
private static <T> T valueOrProperty(@Nullable final T value, @Nonnull final Property property, @Nullable final T defaultValue) {
143175
if (!valueNullOrEmpty(value)) {
144176
LOGGER.trace("using provided argument value {} for property {}", value, property);
145177
return value;
@@ -156,11 +188,20 @@ public static <T> T valueOrProperty(final T value, final Property property, fina
156188
public static synchronized boolean hasProperty(final Property property) {
157189
return !valueNullOrEmpty(RESOLVED_PROPERTIES.get(property));
158190
}
159-
160-
public static synchronized <T> T getProperty(final Property property) {
161-
//noinspection unchecked
191+
192+
193+
@SuppressWarnings("unchecked")
194+
public static <T> T getProperty(Property property) {
162195
return (T) RESOLVED_PROPERTIES.get(property);
163196
}
197+
198+
public static synchronized String getStringProperty(final Property property) {
199+
return SimpleConversions.convertToString(RESOLVED_PROPERTIES.get(property));
200+
}
201+
202+
public static synchronized Integer getIntegerProperty(final Property property) {
203+
return SimpleConversions.convertToInteger(RESOLVED_PROPERTIES.get(property));
204+
}
164205

165206
/**
166207
* Loads properties from property file on the classpath, if provided. Calling this method only has effect on new Email and Mailer instances after

src/test/java/org/simplejavamail/util/ConfigLoaderTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public void valueOrPropertyDefaultValue()
6464
properties.put(TRANSPORT_STRATEGY, "preconfiguredValue");
6565
ConfigLoaderTestHelper.setResolvedProperties(properties);
6666

67-
assertThat(ConfigLoader.valueOrProperty("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
68-
assertThat(ConfigLoader.valueOrProperty(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("preconfiguredValue");
69-
assertThat(ConfigLoader.valueOrProperty(null, SMTP_HOST, "backup")).isEqualTo("backup");
70-
assertThat(ConfigLoader.valueOrProperty(null, SMTP_HOST, null)).isNull();
67+
assertThat(ConfigLoader.valueOrPropertyAsString("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
68+
assertThat(ConfigLoader.valueOrPropertyAsString(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("preconfiguredValue");
69+
assertThat(ConfigLoader.valueOrPropertyAsString(null, SMTP_HOST, "backup")).isEqualTo("backup");
70+
assertThat(ConfigLoader.valueOrPropertyAsString(null, SMTP_HOST, null)).isNull();
7171
}
7272

7373
@Test
@@ -77,10 +77,10 @@ public void valueOrPropertyEmptyDefaultValue()
7777
properties.put(TRANSPORT_STRATEGY, "default");
7878
ConfigLoaderTestHelper.setResolvedProperties(properties);
7979

80-
assertThat(ConfigLoader.valueOrProperty("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
81-
assertThat(ConfigLoader.valueOrProperty(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("default");
82-
assertThat(ConfigLoader.valueOrProperty("", TRANSPORT_STRATEGY, "backup")).isEqualTo("default");
83-
assertThat(ConfigLoader.valueOrProperty(null, TRANSPORT_STRATEGY, null)).isEqualTo("default");
80+
assertThat(ConfigLoader.valueOrPropertyAsString("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
81+
assertThat(ConfigLoader.valueOrPropertyAsString(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("default");
82+
assertThat(ConfigLoader.valueOrPropertyAsString("", TRANSPORT_STRATEGY, "backup")).isEqualTo("default");
83+
assertThat(ConfigLoader.valueOrPropertyAsString(null, TRANSPORT_STRATEGY, null)).isEqualTo("default");
8484
}
8585

8686
@Test
@@ -90,10 +90,10 @@ public void valueOrPropertyDefaultValueEmptyDefault()
9090
properties.put(TRANSPORT_STRATEGY, "");
9191
ConfigLoaderTestHelper.setResolvedProperties(properties);
9292

93-
assertThat(ConfigLoader.valueOrProperty("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
94-
assertThat(ConfigLoader.valueOrProperty(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("backup");
95-
assertThat(ConfigLoader.valueOrProperty("", TRANSPORT_STRATEGY, "backup")).isEqualTo("backup");
96-
assertThat(ConfigLoader.valueOrProperty(null, TRANSPORT_STRATEGY, null)).isNull();
93+
assertThat(ConfigLoader.valueOrPropertyAsString("value", TRANSPORT_STRATEGY, "backup")).isEqualTo("value");
94+
assertThat(ConfigLoader.valueOrPropertyAsString(null, TRANSPORT_STRATEGY, "backup")).isEqualTo("backup");
95+
assertThat(ConfigLoader.valueOrPropertyAsString("", TRANSPORT_STRATEGY, "backup")).isEqualTo("backup");
96+
assertThat(ConfigLoader.valueOrPropertyAsString(null, TRANSPORT_STRATEGY, null)).isNull();
9797
}
9898

9999
@Test
@@ -102,8 +102,8 @@ public void overridefromSystemVariables()
102102
Map<Property, Object> properties = new HashMap<>();
103103
properties.put(TRANSPORT_STRATEGY, "preconfiguredValue");
104104

105-
assertThat(ConfigLoader.valueOrProperty("value", PROXY_USERNAME, "backup")).isEqualTo("value");
106-
assertThat(ConfigLoader.valueOrProperty(null, PROXY_USERNAME, "backup")).isEqualTo("username proxy"); // from config file
105+
assertThat(ConfigLoader.valueOrPropertyAsString("value", PROXY_USERNAME, "backup")).isEqualTo("value");
106+
assertThat(ConfigLoader.valueOrPropertyAsString(null, PROXY_USERNAME, "backup")).isEqualTo("username proxy"); // from config file
107107
// cannot be tested:
108108
// System.getenv().put("simplejavamail.proxy.username", "override1");
109109
// restoreOriginalStaticProperties();
@@ -112,7 +112,7 @@ public void overridefromSystemVariables()
112112
System.setProperty("simplejavamail.proxy.username", "override2");
113113
System.out.println("simplejavamail.proxy.username" + ": " + System.getProperty("simplejavamail.proxy.username"));
114114
restoreOriginalStaticProperties();
115-
assertThat(ConfigLoader.valueOrProperty(null, PROXY_USERNAME, "backup")).isEqualTo("override2");
115+
assertThat(ConfigLoader.valueOrPropertyAsString(null, PROXY_USERNAME, "backup")).isEqualTo("override2");
116116
}
117117

118118
@Test

0 commit comments

Comments
 (0)