Skip to content

Commit 328d345

Browse files
committed
Merge pull request docker-java#63 from albers/bind-parse
Bind.parse() fails when access mode is specified
2 parents 6d7d8bd + ed92136 commit 328d345

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public boolean isReadOnly() {
3333
return readOnly;
3434
}
3535

36+
/**
37+
* Parses a bind mount specification to a {@link Bind}.
38+
*
39+
* @param serialized the specification, e.g. <code>/host:/container:ro</code>
40+
* @return a {@link Bind} matching the specification
41+
* @throws IllegalArgumentException if the specification cannot be parsed
42+
*/
3643
public static Bind parse(String serialized) {
3744
try {
3845
String[] parts = serialized.split(":");
@@ -41,19 +48,19 @@ public static Bind parse(String serialized) {
4148
return new Bind(parts[0], Volume.parse(parts[1]));
4249
}
4350
case 3: {
44-
if ("rw".equals(parts[3].toLowerCase()))
51+
if ("rw".equals(parts[2].toLowerCase()))
52+
return new Bind(parts[0], Volume.parse(parts[1]), false);
53+
else if ("ro".equals(parts[2].toLowerCase()))
4554
return new Bind(parts[0], Volume.parse(parts[1]), true);
4655
else
47-
throw new RuntimeException("Error parsing Bind '"
48-
+ serialized + "'");
56+
throw new IllegalArgumentException();
4957
}
5058
default: {
51-
throw new RuntimeException("Error parsing Bind '" + serialized
52-
+ "'");
59+
throw new IllegalArgumentException();
5360
}
5461
}
5562
} catch (Exception e) {
56-
throw new RuntimeException("Error parsing Bind '" + serialized
63+
throw new IllegalArgumentException("Error parsing Bind '" + serialized
5764
+ "'");
5865
}
5966
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import org.testng.annotations.Test;
6+
7+
public class BindTest {
8+
9+
@Test
10+
public void parseUsingDefaultAccessMode() {
11+
Bind bind = Bind.parse("/host:/container");
12+
assertEquals(bind.getPath(), "/host");
13+
assertEquals(bind.getVolume().getPath(), "/container");
14+
assertEquals(bind.isReadOnly(), false);
15+
}
16+
17+
@Test
18+
public void parseReadWrite() {
19+
Bind bind = Bind.parse("/host:/container:rw");
20+
assertEquals(bind.getPath(), "/host");
21+
assertEquals(bind.getVolume().getPath(), "/container");
22+
assertEquals(bind.isReadOnly(), false);
23+
}
24+
25+
@Test
26+
public void parseReadOnly() {
27+
Bind bind = Bind.parse("/host:/container:ro");
28+
assertEquals(bind.getPath(), "/host");
29+
assertEquals(bind.getVolume().getPath(), "/container");
30+
assertEquals(bind.isReadOnly(), true);
31+
}
32+
33+
@Test(expectedExceptions = IllegalArgumentException.class,
34+
expectedExceptionsMessageRegExp = "Error parsing Bind.*")
35+
public void parseInvalidAccessMode() {
36+
Bind.parse("/host:/container:xx");
37+
}
38+
39+
@Test(expectedExceptions = IllegalArgumentException.class,
40+
expectedExceptionsMessageRegExp = "Error parsing Bind 'nonsense'")
41+
public void parseInvalidInput() {
42+
Bind.parse("nonsense");
43+
}
44+
45+
@Test(expectedExceptions = IllegalArgumentException.class,
46+
expectedExceptionsMessageRegExp = "Error parsing Bind 'null'")
47+
public void parseNull() {
48+
Bind.parse(null);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)