-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add support for URL & URI * Fix handling "get" methods * Add support for Optional * Add support for date & time * Refactoring * Prepare release * Fix sonar warnings --------- Co-authored-by: kaklakariada <kaklakariada@users.noreply.github.com>
- Loading branch information
1 parent
ae63db0
commit d6e155d
Showing
8 changed files
with
230 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ dependencies { | |
} | ||
|
||
group 'org.itsallcode' | ||
version = '0.5.1' | ||
version = '0.6.0' | ||
|
||
java { | ||
toolchain { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/java/org/itsallcode/matcher/auto/OptionalMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.itsallcode.matcher.auto; | ||
|
||
import java.util.Optional; | ||
|
||
import org.hamcrest.*; | ||
|
||
/** | ||
* Provides a set of Hamcrest matchers for {@code java.util.Optional}: | ||
* <ul> | ||
* <li>{@link #isEmpty()} - matches when the examined {@code Optional} contains | ||
* no value.</li> | ||
* <li>{@link #isPresentAnd(Matcher)} - matches when the examined | ||
* {@code Optional} contains a value that satisfies the specified matcher.</li> | ||
* </ul> | ||
* | ||
* This was copied from | ||
* {@link https://github.com/npathai/hamcrest-optional/blob/master/src/main/java/com/github/npathai/hamcrestopt/OptionalMatchers.java} | ||
* | ||
* @author npathai, sweiler | ||
*/ | ||
class OptionalMatchers { | ||
|
||
/** | ||
* Creates a matcher that matches when the examined {@code Optional} contains no | ||
* value. | ||
* | ||
* <pre> | ||
* Optional<String> optionalObject = Optional.empty(); | ||
* assertThat(optionalObject, isEmpty()); | ||
* </pre> | ||
* | ||
* @return a matcher that matches when the examined {@code Optional} contains no | ||
* value. | ||
*/ | ||
@SuppressWarnings("java:S1452") // Generic wildcard required here | ||
static Matcher<Optional<?>> isEmpty() { | ||
return new EmptyMatcher(); | ||
} | ||
|
||
private static class EmptyMatcher extends TypeSafeMatcher<Optional<?>> { | ||
|
||
public void describeTo(final Description description) { | ||
description.appendText("is <Empty>"); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(final Optional<?> item) { | ||
return !item.isPresent(); | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(final Optional<?> item, final Description mismatchDescription) { | ||
mismatchDescription.appendText("had value "); | ||
mismatchDescription.appendValue(item.orElseThrow()); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a matcher that matches when the examined {@code Optional} contains a | ||
* value that satisfies the specified matcher. | ||
* | ||
* <pre> | ||
* Optional<String> optionalObject = Optional.of("dummy value"); | ||
* assertThat(optionalObject, isPresentAnd(startsWith("dummy"))); | ||
* </pre> | ||
* | ||
* @param matcher a matcher for the value of the examined {@code Optional}. | ||
* @param <T> the class of the value. | ||
* @return a matcher that matches when the examined {@code Optional} contains a | ||
* value that satisfies the specified matcher. | ||
*/ | ||
public static <T> Matcher<Optional<T>> isPresentAnd(final Matcher<? super T> matcher) { | ||
return new HasValue<>(matcher); | ||
} | ||
|
||
private static class HasValue<T> extends TypeSafeMatcher<Optional<T>> { | ||
private final Matcher<? super T> matcher; | ||
|
||
public HasValue(final Matcher<? super T> matcher) { | ||
this.matcher = matcher; | ||
} | ||
|
||
@Override | ||
public void describeTo(final Description description) { | ||
description.appendText("has value that is "); | ||
matcher.describeTo(description); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(final Optional<T> item) { | ||
return item.isPresent() && matcher.matches(item.get()); | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(final Optional<T> item, final Description mismatchDescription) { | ||
if (item.isPresent()) { | ||
mismatchDescription.appendText("value "); | ||
matcher.describeMismatch(item.get(), mismatchDescription); | ||
} else { | ||
mismatchDescription.appendText("was <Empty>"); | ||
} | ||
} | ||
} | ||
|
||
// This is an utility class that must not be instantiated. | ||
private OptionalMatchers() { | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/org/itsallcode/matcher/auto/AutoConfigBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.itsallcode.matcher.auto; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
class AutoConfigBuilderTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({ "getName,name", "getname,name", "isTrue,true", "istrue,true", "get,get", "is,is", | ||
"otherName,otherName", "OtherName,OtherName" }) | ||
void getPropertyName(final String methodName, final String expectedPropertyName) { | ||
assertEquals(expectedPropertyName, AutoConfigBuilder.getPropertyName(methodName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters