Skip to content

Commit dcd258c

Browse files
committed
Update dependencies and fix code style and behaviour issues
1 parent 7af6e8b commit dcd258c

17 files changed

Lines changed: 269 additions & 191 deletions

config/pmd/pmd-rulesets.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@
3636
<rule ref="category/java/design.xml/CollapsibleIfStatements"/>
3737
<rule ref="category/java/design.xml/SimplifiedTernary"/>
3838

39-
<!-- Braces -->
40-
41-
<rule ref="category/java/codestyle.xml/ForLoopsMustUseBraces"/>
42-
<rule ref="category/java/codestyle.xml/IfElseStmtsMustUseBraces"/>
43-
<rule ref="category/java/codestyle.xml/IfStmtsMustUseBraces"/>
44-
<rule ref="category/java/codestyle.xml/WhileLoopsMustUseBraces"/>
45-
4639
<!-- Clone -->
4740

4841
<rule ref="category/java/errorprone.xml/CloneMethodMustBePublic"/>

gradle/versions.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ ext {
99
buildConfigPluginVersion = '1.1.8'
1010
freefairPluginsVersion = '2.4.2'
1111

12-
jacocoVersion = '0.8.0'
13-
checkstyleVersion = '8.8'
14-
errorproneVersion = '2.2.0'
12+
jacocoVersion = '0.8.1'
13+
checkstyleVersion = '8.10'
14+
errorproneVersion = '2.3.1'
1515
findbugsVersion = '3.0.1'
16-
pmdVersion = '6.1.0'
16+
pmdVersion = '6.3.0'
1717

1818
lombokVersion = '1.16.20'
1919
jsr305Version = '3.0.2'
2020
javaxExtrasVersion = '0.1.0'
2121

2222
retrofitVersion = '2.4.0'
2323
okHttpVersion = '3.10.0'
24-
moshiVersion = '1.5.0'
25-
moshiLazyAdaptersVersion = '2.1'
24+
moshiVersion = '1.6.0'
25+
moshiLazyAdaptersVersion = '2.2'
2626

2727
junitVersion = '4.12'
28-
assertjVersion = '3.9.1'
28+
assertjVersion = '3.10.0'
2929
privateConstructorVersion = '1.2.0'
3030

3131
deps = [

library/src/main/java/me/proxer/library/api/BooleanAdapter.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package me.proxer.library.api;
2+
3+
import com.squareup.moshi.JsonAdapter;
4+
import com.squareup.moshi.JsonDataException;
5+
import com.squareup.moshi.JsonReader;
6+
import com.squareup.moshi.JsonWriter;
7+
import com.squareup.moshi.Moshi;
8+
9+
import javax.annotation.Nullable;
10+
import java.io.IOException;
11+
import java.lang.annotation.Annotation;
12+
import java.lang.reflect.Type;
13+
import java.util.Set;
14+
15+
/**
16+
* @author Ruben Gees
17+
*/
18+
class BooleanAdapterFactory implements JsonAdapter.Factory {
19+
20+
@Nullable
21+
@Override
22+
public JsonAdapter<?> create(final Type type, final Set<? extends Annotation> annotations, final Moshi moshi) {
23+
if (type != boolean.class && type != Boolean.class) {
24+
return null;
25+
}
26+
27+
return new BooleanAdapter();
28+
}
29+
30+
private static class BooleanAdapter extends JsonAdapter<Boolean> {
31+
32+
@Nullable
33+
@Override
34+
public Boolean fromJson(final JsonReader reader) throws IOException {
35+
final JsonReader.Token nextToken = reader.peek();
36+
37+
if (nextToken == JsonReader.Token.BOOLEAN) {
38+
return reader.nextBoolean();
39+
} else if (nextToken == JsonReader.Token.NUMBER) {
40+
final int number = reader.nextInt();
41+
42+
return intToBoolean(number, reader.getPath());
43+
} else if (nextToken == JsonReader.Token.STRING) {
44+
final String jsonString = reader.nextString();
45+
final int jsonNumber = toIntOrNull(jsonString);
46+
47+
if (jsonNumber < 0) {
48+
return stringToBoolean(jsonString, reader.getPath());
49+
} else {
50+
return intToBoolean(jsonNumber, reader.getPath());
51+
}
52+
} else if (nextToken == JsonReader.Token.NULL) {
53+
return reader.nextNull();
54+
} else {
55+
throw new JsonDataException("Expected a boolean, number, string or null but was " + nextToken.name()
56+
+ " at path " + reader.getPath());
57+
}
58+
}
59+
60+
@Override
61+
public void toJson(final JsonWriter writer, @Nullable final Boolean value) throws IOException {
62+
writer.value(value);
63+
}
64+
65+
private int toIntOrNull(final String candidate) {
66+
try {
67+
return Integer.parseInt(candidate);
68+
} catch (NumberFormatException ignored) {
69+
return -1;
70+
}
71+
}
72+
73+
private boolean intToBoolean(final int subject, final String path) {
74+
switch (subject) {
75+
case 0:
76+
return false;
77+
case 1:
78+
return true;
79+
default:
80+
throw new JsonDataException("Expected a 1 or 0 but was " + subject + " at path " + path);
81+
}
82+
}
83+
84+
private boolean stringToBoolean(final String subject, final String path) {
85+
switch (subject) {
86+
case "true":
87+
return true;
88+
case "false":
89+
return false;
90+
default:
91+
throw new JsonDataException("Expected a true or false but was " + subject + " at path " + path);
92+
}
93+
}
94+
}
95+
}

library/src/main/java/me/proxer/library/api/PrimitiveBooleanAdapter.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

library/src/main/java/me/proxer/library/api/ProxerApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,13 @@ private void initMoshi() {
253253
.add(new VoidAdapter())
254254
.add(new DateAdapter())
255255
.add(new PageAdapter())
256-
.add(new BooleanAdapter())
257256
.add(new HttpUrlAdapter())
258257
.add(new ConferenceAdapter())
259258
.add(new EpisodeInfoAdapter())
260259
.add(new AdaptionInfoAdapter())
261260
.add(new NotificationAdapter())
262261
.add(new ConferenceInfoAdapter())
263-
.add(new PrimitiveBooleanAdapter())
262+
.add(new BooleanAdapterFactory())
264263
.add(new NotificationInfoAdapter())
265264
.add(new FixRatingDetailsAdapter())
266265
.add(new DelimitedEnumSetAdapterFactory())

library/src/main/java/me/proxer/library/entity/info/AdaptionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class AdaptionInfo implements ProxerIdItem {
3333
@Json(name = "medium")
3434
private Medium medium;
3535

36-
public AdaptionInfo(final String id, final String name, final Medium medium) {
36+
public AdaptionInfo(final String id, final String name, @Nullable final Medium medium) {
3737
this.id = id;
3838
this.name = name;
3939
this.medium = medium;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package me.proxer.library.api;
2+
3+
import com.squareup.moshi.JsonAdapter;
4+
import com.squareup.moshi.JsonDataException;
5+
import com.squareup.moshi.Moshi;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.HashSet;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
14+
15+
/**
16+
* @author Ruben Gees
17+
*/
18+
public class BooleanAdapterFactoryTest {
19+
20+
private BooleanAdapterFactory factory;
21+
private JsonAdapter<Boolean> adapter;
22+
23+
@SuppressWarnings("unchecked")
24+
@Before
25+
public void setUp() {
26+
factory = new BooleanAdapterFactory();
27+
adapter = (JsonAdapter<Boolean>) factory.create(Boolean.class, new HashSet<>(), new Moshi.Builder().build());
28+
}
29+
30+
@Test
31+
public void testCreatePrimitive() {
32+
assertThat(factory.create(Boolean.class, new HashSet<>(), new Moshi.Builder().build())).isNotNull();
33+
}
34+
35+
@Test
36+
public void testCreateBoxed() {
37+
assertThat(factory.create(boolean.class, new HashSet<>(), new Moshi.Builder().build())).isNotNull();
38+
}
39+
40+
@Test
41+
public void testCreateNonBoolean() {
42+
assertThat(factory.create(String.class, new HashSet<>(), new Moshi.Builder().build())).isNull();
43+
}
44+
45+
@Test
46+
public void testFromBoolean() throws IOException {
47+
assertThat(adapter.fromJson("true")).isTrue();
48+
}
49+
50+
@Test
51+
public void testFromBooleanFalse() throws IOException {
52+
assertThat(adapter.fromJson("false")).isFalse();
53+
}
54+
55+
@Test
56+
public void testFromInt() throws IOException {
57+
assertThat(adapter.fromJson("1")).isTrue();
58+
}
59+
60+
@Test
61+
public void testFromIntFalse() throws IOException {
62+
assertThat(adapter.fromJson("0")).isFalse();
63+
}
64+
65+
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
66+
@Test
67+
public void testFromIntInvalid() {
68+
assertThatExceptionOfType(JsonDataException.class)
69+
.isThrownBy(() -> adapter.fromJson("3"))
70+
.withMessage("Expected a 1 or 0 but was 3 at path $");
71+
}
72+
73+
@Test
74+
public void testFromString() throws IOException {
75+
assertThat(adapter.fromJson("\"true\"")).isTrue();
76+
}
77+
78+
@Test
79+
public void testFromStringFalse() throws IOException {
80+
assertThat(adapter.fromJson("\"false\"")).isFalse();
81+
}
82+
83+
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
84+
@Test
85+
public void testFromStringInvalid() {
86+
assertThatExceptionOfType(JsonDataException.class)
87+
.isThrownBy(() -> adapter.fromJson("\"invalid\""))
88+
.withMessage("Expected a true or false but was invalid at path $");
89+
}
90+
91+
@Test
92+
public void testFromStringInt() throws IOException {
93+
assertThat(adapter.fromJson("\"1\"")).isTrue();
94+
}
95+
96+
@Test
97+
public void testFromStringIntFalse() throws IOException {
98+
assertThat(adapter.fromJson("\"0\"")).isFalse();
99+
}
100+
101+
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
102+
@Test
103+
public void testFromStringIntInvalid() {
104+
assertThatExceptionOfType(JsonDataException.class)
105+
.isThrownBy(() -> adapter.fromJson("\"3\""))
106+
.withMessage("Expected a 1 or 0 but was 3 at path $");
107+
}
108+
109+
@Test
110+
public void testFromNull() throws IOException {
111+
assertThat(adapter.fromJson("null")).isNull();
112+
}
113+
114+
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
115+
@Test
116+
public void testFromInvalid() {
117+
assertThatExceptionOfType(JsonDataException.class)
118+
.isThrownBy(() -> adapter.fromJson("[]"))
119+
.withMessage("Expected a boolean, number, string or null but was BEGIN_ARRAY at path $");
120+
}
121+
122+
@Test
123+
public void testToJson() {
124+
assertThat(adapter.toJson(true)).isEqualTo("true");
125+
}
126+
127+
@Test
128+
public void testToJsonFalse() {
129+
assertThat(adapter.toJson(false)).isEqualTo("false");
130+
}
131+
132+
@Test
133+
public void testToJsonNull() {
134+
assertThat(adapter.toJson(null)).isEqualTo("null");
135+
}
136+
}

0 commit comments

Comments
 (0)