james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 06/24: JAMES-2688 LocalFileExport.Configuration creation from properties
Date Thu, 11 Apr 2019 00:33:37 GMT
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 bc9fafe1e78fae6dfc10c87729753f66ac3001a7
Author: Tran Tien Duc <dtran@linagora.com>
AuthorDate: Wed Mar 27 16:45:46 2019 +0700

    JAMES-2688 LocalFileExport.Configuration creation from properties
---
 server/blob/blob-export-file/pom.xml               |  6 +++-
 .../export/file/LocalFileBlobExportMechanism.java  | 36 ++++++++++++++++++++++
 .../blob/export/file/FileSystemExtension.java      |  4 ++-
 .../file/LocalFileBlobExportMechanismTest.java     | 33 ++++++++++++++++++++
 4 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/server/blob/blob-export-file/pom.xml b/server/blob/blob-export-file/pom.xml
index ecb1d9c..c74fffe 100644
--- a/server/blob/blob-export-file/pom.xml
+++ b/server/blob/blob-export-file/pom.xml
@@ -59,7 +59,11 @@
             <groupId>${james.groupId}</groupId>
             <artifactId>james-server-filesystem-api</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>nl.jqno.equalsverifier</groupId>
+            <artifactId>equalsverifier</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.assertj</groupId>
             <artifactId>assertj-core</artifactId>
diff --git a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
index 5982c5b..347ed7d 100644
--- a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
+++ b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
@@ -22,6 +22,7 @@ package org.apache.james.blob.export.file;
 import java.io.File;
 import java.io.IOException;
 import java.net.UnknownHostException;
+import java.util.Objects;
 import java.util.Optional;
 
 import javax.inject.Inject;
@@ -39,6 +40,9 @@ import org.apache.james.filesystem.api.FileSystem;
 import org.apache.james.server.core.MailImpl;
 import org.apache.mailet.MailetContext;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
 public class LocalFileBlobExportMechanism implements BlobExportMechanism {
     private static final int STRING_LENGTH = 32;
     private static final boolean WITH_LETTERS = true;
@@ -48,14 +52,46 @@ public class LocalFileBlobExportMechanism implements BlobExportMechanism
{
 
     public static class Configuration {
 
+        public static Configuration from(org.apache.commons.configuration.Configuration propertiesConfiguration)
{
+            String exportDirectory = propertiesConfiguration.getString(DIRECTORY_LOCATION_PROPERTY);
+            return of(exportDirectory);
+        }
+
+        @VisibleForTesting
+        static Configuration of(String exportDirectory) {
+            return new Configuration(exportDirectory);
+        }
+
+        private static final String DIRECTORY_LOCATION_PROPERTY = "blob.export.localFile.storingDirectory";
         private static final String DEFAULT_DIRECTORY_LOCATION = "file://var/blobExporting";
         public static final Configuration DEFAULT_CONFIGURATION = new Configuration(DEFAULT_DIRECTORY_LOCATION);
 
         private final String exportDirectory;
 
         public Configuration(String exportDirectory) {
+            Preconditions.checkNotNull(exportDirectory);
+
             this.exportDirectory = exportDirectory;
         }
+
+        public String getExportDirectory() {
+            return exportDirectory;
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Configuration) {
+                Configuration that = (Configuration) o;
+
+                return Objects.equals(this.exportDirectory, that.exportDirectory);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(exportDirectory);
+        }
     }
 
     private final MailetContext mailetContext;
diff --git a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/FileSystemExtension.java
b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/FileSystemExtension.java
index ed1dbd8..80410b3 100644
--- a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/FileSystemExtension.java
+++ b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/FileSystemExtension.java
@@ -43,7 +43,9 @@ public class FileSystemExtension implements ParameterResolver, BeforeAllCallback
 
     @Override
     public void afterAll(ExtensionContext context) throws Exception {
-        FileUtils.forceDelete(fileSystem.getBasedir());
+        if (fileSystem.getBasedir().exists()) {
+            FileUtils.forceDelete(fileSystem.getBasedir());
+        }
     }
 
     @Override
diff --git a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
index b824afa..e33c025 100644
--- a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
+++ b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
@@ -32,12 +32,14 @@ import java.nio.charset.StandardCharsets;
 import javax.mail.Message;
 import javax.mail.internet.InternetAddress;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.BlobStore;
 import org.apache.james.blob.api.HashBlobId;
 import org.apache.james.blob.api.ObjectStoreException;
 import org.apache.james.blob.export.api.FileExtension;
+import org.apache.james.blob.export.file.LocalFileBlobExportMechanism.Configuration;
 import org.apache.james.blob.memory.MemoryBlobStore;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.filesystem.api.FileSystem;
@@ -45,9 +47,12 @@ import org.apache.james.util.MimeMessageUtil;
 import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 
+import nl.jqno.equalsverifier.EqualsVerifier;
+
 @ExtendWith(FileSystemExtension.class)
 class LocalFileBlobExportMechanismTest {
     private static final byte[] BLOB_CONTENT = "blob_content".getBytes(StandardCharsets.UTF_8);
@@ -185,4 +190,32 @@ class LocalFileBlobExportMechanismTest {
                 }
             });
     }
+
+        @Nested
+    class ConfigurationTest {
+
+        @Test
+        void shouldMatchBeanContract() {
+            EqualsVerifier.forClass(Configuration.class)
+                .verify();
+        }
+        @Test
+        void fromShouldThrowWhenDirectoryIsMissing() {
+            PropertiesConfiguration configuration = new PropertiesConfiguration();
+            configuration.addProperty("blob.export.localFile.storingDirectory", null);
+
+            assertThatThrownBy(() -> Configuration.from(configuration))
+                .isInstanceOf(NullPointerException.class);
+        }
+
+        @Test
+        void fromShouldReturnConfigurationWhenDirectoryIsSpecified() {
+            PropertiesConfiguration configuration = new PropertiesConfiguration();
+            String exportDirectory = "file://var/localFileBlobExport";
+            configuration.addProperty("blob.export.localFile.storingDirectory", exportDirectory);
+
+            assertThat(Configuration.from(configuration))
+                .isEqualTo(Configuration.of(exportDirectory));
+        }
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message