| 1 | package fr.sii.ogham.testing.extension.junit.email; | |
| 2 | ||
| 3 | import fr.sii.ogham.testing.util.RandomPortUtils; | |
| 4 | import ogham.testing.com.icegreen.greenmail.junit.GreenMailRule; | |
| 5 | import ogham.testing.com.icegreen.greenmail.server.AbstractServer; | |
| 6 | import ogham.testing.com.icegreen.greenmail.util.ServerSetup; | |
| 7 | import org.junit.runners.model.FrameworkMethod; | |
| 8 | import org.junit.runners.model.Statement; | |
| 9 | ||
| 10 | import java.util.List; | |
| 11 | ||
| 12 | import static java.util.Arrays.stream; | |
| 13 | import static java.util.stream.Collectors.toList; | |
| 14 | ||
| 15 | /** | |
| 16 | * Just an extension for {@link GreenMailRule} to handle random port instead of | |
| 17 | * fixed port. | |
| 18 | * | |
| 19 | * @author Aurélien Baudet | |
| 20 | * | |
| 21 | */ | |
| 22 | public class RandomPortGreenMailRule extends GreenMailRule { | |
| 23 | ||
| 24 | /** | |
| 25 | * Initialize the rule for the SMTP protocol only (random port). | |
| 26 | */ | |
| 27 | public RandomPortGreenMailRule() { | |
| 28 | this(ServerSetup.PROTOCOL_SMTP); | |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * Initialize the rule for the provided protocols (random port for each | |
| 33 | * protocol). | |
| 34 | * | |
| 35 | * The random port range is [{@link RandomPortUtils#PORT_RANGE_MIN}, | |
| 36 | * {@link RandomPortUtils#PORT_RANGE_MAX}]. | |
| 37 | * | |
| 38 | * @param protocols | |
| 39 | * the list of protocols to start | |
| 40 | */ | |
| 41 | public RandomPortGreenMailRule(String... protocols) { | |
| 42 | this(RandomPortUtils.PORT_RANGE_MIN, RandomPortUtils.PORT_RANGE_MAX, protocols); | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Initialize the rule for the provided protocols (random port for each | |
| 47 | * protocol). | |
| 48 | * | |
| 49 | * The random port range is [{@link RandomPortUtils#PORT_RANGE_MIN}, | |
| 50 | * maxPort]. | |
| 51 | * | |
| 52 | * @param maxPort | |
| 53 | * the maximum port | |
| 54 | * @param protocols | |
| 55 | * the list of protocols to start | |
| 56 | */ | |
| 57 | public RandomPortGreenMailRule(int maxPort, String... protocols) { | |
| 58 | this(RandomPortUtils.PORT_RANGE_MIN, maxPort, protocols); | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Initialize the rule for the provided protocols (random port for each | |
| 63 | * protocol). | |
| 64 | * | |
| 65 | * The random port range is [minPort, maxPort]. | |
| 66 | * | |
| 67 | * @param minPort | |
| 68 | * the minimum port | |
| 69 | * @param maxPort | |
| 70 | * the maximum port | |
| 71 | * @param protocols | |
| 72 | * the list of protocols to start | |
| 73 | */ | |
| 74 | public RandomPortGreenMailRule(int minPort, int maxPort, String... protocols) { | |
| 75 | super(toServerSetup(minPort, maxPort, protocols)); | |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | public Statement apply(final Statement base, FrameworkMethod method, Object target) { | |
| 80 |
1
1. apply : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::apply → NO_COVERAGE |
return super.apply(new Statement() { |
| 81 | @Override | |
| 82 | public void evaluate() throws Throwable { | |
| 83 | try { | |
| 84 |
1
1. evaluate : removed call to org/junit/runners/model/Statement::evaluate → NO_COVERAGE |
base.evaluate(); |
| 85 | } finally { | |
| 86 |
1
1. evaluate : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::access$000 → NO_COVERAGE |
resetPorts(); |
| 87 | } | |
| 88 | } | |
| 89 | }, method, target); | |
| 90 | } | |
| 91 | ||
| 92 | private void resetPorts() { | |
| 93 |
1
1. resetPorts : negated conditional → NO_COVERAGE |
if (getGreenMail() != null) { |
| 94 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getImap()); |
| 95 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getImaps()); |
| 96 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getPop3()); |
| 97 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getPop3s()); |
| 98 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getSmtp()); |
| 99 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getSmtps()); |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | private static void resetPort(AbstractServer server) { | |
| 104 |
1
1. resetPort : negated conditional → NO_COVERAGE |
if (server != null) { |
| 105 |
1
1. resetPort : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::resetPort → NO_COVERAGE |
resetPort(server.getServerSetup()); |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | private static void resetPort(ServerSetup setup) { | |
| 110 |
1
1. resetPort : negated conditional → NO_COVERAGE |
if (setup instanceof RandomPortServerSetup) { |
| 111 |
1
1. resetPort : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortServerSetup::resetPort → NO_COVERAGE |
((RandomPortServerSetup) setup).resetPort(); |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | private static ServerSetup[] toServerSetup(int minPort, int maxPort, String... protocols) { | |
| 116 |
1
1. lambda$toServerSetup$0 : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::lambda$toServerSetup$0 → NO_COVERAGE |
List<ServerSetup> list = stream(protocols).map(p -> new RandomPortServerSetup(minPort, maxPort, p)).collect(toList()); |
| 117 |
1
1. toServerSetup : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailRule::toServerSetup → NO_COVERAGE |
return list.toArray(new ServerSetup[list.size()]); |
| 118 | } | |
| 119 | } | |
Mutations | ||
| 80 |
1.1 |
|
| 84 |
1.1 |
|
| 86 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 104 |
1.1 |
|
| 105 |
1.1 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |