diff --git a/src/main/java/com/github/dockerjava/api/model/Bind.java b/src/main/java/com/github/dockerjava/api/model/Bind.java index 6779e2580..0c4dd3150 100644 --- a/src/main/java/com/github/dockerjava/api/model/Bind.java +++ b/src/main/java/com/github/dockerjava/api/model/Bind.java @@ -33,6 +33,13 @@ public boolean isReadOnly() { return readOnly; } + /** + * Parses a bind mount specification to a {@link Bind}. + * + * @param serialized the specification, e.g. /host:/container:ro + * @return a {@link Bind} matching the specification + * @throws IllegalArgumentException if the specification cannot be parsed + */ public static Bind parse(String serialized) { try { String[] parts = serialized.split(":"); @@ -41,19 +48,19 @@ public static Bind parse(String serialized) { return new Bind(parts[0], Volume.parse(parts[1])); } case 3: { - if ("rw".equals(parts[3].toLowerCase())) + 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 RuntimeException("Error parsing Bind '" - + serialized + "'"); + throw new IllegalArgumentException(); } default: { - throw new RuntimeException("Error parsing Bind '" + serialized - + "'"); + throw new IllegalArgumentException(); } } } catch (Exception e) { - throw new RuntimeException("Error parsing Bind '" + serialized + throw new IllegalArgumentException("Error parsing Bind '" + serialized + "'"); } } diff --git a/src/test/java/com/github/dockerjava/api/model/BindTest.java b/src/test/java/com/github/dockerjava/api/model/BindTest.java new file mode 100644 index 000000000..412c537fa --- /dev/null +++ b/src/test/java/com/github/dockerjava/api/model/BindTest.java @@ -0,0 +1,51 @@ +package com.github.dockerjava.api.model; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class BindTest { + + @Test + public void parseUsingDefaultAccessMode() { + Bind bind = Bind.parse("/host:/container"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), false); + } + + @Test + public void parseReadWrite() { + Bind bind = Bind.parse("/host:/container:rw"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), false); + } + + @Test + public void parseReadOnly() { + Bind bind = Bind.parse("/host:/container:ro"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), true); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind.*") + public void parseInvalidAccessMode() { + Bind.parse("/host:/container:xx"); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind 'nonsense'") + public void parseInvalidInput() { + Bind.parse("nonsense"); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind 'null'") + public void parseNull() { + Bind.parse(null); + } + +}