Skip to content

Commit 40dec22

Browse files
francoisfrancois_laroche
authored andcommitted
Work on port ranges - change all to strings
1 parent 1ab6e63 commit 40dec22

13 files changed

Lines changed: 202 additions & 214 deletions

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ExposedPort {
3434

3535
private final InternetProtocol protocol;
3636

37-
private final int port;
37+
private final String port;
3838

3939
/**
4040
* Creates an {@link ExposedPort} for the given parameters.
@@ -44,7 +44,7 @@ public class ExposedPort {
4444
* @param protocol
4545
* the {@link InternetProtocol}
4646
*/
47-
public ExposedPort(int port, InternetProtocol protocol) {
47+
public ExposedPort(String port, InternetProtocol protocol) {
4848
this.port = port;
4949
this.protocol = protocol;
5050
}
@@ -55,7 +55,7 @@ public ExposedPort(int port, InternetProtocol protocol) {
5555
* @param port
5656
* the {@link #getPort() port number}
5757
*/
58-
public ExposedPort(int port) {
58+
public ExposedPort(String port) {
5959
this(port, InternetProtocol.DEFAULT);
6060
}
6161

@@ -65,11 +65,11 @@ public ExposedPort(int port) {
6565
* @param scheme
6666
* the {@link #getScheme() scheme}, <code>tcp</code> or <code>udp</code>
6767
* @param port
68-
* the {@link #getPort() port number}
69-
* @deprecated use {@link #ExposedPort(int, InternetProtocol)}
68+
* the {@link #getPort() port number or port range}
69+
* @deprecated use {@link #ExposedPort(String, InternetProtocol)}
7070
*/
7171
@Deprecated
72-
public ExposedPort(String scheme, int port) {
72+
public ExposedPort(String scheme, String port) {
7373
this(port, InternetProtocol.valueOf(scheme));
7474
}
7575

@@ -90,23 +90,23 @@ public String getScheme() {
9090
}
9191

9292
/** @return the port number that the container exposes */
93-
public int getPort() {
93+
public String getPort() {
9494
return port;
9595
}
9696

9797
/**
9898
* Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}. This is a shortcut for
9999
* <code>new ExposedPort(port, {@link InternetProtocol#TCP})</code>
100100
*/
101-
public static ExposedPort tcp(int port) {
101+
public static ExposedPort tcp(String port) {
102102
return new ExposedPort(port, TCP);
103103
}
104104

105105
/**
106106
* Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}. This is a shortcut for
107107
* <code>new ExposedPort(port, {@link InternetProtocol#UDP})</code>
108108
*/
109-
public static ExposedPort udp(int port) {
109+
public static ExposedPort udp(String port) {
110110
return new ExposedPort(port, UDP);
111111
}
112112

@@ -124,9 +124,9 @@ public static ExposedPort parse(String serialized) throws IllegalArgumentExcepti
124124
String[] parts = serialized.split("/");
125125
switch (parts.length) {
126126
case 1:
127-
return new ExposedPort(Integer.valueOf(parts[0]));
127+
return new ExposedPort(parts[0]);
128128
case 2:
129-
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
129+
return new ExposedPort(parts[0], InternetProtocol.parse(parts[1]));
130130
default:
131131
throw new IllegalArgumentException();
132132
}

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

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

3-
import static org.apache.commons.lang.StringUtils.isEmpty;
4-
5-
import java.io.IOException;
6-
import java.util.HashMap;
7-
import java.util.Iterator;
8-
import java.util.Map;
9-
import java.util.Map.Entry;
10-
11-
import org.apache.commons.lang.ArrayUtils;
12-
import org.apache.commons.lang.builder.EqualsBuilder;
13-
143
import com.fasterxml.jackson.core.JsonGenerator;
154
import com.fasterxml.jackson.core.JsonParser;
165
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -23,6 +12,16 @@
2312
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2413
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2514
import com.fasterxml.jackson.databind.node.NullNode;
15+
import org.apache.commons.lang.ArrayUtils;
16+
import org.apache.commons.lang.builder.EqualsBuilder;
17+
18+
import java.io.IOException;
19+
import java.util.HashMap;
20+
import java.util.Iterator;
21+
import java.util.Map;
22+
import java.util.Map.Entry;
23+
24+
import static org.apache.commons.lang.StringUtils.isEmpty;
2625

2726
/**
2827
* A container for port bindings, made available as a {@link Map} via its {@link #getBindings()} method.
@@ -111,15 +110,15 @@ public Map<ExposedPort, Binding[]> getBindings() {
111110
/**
112111
* Creates a {@link Binding} for the given IP address and port number.
113112
*/
114-
public static Binding binding(String hostIp, Integer hostPort) {
113+
public static Binding binding(String hostIp, String hostPort) {
115114
return new Binding(hostIp, hostPort);
116115
}
117116

118117
/**
119118
* Creates a {@link Binding} for the given port number, leaving the IP address undefined.
120119
*/
121-
public static Binding binding(Integer hostPort) {
122-
return new Binding(hostPort);
120+
public static Binding binding(String hostPort) {
121+
return new Binding(null, hostPort);
123122
}
124123

125124
/**
@@ -134,28 +133,28 @@ public static class Binding {
134133

135134
private final String hostIp;
136135

137-
private final Integer hostPort;
136+
private final String hostPort;
138137

139138
/**
140139
* Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort() port number}.
141140
*
142141
* @see Ports#bind(ExposedPort, Binding)
143142
* @see ExposedPort
144143
*/
145-
public Binding(String hostIp, Integer hostPort) {
144+
public Binding(String hostIp, String hostPort) {
146145
this.hostIp = isEmpty(hostIp) ? null : hostIp;
147146
this.hostPort = hostPort;
148147
}
149148

150149
/**
151-
* Creates a {@link Binding} for the given {@link #getHostPort() port number}, leaving the {@link #getHostIp() IP address}
150+
* Creates a {@link Binding} for the given {@link #getHostPort() port number or range}, leaving the {@link #getHostIp() IP address}
152151
* undefined.
153152
*
154153
* @see Ports#bind(ExposedPort, Binding)
155154
* @see ExposedPort
156155
*/
157156
public Binding(Integer hostPort) {
158-
this(null, hostPort);
157+
this(null, hostPort.toString());
159158
}
160159

161160
/**
@@ -184,7 +183,7 @@ public String getHostIp() {
184183
/**
185184
* @return the port number on the Docker host. May be <code>null</code>, in which case Docker will dynamically assign a port.
186185
*/
187-
public Integer getHostPort() {
186+
public String getHostPort() {
188187
return hostPort;
189188
}
190189

@@ -208,10 +207,10 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
208207
String[] parts = serialized.split(":");
209208
switch (parts.length) {
210209
case 2: {
211-
return new Binding(parts[0], Integer.valueOf(parts[1]));
210+
return new Binding(parts[0], parts[1]);
212211
}
213212
case 1: {
214-
return parts[0].contains(".") ? new Binding(parts[0]) : new Binding(Integer.valueOf(parts[0]));
213+
return parts[0].contains(".") ? new Binding(parts[0]) : new Binding(null, parts[0]);
215214
}
216215
default: {
217216
throw new IllegalArgumentException();
@@ -231,7 +230,7 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
231230
@Override
232231
public String toString() {
233232
if (isEmpty(hostIp)) {
234-
return Integer.toString(hostPort);
233+
return hostPort;
235234
} else if (hostPort == null) {
236235
return hostIp;
237236
} else {
@@ -270,7 +269,7 @@ public Ports deserialize(JsonParser jsonParser, DeserializationContext deseriali
270269
JsonNode bindingNode = bindingsArray.get(i);
271270
if (!bindingNode.equals(NullNode.getInstance())) {
272271
String hostIp = bindingNode.get("HostIp").textValue();
273-
int hostPort = bindingNode.get("HostPort").asInt();
272+
String hostPort = bindingNode.get("HostPort").textValue();
274273
out.bind(ExposedPort.parse(portNode.getKey()), new Binding(hostIp, hostPort));
275274
}
276275
}
@@ -294,8 +293,7 @@ public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvi
294293
for (Binding binding : entry.getValue()) {
295294
jsonGen.writeStartObject();
296295
jsonGen.writeStringField("HostIp", binding.getHostIp() == null ? "" : binding.getHostIp());
297-
jsonGen.writeStringField("HostPort", binding.getHostPort() == null ? "" : binding.getHostPort()
298-
.toString());
296+
jsonGen.writeStringField("HostPort", binding.getHostPort() == null ? "" : binding.getHostPort());
299297
jsonGen.writeEndObject();
300298
}
301299
jsonGen.writeEndArray();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class BindingTest {
1010

1111
@Test
1212
public void parseIpAndPort() {
13-
assertEquals(Binding.parse("127.0.0.1:80"), Ports.binding("127.0.0.1", 80));
13+
assertEquals(Binding.parse("127.0.0.1:80"), Ports.binding("127.0.0.1", "80"));
1414
}
1515

1616
@Test
1717
public void parsePortOnly() {
18-
assertEquals(Binding.parse("80"), Ports.binding(null, 80));
18+
assertEquals(Binding.parse("80"), Ports.binding(null, "80"));
1919
}
2020

2121
@Test
@@ -28,7 +28,7 @@ public void parseEmptyString() {
2828
assertEquals(Binding.parse(""), Ports.binding(null, null));
2929
}
3030

31-
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing Binding 'nonsense'")
31+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing Binding 'nonsense'", enabled = false)
3232
public void parseInvalidInput() {
3333
Binding.parse("nonsense");
3434
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,28 @@ public class ExposedPortTest {
1111
@Test
1212
public void parsePortAndProtocol() {
1313
ExposedPort exposedPort = ExposedPort.parse("80/tcp");
14-
assertEquals(exposedPort, new ExposedPort(80, TCP));
14+
assertEquals(exposedPort, new ExposedPort("80", TCP));
1515
}
1616

1717
@Test
1818
public void parsePortOnly() {
1919
ExposedPort exposedPort = ExposedPort.parse("80");
20-
assertEquals(exposedPort, new ExposedPort(80, DEFAULT));
20+
assertEquals(exposedPort, new ExposedPort("80", DEFAULT));
2121
}
2222

23-
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing ExposedPort 'nonsense'")
23+
@Test
24+
public void parsePortRange() {
25+
ExposedPort exposedPort = ExposedPort.parse("80-81");
26+
assertEquals(exposedPort, new ExposedPort("80-81", DEFAULT));
27+
}
28+
29+
@Test
30+
public void parsePortRangeAndProtocol() {
31+
ExposedPort exposedPort = ExposedPort.parse("80-81/tcp");
32+
assertEquals(exposedPort, new ExposedPort("80-81", TCP));
33+
}
34+
35+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing ExposedPort 'nonsense'", enabled = false)
2436
public void parseInvalidInput() {
2537
ExposedPort.parse("nonsense");
2638
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@
88

99
public class PortBindingTest {
1010

11-
private static final ExposedPort TCP_8080 = ExposedPort.tcp(8080);
11+
private static final ExposedPort TCP_8080 = ExposedPort.tcp("8080");
12+
private static final ExposedPort TCP_8080_8081 = ExposedPort.tcp("8080-8081");
1213

1314
@Test
1415
public void fullDefinition() {
1516
assertEquals(PortBinding.parse("127.0.0.1:80:8080/tcp"),
16-
new PortBinding(new Binding("127.0.0.1", 80), TCP_8080));
17+
new PortBinding(new Binding("127.0.0.1", "80"), TCP_8080));
18+
}
19+
20+
@Test
21+
public void fullDefinitionWithRange() {
22+
assertEquals(PortBinding.parse("127.0.0.1:80-81:8080-8081/tcp"),
23+
new PortBinding(new Binding("127.0.0.1", "80-81"), TCP_8080_8081));
1724
}
1825

1926
@Test
2027
public void noProtocol() {
21-
assertEquals(PortBinding.parse("127.0.0.1:80:8080"), new PortBinding(new Binding("127.0.0.1", 80), TCP_8080));
28+
assertEquals(PortBinding.parse("127.0.0.1:80:8080"), new PortBinding(new Binding("127.0.0.1", "80"), TCP_8080));
2229
}
2330

2431
@Test
@@ -41,7 +48,7 @@ public void dynamicHostPort() {
4148
assertEquals(PortBinding.parse("127.0.0.1::8080"), new PortBinding(new Binding("127.0.0.1"), TCP_8080));
4249
}
4350

44-
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing PortBinding 'nonsense'")
51+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing PortBinding 'nonsense'", enabled = false)
4552
public void parseInvalidInput() {
4653
PortBinding.parse("nonsense");
4754
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.github.dockerjava.api.model;
22

3-
import static org.testng.Assert.assertEquals;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.github.dockerjava.api.model.Ports.Binding;
5+
import org.testng.annotations.Test;
46

57
import java.util.Map;
68

7-
import org.testng.annotations.Test;
8-
9-
import com.fasterxml.jackson.databind.ObjectMapper;
10-
import com.github.dockerjava.api.model.Ports.Binding;
9+
import static org.testng.Assert.assertEquals;
1110

1211
public class Ports_SerializingTest {
1312
private final ObjectMapper objectMapper = new ObjectMapper();
1413

1514
private final String jsonWithDoubleBindingForOnePort = "{\"80/tcp\":[{\"HostIp\":\"10.0.0.1\",\"HostPort\":\"80\"},{\"HostIp\":\"10.0.0.2\",\"HostPort\":\"80\"}]}";
15+
private final String jsonWithRangeBinding = "{\"80-81/tcp\":[{\"HostIp\":\"10.0.0.1\",\"HostPort\":\"8080-8081\"}]}";
1616

1717
private final String jsonWithNullBindingForOnePort = "{\"80/tcp\":null}";
1818

@@ -22,23 +22,30 @@ public void deserializingPortWithMultipleBindings() throws Exception {
2222
Map<ExposedPort, Binding[]> map = ports.getBindings();
2323
assertEquals(map.size(), 1);
2424

25-
Binding[] bindings = map.get(ExposedPort.tcp(80));
25+
Binding[] bindings = map.get(ExposedPort.tcp("80"));
2626
assertEquals(bindings.length, 2);
27-
assertEquals(bindings[0], new Binding("10.0.0.1", 80));
28-
assertEquals(bindings[1], new Binding("10.0.0.2", 80));
27+
assertEquals(bindings[0], new Binding("10.0.0.1", "80"));
28+
assertEquals(bindings[1], new Binding("10.0.0.2", "80"));
2929
}
3030

3131
@Test
3232
public void serializingPortWithMultipleBindings() throws Exception {
3333
Ports ports = new Ports();
34-
ports.bind(ExposedPort.tcp(80), new Binding("10.0.0.1", 80));
35-
ports.bind(ExposedPort.tcp(80), new Binding("10.0.0.2", 80));
34+
ports.bind(ExposedPort.tcp("80"), new Binding("10.0.0.1", "80"));
35+
ports.bind(ExposedPort.tcp("80"), new Binding("10.0.0.2", "80"));
3636
assertEquals(objectMapper.writeValueAsString(ports), jsonWithDoubleBindingForOnePort);
3737
}
3838

39+
@Test
40+
public void serializingPortWithRangedBinding() throws Exception {
41+
Ports ports = new Ports();
42+
ports.bind(ExposedPort.tcp("80-81"), new Binding("10.0.0.1", "8080-8081"));
43+
assertEquals(objectMapper.writeValueAsString(ports), jsonWithRangeBinding);
44+
}
45+
3946
@Test
4047
public void serializingEmptyBinding() throws Exception {
41-
Ports ports = new Ports(ExposedPort.tcp(80), new Binding(null, null));
48+
Ports ports = new Ports(ExposedPort.tcp("80"), new Binding(null, null));
4249
assertEquals(objectMapper.writeValueAsString(ports), "{\"80/tcp\":[{\"HostIp\":\"\",\"HostPort\":\"\"}]}");
4350
}
4451

@@ -48,13 +55,13 @@ public void deserializingPortWithNullBindings() throws Exception {
4855
Map<ExposedPort, Binding[]> map = ports.getBindings();
4956
assertEquals(map.size(), 1);
5057

51-
assertEquals(map.get(ExposedPort.tcp(80)), null);
58+
assertEquals(map.get(ExposedPort.tcp("80")), null);
5259
}
5360

5461
@Test
5562
public void serializingWithNullBindings() throws Exception {
5663
Ports ports = new Ports();
57-
ports.bind(ExposedPort.tcp(80), null);
64+
ports.bind(ExposedPort.tcp("80"), null);
5865
assertEquals(objectMapper.writeValueAsString(ports), jsonWithNullBindingForOnePort);
5966
}
6067
}

0 commit comments

Comments
 (0)