Skip to content

Commit

Permalink
Check that Nullable / NonNull annotations are correct, when a fresh o…
Browse files Browse the repository at this point in the history
…bject is instantiated.
  • Loading branch information
sdstoehr committed Dec 21, 2024
1 parent 40caea5 commit cd39e90
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/sstoehr/harreader/HarReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HarReader extends AbstractHarIO {
public HarReader() {
super();
}

public HarReader(MapperFactory mapperFactory) {
super(mapperFactory);
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/de/sstoehr/harreader/HarWriter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.sstoehr.harreader;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.function.Function;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/de/sstoehr/harreader/model/HarTiming.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package de.sstoehr.harreader.model;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

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

public HarTiming() {
this(DEFAULT_TIME, DEFAULT_TIME, DEFAULT_TIME, null, null, null, DEFAULT_TIME, null, new HashMap<>());
this(DEFAULT_TIME, DEFAULT_TIME, DEFAULT_TIME,
null, null, null, DEFAULT_TIME, null, new HashMap<>());
}

public HarTiming(@Nullable Integer blocked,
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/de/sstoehr/harreader/HarReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ void shouldReadFromFile() throws HarReaderException {
}

@Test
void shouldReadFromInputStream() throws HarReaderException, IOException {
void shouldReadFromInputStream() throws IOException {
File harFile = new File(PATH_TO_VALID_HAR);
InputStream inputStream = Files.newInputStream(harFile.toPath());
Har har = harReader.readFromInputStream(inputStream);
assertNotNull(har);
}

@Test
void shouldReadFromString() throws HarReaderException, IOException {
void shouldReadFromString() throws IOException {
byte[] bytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
String harAsString = new String(bytes, StandardCharsets.UTF_8);
Har har = harReader.readFromString(harAsString);
assertNotNull(har);
}

@Test
void shouldReadFromBytes() throws HarReaderException, IOException {
void shouldReadFromBytes() throws IOException {
byte[] harAsBytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
Har har = harReader.readFromBytes(harAsBytes);
assertNotNull(har);
Expand All @@ -57,7 +57,7 @@ void missingLog() throws HarReaderException {
}

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

@Test
void invalidIntegerStrict() throws HarReaderException {
void invalidIntegerStrict() {
File harFile = new File("src/test/resources/sstoehr.invalid-integer.har");
assertThrows(HarReaderException.class, () -> harReader.readFromFile(harFile));
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/de/sstoehr/harreader/HarWriterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
import org.junit.jupiter.api.Test;

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

@Test
void shouldWriteHarAsString() throws HarReaderException, IOException, HarWriterException {
void shouldWriteHarAsString() throws IOException {
Har expected = MAPPER.readValue(HAR_FILE, Har.class);

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

@Test
void shouldWriteHarAsBytes() throws HarReaderException, IOException, HarWriterException {
void shouldWriteHarAsBytes() throws IOException {
Har expected = MAPPER.readValue(HAR_FILE, Har.class);

Har har = new HarReader().readFromFile(HAR_FILE);
HarWriter writer = new HarWriter();
HarWriter writer = new HarWriter(new DefaultMapperFactory());
assertEquals(MAPPER.writeValueAsString(expected), new String(writer.writeAsBytes(har), StandardCharsets.UTF_8));
}

@Test
void testWriteToOutputStream() throws IOException, HarWriterException {
void testWriteToOutputStream() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Har expected = MAPPER.readValue(HAR_FILE, Har.class);

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

@Test
void testWriteToFile() throws IOException, HarWriterException {
void testWriteToFile() throws IOException {
File file = File.createTempFile("pref", "suff");
Har expected = MAPPER.readValue(HAR_FILE, Har.class);

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

@Test
void testWriteToWriter() throws IOException, HarWriterException {
void testWriteToWriter() throws IOException {
StringWriter sw = new StringWriter();
Har expected = MAPPER.readValue(HAR_FILE, Har.class);

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/AbstractMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
import org.junit.jupiter.api.Assertions;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbstractMapperTest<T> {

Expand All @@ -20,4 +30,20 @@ public T map(String input, Class<T> tClass) {
}
return null;
}

public void testNullability(T object) {
Arrays.stream(object.getClass().getDeclaredFields())
.forEach(f -> {
f.setAccessible(true);
try {
if (f.isAnnotationPresent(Nonnull.class)) {
Assertions.assertNotNull(f.get(object), "Field " + f.getName() + " should not be null");
} else if (f.isAnnotationPresent(Nullable.class)) {
Assertions.assertNull(f.get(object), "Field " + f.getName() + " should be null");
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
}
}
43 changes: 43 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarCacheInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.sstoehr.harreader.model;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class HarCacheInfoTest extends AbstractMapperTest<HarCache.HarCacheInfo> {

private final static ZonedDateTime EXPECTED_EXPIRES = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1388577600000L), ZoneId.of("Z"));
private final static ZonedDateTime EXPECTED_LAST_ACCESS = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1370088000000L), ZoneId.of("Z"));

@Override
@Test
void testMapping() {
HarCache.HarCacheInfo cacheInfo = map("{\"expires\":\"2014-01-01T12:00:00Z\",\"lastAccess\":\"2013-06-01T12:00:00Z\",\"eTag\":\"abc123\"," +
"\"hitCount\":3,\"comment\":\"my comment\", \"_unknown\":\"unknown\"}", HarCache.HarCacheInfo.class);

assertEquals(EXPECTED_EXPIRES, cacheInfo.expires());
assertEquals(EXPECTED_LAST_ACCESS, cacheInfo.lastAccess());
assertEquals("abc123", cacheInfo.eTag());
assertEquals(3, (long) cacheInfo.hitCount());
assertEquals("my comment", cacheInfo.comment());

assertNotNull(cacheInfo.additional());
assertEquals("unknown", cacheInfo.additional().get("_unknown"));
}

@Test
void testNullability() {
testNullability(new HarCache.HarCacheInfo());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarCache.HarCacheInfo.class).verify();
}
}
6 changes: 5 additions & 1 deletion src/test/java/de/sstoehr/harreader/model/HarCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public void testMapping() {

}

@Test
void testNullability() {
testNullability(new HarCache());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarCache.class).verify();
EqualsVerifier.simple().forClass(HarCache.HarCacheInfo.class).verify();
}
}
6 changes: 6 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import nl.jqno.equalsverifier.EqualsVerifier;


class HarContentTest extends AbstractMapperTest<HarContent> {

@Override
Expand All @@ -29,6 +30,11 @@ public void testMapping() {
assertNotNull(content);
}

@Test
void testNullability() {
testNullability(new HarContent());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarContent.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarCookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void testMapping() {
assertNotNull(cookie);
}

@Test
void testNullability() {
testNullability(new HarCookie());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarCookie.class).verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public void testMapping() {
assertNotNull(creatorBrowser);
}

@Test
void testNullability() {
testNullability(new HarCreatorBrowser());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarCreatorBrowser.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void testTimingsNull() {
assertNotNull(entry.timings());
}

@Test
void testNullability() {
testNullability(new HarEntry());
}

@Test
public void equalsContract() {
EqualsVerifier.simple().forClass(HarEntry.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public void testMapping() {
assertNotNull(header);
}

@Test
void testNullability() {
testNullability(new HarHeader());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarHeader.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ void testMapping() {
assertNotNull(log);
}

@Test
void testNullability() {
testNullability(new HarLog());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarLog.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ void testPageTimingsNull() {
assertNotNull(page.pageTimings());
}

@Test
void testNullability() {
testNullability(new HarPage());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarPage.class).verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void testMapping() {
assertNotNull(pageTiming);
}

@Test
void testNullability() {
testNullability(new HarPageTiming());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarPageTiming.class).verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ void testMapping() {
assertNotNull(postDataParam);
}

@Test
void testNullability() {
testNullability(new HarPostDataParam());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarPostDataParam.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarPostDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void testParams() {
assertNotNull(postData.params());
}

@Test
void testNullability() {
testNullability(new HarPostData());
}

@Test
public void equalsContract() {
EqualsVerifier.simple().forClass(HarPostData.class).verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ void testMapping() {
assertEquals("unknown", queryParam.additional().get("_unknown"));
}

@Test
void testNullability() {
testNullability(new HarQueryParam());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarQueryParam.class).verify();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/sstoehr/harreader/model/HarRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void testNullMethodRaw() {
assertEquals(HttpMethod.UNKNOWN, request.httpMethod());
}

@Test
void testNullability() {
testNullability(new HarRequest());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(HarRequest.class).verify();
Expand Down
Loading

0 comments on commit cd39e90

Please sign in to comment.