Skip to content

Commit 3b14b1f

Browse files
committed
Allow port-only as a legal syntax for ExposedPort.parse(String)
1 parent 97bf5ef commit 3b14b1f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public ExposedPort(int port, InternetProtocol protocol) {
4848
this.protocol = protocol;
4949
}
5050

51+
/**
52+
* Creates an {@link ExposedPort} for the given
53+
* {@link #getPort() port number} and {@link InternetProtocol#DEFAULT}.
54+
*
55+
* @param port the {@link #getPort() port number}
56+
*/
57+
public ExposedPort(int port) {
58+
this(port, InternetProtocol.DEFAULT);
59+
}
60+
5161
/**
5262
* Creates an {@link ExposedPort} for the given parameters.
5363
*
@@ -61,7 +71,8 @@ public ExposedPort(String scheme, int port) {
6171
this(port, InternetProtocol.valueOf(scheme));
6272
}
6373

64-
/** @return the {@link InternetProtocol} */
74+
/** @return the {@link InternetProtocol} of the {@link #getPort() port}
75+
* that the container exposes */
6576
public InternetProtocol getProtocol() {
6677
return protocol;
6778
}
@@ -75,7 +86,7 @@ public String getScheme() {
7586
return protocol.toString();
7687
}
7788

78-
/** @return the port number */
89+
/** @return the port number that the container exposes */
7990
public int getPort() {
8091
return port;
8192
}
@@ -107,7 +118,14 @@ public static ExposedPort udp(int port) {
107118
public static ExposedPort parse(String serialized) throws IllegalArgumentException {
108119
try {
109120
String[] parts = serialized.split("/");
110-
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
121+
switch (parts.length) {
122+
case 1:
123+
return new ExposedPort(Integer.valueOf(parts[0]));
124+
case 2:
125+
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
126+
default:
127+
throw new IllegalArgumentException();
128+
}
111129
} catch (Exception e) {
112130
throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'");
113131
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.github.dockerjava.api.model;
22

3+
import static com.github.dockerjava.api.model.InternetProtocol.DEFAULT;
4+
import static com.github.dockerjava.api.model.InternetProtocol.TCP;
35
import static org.testng.Assert.assertEquals;
46

57
import org.testng.annotations.Test;
68

79
public class ExposedPortTest {
810

911
@Test
10-
public void parse() {
12+
public void parsePortAndProtocol() {
1113
ExposedPort exposedPort = ExposedPort.parse("80/tcp");
12-
assertEquals(exposedPort.getPort(), 80);
14+
assertEquals(exposedPort, new ExposedPort(80, TCP));
15+
}
16+
17+
@Test
18+
public void parsePortOnly() {
19+
ExposedPort exposedPort = ExposedPort.parse("80");
20+
assertEquals(exposedPort, new ExposedPort(80, DEFAULT));
1321
}
1422

1523
@Test(expectedExceptions = IllegalArgumentException.class,

0 commit comments

Comments
 (0)