Repository: james-project Updated Branches: refs/heads/master e1c486e24 -> f8506f768
MAILET-155 Add a utility for integer parsing as part of mailets Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/49b99519 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/49b99519 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/49b99519 Branch: refs/heads/master Commit: 49b99519dbbca70f969b4b09629bd24e528473ed Parents: e1c486e Author: benwa <btell...@linagora.com> Authored: Fri Apr 7 12:04:43 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Mon Apr 10 17:22:12 2017 +0700 ---------------------------------------------------------------------- .../java/org/apache/mailet/base/MailetUtil.java | 36 ++++++++ .../org/apache/mailet/base/MailetUtilTest.java | 90 +++++++++++++++++++- .../james/transport/matchers/RelayLimit.java | 10 +-- .../james/transport/matchers/TooManyLines.java | 21 +---- .../transport/matchers/TooManyRecipients.java | 17 +--- 5 files changed, 131 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java ---------------------------------------------------------------------- diff --git a/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java b/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java index 642f760..ae16f7c 100644 --- a/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java +++ b/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java @@ -21,9 +21,12 @@ package org.apache.mailet.base; +import javax.mail.MessagingException; + import org.apache.mailet.MailetConfig; import com.google.common.base.Optional; +import com.google.common.base.Strings; /** @@ -107,4 +110,37 @@ public class MailetUtil { } return Optional.absent(); } + + public static int getInitParameterAsStrictlyPositiveInteger(String condition, int defaultValue) throws MessagingException { + String defaultStringValue = String.valueOf(defaultValue); + return getInitParameterAsStrictlyPositiveInteger(condition, Optional.of(defaultStringValue)); + } + + public static int getInitParameterAsStrictlyPositiveInteger(String condition) throws MessagingException { + return getInitParameterAsStrictlyPositiveInteger(condition, Optional.<String>absent()); + } + + public static int getInitParameterAsStrictlyPositiveInteger(String condition, Optional<String> defaultValue) throws MessagingException { + Optional<String> value = Optional.fromNullable(condition) + .or(defaultValue); + + if (Strings.isNullOrEmpty(value.orNull())) { + throw new MessagingException("Condition is required. It should be a strictly positive integer"); + } + + int valueAsInt = tryParseInteger(value.orNull()); + + if (valueAsInt < 1) { + throw new MessagingException("Expecting condition to be a strictly positive integer. Got " + value.get()); + } + return valueAsInt; + } + + private static int tryParseInteger(String value) throws MessagingException { + try { + return Integer.valueOf(value); + } catch (NumberFormatException e) { + throw new MessagingException("Expecting condition to be a strictly positive integer. Got " + value); + } + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java ---------------------------------------------------------------------- diff --git a/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java b/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java index cb56b99..be11e15 100644 --- a/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java +++ b/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java @@ -22,13 +22,20 @@ package org.apache.mailet.base; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat; +import javax.mail.MessagingException; + import org.apache.mailet.base.test.FakeMailetConfig; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class MailetUtilTest { private static final String A_PARAMETER = "aParameter"; - public static final String DEFAULT_VALUE = "default"; + public static final int DEFAULT_VALUE = 36; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test public void getInitParameterShouldReturnTrueWhenIsValueTrueLowerCase() { @@ -85,6 +92,87 @@ public class MailetUtilTest { assertThat(MailetUtil.getInitParameter(mailetConfig, A_PARAMETER)).isAbsent(); } + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnEmptyString() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger(""); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnNull() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger(null); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnInvalid() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("invalid"); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnNegativeNumber() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("-1"); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnZero() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("0"); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerShouldParseCorrectValue() throws Exception { + assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger("1")) + .isEqualTo(1); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnEmptyString() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("", DEFAULT_VALUE); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldReturnDefaultValueOnNull() throws Exception { + assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger(null, DEFAULT_VALUE)) + .isEqualTo(DEFAULT_VALUE); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnInvalid() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("invalid", DEFAULT_VALUE); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnNegativeNumber() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("-1", DEFAULT_VALUE); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnZero() throws Exception { + expectedException.expect(MessagingException.class); + + MailetUtil.getInitParameterAsStrictlyPositiveInteger("0", DEFAULT_VALUE); + } + + @Test + public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldParseCorrectValue() throws Exception { + assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger("1", DEFAULT_VALUE)) + .isEqualTo(1); + } + private boolean getParameterValued(String value, boolean defaultValue) { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .setProperty(A_PARAMETER, value) http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java index 2b5d52c..ddd91a0 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java @@ -21,6 +21,7 @@ package org.apache.james.transport.matchers; +import org.apache.mailet.base.MailetUtil; import org.apache.mailet.base.RFC2822Headers; import org.apache.mailet.base.GenericMatcher; import org.apache.mailet.Mail; @@ -42,14 +43,7 @@ public class RelayLimit extends GenericMatcher { int limit = 30; public void init() throws MessagingException { - try { - limit = Integer.parseInt(getCondition()); - } catch (NumberFormatException e) { - throw new MessagingException("No valid integer: " + getCondition()); - } - if (limit <= 0) { - throw new MessagingException("Relay limit should be superior to 0"); - } + limit = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition()); } public Collection<MailAddress> match(Mail mail) throws javax.mail.MessagingException { http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java index ef2743f..aa37378 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java @@ -26,6 +26,7 @@ import javax.mail.MessagingException; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.base.GenericMatcher; +import org.apache.mailet.base.MailetUtil; import com.google.common.collect.ImmutableList; @@ -35,25 +36,7 @@ public class TooManyLines extends GenericMatcher { @Override public void init() throws MessagingException { - String condition = getCondition(); - - maximumLineCount = parseCondition(condition); - - if (maximumLineCount < 1) { - throw new MessagingException("Condition should be strictly positive"); - } - } - - private int parseCondition(String condition) throws MessagingException { - if (condition == null) { - throw new MessagingException("Missing condition"); - } - - try { - return Integer.valueOf(condition); - } catch (NumberFormatException e) { - throw new MessagingException("Invalid formating. Condition is expected to be an integer"); - } + maximumLineCount = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition()); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java index 51895a3..abaeb96 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java @@ -26,6 +26,7 @@ import javax.mail.MessagingException; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.base.GenericMatcher; +import org.apache.mailet.base.MailetUtil; import com.google.common.collect.ImmutableList; @@ -35,21 +36,7 @@ public class TooManyRecipients extends GenericMatcher { @Override public void init() throws MessagingException { - String condition = getCondition(); - - if (condition == null) { - throw new MessagingException("it should have a condition"); - } - - try { - maximumRecipientCount = Integer.parseInt(condition); - } catch (Exception e) { - throw new MessagingException("Condition should be a number"); - } - - if (maximumRecipientCount < 1) { - throw new MessagingException("it should be positive condition"); - } + maximumRecipientCount = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition()); } @Override --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org