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
Next Next commit
Use Bind.toString() in serialization
This also changes the serialization in Binds.Serializer to always
include the access mode. This is no problem in Docker API.
  • Loading branch information
albers committed Oct 7, 2014
commit 1c91db0dd95a68c861edd6d1abf53030095ee18e
19 changes: 19 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/Bind.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
* Represents a host path being bind mounted as a {@link Volume}
* in a Docker container.
* The Bind can be in read only or read write access mode.
*/
public class Bind {

private String path;
Expand Down Expand Up @@ -81,4 +86,18 @@ public int hashCode() {
return new HashCodeBuilder().append(path).append(volume)
.append(readOnly).toHashCode();
}

/**
* Returns a string representation of this {@link Bind} suitable
* for inclusion in a JSON message.
* The format is <code>&lt;host path&gt;:&lt;container path&gt;:&lt;access mode&gt;</code>,
* like the argument in {@link #parse(String)}.
*
* @return a string representation of this {@link Bind}
*/
@Override
public String toString() {
return path + ":" + volume.toString() + (readOnly ? ":ro" : ":rw");
}

}
5 changes: 1 addition & 4 deletions src/main/java/com/github/dockerjava/api/model/Binds.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public void serialize(Binds binds, JsonGenerator jsonGen,
//
jsonGen.writeStartArray();
for (Bind bind : binds.getBinds()) {
String s = bind.getPath() + ":" + bind.getVolume().toString();
if(bind.isReadOnly()) s += ":ro";
jsonGen.writeString(s);

jsonGen.writeString(bind.toString());
}
jsonGen.writeEndArray();
//
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/Volume.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.NullNode;

/**
* Represents a bind mounted volume in a Docker container.
*
* @see Bind
*/
@JsonDeserialize(using = Volume.Deserializer.class)
@JsonSerialize(using = Volume.Serializer.class)
public class Volume {
Expand All @@ -43,6 +48,13 @@ public static Volume parse(String serialized) {
return new Volume(serialized);
}

/**
* Returns a string representation of this {@link Volume} suitable
* for inclusion in a JSON message.
* The returned String is simply the container path, {@link #getPath()}.
*
* @return a string representation of this {@link Volume}
*/
@Override
public String toString() {
return getPath();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/github/dockerjava/api/model/BindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public void parseNull() {
Bind.parse(null);
}

@Test
public void toStringReadOnly() {
assertEquals(Bind.parse("/host:/container:ro").toString(), "/host:/container:ro");
}

@Test
public void toStringReadWrite() {
assertEquals(Bind.parse("/host:/container:rw").toString(), "/host:/container:rw");
}

@Test
public void toStringDefaultAccessMode() {
assertEquals(Bind.parse("/host:/container").toString(), "/host:/container:rw");
}

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

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class VolumeTest {
@Test
public void stringify() {
assertEquals(Volume.parse("/path").toString(), "/path");
}
}