This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 3a64f618bb798711c63bd554c25636c09036a2f6 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Mon Mar 4 11:25:20 2019 +0700 JAMES-2664 Configuration objects for PreDeletionHooks --- server/container/guice/mailbox/pom.xml | 5 + .../mailbox/PreDeletionHookConfiguration.java | 74 ++++++++++++++ .../mailbox/PreDeletionHooksConfiguration.java | 72 ++++++++++++++ .../mailbox/PreDeletionHookConfigurationTest.java | 64 ++++++++++++ .../mailbox/PreDeletionHooksConfigurationTest.java | 107 +++++++++++++++++++++ 5 files changed, 322 insertions(+) diff --git a/server/container/guice/mailbox/pom.xml b/server/container/guice/mailbox/pom.xml index 04ef31d..4897f64 100644 --- a/server/container/guice/mailbox/pom.xml +++ b/server/container/guice/mailbox/pom.xml @@ -61,6 +61,11 @@ <artifactId>guice-multibindings</artifactId> </dependency> <dependency> + <groupId>nl.jqno.equalsverifier</groupId> + <artifactId>equalsverifier</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookConfiguration.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookConfiguration.java new file mode 100644 index 0000000..9e36255 --- /dev/null +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookConfiguration.java @@ -0,0 +1,74 @@ +/**************************************************************** + * 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.modules.mailbox; + +import java.util.Objects; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +public class PreDeletionHookConfiguration { + + public static final String CLASS_NAME_CONFIGURATION_ENTRY = "class"; + + public static PreDeletionHookConfiguration from(HierarchicalConfiguration configuration) throws ConfigurationException { + Preconditions.checkNotNull(configuration); + + try { + return forClass(configuration.getString(CLASS_NAME_CONFIGURATION_ENTRY)); + } catch (Exception e) { + throw new ConfigurationException("Exception encountered in PreDeletionHook configuration", e); + } + } + + public static PreDeletionHookConfiguration forClass(String clazz) { + return new PreDeletionHookConfiguration(clazz); + } + + private final String clazz; + + private PreDeletionHookConfiguration(String clazz) { + Preconditions.checkNotNull(clazz, "class name is mandatory"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(clazz), "class name should not be empty"); + + this.clazz = clazz; + } + + public String getClazz() { + return clazz; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof PreDeletionHookConfiguration) { + PreDeletionHookConfiguration that = (PreDeletionHookConfiguration) o; + + return Objects.equals(this.clazz, that.clazz); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(clazz); + } +} \ No newline at end of file diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHooksConfiguration.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHooksConfiguration.java new file mode 100644 index 0000000..14df7cb --- /dev/null +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHooksConfiguration.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.james.modules.mailbox; + +import java.util.List; +import java.util.Objects; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; + +import com.github.fge.lambdas.Throwing; +import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; + +public class PreDeletionHooksConfiguration { + static final String CONFIGURATION_ENTRY_NAME = "preDeletionHook"; + + public static PreDeletionHooksConfiguration from(HierarchicalConfiguration configuration) throws ConfigurationException { + return new PreDeletionHooksConfiguration( + configuration.configurationsAt(CONFIGURATION_ENTRY_NAME) + .stream() + .map(Throwing.function(PreDeletionHookConfiguration::from).sneakyThrow()) + .collect(Guavate.toImmutableList())); + } + + public static PreDeletionHooksConfiguration none() { + return new PreDeletionHooksConfiguration(ImmutableList.of()); + } + + private final List<PreDeletionHookConfiguration> hooksConfiguration; + + @VisibleForTesting + PreDeletionHooksConfiguration(List<PreDeletionHookConfiguration> hooksConfiguration) { + this.hooksConfiguration = hooksConfiguration; + } + + public List<PreDeletionHookConfiguration> getHooksConfiguration() { + return hooksConfiguration; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof PreDeletionHooksConfiguration) { + PreDeletionHooksConfiguration that = (PreDeletionHooksConfiguration) o; + + return Objects.equals(this.hooksConfiguration, that.hooksConfiguration); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(hooksConfiguration); + } +} diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookConfigurationTest.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookConfigurationTest.java new file mode 100644 index 0000000..355eab1 --- /dev/null +++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookConfigurationTest.java @@ -0,0 +1,64 @@ +/**************************************************************** + * 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.modules.mailbox; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class PreDeletionHookConfigurationTest { + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(PreDeletionHookConfiguration.class) + .verify(); + } + + @Test + void fromShouldThrowWhenClassNameIsMissing() { + HierarchicalConfiguration configuration = new HierarchicalConfiguration(); + + assertThatThrownBy(() -> PreDeletionHookConfiguration.from(configuration)) + .isInstanceOf(ConfigurationException.class); + } + + @Test + void fromShouldThrowWhenClassNameIsEmpty() { + HierarchicalConfiguration configuration = new HierarchicalConfiguration(); + configuration.addProperty("class", ""); + + assertThatThrownBy(() -> PreDeletionHookConfiguration.from(configuration)) + .isInstanceOf(ConfigurationException.class); + } + + @Test + void fromShouldReturnValueWithCorrectClassName() throws ConfigurationException { + HierarchicalConfiguration configuration = new HierarchicalConfiguration(); + String className = "a.class"; + configuration.addProperty("class", className); + + assertThat(PreDeletionHookConfiguration.from(configuration)) + .isEqualTo(PreDeletionHookConfiguration.forClass(className)); + } +} \ No newline at end of file diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHooksConfigurationTest.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHooksConfigurationTest.java new file mode 100644 index 0000000..f14a977 --- /dev/null +++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHooksConfigurationTest.java @@ -0,0 +1,107 @@ +/**************************************************************** + * 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.modules.mailbox; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.commons.configuration.XMLConfiguration; +import org.junit.jupiter.api.Test; + +import com.google.common.collect.ImmutableList; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class PreDeletionHooksConfigurationTest { + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(PreDeletionHooksConfiguration.class) + .verify(); + } + + @Test + void fromShouldReturnNoneWhenEmpty() throws Exception { + HierarchicalConfiguration configuration = new HierarchicalConfiguration(); + + assertThat(PreDeletionHooksConfiguration.from(configuration)) + .isEqualTo(PreDeletionHooksConfiguration.none()); + } + + @Test + void fromShouldThrowWhenInvalidHookConfiguration() throws Exception { + XMLConfiguration configuration = new XMLConfiguration(); + configuration.load(new ByteArrayInputStream(( + "<preDeletionHooks>" + + " <preDeletionHook>" + + " <class></class>" + + " </preDeletionHook>" + + "</preDeletionHooks>") + .getBytes(StandardCharsets.UTF_8))); + + HierarchicalConfiguration invalidConfigurationEntry = new HierarchicalConfiguration(); + configuration.addProperty(PreDeletionHooksConfiguration.CONFIGURATION_ENTRY_NAME, ImmutableList.of(invalidConfigurationEntry)); + + assertThatThrownBy(() -> PreDeletionHooksConfiguration.from(configuration)) + .isInstanceOf(ConfigurationException.class); + } + + @Test + void fromShouldReturnConfiguredEntry() throws Exception { + XMLConfiguration configuration = new XMLConfiguration(); + configuration.load(new ByteArrayInputStream(( + "<preDeletionHooks>" + + " <preDeletionHook>" + + " <class>a.class</class>" + + " </preDeletionHook>" + + "</preDeletionHooks>") + .getBytes(StandardCharsets.UTF_8))); + + String className = "a.class"; + assertThat(PreDeletionHooksConfiguration.from(configuration)) + .isEqualTo(new PreDeletionHooksConfiguration(ImmutableList.of(PreDeletionHookConfiguration.forClass(className)))); + } + + @Test + void fromShouldReturnAllConfiguredEntries() throws Exception { + XMLConfiguration configuration = new XMLConfiguration(); + configuration.load(new ByteArrayInputStream(( + "<preDeletionHooks>" + + " <preDeletionHook>" + + " <class>a.class</class>" + + " </preDeletionHook>" + + " <preDeletionHook>" + + " <class>b.class</class>" + + " </preDeletionHook>" + + "</preDeletionHooks>") + .getBytes(StandardCharsets.UTF_8))); + + String className1 = "a.class"; + String className2 = "b.class"; + assertThat(PreDeletionHooksConfiguration.from(configuration)) + .isEqualTo(new PreDeletionHooksConfiguration(ImmutableList.of( + PreDeletionHookConfiguration.forClass(className1), + PreDeletionHookConfiguration.forClass(className2)))); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org