Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
New enum AccessMode improves instantiation and parsing of Bind
  • Loading branch information
albers committed Oct 7, 2014
commit 74ea00bc856b6b5d9d598397d2707c834240abf4
19 changes: 19 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/AccessMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.dockerjava.api.model;

/**
* The access mode of a file system or file: <code>read-write</code>
* or <code>read-only</code>.
*/
public enum AccessMode {
/** read-write */
rw,

/** read-only */
ro;

/**
* The default {@link AccessMode}: {@link #rw}
*/
public static final AccessMode DEFAULT = rw;

}
43 changes: 29 additions & 14 deletions src/main/java/com/github/dockerjava/api/model/Bind.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.api.model.AccessMode.ro;
import static com.github.dockerjava.api.model.AccessMode.rw;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

Expand All @@ -14,16 +17,24 @@ public class Bind {

private Volume volume;

private boolean readOnly = false;
private AccessMode accessMode;

public Bind(String path, Volume volume) {
this(path, volume, false);
this(path, volume, AccessMode.DEFAULT);
}

public Bind(String path, Volume volume, boolean readOnly) {
public Bind(String path, Volume volume, AccessMode accessMode) {
this.path = path;
this.volume = volume;
this.readOnly = readOnly;
this.accessMode = accessMode;
}

/**
* @deprecated use {@link #Bind(String, Volume, AccessMode)}
*/
@Deprecated
public Bind(String path, Volume volume, boolean readOnly) {
this(path, volume, readOnly ? ro : rw);
}

public String getPath() {
Expand All @@ -33,9 +44,17 @@ public String getPath() {
public Volume getVolume() {
return volume;
}

public AccessMode getAccessMode() {
return accessMode;
}

/**
* @deprecated use {@link #getAccessMode()}
*/
@Deprecated
public boolean isReadOnly() {
return readOnly;
return ro.equals(accessMode);
}

/**
Expand All @@ -53,12 +72,8 @@ public static Bind parse(String serialized) {
return new Bind(parts[0], Volume.parse(parts[1]));
}
case 3: {
if ("rw".equals(parts[2].toLowerCase()))
return new Bind(parts[0], Volume.parse(parts[1]), false);
else if ("ro".equals(parts[2].toLowerCase()))
return new Bind(parts[0], Volume.parse(parts[1]), true);
else
throw new IllegalArgumentException();
AccessMode accessMode = AccessMode.valueOf(parts[2].toLowerCase());
return new Bind(parts[0], Volume.parse(parts[1]), accessMode);
}
default: {
throw new IllegalArgumentException();
Expand All @@ -76,15 +91,15 @@ public boolean equals(Object obj) {
Bind other = (Bind) obj;
return new EqualsBuilder().append(path, other.getPath())
.append(volume, other.getVolume())
.append(readOnly, other.isReadOnly()).isEquals();
.append(accessMode, other.getAccessMode()).isEquals();
} else
return super.equals(obj);
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(path).append(volume)
.append(readOnly).toHashCode();
.append(accessMode).toHashCode();
}

/**
Expand All @@ -97,7 +112,7 @@ public int hashCode() {
*/
@Override
public String toString() {
return path + ":" + volume.toString() + (readOnly ? ":ro" : ":rw");
return path + ":" + volume.toString() + ":" + accessMode.toString();
}

}
31 changes: 31 additions & 0 deletions src/test/java/com/github/dockerjava/api/model/AccessModeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.api.model.AccessMode.rw;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class AccessModeTest {

@Test
public void defaultAccessMode() {
assertEquals(AccessMode.DEFAULT, rw);
}

@Test
public void stringify() {
assertEquals(AccessMode.rw.toString(), "rw");
}

@Test
public void fromString() {
assertEquals(AccessMode.valueOf("rw"), rw);
}

@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "No enum constant.*")
public void fromIllegalString() {
AccessMode.valueOf("xx");
}

}
8 changes: 5 additions & 3 deletions src/test/java/com/github/dockerjava/api/model/BindTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.api.model.AccessMode.ro;
import static com.github.dockerjava.api.model.AccessMode.rw;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;
Expand All @@ -11,23 +13,23 @@ public void parseUsingDefaultAccessMode() {
Bind bind = Bind.parse("/host:/container");
assertEquals(bind.getPath(), "/host");
assertEquals(bind.getVolume().getPath(), "/container");
assertEquals(bind.isReadOnly(), false);
assertEquals(bind.getAccessMode(), AccessMode.DEFAULT);
}

@Test
public void parseReadWrite() {
Bind bind = Bind.parse("/host:/container:rw");
assertEquals(bind.getPath(), "/host");
assertEquals(bind.getVolume().getPath(), "/container");
assertEquals(bind.isReadOnly(), false);
assertEquals(bind.getAccessMode(), rw);
}

@Test
public void parseReadOnly() {
Bind bind = Bind.parse("/host:/container:ro");
assertEquals(bind.getPath(), "/host");
assertEquals(bind.getVolume().getPath(), "/container");
assertEquals(bind.isReadOnly(), true);
assertEquals(bind.getAccessMode(), ro);
}

@Test(expectedExceptions = IllegalArgumentException.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dockerjava.core.command;

import static com.github.dockerjava.api.model.AccessMode.ro;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void startContainerWithVolumes() throws DockerException {
assertThat(inspectContainerResponse.getConfig().getVolumes().keySet(),
contains("/opt/webapp1", "/opt/webapp2"));

dockerClient.startContainerCmd(container.getId()).withBinds(new Bind("/src/webapp1", volume1, true), new Bind("/src/webapp2", volume2)).exec();
dockerClient.startContainerCmd(container.getId()).withBinds(new Bind("/src/webapp1", volume1, ro), new Bind("/src/webapp2", volume2)).exec();

dockerClient.waitContainerCmd(container.getId()).exec();

Expand Down