This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
HasEmailRule.java
140 lines (127 loc) · 5.03 KB
/
HasEmailRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2014, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.util;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.mail.internet.MimeMultipart;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.Uninterruptibles;
import lombok.extern.slf4j.Slf4j;
/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Slf4j
public class HasEmailRule extends ExternalResource {
private static final Object wiserLock = new Object();
private volatile static Wiser wiser;
@Override
protected void before() throws Throwable {
super.before();
synchronized (wiserLock) {
if (HasEmailRule.wiser == null) {
String port = PropertiesHolder.getProperty("smtp.port");
int portNum = Integer.parseInt(port);
HasEmailRule.wiser = new Wiser(portNum);
HasEmailRule.wiser.getServer().setBindAddress(
InetAddress.getByName("127.0.0.1"));
HasEmailRule.wiser.start();
// NB we never call wiser.stop() because we want the email
// server to stay running for all tests in this VM
}
}
clearQueue();
}
@Override
protected void after() {
clearQueue();
super.after();
}
private void clearQueue() {
log.info("Clearing email queue");
wiser.getMessages().clear();
}
public List<WiserMessage> getMessages() {
return wiser.getMessages();
}
/**
* Email may arrive a little bit late therefore this method can be used to
* poll and wait until timeout.
*
* @param expectedEmailNum
* expected arriving email numbers
* @param timeoutDuration
* timeout duration
* @param timeoutUnit
* timeout time unit
* @return true if the expected number of emails has arrived or false if it
* fails within the timeout period
*/
public boolean emailsArrivedWithinTimeout(final int expectedEmailNum,
final long timeoutDuration, final TimeUnit timeoutUnit) {
log.info("waiting for email count to be {}", expectedEmailNum);
final CountDownLatch countDownLatch = new CountDownLatch(1);
// poll every half second
final int sleepFor = 500;
final TimeUnit sleepUnit = TimeUnit.MILLISECONDS;
final long sleepTime = sleepUnit.convert(sleepFor, sleepUnit);
final long timeoutTime = sleepUnit.convert(timeoutDuration, timeoutUnit);
Runnable runnable = new Runnable() {
@Override
public void run() {
long slept = 0;
while (wiser.getMessages().size() < expectedEmailNum
&& slept < timeoutTime) {
log.info("Number of arrived emails: {}",
wiser.getMessages().size());
Uninterruptibles.sleepUninterruptibly(sleepFor, sleepUnit);
slept += sleepTime;
}
countDownLatch.countDown();
}
};
Executors.newFixedThreadPool(1).submit(runnable);
try {
return countDownLatch.await(timeoutDuration, timeoutUnit);
} catch (InterruptedException e) {
log.warn("interrupted", e);
return wiser.getMessages().size() == expectedEmailNum;
}
}
public static String getEmailContent(WiserMessage wiserMessage) {
log.info("Query message content");
try {
return ((MimeMultipart) wiserMessage.getMimeMessage().getContent())
.getBodyPart(0).getContent().toString();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}