| 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.junit5.GreenMailExtension; | |
| 5 | import ogham.testing.com.icegreen.greenmail.server.AbstractServer; | |
| 6 | import ogham.testing.com.icegreen.greenmail.util.GreenMail; | |
| 7 | import ogham.testing.com.icegreen.greenmail.util.ServerSetup; | |
| 8 | import org.junit.jupiter.api.extension.ExtensionContext; | |
| 9 | import org.junit.jupiter.api.extension.ParameterContext; | |
| 10 | import org.junit.jupiter.api.extension.ParameterResolutionException; | |
| 11 | import org.junit.jupiter.api.extension.ParameterResolver; | |
| 12 | ||
| 13 | import java.util.List; | |
| 14 | ||
| 15 | import static java.util.Arrays.stream; | |
| 16 | import static java.util.stream.Collectors.toList; | |
| 17 | ||
| 18 | /** | |
| 19 | * Just an extension for {@link GreenMailExtension} to handle random port instead of | |
| 20 | * fixed port. | |
| 21 | * | |
| 22 | * @author Aurélien Baudet | |
| 23 | * | |
| 24 | */ | |
| 25 | public class RandomPortGreenMailExtension extends GreenMailExtension implements ParameterResolver { | |
| 26 | private boolean perMethod = true; | |
| 27 | ||
| 28 | /** | |
| 29 | * Initialize the rule for the SMTP protocol only (random port). | |
| 30 | */ | |
| 31 | public RandomPortGreenMailExtension() { | |
| 32 | this(ServerSetup.PROTOCOL_SMTP); | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * Initialize the rule for the provided protocols (random port for each | |
| 37 | * protocol). | |
| 38 | * | |
| 39 | * The random port range is [{@link RandomPortUtils#PORT_RANGE_MIN}, | |
| 40 | * {@link RandomPortUtils#PORT_RANGE_MAX}]. | |
| 41 | * | |
| 42 | * @param protocols | |
| 43 | * the list of protocols to start | |
| 44 | */ | |
| 45 | public RandomPortGreenMailExtension(String... protocols) { | |
| 46 | this(RandomPortUtils.PORT_RANGE_MIN, RandomPortUtils.PORT_RANGE_MAX, protocols); | |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Initialize the rule for the provided protocols (random port for each | |
| 51 | * protocol). | |
| 52 | * | |
| 53 | * The random port range is [{@link RandomPortUtils#PORT_RANGE_MIN}, | |
| 54 | * maxPort]. | |
| 55 | * | |
| 56 | * @param maxPort | |
| 57 | * the maximum port | |
| 58 | * @param protocols | |
| 59 | * the list of protocols to start | |
| 60 | */ | |
| 61 | public RandomPortGreenMailExtension(int maxPort, String... protocols) { | |
| 62 | this(RandomPortUtils.PORT_RANGE_MIN, maxPort, protocols); | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Initialize the rule for the provided protocols (random port for each | |
| 67 | * protocol). | |
| 68 | * | |
| 69 | * The random port range is [minPort, maxPort]. | |
| 70 | * | |
| 71 | * @param minPort | |
| 72 | * the minimum port | |
| 73 | * @param maxPort | |
| 74 | * the maximum port | |
| 75 | * @param protocols | |
| 76 | * the list of protocols to start | |
| 77 | */ | |
| 78 | public RandomPortGreenMailExtension(int minPort, int maxPort, String... protocols) { | |
| 79 | super(toServerSetup(minPort, maxPort, protocols)); | |
| 80 | } | |
| 81 | ||
| 82 | @Override | |
| 83 | public void afterEach(ExtensionContext context) { | |
| 84 |
1
1. afterEach : removed call to ogham/testing/com/icegreen/greenmail/junit5/GreenMailExtension::afterEach → NO_COVERAGE |
super.afterEach(context); |
| 85 |
1
1. afterEach : negated conditional → NO_COVERAGE |
if (perMethod) { |
| 86 |
1
1. afterEach : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPorts → NO_COVERAGE |
resetPorts(); |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | @Override | |
| 91 | public void afterAll(ExtensionContext context) { | |
| 92 |
1
1. afterAll : removed call to ogham/testing/com/icegreen/greenmail/junit5/GreenMailExtension::afterAll → NO_COVERAGE |
super.afterAll(context); |
| 93 |
1
1. afterAll : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPorts → NO_COVERAGE |
resetPorts(); |
| 94 | } | |
| 95 | ||
| 96 | @Override | |
| 97 | public GreenMailExtension withPerMethodLifecycle(boolean perMethod) { | |
| 98 | this.perMethod = perMethod; | |
| 99 |
1
1. withPerMethodLifecycle : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::withPerMethodLifecycle → NO_COVERAGE |
return super.withPerMethodLifecycle(perMethod); |
| 100 | } | |
| 101 | ||
| 102 | @Override | |
| 103 | public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | |
| 104 |
3
1. supportsParameter : negated conditional → NO_COVERAGE 2. supportsParameter : replaced boolean return with true for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::supportsParameter → NO_COVERAGE 3. supportsParameter : negated conditional → NO_COVERAGE |
return isGreenMailParam(parameterContext) || isGreenMailExtensionParam(parameterContext); |
| 105 | } | |
| 106 | ||
| 107 | @Override | |
| 108 | public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | |
| 109 |
1
1. resolveParameter : negated conditional → NO_COVERAGE |
if (isGreenMailParam(parameterContext)) { |
| 110 |
1
1. resolveParameter : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resolveParameter → NO_COVERAGE |
return getGreenMail(); |
| 111 | } | |
| 112 |
1
1. resolveParameter : negated conditional → NO_COVERAGE |
if (isGreenMailExtensionParam(parameterContext)) { |
| 113 |
1
1. resolveParameter : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resolveParameter → NO_COVERAGE |
return this; |
| 114 | } | |
| 115 | return null; | |
| 116 | } | |
| 117 | ||
| 118 | private boolean isGreenMailParam(ParameterContext parameterContext) { | |
| 119 |
2
1. isGreenMailParam : replaced boolean return with true for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::isGreenMailParam → NO_COVERAGE 2. isGreenMailParam : replaced boolean return with false for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::isGreenMailParam → NO_COVERAGE |
return GreenMail.class.isAssignableFrom(parameterContext.getParameter().getType()); |
| 120 | } | |
| 121 | ||
| 122 | private boolean isGreenMailExtensionParam(ParameterContext parameterContext) { | |
| 123 |
2
1. isGreenMailExtensionParam : replaced boolean return with false for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::isGreenMailExtensionParam → NO_COVERAGE 2. isGreenMailExtensionParam : replaced boolean return with true for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::isGreenMailExtensionParam → NO_COVERAGE |
return GreenMailExtension.class.isAssignableFrom(parameterContext.getParameter().getType()); |
| 124 | } | |
| 125 | ||
| 126 | private void resetPorts() { | |
| 127 |
1
1. resetPorts : negated conditional → NO_COVERAGE |
if (getGreenMail() != null) { |
| 128 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getImap()); |
| 129 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getImaps()); |
| 130 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getPop3()); |
| 131 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getPop3s()); |
| 132 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getSmtp()); |
| 133 |
1
1. resetPorts : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(getGreenMail().getSmtps()); |
| 134 | } | |
| 135 | } | |
| 136 | ||
| 137 | private static void resetPort(AbstractServer server) { | |
| 138 |
1
1. resetPort : negated conditional → NO_COVERAGE |
if (server != null) { |
| 139 |
1
1. resetPort : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::resetPort → NO_COVERAGE |
resetPort(server.getServerSetup()); |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 143 | private static void resetPort(ServerSetup setup) { | |
| 144 |
1
1. resetPort : negated conditional → NO_COVERAGE |
if (setup instanceof RandomPortServerSetup) { |
| 145 |
1
1. resetPort : removed call to fr/sii/ogham/testing/extension/junit/email/RandomPortServerSetup::resetPort → NO_COVERAGE |
((RandomPortServerSetup) setup).resetPort(); |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | private static ServerSetup[] toServerSetup(int minPort, int maxPort, String... protocols) { | |
| 150 | List<ServerSetup> list = stream(protocols) | |
| 151 |
1
1. lambda$toServerSetup$0 : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::lambda$toServerSetup$0 → NO_COVERAGE |
.map(p -> new RandomPortServerSetup(minPort, maxPort, p)) |
| 152 | .collect(toList()); | |
| 153 |
1
1. toServerSetup : replaced return value with null for fr/sii/ogham/testing/extension/junit/email/RandomPortGreenMailExtension::toServerSetup → NO_COVERAGE |
return list.toArray(new ServerSetup[list.size()]); |
| 154 | } | |
| 155 | } | |
Mutations | ||
| 84 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 92 |
1.1 |
|
| 93 |
1.1 |
|
| 99 |
1.1 |
|
| 104 |
1.1 2.2 3.3 |
|
| 109 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 |
|
| 119 |
1.1 2.2 |
|
| 123 |
1.1 2.2 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |
|
| 151 |
1.1 |
|
| 153 |
1.1 |