Skip to content

Commit 74ea00b

Browse files
committed
New enum AccessMode improves instantiation and parsing of Bind
1 parent 1c91db0 commit 74ea00b

File tree

5 files changed

+86
-18
lines changed

5 files changed

+86
-18
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.dockerjava.api.model;
2+
3+
/**
4+
* The access mode of a file system or file: <code>read-write</code>
5+
* or <code>read-only</code>.
6+
*/
7+
public enum AccessMode {
8+
/** read-write */
9+
rw,
10+
11+
/** read-only */
12+
ro;
13+
14+
/**
15+
* The default {@link AccessMode}: {@link #rw}
16+
*/
17+
public static final AccessMode DEFAULT = rw;
18+
19+
}

src/main/java/com/github/dockerjava/api/model/Bind.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.dockerjava.api.model;
22

3+
import static com.github.dockerjava.api.model.AccessMode.ro;
4+
import static com.github.dockerjava.api.model.AccessMode.rw;
5+
36
import org.apache.commons.lang.builder.EqualsBuilder;
47
import org.apache.commons.lang.builder.HashCodeBuilder;
58

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

1518
private Volume volume;
1619

17-
private boolean readOnly = false;
20+
private AccessMode accessMode;
1821

1922
public Bind(String path, Volume volume) {
20-
this(path, volume, false);
23+
this(path, volume, AccessMode.DEFAULT);
2124
}
2225

23-
public Bind(String path, Volume volume, boolean readOnly) {
26+
public Bind(String path, Volume volume, AccessMode accessMode) {
2427
this.path = path;
2528
this.volume = volume;
26-
this.readOnly = readOnly;
29+
this.accessMode = accessMode;
30+
}
31+
32+
/**
33+
* @deprecated use {@link #Bind(String, Volume, AccessMode)}
34+
*/
35+
@Deprecated
36+
public Bind(String path, Volume volume, boolean readOnly) {
37+
this(path, volume, readOnly ? ro : rw);
2738
}
2839

2940
public String getPath() {
@@ -33,9 +44,17 @@ public String getPath() {
3344
public Volume getVolume() {
3445
return volume;
3546
}
47+
48+
public AccessMode getAccessMode() {
49+
return accessMode;
50+
}
3651

52+
/**
53+
* @deprecated use {@link #getAccessMode()}
54+
*/
55+
@Deprecated
3756
public boolean isReadOnly() {
38-
return readOnly;
57+
return ro.equals(accessMode);
3958
}
4059

4160
/**
@@ -53,12 +72,8 @@ public static Bind parse(String serialized) {
5372
return new Bind(parts[0], Volume.parse(parts[1]));
5473
}
5574
case 3: {
56-
if ("rw".equals(parts[2].toLowerCase()))
57-
return new Bind(parts[0], Volume.parse(parts[1]), false);
58-
else if ("ro".equals(parts[2].toLowerCase()))
59-
return new Bind(parts[0], Volume.parse(parts[1]), true);
60-
else
61-
throw new IllegalArgumentException();
75+
AccessMode accessMode = AccessMode.valueOf(parts[2].toLowerCase());
76+
return new Bind(parts[0], Volume.parse(parts[1]), accessMode);
6277
}
6378
default: {
6479
throw new IllegalArgumentException();
@@ -76,15 +91,15 @@ public boolean equals(Object obj) {
7691
Bind other = (Bind) obj;
7792
return new EqualsBuilder().append(path, other.getPath())
7893
.append(volume, other.getVolume())
79-
.append(readOnly, other.isReadOnly()).isEquals();
94+
.append(accessMode, other.getAccessMode()).isEquals();
8095
} else
8196
return super.equals(obj);
8297
}
8398

8499
@Override
85100
public int hashCode() {
86101
return new HashCodeBuilder().append(path).append(volume)
87-
.append(readOnly).toHashCode();
102+
.append(accessMode).toHashCode();
88103
}
89104

90105
/**
@@ -97,7 +112,7 @@ public int hashCode() {
97112
*/
98113
@Override
99114
public String toString() {
100-
return path + ":" + volume.toString() + (readOnly ? ":ro" : ":rw");
115+
return path + ":" + volume.toString() + ":" + accessMode.toString();
101116
}
102117

103118
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import static com.github.dockerjava.api.model.AccessMode.rw;
4+
import static org.testng.Assert.assertEquals;
5+
6+
import org.testng.annotations.Test;
7+
8+
public class AccessModeTest {
9+
10+
@Test
11+
public void defaultAccessMode() {
12+
assertEquals(AccessMode.DEFAULT, rw);
13+
}
14+
15+
@Test
16+
public void stringify() {
17+
assertEquals(AccessMode.rw.toString(), "rw");
18+
}
19+
20+
@Test
21+
public void fromString() {
22+
assertEquals(AccessMode.valueOf("rw"), rw);
23+
}
24+
25+
@Test(expectedExceptions = IllegalArgumentException.class,
26+
expectedExceptionsMessageRegExp = "No enum constant.*")
27+
public void fromIllegalString() {
28+
AccessMode.valueOf("xx");
29+
}
30+
31+
}

src/test/java/com/github/dockerjava/api/model/BindTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.dockerjava.api.model;
22

3+
import static com.github.dockerjava.api.model.AccessMode.ro;
4+
import static com.github.dockerjava.api.model.AccessMode.rw;
35
import static org.testng.Assert.assertEquals;
46

57
import org.testng.annotations.Test;
@@ -11,23 +13,23 @@ public void parseUsingDefaultAccessMode() {
1113
Bind bind = Bind.parse("/host:/container");
1214
assertEquals(bind.getPath(), "/host");
1315
assertEquals(bind.getVolume().getPath(), "/container");
14-
assertEquals(bind.isReadOnly(), false);
16+
assertEquals(bind.getAccessMode(), AccessMode.DEFAULT);
1517
}
1618

1719
@Test
1820
public void parseReadWrite() {
1921
Bind bind = Bind.parse("/host:/container:rw");
2022
assertEquals(bind.getPath(), "/host");
2123
assertEquals(bind.getVolume().getPath(), "/container");
22-
assertEquals(bind.isReadOnly(), false);
24+
assertEquals(bind.getAccessMode(), rw);
2325
}
2426

2527
@Test
2628
public void parseReadOnly() {
2729
Bind bind = Bind.parse("/host:/container:ro");
2830
assertEquals(bind.getPath(), "/host");
2931
assertEquals(bind.getVolume().getPath(), "/container");
30-
assertEquals(bind.isReadOnly(), true);
32+
assertEquals(bind.getAccessMode(), ro);
3133
}
3234

3335
@Test(expectedExceptions = IllegalArgumentException.class,

src/test/java/com/github/dockerjava/core/command/StartContainerCmdImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.core.command;
22

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

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

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

0 commit comments

Comments
 (0)