Repository: docker-java/docker-java (java)

Issue:
non-void setters for AuthConfig
Hope class shouldn't match java beans and can provide fluent setters.

Pull request:
Make AuthConfig fluent, setters -> with.
Resolves #453

Patch:
diff --git a/src/main/java/com/github/dockerjava/api/model/AuthConfig.java b/src/main/java/com/github/dockerjava/api/model/AuthConfig.java
index 3dc3ef34..ae7ebdda 100644
--- a/src/main/java/com/github/dockerjava/api/model/AuthConfig.java
+++ b/src/main/java/com/github/dockerjava/api/model/AuthConfig.java
@@ -3,7 +3,6 @@ package com.github.dockerjava.api.model;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -28,50 +27,54 @@ public class AuthConfig {
     private String email;
 
     @JsonProperty("serveraddress")
-    private String serverAddress = DEFAULT_SERVER_ADDRESS;
+    private String registryAddress = DEFAULT_SERVER_ADDRESS;
 
+    @JsonProperty("auth")
     private String auth;
 
     public String getUsername() {
         return username;
     }
 
-    public void setUsername(String username) {
+    public AuthConfig withUsername(String username) {
         this.username = username;
+        return this;
     }
 
     public String getPassword() {
         return password;
     }
 
-    public void setPassword(String password) {
+    public AuthConfig withPassword(String password) {
         this.password = password;
+        return this;
     }
 
     public String getEmail() {
         return email;
     }
 
-    public void setEmail(String email) {
+    public AuthConfig withEmail(String email) {
         this.email = email;
+        return this;
     }
 
-    public String getServerAddress() {
-        return serverAddress;
+    public String getRegistryAddress() {
+        return registryAddress;
     }
 
-    public void setServerAddress(String serverAddress) {
-        this.serverAddress = serverAddress;
+    public AuthConfig withRegistryAddress(String registryAddress) {
+        this.registryAddress = registryAddress;
+        return this;
     }
 
-    @JsonIgnore
     public String getAuth() {
         return auth;
     }
 
-    @JsonProperty("auth")
-    public void setAuth(String auth) {
+    public AuthConfig withAuth(String auth) {
         this.auth = auth;
+        return this;
     }
 
     @Override
@@ -87,7 +90,7 @@ public class AuthConfig {
         result = prime * result + ((auth == null) ? 0 : auth.hashCode());
         result = prime * result + ((email == null) ? 0 : email.hashCode());
         result = prime * result + ((password == null) ? 0 : password.hashCode());
-        result = prime * result + ((serverAddress == null) ? 0 : serverAddress.hashCode());
+        result = prime * result + ((registryAddress == null) ? 0 : registryAddress.hashCode());
         result = prime * result + ((username == null) ? 0 : username.hashCode());
         return result;
     }
@@ -116,10 +119,10 @@ public class AuthConfig {
                 return false;
         } else if (!password.equals(other.password))
             return false;
-        if (serverAddress == null) {
-            if (other.serverAddress != null)
+        if (registryAddress == null) {
+            if (other.registryAddress != null)
                 return false;
-        } else if (!serverAddress.equals(other.serverAddress))
+        } else if (!registryAddress.equals(other.registryAddress))
             return false;
         if (username == null) {
             if (other.username != null)
diff --git a/src/main/java/com/github/dockerjava/api/model/AuthConfigurations.java b/src/main/java/com/github/dockerjava/api/model/AuthConfigurations.java
index 21451e64..e8edbc95 100644
--- a/src/main/java/com/github/dockerjava/api/model/AuthConfigurations.java
+++ b/src/main/java/com/github/dockerjava/api/model/AuthConfigurations.java
@@ -11,7 +11,7 @@ public class AuthConfigurations {
     private Map<String, AuthConfig> configs = new TreeMap<>();
 
     public void addConfig(AuthConfig authConfig) {
-        configs.put(authConfig.getServerAddress(), authConfig);
+        configs.put(authConfig.getRegistryAddress(), authConfig);
     }
 
     public Map<String, AuthConfig> getConfigs() {
diff --git a/src/main/java/com/github/dockerjava/core/AuthConfigFile.java b/src/main/java/com/github/dockerjava/core/AuthConfigFile.java
index 1a4ef5a4..f796fd7c 100644
--- a/src/main/java/com/github/dockerjava/core/AuthConfigFile.java
+++ b/src/main/java/com/github/dockerjava/core/AuthConfigFile.java
@@ -29,7 +29,7 @@ public class AuthConfigFile {
     }
 
     void addConfig(AuthConfig config) {
-        authConfigMap.put(config.getServerAddress(), config);
+        authConfigMap.put(config.getRegistryAddress(), config);
     }
 
     public AuthConfig resolveAuthConfig(String hostname) {
@@ -111,8 +111,8 @@ public class AuthConfigFile {
             for (Map.Entry<String, AuthConfig> entry : configMap.entrySet()) {
                 AuthConfig authConfig = entry.getValue();
                 decodeAuth(authConfig.getAuth(), authConfig);
-                authConfig.setAuth(null);
-                authConfig.setServerAddress(entry.getKey());
+                authConfig.withAuth(null);
+                authConfig.withRegistryAddress(entry.getKey());
                 configFile.addConfig(authConfig);
             }
         } else {
@@ -131,7 +131,7 @@ public class AuthConfigFile {
             if (origEmail.length != 2) {
                 throw new IOException("Invalid Auth config file");
             }
-            config.setEmail(origEmail[1]);
+            config.withEmail(origEmail[1]);
             configFile.addConfig(config);
         }
         return configFile;
@@ -144,8 +144,8 @@ public class AuthConfigFile {
         if (parts.length != 2) {
             throw new IOException("Invalid auth configuration file");
         }
-        config.setUsername(parts[0]);
-        config.setPassword(parts[1]);
+        config.withUsername(parts[0]);
+        config.withPassword(parts[1]);
     }
 
     static String convertToHostname(String server) {
diff --git a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java
index 5ebd9aa7..d3897199 100644
--- a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java
+++ b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java
@@ -278,11 +278,11 @@ public class DockerClientConfig implements Serializable {
         AuthConfig authConfig = null;
         if (getRegistryUsername() != null && getRegistryPassword() != null && getRegistryEmail() != null
                 && getRegistryUrl() != null) {
-            authConfig = new AuthConfig();
-            authConfig.setUsername(getRegistryUsername());
-            authConfig.setPassword(getRegistryPassword());
-            authConfig.setEmail(getRegistryEmail());
-            authConfig.setServerAddress(getRegistryUrl());
+            authConfig = new AuthConfig()
+                    .withUsername(getRegistryUsername())
+                    .withPassword(getRegistryPassword())
+                    .withEmail(getRegistryEmail())
+                    .withRegistryAddress(getRegistryUrl());
         }
         return authConfig;
     }
diff --git a/src/main/java/com/github/dockerjava/core/DockerClientImpl.java b/src/main/java/com/github/dockerjava/core/DockerClientImpl.java
index bbf667a8..59e18bc8 100644
--- a/src/main/java/com/github/dockerjava/core/DockerClientImpl.java
+++ b/src/main/java/com/github/dockerjava/core/DockerClientImpl.java
@@ -164,13 +164,11 @@ public class DockerClientImpl implements Closeable, DockerClient {
         checkNotNull(dockerClientConfig.getRegistryUsername(), "Configured username is null.");
         checkNotNull(dockerClientConfig.getRegistryUrl(), "Configured serverAddress is null.");
 
-        AuthConfig authConfig = new AuthConfig();
-        authConfig.setUsername(dockerClientConfig.getRegistryUsername());
-        authConfig.setPassword(dockerClientConfig.getRegistryPassword());
-        authConfig.setEmail(dockerClientConfig.getRegistryEmail());
-        authConfig.setServerAddress(dockerClientConfig.getRegistryUrl());
-
-        return authConfig;
+        return new AuthConfig()
+                .withUsername(dockerClientConfig.getRegistryUsername())
+                .withPassword(dockerClientConfig.getRegistryPassword())
+                .withEmail(dockerClientConfig.getRegistryEmail())
+                .withRegistryAddress(dockerClientConfig.getRegistryUrl());
     }
 
     /**
diff --git a/src/main/java/com/github/dockerjava/core/GoLangFileMatch.java b/src/main/java/com/github/dockerjava/core/GoLangFileMatch.java
index 5dabdf40..62dce2ad 100644
--- a/src/main/java/com/github/dockerjava/core/GoLangFileMatch.java
+++ b/src/main/java/com/github/dockerjava/core/GoLangFileMatch.java
@@ -37,7 +37,7 @@ import com.github.dockerjava.core.exception.GoLangFileMatchException;
  *  is malformed.
  *
  * On Windows, escaping is disabled. Instead, '\\' is treated as
- *  path separator.
+ AuthConfigTest *  path separator.
  * </pre>
  *
  * @author tedo


Test patch:
diff --git a/src/test/java/com/github/dockerjava/api/model/AuthConfigTest.java b/src/test/java/com/github/dockerjava/api/model/AuthConfigTest.java
index 33066a84..8f5bd64f 100644
--- a/src/test/java/com/github/dockerjava/api/model/AuthConfigTest.java
+++ b/src/test/java/com/github/dockerjava/api/model/AuthConfigTest.java
@@ -11,15 +11,15 @@ public class AuthConfigTest {
 
     @BeforeMethod
     public void setUp() throws Exception {
-        authConfig = new AuthConfig();
-        authConfig.setEmail("foo");
-        authConfig.setPassword("bar");
-        authConfig.setServerAddress("baz");
-        authConfig.setUsername("qux");
+        authConfig = new AuthConfig()
+                .withEmail("foo")
+                .withPassword("bar")
+                .withRegistryAddress("baz")
+                .withUsername("qux");
     }
 
     @Test
     public void defaultServerAddress() throws Exception {
-        assertEquals(new AuthConfig().getServerAddress(), "https://index.docker.io/v1/");
+        assertEquals(new AuthConfig().getRegistryAddress(), "https://index.docker.io/v1/");
     }
 }
diff --git a/src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java b/src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java
index e1c1b6f8..221a926b 100644
--- a/src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java
+++ b/src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java
@@ -48,17 +48,17 @@ public class AuthConfigFileTest {
 
     @Test
     public void validJson() throws IOException {
-        AuthConfig authConfig1 = new AuthConfig();
-        authConfig1.setEmail("foo@example.com");
-        authConfig1.setUsername("foo");
-        authConfig1.setPassword("bar");
-        authConfig1.setServerAddress("quay.io");
-
-        AuthConfig authConfig2 = new AuthConfig();
-        authConfig2.setEmail("moo@example.com");
-        authConfig2.setUsername("foo1");
-        authConfig2.setPassword("bar1");
-        authConfig2.setServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
+        AuthConfig authConfig1 = new AuthConfig()
+                .withEmail("foo@example.com")
+                .withUsername("foo")
+                .withPassword("bar")
+                .withRegistryAddress("quay.io");
+
+        AuthConfig authConfig2 = new AuthConfig()
+                .withEmail("moo@example.com")
+                .withUsername("foo1")
+                .withPassword("bar1")
+                .withRegistryAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
 
         AuthConfigFile expected = new AuthConfigFile();
         expected.addConfig(authConfig1);
@@ -70,11 +70,11 @@ public class AuthConfigFileTest {
 
     @Test
     public void validLegacy() throws IOException {
-        AuthConfig authConfig = new AuthConfig();
-        authConfig.setEmail("foo@example.com");
-        authConfig.setUsername("foo");
-        authConfig.setPassword("bar");
-        authConfig.setServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
+        AuthConfig authConfig = new AuthConfig()
+                .withEmail("foo@example.com")
+                .withUsername("foo")
+                .withPassword("bar")
+                .withRegistryAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
 
         AuthConfigFile expected = new AuthConfigFile();
         expected.addConfig(authConfig);
diff --git a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
index 775b12b1..99eefb4d 100644
--- a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
+++ b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
@@ -274,13 +274,12 @@ public class BuildImageCmdImplTest extends AbstractDockerClientTest {
         // wait for registry to boot
         Thread.sleep(3000);
 
-        AuthConfig authConfig = new AuthConfig();
-
         // credentials as configured in /auth/htpasswd
-        authConfig.setUsername("testuser");
-        authConfig.setPassword("testpassword");
-        authConfig.setEmail("foo@bar.de");
-        authConfig.setServerAddress("localhost:5000");
+        AuthConfig authConfig = new AuthConfig()
+                .withUsername("testuser")
+                .withPassword("testpassword")
+                .withEmail("foo@bar.de")
+                .withRegistryAddress("localhost:5000");
 
         dockerClient.authCmd().withAuthConfig(authConfig).exec();
         dockerClient.tagImageCmd("busybox:latest", "localhost:5000/testuser/busybox", "latest").withForce().exec();
diff --git a/src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java b/src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java
index fad4ef7b..8a19c7eb 100644
--- a/src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java
+++ b/src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java
@@ -266,13 +266,12 @@ public class BuildImageCmdExecTest extends AbstractNettyDockerClientTest {
         // wait for registry to boot
         Thread.sleep(3000);
 
-        AuthConfig authConfig = new AuthConfig();
-
         // credentials as configured in /auth/htpasswd
-        authConfig.setUsername("testuser");
-        authConfig.setPassword("testpassword");
-        authConfig.setEmail("foo@bar.de");
-        authConfig.setServerAddress("localhost:5000");
+        AuthConfig authConfig = new AuthConfig()
+                .withUsername("testuser")
+                .withPassword("testpassword")
+                .withEmail("foo@bar.de")
+                .withRegistryAddress("localhost:5000");
 
         dockerClient.authCmd().withAuthConfig(authConfig).exec();
         dockerClient.tagImageCmd("busybox:latest", "localhost:5000/testuser/busybox", "latest").withForce().exec();