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 a342b9e09dc00cca7674fe8b1ba4e3e4d8c050c5 Author: Tran Tien Duc <dt...@linagora.com> AuthorDate: Wed Mar 27 16:47:38 2019 +0700 JAMES-2688 POJO for the chosen of which BlobExport is used --- .../apache/james/modules/BlobExportImplChoice.java | 113 +++++++++++++++++++++ .../james/modules/BlobExportImplChoiceTest.java | 82 +++++++++++++++ 2 files changed, 195 insertions(+) diff --git a/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java new file mode 100644 index 0000000..a28e622 --- /dev/null +++ b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java @@ -0,0 +1,113 @@ +/**************************************************************** + * 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; + +import java.util.Locale; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +import org.apache.commons.configuration.Configuration; + +import com.github.steveash.guavate.Guavate; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +class BlobExportImplChoice { + + enum BlobExportImplName { + LOCAL_FILE("localfile"); + + private static Optional<BlobExportImplName> from(String implNameString) { + Preconditions.checkNotNull(implNameString); + + return Stream.of(values()) + .filter(impl -> impl.name.equals(implNameString)) + .findFirst(); + } + + private static ImmutableList<String> plainImplNames() { + return Stream.of(values()) + .map(impl -> impl.name) + .collect(Guavate.toImmutableList()); + } + + private final String name; + + BlobExportImplName(String name) { + this.name = name; + } + + String getImplName() { + return name; + } + } + + static BlobExportImplChoice localFile() { + return new BlobExportImplChoice(BlobExportImplName.LOCAL_FILE); + } + + static BlobExportImplChoice from(Configuration configuration) { + String blobExportImpl = configuration.getString(BLOB_EXPORT_MECHANISM_IMPL); + + String sanitizedImplName = Optional.ofNullable(blobExportImpl) + .map(String::trim) + .map(implName -> implName.toLowerCase(Locale.US)) + .orElseThrow(() -> new NullPointerException(BLOB_EXPORT_MECHANISM_IMPL + " property is mandatory")); + + return BlobExportImplName.from(sanitizedImplName) + .map(BlobExportImplChoice::new) + .orElseThrow(() -> new IllegalArgumentException(unknownBlobExportErrorMessage(blobExportImpl))); + } + + private static String unknownBlobExportErrorMessage(String blobExportImpl) { + return String.format("unknow blob export mechanism '%s', please chose one in supported implementations(%s)", + blobExportImpl, + Joiner.on(",").join(BlobExportImplName.plainImplNames())); + } + + private static final String BLOB_EXPORT_MECHANISM_IMPL = "blob.export.implementation"; + + private final BlobExportImplName impl; + + private BlobExportImplChoice(BlobExportImplName implName) { + this.impl = implName; + } + + public BlobExportImplName getImpl() { + return impl; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof BlobExportImplChoice) { + BlobExportImplChoice that = (BlobExportImplChoice) o; + + return Objects.equals(this.impl, that.impl); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(impl); + } +} diff --git a/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java new file mode 100644 index 0000000..e3a2c98 --- /dev/null +++ b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java @@ -0,0 +1,82 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.modules.BlobExportImplChoice.BlobExportImplName; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class BlobExportImplChoiceTest { + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(BlobExportImplChoice.class) + .verify(); + } + + @Test + void fromShouldThrowWhenImplIsNull() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + + assertThatThrownBy(() -> BlobExportImplChoice.from(configuration)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void fromShouldThrowWhenImplIsNotInAvailableList() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("blob.export.implementation", "unknown"); + + assertThatThrownBy(() -> BlobExportImplChoice.from(configuration)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void fromShouldReturnLocalFileImplWhenPassingLocalFileImplConfiguration() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("blob.export.implementation", "localfile"); + + assertThat(BlobExportImplChoice.from(configuration).getImpl()) + .isEqualTo(BlobExportImplName.LOCAL_FILE); + } + + @Test + void fromShouldBeCaseInSensitive() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("blob.export.implementation", "localFILE"); + + assertThat(BlobExportImplChoice.from(configuration).getImpl()) + .isEqualTo(BlobExportImplName.LOCAL_FILE); + } + + @Test + void fromShouldIgnoreBlankSpacesBeforeAndAfter() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("blob.export.implementation", " localfile "); + + assertThat(BlobExportImplChoice.from(configuration).getImpl()) + .isEqualTo(BlobExportImplName.LOCAL_FILE); + } +} \ 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