Skip to content

Commit 09bfe29

Browse files
committed
Check that @nullable / @nonnull annotations are correct, when a fresh object is instantiated.
1 parent 40caea5 commit 09bfe29

23 files changed

+167
-18
lines changed

src/main/java/de/sstoehr/harreader/HarReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class HarReader extends AbstractHarIO {
1515
public HarReader() {
1616
super();
1717
}
18-
18+
1919
public HarReader(MapperFactory mapperFactory) {
2020
super(mapperFactory);
2121
}

src/main/java/de/sstoehr/harreader/HarWriter.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package de.sstoehr.harreader;
22

33
import java.io.File;
4-
import java.io.IOException;
54
import java.io.OutputStream;
65
import java.io.Writer;
7-
import java.util.function.Function;
86

97
import com.fasterxml.jackson.databind.ObjectMapper;
108

src/main/java/de/sstoehr/harreader/model/HarTiming.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package de.sstoehr.harreader.model;
22

3-
import com.fasterxml.jackson.annotation.*;
3+
4+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
5+
import com.fasterxml.jackson.annotation.JsonAnySetter;
6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
49

510
import javax.annotation.Nonnull;
611
import javax.annotation.Nullable;
@@ -23,7 +28,8 @@ public record HarTiming(
2328
protected static final Integer DEFAULT_TIME = -1;
2429

2530
public HarTiming() {
26-
this(DEFAULT_TIME, DEFAULT_TIME, DEFAULT_TIME, null, null, null, DEFAULT_TIME, null, new HashMap<>());
31+
this(DEFAULT_TIME, DEFAULT_TIME, DEFAULT_TIME,
32+
null, null, null, DEFAULT_TIME, null, new HashMap<>());
2733
}
2834

2935
public HarTiming(@Nullable Integer blocked,

src/test/java/de/sstoehr/harreader/HarReaderTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ void shouldReadFromFile() throws HarReaderException {
2828
}
2929

3030
@Test
31-
void shouldReadFromInputStream() throws HarReaderException, IOException {
31+
void shouldReadFromInputStream() throws IOException {
3232
File harFile = new File(PATH_TO_VALID_HAR);
3333
InputStream inputStream = Files.newInputStream(harFile.toPath());
3434
Har har = harReader.readFromInputStream(inputStream);
3535
assertNotNull(har);
3636
}
3737

3838
@Test
39-
void shouldReadFromString() throws HarReaderException, IOException {
39+
void shouldReadFromString() throws IOException {
4040
byte[] bytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
4141
String harAsString = new String(bytes, StandardCharsets.UTF_8);
4242
Har har = harReader.readFromString(harAsString);
4343
assertNotNull(har);
4444
}
4545

4646
@Test
47-
void shouldReadFromBytes() throws HarReaderException, IOException {
47+
void shouldReadFromBytes() throws IOException {
4848
byte[] harAsBytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
4949
Har har = harReader.readFromBytes(harAsBytes);
5050
assertNotNull(har);
@@ -57,7 +57,7 @@ void missingLog() throws HarReaderException {
5757
}
5858

5959
@Test
60-
void invalidDateStrict() throws HarReaderException {
60+
void invalidDateStrict() {
6161
File harFile = new File("src/test/resources/sstoehr.invalid-date.har");
6262
assertThrows(HarReaderException.class, () -> harReader.readFromFile(harFile));
6363
}
@@ -70,7 +70,7 @@ void invalidDateLax() throws HarReaderException {
7070
}
7171

7272
@Test
73-
void invalidIntegerStrict() throws HarReaderException {
73+
void invalidIntegerStrict() {
7474
File harFile = new File("src/test/resources/sstoehr.invalid-integer.har");
7575
assertThrows(HarReaderException.class, () -> harReader.readFromFile(harFile));
7676
}

src/test/java/de/sstoehr/harreader/HarWriterTests.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.fasterxml.jackson.databind.ObjectMapper;
1313

1414
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
15+
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
1516
import org.junit.jupiter.api.Test;
1617

1718
import de.sstoehr.harreader.model.Har;
@@ -23,7 +24,7 @@ class HarWriterTests {
2324
private static final File HAR_FILE = new File("src/test/resources/sstoehr.har");
2425

2526
@Test
26-
void shouldWriteHarAsString() throws HarReaderException, IOException, HarWriterException {
27+
void shouldWriteHarAsString() throws IOException {
2728
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
2829

2930
Har har = new HarReader().readFromFile(HAR_FILE);
@@ -32,16 +33,16 @@ void shouldWriteHarAsString() throws HarReaderException, IOException, HarWriterE
3233
}
3334

3435
@Test
35-
void shouldWriteHarAsBytes() throws HarReaderException, IOException, HarWriterException {
36+
void shouldWriteHarAsBytes() throws IOException {
3637
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
3738

3839
Har har = new HarReader().readFromFile(HAR_FILE);
39-
HarWriter writer = new HarWriter();
40+
HarWriter writer = new HarWriter(new DefaultMapperFactory());
4041
assertEquals(MAPPER.writeValueAsString(expected), new String(writer.writeAsBytes(har), StandardCharsets.UTF_8));
4142
}
4243

4344
@Test
44-
void testWriteToOutputStream() throws IOException, HarWriterException {
45+
void testWriteToOutputStream() throws IOException {
4546
ByteArrayOutputStream baos = new ByteArrayOutputStream();
4647
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
4748

@@ -51,7 +52,7 @@ void testWriteToOutputStream() throws IOException, HarWriterException {
5152
}
5253

5354
@Test
54-
void testWriteToFile() throws IOException, HarWriterException {
55+
void testWriteToFile() throws IOException {
5556
File file = File.createTempFile("pref", "suff");
5657
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
5758

@@ -62,7 +63,7 @@ void testWriteToFile() throws IOException, HarWriterException {
6263
}
6364

6465
@Test
65-
void testWriteToWriter() throws IOException, HarWriterException {
66+
void testWriteToWriter() throws IOException {
6667
StringWriter sw = new StringWriter();
6768
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
6869

src/test/java/de/sstoehr/harreader/model/AbstractMapperTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
7+
import org.junit.jupiter.api.Assertions;
8+
9+
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
11+
import java.lang.reflect.Field;
12+
import java.util.Arrays;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
717

818
public abstract class AbstractMapperTest<T> {
919

@@ -20,4 +30,20 @@ public T map(String input, Class<T> tClass) {
2030
}
2131
return null;
2232
}
33+
34+
public void testNullability(T object) {
35+
Arrays.stream(object.getClass().getDeclaredFields())
36+
.forEach(f -> {
37+
f.setAccessible(true);
38+
try {
39+
if (f.isAnnotationPresent(Nonnull.class)) {
40+
Assertions.assertNotNull(f.get(object), "Field " + f.getName() + " should not be null");
41+
} else if (f.isAnnotationPresent(Nullable.class)) {
42+
Assertions.assertNull(f.get(object), "Field " + f.getName() + " should be null");
43+
}
44+
} catch (IllegalAccessException e) {
45+
throw new RuntimeException(e);
46+
}
47+
});
48+
}
2349
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package de.sstoehr.harreader.model;
2+
3+
import nl.jqno.equalsverifier.EqualsVerifier;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.time.Instant;
7+
import java.time.ZoneId;
8+
import java.time.ZonedDateTime;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertNotNull;
12+
13+
class HarCacheInfoTest extends AbstractMapperTest<HarCache.HarCacheInfo> {
14+
15+
private final static ZonedDateTime EXPECTED_EXPIRES = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1388577600000L), ZoneId.of("Z"));
16+
private final static ZonedDateTime EXPECTED_LAST_ACCESS = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1370088000000L), ZoneId.of("Z"));
17+
18+
@Override
19+
@Test
20+
void testMapping() {
21+
HarCache.HarCacheInfo cacheInfo = map("{\"expires\":\"2014-01-01T12:00:00Z\",\"lastAccess\":\"2013-06-01T12:00:00Z\",\"eTag\":\"abc123\"," +
22+
"\"hitCount\":3,\"comment\":\"my comment\", \"_unknown\":\"unknown\"}", HarCache.HarCacheInfo.class);
23+
24+
assertEquals(EXPECTED_EXPIRES, cacheInfo.expires());
25+
assertEquals(EXPECTED_LAST_ACCESS, cacheInfo.lastAccess());
26+
assertEquals("abc123", cacheInfo.eTag());
27+
assertEquals(3, (long) cacheInfo.hitCount());
28+
assertEquals("my comment", cacheInfo.comment());
29+
30+
assertNotNull(cacheInfo.additional());
31+
assertEquals("unknown", cacheInfo.additional().get("_unknown"));
32+
}
33+
34+
@Test
35+
void testNullability() {
36+
testNullability(new HarCache.HarCacheInfo());
37+
}
38+
39+
@Test
40+
void equalsContract() {
41+
EqualsVerifier.simple().forClass(HarCache.HarCacheInfo.class).verify();
42+
}
43+
}

src/test/java/de/sstoehr/harreader/model/HarCacheTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ public void testMapping() {
4040

4141
}
4242

43+
@Test
44+
void testNullability() {
45+
testNullability(new HarCache());
46+
}
47+
4348
@Test
4449
void equalsContract() {
4550
EqualsVerifier.simple().forClass(HarCache.class).verify();
46-
EqualsVerifier.simple().forClass(HarCache.HarCacheInfo.class).verify();
4751
}
4852
}

src/test/java/de/sstoehr/harreader/model/HarContentTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import nl.jqno.equalsverifier.EqualsVerifier;
99

10+
1011
class HarContentTest extends AbstractMapperTest<HarContent> {
1112

1213
@Override
@@ -29,6 +30,11 @@ public void testMapping() {
2930
assertNotNull(content);
3031
}
3132

33+
@Test
34+
void testNullability() {
35+
testNullability(new HarContent());
36+
}
37+
3238
@Test
3339
void equalsContract() {
3440
EqualsVerifier.simple().forClass(HarContent.class).verify();

src/test/java/de/sstoehr/harreader/model/HarCookieTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public void testMapping() {
3838
assertNotNull(cookie);
3939
}
4040

41+
@Test
42+
void testNullability() {
43+
testNullability(new HarCookie());
44+
}
45+
4146
@Test
4247
void equalsContract() {
4348
EqualsVerifier.simple().forClass(HarCookie.class).verify();

src/test/java/de/sstoehr/harreader/model/HarCreatorBrowserTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public void testMapping() {
2626
assertNotNull(creatorBrowser);
2727
}
2828

29+
@Test
30+
void testNullability() {
31+
testNullability(new HarCreatorBrowser());
32+
}
33+
2934
@Test
3035
void equalsContract() {
3136
EqualsVerifier.simple().forClass(HarCreatorBrowser.class).verify();

src/test/java/de/sstoehr/harreader/model/HarEntryTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public void testTimingsNull() {
6363
assertNotNull(entry.timings());
6464
}
6565

66+
@Test
67+
void testNullability() {
68+
testNullability(new HarEntry());
69+
}
70+
6671
@Test
6772
public void equalsContract() {
6873
EqualsVerifier.simple().forClass(HarEntry.class).verify();

src/test/java/de/sstoehr/harreader/model/HarHeaderTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public void testMapping() {
2626
assertNotNull(header);
2727
}
2828

29+
@Test
30+
void testNullability() {
31+
testNullability(new HarHeader());
32+
}
33+
2934
@Test
3035
void equalsContract() {
3136
EqualsVerifier.simple().forClass(HarHeader.class).verify();

src/test/java/de/sstoehr/harreader/model/HarLogTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ void testMapping() {
8484
assertNotNull(log);
8585
}
8686

87+
@Test
88+
void testNullability() {
89+
testNullability(new HarLog());
90+
}
91+
8792
@Test
8893
void equalsContract() {
8994
EqualsVerifier.simple().forClass(HarLog.class).verify();

src/test/java/de/sstoehr/harreader/model/HarPageTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ void testPageTimingsNull() {
3939
assertNotNull(page.pageTimings());
4040
}
4141

42+
@Test
43+
void testNullability() {
44+
testNullability(new HarPage());
45+
}
46+
4247
@Test
4348
void equalsContract() {
4449
EqualsVerifier.simple().forClass(HarPage.class).verify();

src/test/java/de/sstoehr/harreader/model/HarPageTimingTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ void testMapping() {
4545
assertNotNull(pageTiming);
4646
}
4747

48+
@Test
49+
void testNullability() {
50+
testNullability(new HarPageTiming());
51+
}
52+
4853
@Test
4954
void equalsContract() {
5055
EqualsVerifier.simple().forClass(HarPageTiming.class).verify();

src/test/java/de/sstoehr/harreader/model/HarPostDataParamTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ void testMapping() {
2727
assertNotNull(postDataParam);
2828
}
2929

30+
@Test
31+
void testNullability() {
32+
testNullability(new HarPostDataParam());
33+
}
34+
3035
@Test
3136
void equalsContract() {
3237
EqualsVerifier.simple().forClass(HarPostDataParam.class).verify();

src/test/java/de/sstoehr/harreader/model/HarPostDataTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public void testParams() {
3636
assertNotNull(postData.params());
3737
}
3838

39+
@Test
40+
void testNullability() {
41+
testNullability(new HarPostData());
42+
}
43+
3944
@Test
4045
public void equalsContract() {
4146
EqualsVerifier.simple().forClass(HarPostData.class).verify();

src/test/java/de/sstoehr/harreader/model/HarQueryParamTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ void testMapping() {
2222
assertEquals("unknown", queryParam.additional().get("_unknown"));
2323
}
2424

25+
@Test
26+
void testNullability() {
27+
testNullability(new HarQueryParam());
28+
}
29+
2530
@Test
2631
void equalsContract() {
2732
EqualsVerifier.simple().forClass(HarQueryParam.class).verify();

src/test/java/de/sstoehr/harreader/model/HarRequestTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ void testNullMethodRaw() {
9292
assertEquals(HttpMethod.UNKNOWN, request.httpMethod());
9393
}
9494

95+
@Test
96+
void testNullability() {
97+
testNullability(new HarRequest());
98+
}
99+
95100
@Test
96101
void equalsContract() {
97102
EqualsVerifier.simple().forClass(HarRequest.class).verify();

0 commit comments

Comments
 (0)