Skip to content

Commit b8c63ec

Browse files
committed
#260: Added api for setting baseUrl as String and added more junit tests and fixed subsequent bug
1 parent 20c2158 commit b8c63ec

File tree

9 files changed

+116
-14
lines changed

9 files changed

+116
-14
lines changed

modules/core-module/src/main/java/org/simplejavamail/api/email/EmailPopulatingBuilder.java

+7
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ public interface EmailPopulatingBuilder {
881881
*/
882882
EmailPopulatingBuilder withEmbeddedImageBaseClassPath(@NotNull final String embeddedImageBaseClassPath);
883883

884+
/**
885+
* Delegates to {@link #withEmbeddedImageBaseUrl(URL)}.
886+
*
887+
* @param embeddedImageBaseUrl The base URL used when resolving images sources in HTML text.
888+
*/
889+
EmailPopulatingBuilder withEmbeddedImageBaseUrl(@NotNull final String embeddedImageBaseUrl);
890+
884891
/**
885892
* Sets the base URL used when resolving images sources in HTML text. Without this, the resource needs to be an absolute URL (or a file/classpath resource).
886893
* <p>

modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailException.java

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class EmailException extends MailException {
1313
static final String ERROR_READING_FROM_PEM_INPUTSTREAM = "Was unable to convert PEM data to X509 certificate";
1414
static final String ERROR_LOADING_PROVIDER_FOR_SMIME_SUPPORT = "Unable to load certificate (missing bouncy castle), is the S/MIME module on the class path?";
1515
static final String ERROR_RESOLVING_IMAGE_DATASOURCE = "Unable to dynamically resolve data source for the following image src: %s";
16+
static final String ERROR_PARSING_URL = "Unable to parse URL: %s";
1617

1718
EmailException(@SuppressWarnings("SameParameterValue") final String message) {
1819
super(message);

modules/simple-java-mail/src/main/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
6+
import org.simplejavamail.MailException;
67
import org.simplejavamail.api.email.AttachmentResource;
78
import org.simplejavamail.api.email.CalendarMethod;
89
import org.simplejavamail.api.email.Email;
@@ -28,6 +29,7 @@
2829
import java.io.FileNotFoundException;
2930
import java.io.IOException;
3031
import java.io.InputStream;
32+
import java.net.MalformedURLException;
3133
import java.net.URL;
3234
import java.security.NoSuchProviderException;
3335
import java.security.cert.CertificateException;
@@ -73,6 +75,7 @@
7375
import static org.simplejavamail.config.ConfigLoader.getStringProperty;
7476
import static org.simplejavamail.config.ConfigLoader.hasProperty;
7577
import static org.simplejavamail.email.internal.EmailException.ERROR_LOADING_PROVIDER_FOR_SMIME_SUPPORT;
78+
import static org.simplejavamail.email.internal.EmailException.ERROR_PARSING_URL;
7679
import static org.simplejavamail.email.internal.EmailException.ERROR_READING_FROM_FILE;
7780
import static org.simplejavamail.email.internal.EmailException.ERROR_READING_FROM_PEM_INPUTSTREAM;
7881
import static org.simplejavamail.email.internal.EmailException.ERROR_RESOLVING_IMAGE_DATASOURCE;
@@ -335,10 +338,10 @@ public class EmailPopulatingBuilderImpl implements InternalEmailPopulatingBuilde
335338
withEmbeddedImageBaseDir(assumeNonNull(getStringProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_DIR)));
336339
}
337340
if (hasProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL)) {
338-
withEmbeddedImageBaseDir(assumeNonNull(getStringProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL)));
341+
withEmbeddedImageBaseUrl(assumeNonNull(getStringProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL)));
339342
}
340343
if (hasProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH)) {
341-
withEmbeddedImageBaseDir(assumeNonNull(getStringProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH)));
344+
withEmbeddedImageBaseClassPath(assumeNonNull(getStringProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH)));
342345
}
343346
}
344347
}
@@ -1426,6 +1429,18 @@ public EmailPopulatingBuilder withEmbeddedImageBaseClassPath(@NotNull final Stri
14261429
return this;
14271430
}
14281431

1432+
/**
1433+
* @see EmailPopulatingBuilder#withEmbeddedImageBaseUrl(String)
1434+
*/
1435+
@Override
1436+
public EmailPopulatingBuilder withEmbeddedImageBaseUrl(@NotNull final String embeddedImageBaseUrl) {
1437+
try {
1438+
return withEmbeddedImageBaseUrl(new URL(embeddedImageBaseUrl));
1439+
} catch (MalformedURLException e) {
1440+
throw new EmailException(format(ERROR_PARSING_URL, embeddedImageBaseUrl), e);
1441+
}
1442+
}
1443+
14291444
/**
14301445
* @see EmailPopulatingBuilder#withEmbeddedImageBaseUrl(URL)
14311446
*/

modules/simple-java-mail/src/test/java/org/simplejavamail/config/ConfigLoaderTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_SUBJECT;
2525
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_TO_ADDRESS;
2626
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_TO_NAME;
27+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH;
28+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_DIR;
29+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL;
2730
import static org.simplejavamail.config.ConfigLoader.Property.JAVAXMAIL_DEBUG;
2831
import static org.simplejavamail.config.ConfigLoader.Property.PROXY_HOST;
2932
import static org.simplejavamail.config.ConfigLoader.Property.PROXY_PASSWORD;
@@ -198,6 +201,10 @@ public void loadPropertiesFromFileClassPath() {
198201
assertThat(ConfigLoader.getProperty(SMIME_SIGNING_KEY_ALIAS)).isEqualTo("smime_test_user_alias");
199202
assertThat(ConfigLoader.getProperty(SMIME_SIGNING_KEY_PASSWORD)).isEqualTo("letmein");
200203
assertThat(ConfigLoader.getProperty(SMIME_ENCRYPTION_CERTIFICATE)).isEqualTo("src/test/resources/pkcs12/smime_test_user.pem.standard.crt");
204+
205+
assertThat(ConfigLoader.getProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_DIR)).isEqualTo("unable to test this as the value is variable depending on junit working folder");
206+
assertThat(ConfigLoader.getProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL)).isEqualTo("http://www.simplejavamail.org");
207+
assertThat(ConfigLoader.getProperty(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH)).isEqualTo("/pkcs12");
201208
}
202209

203210
@Test
@@ -220,7 +227,6 @@ public void loadPropertiesAddingMode() {
220227

221228
@Test
222229
public void loadPropertiesFromInputStream() {
223-
224230
String s = "simplejavamail.javaxmail.debug=true\n"
225231
+ "simplejavamail.transportstrategy=SMTPS\n"
226232
+ "simplejavamail.smtp.host=smtp.default.com\n"

modules/simple-java-mail/src/test/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl1Test.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
4747
import static org.assertj.core.api.Assumptions.assumeThat;
4848
import static org.mockito.Mockito.mock;
49+
import static org.simplejavamail.util.TestDataHelper.getUrl;
4950
import static org.simplejavamail.util.TestDataHelper.loadPkcs12KeyStore;
5051
import static testutil.CertificationUtil.extractSignedBy;
5152

@@ -959,14 +960,6 @@ public void testEmbeddingImagesWithDynamicDataSourceResolution_relativeUrlWithBa
959960
verifyEmbeddedImage(email, "Download Simple Java Mail");
960961
}
961962

962-
private int getUrl(String urlStr) {
963-
try {
964-
return ((HttpURLConnection) new URL(urlStr).openConnection()).getResponseCode();
965-
} catch (IOException e) {
966-
return HttpURLConnection.HTTP_NOT_FOUND;
967-
}
968-
}
969-
970963
@Test
971964
public void testEmbeddingImagesWithDynamicDataSourceResolution_classPathPath()
972965
throws IOException {

modules/simple-java-mail/src/test/java/org/simplejavamail/email/internal/EmailPopulatingBuilderImpl2Test.java

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package org.simplejavamail.email.internal;
22

33
import org.apache.commons.collections4.map.HashedMap;
4+
import org.assertj.core.api.iterable.Extractor;
45
import org.junit.Test;
6+
import org.simplejavamail.api.email.AttachmentResource;
57
import org.simplejavamail.api.email.Email;
68
import org.simplejavamail.api.email.EmailAssert;
79
import org.simplejavamail.api.email.Recipient;
8-
import org.simplejavamail.api.mailer.config.Pkcs12Config;
910
import org.simplejavamail.config.ConfigLoader.Property;
1011
import org.simplejavamail.email.EmailBuilder;
1112
import testutil.ConfigLoaderTestHelper;
1213

14+
import java.io.IOException;
15+
import java.net.HttpURLConnection;
1316
import java.util.Map;
1417

18+
import static demo.ResourceFolderHelper.determineResourceFolder;
1519
import static javax.mail.Message.RecipientType.BCC;
1620
import static javax.mail.Message.RecipientType.CC;
1721
import static javax.mail.Message.RecipientType.TO;
1822
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assumptions.assumeThat;
1924
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_BCC_ADDRESS;
2025
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_BCC_NAME;
2126
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_BOUNCETO_ADDRESS;
@@ -29,15 +34,25 @@
2934
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_SUBJECT;
3035
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_TO_ADDRESS;
3136
import static org.simplejavamail.config.ConfigLoader.Property.DEFAULT_TO_NAME;
37+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH;
38+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_DIR;
39+
import static org.simplejavamail.config.ConfigLoader.Property.EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL;
3240
import static org.simplejavamail.config.ConfigLoader.Property.SMIME_ENCRYPTION_CERTIFICATE;
3341
import static org.simplejavamail.config.ConfigLoader.Property.SMIME_SIGNING_KEYSTORE;
3442
import static org.simplejavamail.config.ConfigLoader.Property.SMIME_SIGNING_KEYSTORE_PASSWORD;
3543
import static org.simplejavamail.config.ConfigLoader.Property.SMIME_SIGNING_KEY_ALIAS;
3644
import static org.simplejavamail.config.ConfigLoader.Property.SMIME_SIGNING_KEY_PASSWORD;
45+
import static org.simplejavamail.util.TestDataHelper.getUrl;
3746
import static org.simplejavamail.util.TestDataHelper.loadPkcs12KeyStore;
3847

3948
public class EmailPopulatingBuilderImpl2Test {
4049

50+
private static final String RESOURCES_PATH = determineResourceFolder("simple-java-mail") + "/test/resources";
51+
52+
private static final String DOWNLOAD_SIMPLE_JAVA_MAIL = "Download Simple Java Mail";
53+
private static final String CREATE_SELF_SIGNED_S_MIME_CERTIFICATES = "Create Self-Signed S/MIME Certificates";
54+
private static final String CONSOLE_NAME_CONSOLE_TARGET_SYSTEM_OUT = "<Console name=\"console\" target=\"SYSTEM_OUT\">";
55+
4156
@Test
4257
public void testConstructorApplyingPreconfiguredDefaults1() throws Exception {
4358
Map<Property, Object> value = new HashedMap<>();
@@ -100,4 +115,51 @@ public void testConstructorApplyingPreconfiguredDefaults2() throws Exception {
100115

101116
assertThat(email.getX509CertificateForSmimeEncryption()).isNotNull();
102117
}
118+
119+
@Test
120+
public void testConstructorApplyingPreconfiguredDefaults_EmbeddedImageResolving() throws Exception {
121+
assumeThat(getUrl("http://www.simplejavamail.org")).isEqualTo(HttpURLConnection.HTTP_OK);
122+
123+
HashedMap<Property, Object> value = new HashedMap<>();
124+
125+
value.put(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_DIR, RESOURCES_PATH);
126+
value.put(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_URL, "http://www.simplejavamail.org");
127+
value.put(EMBEDDEDIMAGES_DYNAMICRESOLUTION_BASE_CLASSPATH, "/pkcs12");
128+
129+
ConfigLoaderTestHelper.setResolvedProperties(value);
130+
131+
Email email = EmailBuilder.startingBlank()
132+
.withHTMLText("<img src=\"cid:cid_name\"/>")
133+
.appendTextHTML("<img src=\"download.html\"/>") // comes from simplejavamail.org
134+
.appendTextHTML("<img src=\"/how-to.html\"/>") // comes from classpath
135+
.appendTextHTML("<img src=\"log4j2.xml\"/>") // comes from folder
136+
.buildEmail();
137+
138+
assertThat(email.getEmbeddedImages())
139+
.extracting(new DatasourceReadingExtractor())
140+
.containsExactlyInAnyOrder(
141+
DOWNLOAD_SIMPLE_JAVA_MAIL,
142+
CREATE_SELF_SIGNED_S_MIME_CERTIFICATES,
143+
CONSOLE_NAME_CONSOLE_TARGET_SYSTEM_OUT
144+
);
145+
}
146+
147+
private static class DatasourceReadingExtractor implements Extractor<AttachmentResource, String> {
148+
@Override
149+
public String extract(final AttachmentResource input) {
150+
try {
151+
final String sourceContent = input.readAllData();
152+
if (sourceContent.contains(DOWNLOAD_SIMPLE_JAVA_MAIL)) {
153+
return DOWNLOAD_SIMPLE_JAVA_MAIL;
154+
} else if (sourceContent.contains(CREATE_SELF_SIGNED_S_MIME_CERTIFICATES)) {
155+
return CREATE_SELF_SIGNED_S_MIME_CERTIFICATES;
156+
} else if (sourceContent.contains(CONSOLE_NAME_CONSOLE_TARGET_SYSTEM_OUT)) {
157+
return CONSOLE_NAME_CONSOLE_TARGET_SYSTEM_OUT;
158+
}
159+
return "";
160+
} catch (IOException e) {
161+
throw new AssertionError();
162+
}
163+
}
164+
}
103165
}

modules/simple-java-mail/src/test/java/org/simplejavamail/util/TestDataHelper.java

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.jetbrains.annotations.NotNull;
88
import javax.mail.util.ByteArrayDataSource;
99

10+
import java.io.IOException;
11+
import java.net.HttpURLConnection;
12+
import java.net.URL;
13+
1014
import static demo.ResourceFolderHelper.determineResourceFolder;
1115
import static java.util.Objects.requireNonNull;
1216

@@ -39,4 +43,12 @@ public static Pkcs12Config loadPkcs12KeyStore() {
3943
.keyPassword("letmein")
4044
.build();
4145
}
46+
47+
public static int getUrl(String urlStr) {
48+
try {
49+
return ((HttpURLConnection) new URL(urlStr).openConnection()).getResponseCode();
50+
} catch (IOException e) {
51+
return HttpURLConnection.HTTP_NOT_FOUND;
52+
}
53+
}
4254
}

modules/simple-java-mail/src/test/resources/simplejavamail.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ simplejavamail.smime.signing.keystore=src/test/resources/pkcs12/smime_keystore.p
2727
simplejavamail.smime.signing.keystore_password=letmein
2828
simplejavamail.smime.signing.key_alias=smime_test_user_alias
2929
simplejavamail.smime.signing.key_password=letmein
30-
simplejavamail.smime.encryption.certificate=src/test/resources/pkcs12/smime_test_user.pem.standard.crt
30+
simplejavamail.smime.encryption.certificate=src/test/resources/pkcs12/smime_test_user.pem.standard.crt
31+
simplejavamail.embeddedimages.dynamicresolution.base.dir=
32+
simplejavamail.embeddedimages.dynamicresolution.base.url=
33+
simplejavamail.embeddedimages.dynamicresolution.base.classpath=

modules/spring-module/src/test/resources/simplejavamail.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ simplejavamail.smime.signing.keystore=src/test/resources/pkcs12/smime_keystore.p
2727
simplejavamail.smime.signing.keystore_password=letmein
2828
simplejavamail.smime.signing.key_alias=smime_test_user_alias
2929
simplejavamail.smime.signing.key_password=letmein
30-
simplejavamail.smime.encryption.certificate=src/test/resources/pkcs12/smime_test_user.pem.standard.crt
30+
simplejavamail.smime.encryption.certificate=src/test/resources/pkcs12/smime_test_user.pem.standard.crt
31+
simplejavamail.embeddedimages.dynamicresolution.base.dir=
32+
simplejavamail.embeddedimages.dynamicresolution.base.url=
33+
simplejavamail.embeddedimages.dynamicresolution.base.classpath=

0 commit comments

Comments
 (0)