X Tutup
Skip to content

Commit 4bf2648

Browse files
author
francois
committed
Work on port ranges - change all to strings
1 parent 1ab6e63 commit 4bf2648

17 files changed

+253
-196
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ 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}
68+
* the {@link #getPort() port number or port range}
6969
* @deprecated use {@link #ExposedPort(int, InternetProtocol)}
7070
*/
7171
@Deprecated
@@ -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(Integer.parseInt(parts[0]));
128128
case 2:
129-
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
129+
return new ExposedPort(Integer.parseInt(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: 37 additions & 39 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
/**
@@ -132,38 +131,38 @@ public static Binding binding(Integer hostPort) {
132131
*/
133132
public static class Binding {
134133

135-
private final String hostIp;
136-
137-
private final Integer hostPort;
138-
139134
/**
140-
* Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort() port number}.
135+
* Creates a {@link Binding} for the given {@link #getHostPort() port number or range}, leaving the {@link #getHostIp() IP address}
136+
* undefined.
141137
*
142138
* @see Ports#bind(ExposedPort, Binding)
143139
* @see ExposedPort
144140
*/
145-
public Binding(String hostIp, Integer hostPort) {
146-
this.hostIp = isEmpty(hostIp) ? null : hostIp;
147-
this.hostPort = hostPort;
141+
public static Binding forPort(String port) {
142+
return new Binding(null, port);
148143
}
149144

150145
/**
151-
* Creates a {@link Binding} for the given {@link #getHostPort() port number}, leaving the {@link #getHostIp() IP address}
146+
* Creates a {@link Binding} for the given {@link #getHostIp() IP address}, leaving the {@link #getHostPort() port number}
152147
* undefined.
153-
*
154-
* @see Ports#bind(ExposedPort, Binding)
155-
* @see ExposedPort
156148
*/
157-
public Binding(Integer hostPort) {
158-
this(null, hostPort);
149+
public static Binding forIp(String hostIp) {
150+
return new Binding(hostIp, null);
159151
}
160152

153+
private final String hostIp;
154+
155+
private final String hostPort;
156+
161157
/**
162-
* Creates a {@link Binding} for the given {@link #getHostIp() IP address}, leaving the {@link #getHostPort() port number}
163-
* undefined.
158+
* Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort() port number}.
159+
*
160+
* @see Ports#bind(ExposedPort, Binding)
161+
* @see ExposedPort
164162
*/
165-
public Binding(String hostIp) {
166-
this(hostIp, null);
163+
public Binding(String hostIp, String hostPort) {
164+
this.hostIp = isEmpty(hostIp) ? null : hostIp;
165+
this.hostPort = hostPort;
167166
}
168167

169168
/**
@@ -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(".") ? Binding.forIp(parts[0]) : Binding.forPort(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/command/InspectContainerResponseTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import static org.hamcrest.Matchers.isEmptyString;
2626
import static org.hamcrest.Matchers.nullValue;
2727
import static org.hamcrest.core.IsNot.not;
28-
import static org.testng.Assert.*;
28+
import static org.testng.Assert.assertEquals;
29+
import static org.testng.Assert.assertFalse;
30+
import static org.testng.Assert.assertTrue;
2931

3032
/**
3133
* Tests for {@link InspectContainerResponse}.

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

Lines changed: 4 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,8 @@ public void parseEmptyString() {
2828
assertEquals(Binding.parse(""), Ports.binding(null, null));
2929
}
3030

31-
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing Binding 'nonsense'")
31+
// Strings can be used since it can be a range. Let the docker daemon do the validation.
32+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing Binding 'nonsense'", enabled = false)
3233
public void parseInvalidInput() {
3334
Binding.parse("nonsense");
3435
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ public class PortBindingTest {
1313
@Test
1414
public void fullDefinition() {
1515
assertEquals(PortBinding.parse("127.0.0.1:80:8080/tcp"),
16-
new PortBinding(new Binding("127.0.0.1", 80), TCP_8080));
16+
new PortBinding(new Binding("127.0.0.1", "80"), TCP_8080));
1717
}
1818

1919
@Test
2020
public void noProtocol() {
21-
assertEquals(PortBinding.parse("127.0.0.1:80:8080"), new PortBinding(new Binding("127.0.0.1", 80), TCP_8080));
21+
assertEquals(PortBinding.parse("127.0.0.1:80:8080"), new PortBinding(new Binding("127.0.0.1", "80"), TCP_8080));
2222
}
2323

2424
@Test
2525
public void noHostIp() {
26-
assertEquals(PortBinding.parse("80:8080/tcp"), new PortBinding(new Binding(80), TCP_8080));
26+
assertEquals(PortBinding.parse("80:8080/tcp"), new PortBinding(Binding.forPort("80"), TCP_8080));
2727
}
2828

2929
@Test
3030
public void portsOnly() {
31-
assertEquals(PortBinding.parse("80:8080"), new PortBinding(new Binding(80), TCP_8080));
31+
assertEquals(PortBinding.parse("80:8080"), new PortBinding(Binding.forPort("80"), TCP_8080));
3232
}
3333

3434
@Test
@@ -38,10 +38,10 @@ public void exposedPortOnly() {
3838

3939
@Test
4040
public void dynamicHostPort() {
41-
assertEquals(PortBinding.parse("127.0.0.1::8080"), new PortBinding(new Binding("127.0.0.1"), TCP_8080));
41+
assertEquals(PortBinding.parse("127.0.0.1::8080"), new PortBinding(Binding.forIp("127.0.0.1"), TCP_8080));
4242
}
4343

44-
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing PortBinding 'nonsense'")
44+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Error parsing PortBinding 'nonsense'", enabled = false)
4545
public void parseInvalidInput() {
4646
PortBinding.parse("nonsense");
4747
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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();
@@ -24,15 +23,15 @@ public void deserializingPortWithMultipleBindings() throws Exception {
2423

2524
Binding[] bindings = map.get(ExposedPort.tcp(80));
2625
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));
26+
assertEquals(bindings[0], new Binding("10.0.0.1", "80"));
27+
assertEquals(bindings[1], new Binding("10.0.0.2", "80"));
2928
}
3029

3130
@Test
3231
public void serializingPortWithMultipleBindings() throws Exception {
3332
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));
33+
ports.bind(ExposedPort.tcp(80), new Binding("10.0.0.1", "80"));
34+
ports.bind(ExposedPort.tcp(80), new Binding("10.0.0.2", "80"));
3635
assertEquals(objectMapper.writeValueAsString(ports), jsonWithDoubleBindingForOnePort);
3736
}
3837

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.github.dockerjava.api.model;
22

3-
import static org.testng.Assert.assertEquals;
4-
5-
import java.util.Map;
6-
3+
import com.github.dockerjava.api.model.Ports.Binding;
74
import org.testng.annotations.BeforeMethod;
85
import org.testng.annotations.Test;
96

10-
import com.github.dockerjava.api.model.Ports.Binding;
7+
import java.util.Map;
8+
9+
import static org.testng.Assert.assertEquals;
1110

1211
/**
1312
* As there may be several {@link Binding}s per {@link ExposedPort}, it makes a difference if you add {@link PortBinding}s for the same or
@@ -18,9 +17,9 @@ public class Ports_addBindingsTest {
1817

1918
private static final ExposedPort TCP_90 = ExposedPort.tcp(90);
2019

21-
private static final Binding BINDING_8080 = Ports.binding(8080);
20+
private static final Binding BINDING_8080 = Ports.binding("8080");
2221

23-
private static final Binding BINDING_9090 = Ports.binding(9090);
22+
private static final Binding BINDING_9090 = Ports.binding("9090");
2423

2524
private Ports ports;
2625

0 commit comments

Comments
 (0)
X Tutup