31
31
import java .util .logging .LogManager ;
32
32
import java .util .logging .LogRecord ;
33
33
34
+ import com .icegreen .greenmail .store .FolderException ;
34
35
import com .icegreen .greenmail .user .GreenMailUser ;
35
36
import com .icegreen .greenmail .util .GreenMail ;
36
37
import com .icegreen .greenmail .util .GreenMailUtil ;
52
53
import jakarta .mail .search .AndTerm ;
53
54
import jakarta .mail .search .FlagTerm ;
54
55
import jakarta .mail .search .FromTerm ;
55
- import org .junit .jupiter .api .AfterEach ;
56
+ import org .junit .jupiter .api .AfterAll ;
57
+ import org .junit .jupiter .api .BeforeAll ;
56
58
import org .junit .jupiter .api .BeforeEach ;
57
59
import org .junit .jupiter .api .Test ;
58
60
import org .mockito .Mockito ;
107
109
@ DirtiesContext
108
110
public class ImapMailReceiverTests {
109
111
110
- private AtomicInteger failed ;
112
+ private final static ImapSearchLoggingHandler imapSearches = new ImapSearchLoggingHandler () ;
111
113
112
- private GreenMail imapIdleServer ;
114
+ private static GreenMail imapIdleServer ;
113
115
114
- private GreenMailUser user ;
116
+ private static GreenMailUser user ;
117
+
118
+ private final AtomicInteger failed = new AtomicInteger (0 );
115
119
116
120
@ Autowired
117
121
private ApplicationContext context ;
118
122
119
- static {
123
+ @ BeforeAll
124
+ static void startImapServer () {
120
125
System .setProperty ("java.util.logging.manager" , "org.apache.logging.log4j.jul.LogManager" );
121
- }
122
-
123
- private final ImapSearchLoggingHandler imapSearches = new ImapSearchLoggingHandler ();
124
-
125
- @ BeforeEach
126
- public void setup () {
127
126
LogManager .getLogManager ().getLogger ("" ).setLevel (Level .ALL );
128
127
imapSearches .searches .clear ();
129
128
imapSearches .stores .clear ();
130
129
LogManager .getLogManager ().getLogger ("" ).addHandler (imapSearches );
131
- failed = new AtomicInteger (0 );
132
130
ServerSetup imap = ServerSetupTest .IMAP .dynamicPort ();
133
131
imap .setServerStartupTimeout (10000 );
134
132
imapIdleServer = new GreenMail (imap );
135
133
user = imapIdleServer .setUser ("user" , "pw" );
136
134
imapIdleServer .start ();
137
135
}
138
136
139
- @ AfterEach
140
- public void tearDown () {
141
- this .imapIdleServer .stop ();
137
+ @ BeforeEach
138
+ void cleanup () throws FolderException {
139
+ imapIdleServer .getManagers ().getImapHostManager ().getInbox (user ).deleteAllMessages ();
140
+ }
141
+
142
+ @ AfterAll
143
+ static void stopImapServer () {
144
+ imapIdleServer .stop ();
142
145
LogManager .getLogManager ().getLogger ("" ).removeHandler (imapSearches );
143
146
}
144
147
145
148
@ Test
146
149
public void testIdleWithServerCustomSearch () throws Exception {
147
150
ImapMailReceiver receiver =
148
- new ImapMailReceiver ("imap://user:pw@localhost:" + this . imapIdleServer .getImap ().getPort () + "/INBOX" );
151
+ new ImapMailReceiver ("imap://user:pw@localhost:" + imapIdleServer .getImap ().getPort () + "/INBOX" );
149
152
receiver .setSearchTermStrategy ((supportedFlags , folder ) -> {
150
153
try {
151
154
FromTerm fromTerm = new FromTerm (new InternetAddress ("bar@baz" ));
@@ -161,23 +164,23 @@ public void testIdleWithServerCustomSearch() throws Exception {
161
164
@ Test
162
165
public void testIdleWithServerDefaultSearch () throws Exception {
163
166
ImapMailReceiver receiver =
164
- new ImapMailReceiver ("imap://user:pw@localhost:" + this . imapIdleServer .getImap ().getPort () + "/INBOX" );
167
+ new ImapMailReceiver ("imap://user:pw@localhost:" + imapIdleServer .getImap ().getPort () + "/INBOX" );
165
168
testIdleWithServerGuts (receiver , false );
166
169
assertThat (imapSearches .searches .get (0 )).contains ("testSIUserFlag" );
167
170
}
168
171
169
172
@ Test
170
173
public void testIdleWithMessageMapping () throws Exception {
171
174
ImapMailReceiver receiver =
172
- new ImapMailReceiver ("imap://user:pw@localhost:" + this . imapIdleServer .getImap ().getPort () + "/INBOX" );
175
+ new ImapMailReceiver ("imap://user:pw@localhost:" + imapIdleServer .getImap ().getPort () + "/INBOX" );
173
176
receiver .setHeaderMapper (new DefaultMailHeaderMapper ());
174
177
testIdleWithServerGuts (receiver , true );
175
178
}
176
179
177
180
@ Test
178
181
public void testIdleWithServerDefaultSearchSimple () throws Exception {
179
182
ImapMailReceiver receiver =
180
- new ImapMailReceiver ("imap://user:pw@localhost:" + this . imapIdleServer .getImap ().getPort () + "/INBOX" );
183
+ new ImapMailReceiver ("imap://user:pw@localhost:" + imapIdleServer .getImap ().getPort () + "/INBOX" );
181
184
receiver .setSimpleContent (true );
182
185
testIdleWithServerGuts (receiver , false , true );
183
186
assertThat (imapSearches .searches .get (0 )).contains ("testSIUserFlag" );
@@ -186,7 +189,7 @@ public void testIdleWithServerDefaultSearchSimple() throws Exception {
186
189
@ Test
187
190
public void testIdleWithMessageMappingSimple () throws Exception {
188
191
ImapMailReceiver receiver =
189
- new ImapMailReceiver ("imap://user:pw@localhost:" + this . imapIdleServer .getImap ().getPort () + "/INBOX" );
192
+ new ImapMailReceiver ("imap://user:pw@localhost:" + imapIdleServer .getImap ().getPort () + "/INBOX" );
190
193
receiver .setSimpleContent (true );
191
194
receiver .setHeaderMapper (new DefaultMailHeaderMapper ());
192
195
testIdleWithServerGuts (receiver , true , true );
@@ -217,8 +220,6 @@ public void testIdleWithServerGuts(ImapMailReceiver receiver, boolean mapped, bo
217
220
message .setRecipients (Message .RecipientType .CC , "a@b, c@d" );
218
221
message .setRecipients (Message .RecipientType .BCC , "e@f, g@h" );
219
222
user .deliver (message );
220
- user .deliver (GreenMailUtil .createTextEmail ("to" , "Bar <bar@baz>" , "subject" , "body" ,
221
- imapIdleServer .getImap ().getServerSetup ()));
222
223
if (!mapped ) {
223
224
@ SuppressWarnings ("unchecked" )
224
225
org .springframework .messaging .Message <MimeMessage > received =
@@ -256,6 +257,8 @@ public void testIdleWithServerGuts(ImapMailReceiver receiver, boolean mapped, bo
256
257
assertThat (received .getPayload ()).isEqualTo ("foo" );
257
258
}
258
259
}
260
+ user .deliver (GreenMailUtil .createTextEmail ("to" , "Bar <bar@baz>" , "subject" , "body" ,
261
+ imapIdleServer .getImap ().getServerSetup ()));
259
262
assertThat (channel .receive (20000 )).isNotNull (); // new message after idle
260
263
assertThat (channel .receive (100 )).isNull (); // no new message after second and third idle
261
264
@@ -682,7 +685,7 @@ public void testNoInitialIdleDelayWhenRecentNotSupported() throws Exception {
682
685
* Idle takes 5 seconds; if all is well, we should receive the first message
683
686
* before then.
684
687
*/
685
- assertThat (channel .receive (10000 )).isNotNull ();
688
+ assertThat (channel .receive (20000 )).isNotNull ();
686
689
// We should not receive any more until the next idle elapses
687
690
assertThat (channel .receive (100 )).isNull ();
688
691
assertThat (channel .receive (10000 )).isNotNull ();
@@ -873,8 +876,8 @@ private Folder testAttachmentsGuts(final ImapMailReceiver receiver) throws Messa
873
876
874
877
@ Test
875
878
public void testNullMessages () throws Exception {
876
- Message message1 = spy ( GreenMailUtil .newMimeMessage ("test1" ) );
877
- Message message2 = spy ( GreenMailUtil .newMimeMessage ("test2" ) );
879
+ Message message1 = GreenMailUtil .newMimeMessage ("test1" );
880
+ Message message2 = GreenMailUtil .newMimeMessage ("test2" );
878
881
final Message [] messages1 = new Message [] {null , null , message1 };
879
882
final Message [] messages2 = new Message [] {message2 };
880
883
final SearchTermStrategy searchTermStrategy = mock (SearchTermStrategy .class );
@@ -948,9 +951,7 @@ public void testIdleReconnects() throws Exception {
948
951
adapter .setReconnectDelay (1 );
949
952
adapter .afterPropertiesSet ();
950
953
final CountDownLatch latch = new CountDownLatch (3 );
951
- adapter .setApplicationEventPublisher (e -> {
952
- latch .countDown ();
953
- });
954
+ adapter .setApplicationEventPublisher (e -> latch .countDown ());
954
955
adapter .start ();
955
956
assertThat (latch .await (60 , TimeUnit .SECONDS )).isTrue ();
956
957
verify (store , atLeast (3 )).connect ();
0 commit comments