This is an automated email from the ASF dual-hosted git repository. rmannibucau pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push: new c2bf6945 adding a test showing the PR review comment and why current exception handling is not yet release ready c2bf6945 is described below commit c2bf6945c2ca43befa93e15d821e0dee439aa8de Author: Romain Manni-Bucau <rmannibu...@gmail.com> AuthorDate: Mon May 16 08:53:12 2022 +0200 adding a test showing the PR review comment and why current exception handling is not yet release ready --- .../MapperBeanConstructorExceptionsTest.java | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperBeanConstructorExceptionsTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperBeanConstructorExceptionsTest.java index 2615fe0a..3abb1ce6 100644 --- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperBeanConstructorExceptionsTest.java +++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperBeanConstructorExceptionsTest.java @@ -20,11 +20,50 @@ import org.junit.Test; import java.beans.ConstructorProperties; import java.lang.reflect.Type; +import java.util.Collection; +import java.util.Iterator; +import java.util.Spliterator; + +import static java.util.Spliterators.spliteratorUnknownSize; +import static java.util.stream.Collectors.toList; +import static java.util.stream.StreamSupport.stream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class MapperBeanConstructorExceptionsTest { private static final RuntimeException USER_EXCEPTION = new RuntimeException("I am user, hear me roar"); + @Test + public void singleExceptionMapperInCause() { + try (final Mapper mapper = new MapperBuilder().setSnippetMaxLength(20).build()) { + mapper.readObject("{ \"string\" : \"whatever\" }", Rectangle.class); + fail("should have failed"); + } catch (final MapperException me) { + final Collection<Throwable> exceptionStack = stream(spliteratorUnknownSize(new Iterator<Throwable>() { + private Throwable current = me; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Throwable next() { + final Throwable throwable = current; + current = current.getCause() == current ? null : current.getCause(); + return throwable; + } + }, Spliterator.IMMUTABLE), false).collect(toList()); + assertEquals(3, exceptionStack.size()); + + // warn: this *must* be 1 which requires to ensure the MapperException thrown by + // org.apache.johnzon.mapper.MappingParserImpl.toValue is mutable to add the context/right message + // (no need to strip it, an alternative could be to use addSuppressed but it would keep creating stacks for nothing) + assertEquals(2, exceptionStack.stream().filter(MapperException.class::isInstance).count()); + } + } + @Test public void constructor() { ExceptionAsserts.fromMapperReadObject("{ \"string\" : \"Supercalifragilisticexpialidocious\" }", Circle.class) @@ -105,6 +144,23 @@ public class MapperBeanConstructorExceptionsTest { } } + public static class Rectangle { + private String string; + + @ConstructorProperties({"string"}) + public Rectangle(@JohnzonConverter(FailingConverter.class) final String string) { + fail("shouldn't be reached"); + } + + public String getString() { + return string; + } + + public void setString(final String string) { + this.string = string; + } + } + public static class Oval<T> { private String s; @@ -128,5 +184,15 @@ public class MapperBeanConstructorExceptionsTest { public interface Sphere { } + public static class FailingConverter<T> implements Converter<T> { + @Override + public String toString(final T instance) { + throw USER_EXCEPTION; + } + @Override + public T fromString(final String text) { + throw USER_EXCEPTION; + } + } }