Skip to content

Commit 977b35e

Browse files
committed
Support equals() for IntOrString
1 parent 73c866c commit 977b35e

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

kubernetes/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@
158158
<artifactId>commons-lang3</artifactId>
159159
</dependency>
160160
<!-- test dependencies -->
161+
<dependency>
162+
<groupId>org.hamcrest</groupId>
163+
<artifactId>hamcrest-junit</artifactId>
164+
<version>2.0.0.0</version>
165+
<scope>test</scope>
166+
</dependency>
161167
<dependency>
162168
<groupId>junit</groupId>
163169
<artifactId>junit</artifactId>

kubernetes/src/main/java/io/kubernetes/client/custom/IntOrString.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.kubernetes.client.custom;
22

3+
import java.io.IOException;
4+
import java.util.Objects;
5+
36
import com.google.gson.TypeAdapter;
47
import com.google.gson.annotations.JsonAdapter;
58
import com.google.gson.stream.JsonReader;
69
import com.google.gson.stream.JsonToken;
710
import com.google.gson.stream.JsonWriter;
811

9-
import java.io.IOException;
10-
1112
@JsonAdapter(IntOrString.IntOrStringAdapter.class)
1213
public class IntOrString {
1314
private final boolean isInt;
@@ -47,7 +48,22 @@ public Integer getIntValue() {
4748
@Override
4849
public String toString() {
4950
return (isInt ? String.valueOf(intValue) : strValue);
50-
}
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
return this == o || (o instanceof IntOrString && equals((IntOrString) o));
56+
}
57+
58+
private boolean equals(IntOrString o) {
59+
if (isInt != o.isInt) return false;
60+
return isInt ? Objects.equals(intValue, o.intValue) : Objects.equals(strValue, o.strValue);
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(isInt, strValue, intValue);
66+
}
5167

5268
public static class IntOrStringAdapter extends TypeAdapter<IntOrString> {
5369
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package io.kubernetes.client.custom;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.Matchers.equalTo;
6+
import static org.hamcrest.Matchers.is;
7+
import static org.hamcrest.Matchers.not;
8+
import static org.junit.Assert.*;
9+
10+
public class IntOrStringTest {
11+
@Test
12+
public void whenCreatedWithInt_isInteger() {
13+
IntOrString intOrString = new IntOrString(17);
14+
15+
assertThat(intOrString.isInteger(), is(true));
16+
}
17+
18+
@Test
19+
public void whenCreatedWithInt_canRetrieveIntValue() {
20+
IntOrString intOrString = new IntOrString(17);
21+
22+
assertThat(intOrString.getIntValue(), equalTo(17));
23+
}
24+
25+
@Test(expected = IllegalStateException.class)
26+
public void whenCreatedWithInt_cannotRetrieveStringValue() {
27+
IntOrString intOrString = new IntOrString(17);
28+
29+
intOrString.getStrValue();
30+
}
31+
32+
@Test
33+
public void whenCreatedWithInt_equalsItself() {
34+
IntOrString intOrString = new IntOrString(17);
35+
36+
assertThat(intOrString, equalTo(intOrString));
37+
}
38+
39+
@Test
40+
public void whenCreatedWithInt_equalsAnotherWithSameValue() {
41+
IntOrString intOrString1 = new IntOrString(17);
42+
IntOrString intOrString2 = new IntOrString(17);
43+
44+
assertThat(intOrString1, equalTo(intOrString2));
45+
}
46+
47+
@Test
48+
public void whenCreatedWithInt_notEqualAnotherWithDifferentValue() {
49+
IntOrString intOrString1 = new IntOrString(17);
50+
IntOrString intOrString2 = new IntOrString(13);
51+
52+
assertThat(intOrString1, not(equalTo(intOrString2)));
53+
}
54+
55+
@Test
56+
public void whenCreatedWithInt_notEqualOneCreatedFromString() {
57+
IntOrString intOrString1 = new IntOrString(17);
58+
IntOrString intOrString2 = new IntOrString("17");
59+
60+
assertThat(intOrString1, not(equalTo(intOrString2)));
61+
}
62+
63+
@Test
64+
public void whenCreatedWithString_isNotInteger() {
65+
IntOrString intOrString = new IntOrString("17");
66+
67+
assertThat(intOrString.isInteger(), is(false));
68+
}
69+
70+
@Test(expected = IllegalStateException.class)
71+
public void whenCreatedWithInt_cannotRetrieveIntValue() {
72+
IntOrString intOrString = new IntOrString("17");
73+
74+
intOrString.getIntValue();
75+
}
76+
77+
@Test
78+
public void whenCreatedWithString_canRetrieveStringValue() {
79+
IntOrString intOrString = new IntOrString("17");
80+
81+
assertThat(intOrString.getStrValue(), equalTo("17"));
82+
}
83+
84+
@Test
85+
public void whenCreatedWithString_equalsItself() {
86+
IntOrString intOrString = new IntOrString("17");
87+
88+
assertThat(intOrString, equalTo(intOrString));
89+
}
90+
91+
@Test
92+
public void whenCreatedWithString_equalsAnotherWithSameValue() {
93+
IntOrString intOrString1 = new IntOrString("17");
94+
IntOrString intOrString2 = new IntOrString("17");
95+
96+
assertThat(intOrString1, equalTo(intOrString2));
97+
}
98+
99+
@Test
100+
public void whenCreatedWithString_notEqualAnotherWithDifferentValue() {
101+
IntOrString intOrString1 = new IntOrString("17");
102+
IntOrString intOrString2 = new IntOrString("13");
103+
104+
assertThat(intOrString1, not(equalTo(intOrString2)));
105+
}
106+
107+
108+
}

0 commit comments

Comments
 (0)