JAMES-2578 Introduce Attribute abstraction
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c97bc757 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c97bc757 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c97bc757 Branch: refs/heads/master Commit: c97bc757dcd79b8cc01cf1eac06100ae19155ca9 Parents: 9a96bd5 Author: Raphael Ouazana <raphael.ouaz...@linagora.com> Authored: Fri Oct 26 09:11:06 2018 +0200 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu Nov 1 11:02:43 2018 +0700 ---------------------------------------------------------------------- mailet/api/pom.xml | 37 ++++++++- .../main/java/org/apache/mailet/Attribute.java | 81 ++++++++++++++++++++ .../java/org/apache/mailet/AttributeName.java | 72 +++++++++++++++++ .../java/org/apache/mailet/AttributeValue.java | 73 ++++++++++++++++++ .../org/apache/mailet/AttributeNameTest.java | 53 +++++++++++++ .../java/org/apache/mailet/AttributeTest.java | 46 +++++++++++ 6 files changed, 359 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/api/pom.xml b/mailet/api/pom.xml index 18ee831..5b5d599 100644 --- a/mailet/api/pom.xml +++ b/mailet/api/pom.xml @@ -41,6 +41,22 @@ <artifactId>james-core</artifactId> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-util</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-guava</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jdk8</artifactId> + </dependency> + <dependency> <groupId>com.github.steveash.guavate</groupId> <artifactId>guavate</artifactId> </dependency> @@ -53,9 +69,9 @@ <artifactId>javax.mail</artifactId> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> + <groupId>de.ruedigermoeller</groupId> + <artifactId>fst</artifactId> + <version>2.56</version> </dependency> <dependency> <groupId>nl.jqno.equalsverifier</groupId> @@ -68,6 +84,21 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/src/main/java/org/apache/mailet/Attribute.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/Attribute.java b/mailet/api/src/main/java/org/apache/mailet/Attribute.java new file mode 100644 index 0000000..a4d5d7c --- /dev/null +++ b/mailet/api/src/main/java/org/apache/mailet/Attribute.java @@ -0,0 +1,81 @@ +/**************************************************************** + * 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.mailet; + +import java.util.Objects; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +/** + * Attribute attached to an email + * + * Attributes hold information to be shared during Mail processing, across mailets. + * + * @since Mailet API v3.2 + */ +public class Attribute { + private final AttributeName name; + private final AttributeValue<?> value; + + public Attribute(AttributeName name, AttributeValue<?> value) { + Preconditions.checkNotNull(name, "`name` should not be null"); + Preconditions.checkNotNull(value, "`value` should not be null"); + + this.name = name; + this.value = value; + } + + public AttributeName getName() { + return name; + } + + public AttributeValue<?> getValue() { + return value; + } + + public Attribute duplicate() { + return new Attribute(name, value.duplicate()); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Attribute) { + Attribute that = (Attribute) o; + + return Objects.equals(this.name, that.name) + && Objects.equals(this.value, that.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(name, value); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", name) + .add("value", value) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/src/main/java/org/apache/mailet/AttributeName.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeName.java b/mailet/api/src/main/java/org/apache/mailet/AttributeName.java new file mode 100644 index 0000000..5322d7e --- /dev/null +++ b/mailet/api/src/main/java/org/apache/mailet/AttributeName.java @@ -0,0 +1,72 @@ +/**************************************************************** + * 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.mailet; + +import java.util.Objects; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +/** + * Strong typing for attribute name, which represents the name of an attribute stored in a mail. + * + * @since Mailet API v3.2 + */ +public class AttributeName { + private final String name; + + public static AttributeName of(String name) { + Preconditions.checkNotNull(name, "`name` is compulsory"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "`name` should not be empty"); + + return new AttributeName(name); + } + + private AttributeName(String name) { + this.name = name; + } + + public String asString() { + return name; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof AttributeName) { + AttributeName that = (AttributeName) o; + + return Objects.equals(this.name, that.name); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(name); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", name) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java new file mode 100644 index 0000000..82e520d --- /dev/null +++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java @@ -0,0 +1,73 @@ +/**************************************************************** + * 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.mailet; + +import java.util.Objects; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * Strong typing for attribute value, which represents the value of an attribute stored in a mail. + * + * @since Mailet API v3.2 + */ +public class AttributeValue<T> { + + private final T value; + private final Serializer<T> serializer; + + private AttributeValue(T value, Serializer<T> serializer) { + this.value = value; + this.serializer = serializer; + } + + public T value() { + return value; + } + + public JsonNode toJson() { + ObjectNode serialized = JsonNodeFactory.instance.objectNode(); + serialized.put("serializer", serializer.getName()); + serialized.replace("value", serializer.serialize(value)); + return serialized; + } + + public T getValue() { + return value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof AttributeValue) { + AttributeValue<?> that = (AttributeValue<?>) o; + + return Objects.equals(this.value, that.value) + && Objects.equals(this.serializer, that.serializer); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value, serializer); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/src/test/java/org/apache/mailet/AttributeNameTest.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeNameTest.java b/mailet/api/src/test/java/org/apache/mailet/AttributeNameTest.java new file mode 100644 index 0000000..24a9394 --- /dev/null +++ b/mailet/api/src/test/java/org/apache/mailet/AttributeNameTest.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.mailet; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class AttributeNameTest { + + @Test + void shouldRespectBeanContract() { + EqualsVerifier.forClass(AttributeName.class).verify(); + } + + @Test + void ofShouldRejectNullValue() { + assertThatThrownBy(() -> AttributeName.of(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void ofShouldRejectEmptyString() { + assertThatThrownBy(() -> AttributeName.of("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void asStringShouldReturnWrappedValue() { + String name = "name"; + assertThat(AttributeName.of(name).asString()).isEqualTo(name); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c97bc757/mailet/api/src/test/java/org/apache/mailet/AttributeTest.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeTest.java b/mailet/api/src/test/java/org/apache/mailet/AttributeTest.java new file mode 100644 index 0000000..053f745 --- /dev/null +++ b/mailet/api/src/test/java/org/apache/mailet/AttributeTest.java @@ -0,0 +1,46 @@ +/**************************************************************** + * 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.mailet; + +import static org.assertj.core.api.Assertions.assertThatNullPointerException; + +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class AttributeTest { + + @Test + void shouldRespectBeanContract() { + EqualsVerifier.forClass(Attribute.class).verify(); + } + + @Test + void constructorShouldThrowOnNullName() { + assertThatNullPointerException() + .isThrownBy(() -> new Attribute(null, AttributeValue.of(1))); + } + + @Test + void constructorShouldThrowOnNullValue() { + assertThatNullPointerException() + .isThrownBy(() -> new Attribute(AttributeName.of("name"), null)); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org