Skip to content

Commit a05eccb

Browse files
committed
MultiPartEmail attach methods now throw an IOException if the given file
is a directory. Fix tests on Java 25 and up
1 parent 4c96771 commit a05eccb

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,16 @@
463463
</plugin>
464464
</plugins>
465465
</reporting>
466-
467466
<profiles>
468467
<profile>
469468
<id>java17</id>
470469
<!-- For testing with Powermock. -->
471470
<activation>
472471
<jdk>[17,)</jdk>
473472
</activation>
473+
<properties>
474+
<mockito-core.version>5.21.0</mockito-core.version>
475+
</properties>
474476
<build>
475477
<plugins>
476478
<plugin>
@@ -495,14 +497,13 @@
495497
</profile>
496498
<profile>
497499
<id>java8</id>
498-
<activation>
499-
<jdk>[1.8,11)</jdk>
500-
</activation>
501-
<properties>
502-
<mockito-core.version>4.11.0</mockito-core.version>
503-
</properties>
500+
<activation>
501+
<jdk>[1.8,11)</jdk>
502+
</activation>
503+
<properties>
504+
<mockito-core.version>4.11.0</mockito-core.version>
505+
</properties>
504506
</profile>
505-
506507
<profile>
507508
<id>rc</id>
508509
<distributionManagement>
@@ -515,5 +516,4 @@
515516
</distributionManagement>
516517
</profile>
517518
</profiles>
518-
519519
</project>

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<!-- FIX -->
2828
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix minor PMD violations (Unnecessary qualifier).</action>
2929
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Apache RAT plugin console warnings.</action>
30+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix tests on Java 25 and up.</action>
31+
<action type="fix" dev="ggregory" due-to="Gary Gregory, Henri Cook, Sebb">MultiPartEmail attach methods now throw an IOException if the given file is a directory.</action>
3032
<!-- ADD -->
3133
<!-- UPDATE -->
3234
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 65 to 96.</action>

src/main/java/org/apache/commons/mail/EmailUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2325
import java.util.BitSet;
2426
import java.util.Collection;
2527
import java.util.Locale;
2628
import java.util.Map;
29+
import java.util.Objects;
2730
import java.util.Random;
2831

2932
import javax.mail.MessagingException;
@@ -93,6 +96,42 @@ final class EmailUtils {
9396
SAFE_URL.set('@');
9497
}
9598

99+
/**
100+
* Checks that the given file exists and is not a directory.
101+
*
102+
* @param file the file to check.
103+
* @return the given file if it exists and is not a directory.
104+
* @throws IOException if the given file does not exist or is a directory.
105+
*/
106+
static File check(final File file) throws IOException {
107+
Objects.requireNonNull(file, "file");
108+
if (!file.exists()) {
109+
throw new IOException("\"" + file + "\" does not exist");
110+
}
111+
if (file.isDirectory()) {
112+
throw new IOException("\"" + file + "\" is a directory");
113+
}
114+
return file;
115+
}
116+
117+
/**
118+
* Checks that the given file exists and is not a directory.
119+
*
120+
* @param file the file to check.
121+
* @return the given file if it exists and is not a directory.
122+
* @throws IOException if the given file does not exist or is a directory.
123+
*/
124+
static Path check(final Path file) throws IOException {
125+
Objects.requireNonNull(file, "file");
126+
if (!Files.exists(file)) {
127+
throw new IOException("\"" + file + "\" does not exist");
128+
}
129+
if (Files.isDirectory(file)) {
130+
throw new IOException("\"" + file + "\" is a directory");
131+
}
132+
return file;
133+
}
134+
96135
/**
97136
* Encodes an input string according to RFC 2392. Unsafe characters are escaped.
98137
*

src/main/java/org/apache/commons/mail/MultiPartEmail.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.io.UnsupportedEncodingException;
2323
import java.net.URL;
24-
import java.nio.file.Files;
2524
import java.nio.file.OpenOption;
2625
import java.nio.file.Path;
2726
import java.util.Objects;
@@ -200,7 +199,7 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
200199
String fileName = null;
201200
try {
202201
fileName = attachment.getPath();
203-
final File file = new File(fileName);
202+
final File file = EmailUtils.check(new File(fileName));
204203
if (!file.exists()) {
205204
throw new IOException("\"" + fileName + "\" does not exist");
206205
}
@@ -225,9 +224,7 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
225224
public MultiPartEmail attach(final File file) throws EmailException {
226225
final String fileName = file.getAbsolutePath();
227226
try {
228-
if (!file.exists()) {
229-
throw new IOException("\"" + fileName + "\" does not exist");
230-
}
227+
EmailUtils.check(file);
231228
return attach(new FileDataSource(file), file.getName(), null, EmailAttachment.ATTACHMENT);
232229
} catch (final IOException e) {
233230
throw new EmailException("Cannot attach file \"" + fileName + "\"", e);
@@ -246,9 +243,7 @@ public MultiPartEmail attach(final File file) throws EmailException {
246243
public MultiPartEmail attach(final Path file, final OpenOption... options) throws EmailException {
247244
final Path fileName = file.toAbsolutePath();
248245
try {
249-
if (!Files.exists(file)) {
250-
throw new IOException("\"" + fileName + "\" does not exist");
251-
}
246+
EmailUtils.check(file);
252247
return attach(new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options), Objects.toString(file.getFileName(), null), null,
253248
EmailAttachment.ATTACHMENT);
254249
} catch (final IOException e) {

src/test/java/org/apache/commons/mail/MultiPartEmailTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public void testAttachFile() throws Exception {
133133
final EmailAttachment attachment4 = new EmailAttachment();
134134
attachment4.setPath("");
135135
assertThrows(EmailException.class, () -> email.attach(attachment4));
136+
attachment4.setPath("ThisFileDoesNotExist.txt");
137+
assertThrows(EmailException.class, () -> email.attach(attachment4));
138+
attachment4.setPath("target"); // a directory, not a file
139+
assertThrows(EmailException.class, () -> email.attach(attachment4));
136140
}
137141

138142
@Test
@@ -176,6 +180,10 @@ public void testAttachPath() throws Exception {
176180
final EmailAttachment attachment4 = new EmailAttachment();
177181
attachment4.setPath("");
178182
assertThrows(EmailException.class, () -> email.attach(attachment4));
183+
attachment4.setPath("ThisFileDoesNotExist.txt");
184+
assertThrows(EmailException.class, () -> email.attach(attachment4));
185+
attachment4.setPath("target"); // a directory, not a file
186+
assertThrows(EmailException.class, () -> email.attach(attachment4));
179187
}
180188

181189
/** TODO implement test for GetContainer */

0 commit comments

Comments
 (0)