This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch bugfix/expose-document-location-via-bufferingsinkproxy in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
commit 63a4e3dd56c13536b3a82b0e2ba64ad2a7fa440a Author: Konrad Windszus <[email protected]> AuthorDate: Mon Apr 1 15:21:49 2024 +0200 DOXIA-723 correctly expose document location through BufferingSinkProxy Add tests --- .../doxia/sink/impl/BufferingSinkProxyFactory.java | 9 ++++ .../sink/impl/BufferingSinkProxyFactoryTest.java | 56 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactory.java b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactory.java index 17fb1f0e..3d8a3965 100644 --- a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactory.java +++ b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactory.java @@ -61,11 +61,13 @@ public class BufferingSinkProxyFactory implements SinkWrapperFactory { private final Sink delegate; private static final Method FLUSH_METHOD; private static final Method GET_BUFFERED_SINK_METHOD; + private static final Method GET_DOCUMENT_LOCATOR_METHOD; static { try { FLUSH_METHOD = Sink.class.getMethod("flush"); GET_BUFFERED_SINK_METHOD = BufferingSink.class.getMethod("getBufferedSink"); + GET_DOCUMENT_LOCATOR_METHOD = BufferingSink.class.getMethod("getDocumentLocator"); } catch (NoSuchMethodException | SecurityException e) { throw new IllegalStateException("Could not find flush method in Sink!", e); } @@ -83,9 +85,16 @@ public class BufferingSinkProxyFactory implements SinkWrapperFactory { bufferedInvocations.clear(); } else if (method.equals(GET_BUFFERED_SINK_METHOD)) { return delegate; + } else if (method.equals(GET_DOCUMENT_LOCATOR_METHOD)) { + return delegate.getDocumentLocator(); } else { bufferedInvocations.add(new MethodWithArguments(method, args)); } + if (method.getReturnType() != Void.TYPE) { + throw new IllegalStateException( + "BufferingSinkProxy only works for methods returning void, but given method " + method + + " requires another return type"); + } return null; } } diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactoryTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactoryTest.java new file mode 100644 index 00000000..1864b143 --- /dev/null +++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/BufferingSinkProxyFactoryTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.doxia.sink.impl; + +import org.apache.maven.doxia.parser.AbstractParserTest; +import org.apache.maven.doxia.sink.Sink; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class BufferingSinkProxyFactoryTest { + + @Test + void testCastAsBufferingSink() { + SinkEventTestingSink testingSink = new SinkEventTestingSink(); + assertThrows(IllegalArgumentException.class, () -> BufferingSinkProxyFactory.castAsBufferingSink(testingSink)); + BufferingSinkProxyFactory factory = new BufferingSinkProxyFactory(); + Sink bufferingSink = factory.createWrapper(testingSink); + BufferingSinkProxyFactory.castAsBufferingSink(bufferingSink); + } + + @Test + void testBufferingSink() { + SinkEventTestingSink testingSink = new SinkEventTestingSink(); + BufferingSinkProxyFactory factory = new BufferingSinkProxyFactory(); + Sink bufferingSink = factory.createWrapper(testingSink); + assertNotNull(bufferingSink.getDocumentLocator()); + assertEquals( + testingSink, + BufferingSinkProxyFactory.castAsBufferingSink(bufferingSink).getBufferedSink()); + + bufferingSink.text("buffered text"); + assertEquals(0, testingSink.getEventList().size()); + + bufferingSink.flush(); + AbstractParserTest.assertSinkStartsWith(testingSink.getEventList().iterator(), "text"); + } +}
