-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ctabin/fix-inputstream-close
Fixes non-closed InputStream
- Loading branch information
Showing
3 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import javax.mail.Message; | ||
|
@@ -13,6 +16,7 @@ | |
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class OutlookMessageMIMETest { | ||
|
@@ -113,4 +117,76 @@ public void testSimpleDoubleGeneration() throws Exception { | |
String text2 = IOUtils.toString(multipart2.getBodyPart(1).getInputStream(), StandardCharsets.UTF_8); | ||
assertEquals("Hello, World!", text2); | ||
} | ||
|
||
@Test | ||
public void testInvalidAttachment() throws Exception { | ||
OutlookMessage message = new OutlookMessage(); | ||
message.setSubject("This is a message"); | ||
message.setFrom("[email protected]"); | ||
message.setReplyTo(Arrays.asList("[email protected]", "[email protected]")); | ||
message.setPlainTextBody("Hello,\n\nThis is a simple message.\n\n.Bye."); | ||
message.addRecipient(OutlookMessageRecipient.Type.TO, "[email protected]", "Cédric"); | ||
message.addAttachment("message.txt", "text/plain", a -> null); | ||
|
||
File temporaryFile = new File("tmp"); | ||
try { message.writeTo(temporaryFile); assertTrue(false); } | ||
catch(IllegalStateException ise) { } | ||
temporaryFile.delete(); | ||
|
||
try { message.toMimeMessage(); assertTrue(false); } | ||
catch(IllegalStateException ise) { } | ||
} | ||
|
||
@Test | ||
public void testClosedStream() throws Exception { | ||
OutlookMessage message = new OutlookMessage(); | ||
message.setSubject("This is a message"); | ||
message.setFrom("[email protected]"); | ||
message.setReplyTo(Arrays.asList("[email protected]", "[email protected]")); | ||
message.setPlainTextBody("Hello,\n\nThis is a simple message.\n\n.Bye."); | ||
message.addRecipient(OutlookMessageRecipient.Type.TO, "[email protected]", "Cédric"); | ||
|
||
CheckableInputStream cis = new CheckableInputStream(); | ||
message.addAttachment("message.txt", "text/plain", a -> cis); | ||
|
||
message.toMimeMessage(); | ||
assertTrue(cis.closed); | ||
|
||
try { message.toMimeMessage(); assertTrue(false); } | ||
catch(IllegalStateException ise) { } | ||
} | ||
|
||
@Test | ||
public void testRepeteableStream() throws Exception { | ||
OutlookMessage message = new OutlookMessage(); | ||
message.setSubject("This is a message"); | ||
message.setFrom("[email protected]"); | ||
message.setReplyTo(Arrays.asList("[email protected]", "[email protected]")); | ||
message.setPlainTextBody("Hello,\n\nThis is a simple message.\n\n.Bye."); | ||
message.addRecipient(OutlookMessageRecipient.Type.TO, "[email protected]", "Cédric"); | ||
|
||
CheckableInputStream cis = new CheckableInputStream(); | ||
message.addAttachment("message.txt", "text/plain", cis); | ||
|
||
message.toMimeMessage(); | ||
assertTrue(cis.closed); | ||
|
||
message.toMimeMessage(); | ||
} | ||
|
||
private static class CheckableInputStream extends InputStream { | ||
private boolean closed = false; | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if(closed) { throw new IllegalStateException("stream is closed"); } | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
closed = true; | ||
super.close(); | ||
} | ||
} | ||
} |