Skip to content

Commit

Permalink
#318 Test for different exception types when null is used in local bu…
Browse files Browse the repository at this point in the history
…ilds
  • Loading branch information
ljacqu committed Jun 20, 2023
1 parent 9f33830 commit 3f68392
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
45 changes: 45 additions & 0 deletions src/test/java/ch/jalu/configme/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.jalu.configme.properties.convertresult.PropertyValue;
import org.hamcrest.Matcher;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.function.Executable;

import java.io.IOException;
Expand Down Expand Up @@ -32,6 +33,8 @@
*/
public final class TestUtils {

private static Class<? extends Exception> expectedNullArgExceptionType;

private TestUtils() {
}

Expand Down Expand Up @@ -142,6 +145,32 @@ public static void verifyException(Executable executable, Class<? extends Except
}
}

/**
* Returns the expected exception type when a null argument is supplied where it is not allowed.
* <p>
* Background: Due to usage of IntelliJ's &#64;{@link NotNull} annotation, when compiled locally, IntelliJ adds
* bytecode to guard against null which throws an IllegalStateException or an IllegalArgumentException. When code
* is built outside of IntelliJ (e.g. Maven build), the guards are not added to the bytecode. This results in
* different exceptions being thrown.
*
* @return the expected exception type for a null argument where it is not allowed (NPE or IllegalArgumentException)
*/
public static Class<? extends Exception> getExceptionTypeForNullArg() {
return getOrCaptureNullExceptionType();
}

/**
* Returns the expected exception type when a null value (from a field) is supplied where it is not allowed.
* See {@link #getExceptionTypeForNullArg()} for the background.
*
* @return the expected exception type for a null state where it is not allowed (NPE or IllegalStateException)
*/
public static Class<? extends Exception> getExceptionTypeForNullField() {
return getOrCaptureNullExceptionType().equals(NullPointerException.class)
? NullPointerException.class
: IllegalStateException.class;
}

// -------------
// Convenience methods
// -------------
Expand Down Expand Up @@ -186,4 +215,20 @@ private static Matcher<PropertyValue> isPropertyValueOf(Object expectedValue, bo
Matcher<PropertyValue> validFlagMatcher = hasProperty("validInResource", equalTo(expectedValid));
return both(valueMatcher).and(validFlagMatcher);
}

private static Class<? extends Exception> getOrCaptureNullExceptionType() {
if (expectedNullArgExceptionType == null) {
try {
notNullMethod(null);
throw new IllegalStateException("Expected NPE or IAE");
} catch (NullPointerException | IllegalArgumentException e) {
expectedNullArgExceptionType = e.getClass();
}
}
return expectedNullArgExceptionType;
}

private static void notNullMethod(@NotNull Object arg) {
arg.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.jalu.configme.resource.PropertyReader;
import org.junit.jupiter.api.Test;

import static ch.jalu.configme.TestUtils.getExceptionTypeForNullArg;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -18,14 +19,14 @@ class BasePropertyTest {
@Test
void shouldRejectNullValue() {
// given / when / then
assertThrows(NullPointerException.class,
assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyTestImpl("my.path", null));
}

@Test
void shouldRejectNullPath() {
// given / when / then
assertThrows(NullPointerException.class,
assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyTestImpl(null, (byte) 123));
}

Expand Down
28 changes: 19 additions & 9 deletions src/test/java/ch/jalu/configme/properties/PropertyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Map;
import java.util.Set;

import static ch.jalu.configme.TestUtils.getExceptionTypeForNullArg;
import static ch.jalu.configme.TestUtils.getExceptionTypeForNullField;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.arrayContaining;
Expand Down Expand Up @@ -53,39 +55,45 @@ void shouldCreateCommonPropertyWithCustomFunction() {
@Test
void shouldThrowForMissingPathInCommonPropertyBuilder() {
// given / when
NullPointerException e = assertThrows(NullPointerException.class,
Exception e = assertThrows(getExceptionTypeForNullField(),
() -> new PropertyBuilder.TypeBasedPropertyBuilder<>(PrimitivePropertyType.STRING)
.defaultValue("Hello")
.build());

// then
assertThat(e.getMessage(), equalTo("path"));
if (e instanceof NullPointerException) {
assertThat(e.getMessage(), equalTo("path"));
}
}

@Test
void shouldThrowForMissingDefaultValueInCommonPropertyBuilder() {
// given / when
NullPointerException e = assertThrows(NullPointerException.class,
Exception e = assertThrows(getExceptionTypeForNullField(),
() -> new PropertyBuilder.TypeBasedPropertyBuilder<>(PrimitivePropertyType.STRING)
.path("a.path")
.createFunction((path, val, type) -> new StringProperty(path, val))
.build());

// then
assertThat(e.getMessage(), equalTo("defaultValue"));
if (e instanceof NullPointerException) {
assertThat(e.getMessage(), equalTo("defaultValue"));
}
}

@Test
void shouldThrowForMissingPropertyTypeInCommonPropertyBuilder() {
// given / when
NullPointerException e = assertThrows(NullPointerException.class,
Exception e = assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyBuilder.TypeBasedPropertyBuilder<>(null)
.path("random.path")
.defaultValue("Hello")
.build());

// then
assertThat(e.getMessage(), equalTo("type"));
if (e instanceof NullPointerException) {
assertThat(e.getMessage(), equalTo("type"));
}
}

@Test
Expand Down Expand Up @@ -119,11 +127,13 @@ void shouldCreateMapPropertyWithEmptyMapAsDefault() {
@Test
void shouldThrowForMissingPathInMapBuilder() {
// given / when
NullPointerException e = assertThrows(NullPointerException.class,
Exception e = assertThrows(getExceptionTypeForNullField(),
() -> new PropertyBuilder.MapPropertyBuilder<>(PrimitivePropertyType.DOUBLE).build());

// then
assertThat(e.getMessage(), equalTo("path"));
if (e instanceof NullPointerException) {
assertThat(e.getMessage(), equalTo("path"));
}
}

@Test
Expand Down Expand Up @@ -185,7 +195,7 @@ void shouldCreateSetProperty() {
@Test
void shouldThrowForMissingDefaultValue() {
// given / when / then
assertThrows(NullPointerException.class,
assertThrows(getExceptionTypeForNullField(),
() -> new PropertyBuilder.ListPropertyBuilder<>(PrimitivePropertyType.DOUBLE)
.path("defined.path")
.build());
Expand Down

0 comments on commit 3f68392

Please sign in to comment.