X Tutup
Skip to content

Commit 05005aa

Browse files
committed
Merge pull request #73 from albers/link
Improve parsing and serialization of Link
2 parents ee6248d + 4039ddb commit 05005aa

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
1-
21
package com.github.dockerjava.api.model;
32

43
import org.apache.commons.lang.builder.EqualsBuilder;
54
import org.apache.commons.lang.builder.HashCodeBuilder;
65

6+
/**
7+
* Represents a network link between two Docker containers.
8+
* The container with the name {@link #getName()} is made available in the
9+
* target container with the aliased name {@link #getAlias()}.
10+
* This involves creating an entry in <code>/etc/hosts</code> and some environment
11+
* variables in the target container as well as creating a network bridge between
12+
* both containers.
13+
*/
714
public class Link
815
{
916

1017
private final String name;
1118

1219
private final String alias;
1320

21+
/**
22+
* Creates a {@link Link} for the container with the given name and an aliased
23+
* name for use in the target container.
24+
*
25+
* @param name the name of the container that you want to link into the target
26+
* container
27+
* @param alias the aliased name under which the linked container will be available
28+
* in the target container
29+
*/
1430
public Link(final String name, final String alias)
1531
{
1632
this.name = name;
1733
this.alias = alias;
1834
}
1935

36+
/**
37+
* @return the name of the container that is linked into the target container
38+
*/
2039
public String getName()
2140
{
2241
return name;
2342
}
2443

44+
/**
45+
* @return the aliased name under which the linked container will be available
46+
* in the target container
47+
*/
2548
public String getAlias()
2649
{
2750
return alias;
2851
}
2952

30-
public static Link parse(final String serialized)
53+
/**
54+
* Parses a textual link specification (as used by the Docker CLI) to a {@link Link}.
55+
*
56+
* @param serialized the specification, e.g. <code>name:alias</code>
57+
* @return a {@link Link} matching the specification
58+
* @throws IllegalArgumentException if the specification cannot be parsed
59+
*/
60+
public static Link parse(final String serialized) throws IllegalArgumentException
3161
{
3262
try {
3363
final String[] parts = serialized.split(":");
@@ -36,11 +66,11 @@ public static Link parse(final String serialized)
3666
return new Link(parts[0], parts[1]);
3767
}
3868
default: {
39-
throw new RuntimeException("Error parsing Link '" + serialized + "'");
69+
throw new IllegalArgumentException();
4070
}
4171
}
4272
} catch (final Exception e) {
43-
throw new RuntimeException("Error parsing Link '" + serialized + "'");
73+
throw new IllegalArgumentException("Error parsing Link '" + serialized + "'");
4474
}
4575
}
4676

@@ -60,4 +90,16 @@ public int hashCode()
6090
return new HashCodeBuilder().append(name).append(alias).toHashCode();
6191
}
6292

93+
/**
94+
* Returns a string representation of this {@link Link} suitable
95+
* for inclusion in a JSON message.
96+
* The format is <code>name:alias</code>, like the argument in {@link #parse(String)}.
97+
*
98+
* @return a string representation of this {@link Link}
99+
*/
100+
@Override
101+
public String toString() {
102+
return name + ":" + alias;
103+
}
104+
63105
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public void serialize(final Links links, final JsonGenerator jsonGen, final Seri
4545
{
4646
jsonGen.writeStartArray();
4747
for (final Link link : links.getLinks()) {
48-
final String s = link.getName() + ":" + link.getAlias();
49-
jsonGen.writeString(s);
48+
jsonGen.writeString(link.toString());
5049
}
5150
jsonGen.writeEndArray();
5251
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 LinkTest {
8+
9+
@Test
10+
public void parse() {
11+
Link link = Link.parse("name:alias");
12+
assertEquals(link.getName(), "name");
13+
assertEquals(link.getAlias(), "alias");
14+
}
15+
16+
@Test(expectedExceptions = IllegalArgumentException.class,
17+
expectedExceptionsMessageRegExp = "Error parsing Link 'nonsense'")
18+
public void parseInvalidInput() {
19+
Link.parse("nonsense");
20+
}
21+
22+
@Test(expectedExceptions = IllegalArgumentException.class,
23+
expectedExceptionsMessageRegExp = "Error parsing Link 'null'")
24+
public void parseNull() {
25+
Link.parse(null);
26+
}
27+
28+
@Test
29+
public void stringify() {
30+
assertEquals(Link.parse("name:alias").toString(), "name:alias");
31+
}
32+
33+
}

0 commit comments

Comments
 (0)
X Tutup