Repository: james-project Updated Branches: refs/heads/master 1b670aecc -> a43b38b0b
JAMES-2065 factorise map argument parsing Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d5bddef4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d5bddef4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d5bddef4 Branch: refs/heads/master Commit: d5bddef411231bd05d17f197b5bc833d468d1e79 Parents: 1b670ae Author: Luc DUZAN <[email protected]> Authored: Thu Jun 22 17:47:31 2017 +0200 Committer: benwa <[email protected]> Committed: Tue Aug 29 08:11:59 2017 +0700 ---------------------------------------------------------------------- mailet/standard/pom.xml | 4 + .../mailets/MailAttributesToMimeHeaders.java | 24 ++--- .../transport/mailets/MappingArgument.java | 63 +++++++++++++ .../MailAttributesToMimeHeadersTest.java | 7 +- .../transport/mailets/MappingArgumentTest.java | 95 ++++++++++++++++++++ 5 files changed, 170 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d5bddef4/mailet/standard/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/standard/pom.xml b/mailet/standard/pom.xml index 58a6ba8..5f1a588 100644 --- a/mailet/standard/pom.xml +++ b/mailet/standard/pom.xml @@ -74,6 +74,10 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> + <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient-osgi</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/d5bddef4/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java index 0d71f52..666ba1d 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java @@ -21,7 +21,6 @@ package org.apache.james.transport.mailets; -import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -33,9 +32,7 @@ import org.apache.mailet.base.GenericMailet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.base.Strings; /** * <p>Convert attributes to headers</p> @@ -52,25 +49,14 @@ import com.google.common.collect.ImmutableMap.Builder; public class MailAttributesToMimeHeaders extends GenericMailet { private static final Logger LOGGER = LoggerFactory.getLogger(MailAttributesToMimeHeaders.class); - private static final String CONFIGURATION_ERROR_MESSAGE = "Invalid config. Please use \"attributeName; headerName\""; private Map<String, String> mappings; @Override public void init() throws MessagingException { - String simplemappings = getInitParameter("simplemapping"); - Builder<String, String> mappingsBuilder = ImmutableMap.builder(); - if (simplemappings != null) { - for (String mapping : Splitter.on(',').split(simplemappings)) { - List<String> pair = Splitter.on(';').trimResults().splitToList(mapping); - if (pair.size() != 2) { - throw new MessagingException(CONFIGURATION_ERROR_MESSAGE); - } - mappingsBuilder.put(pair.get(0), pair.get(1)); - } - } else { - throw new MessagingException(CONFIGURATION_ERROR_MESSAGE); - } - mappings = mappingsBuilder.build(); + String simpleMappings = getInitParameter("simplemapping"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(simpleMappings), "simplemapping is required"); + + mappings = MappingArgument.parse(simpleMappings); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/d5bddef4/mailet/standard/src/main/java/org/apache/james/transport/mailets/MappingArgument.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MappingArgument.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MappingArgument.java new file mode 100644 index 0000000..c5cab71 --- /dev/null +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MappingArgument.java @@ -0,0 +1,63 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets; + +import java.util.List; + +import javax.mail.MessagingException; + +import org.apache.commons.lang3.tuple.Pair; + +import com.github.steveash.guavate.Guavate; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableMap; + +public class MappingArgument { + + public static String CONFIGURATION_ERROR_MESSAGE = "Invalid format, please respect key1;value1,key2;value2 format"; + + public static ImmutableMap<String, String> parse(String mapping) throws MessagingException { + Preconditions.checkArgument(mapping != null, "mapping should not be null"); + + if (mapping.trim().isEmpty()) { + return ImmutableMap.of(); + } + + return Splitter.on(',') + .omitEmptyStrings() + .splitToList(mapping) + .stream() + .map(MappingArgument::parseKeyValue) + .collect(Guavate.toImmutableMap(Pair::getLeft, Pair::getRight)); + } + + private static Pair<String, String> parseKeyValue(String keyValue) { + List<String> pair = Splitter.on(';') + .trimResults() + .splitToList(keyValue); + + if (pair.size() != 2) { + throw new IllegalArgumentException(CONFIGURATION_ERROR_MESSAGE); + } + + return Pair.of(pair.get(0), pair.get(1)); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/d5bddef4/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java index 4ebe09e..657b975 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java @@ -20,7 +20,6 @@ package org.apache.james.transport.mailets; - import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; @@ -102,7 +101,7 @@ public class MailAttributesToMimeHeadersTest { .mailetName("Test") .setProperty("simplemapping", "invalidConfigEntry") .build(); - expectedException.expect(MessagingException.class); + expectedException.expect(IllegalArgumentException.class); mailet.init(mailetConfig); } @@ -112,7 +111,7 @@ public class MailAttributesToMimeHeadersTest { .mailetName("Test") .setProperty("simplemapping", "first;second;third") .build(); - expectedException.expect(MessagingException.class); + expectedException.expect(IllegalArgumentException.class); mailet.init(mailetConfig); } @@ -121,7 +120,7 @@ public class MailAttributesToMimeHeadersTest { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName("Test") .build(); - expectedException.expect(MessagingException.class); + expectedException.expect(IllegalArgumentException.class); mailet.init(mailetConfig); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/d5bddef4/mailet/standard/src/test/java/org/apache/james/transport/mailets/MappingArgumentTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MappingArgumentTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MappingArgumentTest.java new file mode 100644 index 0000000..4199b4a --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MappingArgumentTest.java @@ -0,0 +1,95 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.mail.MessagingException; + +import org.assertj.core.data.MapEntry; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MappingArgumentTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void parseShouldFailIfCalledWithNull() throws MessagingException { + expectedException.expect(IllegalArgumentException.class); + MappingArgument.parse(null); + } + + @Test + public void parseShouldFailIfCalledWhenMissingMappingParts() throws MessagingException { + expectedException.expect(IllegalArgumentException.class); + MappingArgument.parse("key1;value1,key2"); + } + + @Test + public void parseShouldFailIfCalledWhenExtraMappingParts() throws MessagingException { + expectedException.expect(IllegalArgumentException.class); + MappingArgument.parse("key1;value1,key2;value1;value3"); + } + + @Test + public void parseShouldWorkForEmptyMapping() throws MessagingException { + assertThat(MappingArgument.parse("")) + .isEmpty(); + } + + @Test + public void parseShouldWorkForEmptyMappingWithSpace() throws MessagingException { + assertThat(MappingArgument.parse(" ")) + .isEmpty(); + } + + @Test + public void parseShouldWorkForValidParsingWithOnlyOneKey() throws MessagingException { + assertThat(MappingArgument.parse("key1;value1")) + .containsExactly(MapEntry.entry("key1", "value1")); + } + + @Test + public void parseShouldWorkForValidParsingWithMoreThanOneKey() throws MessagingException { + assertThat(MappingArgument.parse("key1;value1,key2;value2")) + .containsExactly(MapEntry.entry("key1", "value1"), MapEntry.entry("key2", "value2")); + } + + @Test + public void parserShouldTrimSpacesAroundSemiColon() throws MessagingException { + assertThat(MappingArgument.parse("key1; value1")) + .containsExactly(MapEntry.entry("key1", "value1")); + } + + @Test + public void parserShouldTrimSpacesAroundComa() throws MessagingException { + assertThat(MappingArgument.parse("key1;value1, key2;value2")) + .containsExactly(MapEntry.entry("key1", "value1"), MapEntry.entry("key2", "value2")); + } + + @Test + public void parserShouldNotFailWhenExtraComa() throws MessagingException { + assertThat(MappingArgument.parse("key1;value1,")) + .containsExactly(MapEntry.entry("key1", "value1")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
