From a9d8828bf3d2d627b67fb6d9ecf2df5020329c14 Mon Sep 17 00:00:00 2001 From: Uladzislau Arlouski Date: Mon, 6 Jan 2025 20:05:02 +0300 Subject: [PATCH] [plugin-xml] Add step to check if XML document is well formed (#5643) --- docs/modules/plugins/pages/plugin-xml.adoc | 25 +++++ .../java/org/vividus/steps/xml/XmlSteps.java | 29 +++++- .../java/org/vividus/util/xml/XmlUtils.java | 95 +++++++------------ .../org/vividus/steps/xml/XmlStepsTests.java | 23 ++++- .../org/vividus/util/xml/XmlUtilsTests.java | 12 ++- vividus-util/build.gradle | 1 - .../pool/SuppliedPooledObjectFactory.java | 45 --------- .../util/pool/UnsafeGenericObjectPool.java | 70 -------------- .../SuppliedPooledObjectFactoryTests.java | 46 --------- .../pool/UnsafeGenericObjectPoolTests.java | 80 ---------------- 10 files changed, 112 insertions(+), 314 deletions(-) delete mode 100644 vividus-util/src/main/java/org/vividus/util/pool/SuppliedPooledObjectFactory.java delete mode 100644 vividus-util/src/main/java/org/vividus/util/pool/UnsafeGenericObjectPool.java delete mode 100644 vividus-util/src/test/java/org/vividus/util/pool/SuppliedPooledObjectFactoryTests.java delete mode 100644 vividus-util/src/test/java/org/vividus/util/pool/UnsafeGenericObjectPoolTests.java diff --git a/docs/modules/plugins/pages/plugin-xml.adoc b/docs/modules/plugins/pages/plugin-xml.adoc index 59f7b0a986..265db6cb16 100644 --- a/docs/modules/plugins/pages/plugin-xml.adoc +++ b/docs/modules/plugins/pages/plugin-xml.adoc @@ -191,3 +191,28 @@ Then XML ` ` ---- + +=== Validate well formed XML document + +Validates if the provided XML document is well formed. + +[source,gherkin] +---- +Then XML `$xml` is well formed +---- + +* `$xml` - The {xml} document. + +.Check a message is well formed XML document +[source,gherkin] +---- +Then XML ` + + + Bob + Alice + Reminder + Don't forget to fill TJ gaps for this week + +` is well formed +---- diff --git a/vividus-plugin-xml/src/main/java/org/vividus/steps/xml/XmlSteps.java b/vividus-plugin-xml/src/main/java/org/vividus/steps/xml/XmlSteps.java index 6dfca8961c..ed8fbc2756 100644 --- a/vividus-plugin-xml/src/main/java/org/vividus/steps/xml/XmlSteps.java +++ b/vividus-plugin-xml/src/main/java/org/vividus/steps/xml/XmlSteps.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.Set; +import javax.xml.xpath.XPathExpressionException; + import org.jbehave.core.annotations.Then; import org.jbehave.core.annotations.When; import org.vividus.context.VariableContext; @@ -51,9 +53,11 @@ public XmlSteps(ISoftAssert softAssert, VariableContext variableContext) * @param xml XML * @param scopes The set of variable scopes (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) * @param variableName Name of variable + * @throws XPathExpressionException If an XPath expression error has occurred */ @When("I save data found by xpath `$xpath` in XML `$xml` to $scopes variable `$variableName`") public void saveDataByXpath(String xpath, String xml, Set scopes, String variableName) + throws XPathExpressionException { XmlUtils.getXmlByXpath(xml, xpath).ifPresent( data -> variableContext.putVariable(scopes, variableName, data)); @@ -78,9 +82,11 @@ public void saveTransformedXml(String xml, String xslt, Set scope * Checks if xml contains element by XPath * @param xml XML * @param xpath XPath + * @throws IOException If an I/O error has occurred + * @throws SAXException If an XML processing error has occurred */ @Then("XML `$xml` contains element by xpath `$xpath`") - public void doesElementExistByXpath(String xml, String xpath) + public void doesElementExistByXpath(String xml, String xpath) throws SAXException, IOException { Document doc = XmlUtils.convertToDocument(xml); softAssert.assertThat("XML has element with XPath: " + xpath, doc, hasXPath(xpath)); @@ -129,4 +135,23 @@ public void validateXmlAgainstXsd(String xml, String xsd) softAssert.recordFailedAssertion(e); } } + + /** + * Validates if the XML document is well formed + * + * @param xml The XML document + */ + @Then("XML `$xml` is well formed") + public void validateXmlIsWellFormed(String xml) + { + try + { + XmlUtils.convertToDocument(xml); + softAssert.recordPassedAssertion("The XML document is well formed"); + } + catch (SAXException | IOException e) + { + softAssert.recordFailedAssertion(e); + } + } } diff --git a/vividus-plugin-xml/src/main/java/org/vividus/util/xml/XmlUtils.java b/vividus-plugin-xml/src/main/java/org/vividus/util/xml/XmlUtils.java index a25d02cf38..74f932ba71 100644 --- a/vividus-plugin-xml/src/main/java/org/vividus/util/xml/XmlUtils.java +++ b/vividus-plugin-xml/src/main/java/org/vividus/util/xml/XmlUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,10 +41,6 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; -import org.apache.commons.pool2.BasePooledObjectFactory; -import org.apache.commons.pool2.PooledObject; -import org.apache.commons.pool2.impl.DefaultPooledObject; -import org.vividus.util.pool.UnsafeGenericObjectPool; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -59,59 +55,37 @@ public final class XmlUtils private static final String EXTERNAL_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities"; private static final String EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities"; - private static final UnsafeGenericObjectPool XPATH_FACTORY = new UnsafeGenericObjectPool<>( - XPathFactory::newInstance); - private static final UnsafeGenericObjectPool TRANSFORMER_FACTORY = - new UnsafeGenericObjectPool<>(TransformerFactory::newInstance); - private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); - private static final UnsafeGenericObjectPool DOCUMENT_BUILDER; + private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); + private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance(); + private static final DocumentBuilder DOCUMENT_BUILDER; static { - DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true); try { - DOCUMENT_BUILDER_FACTORY.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - DOCUMENT_BUILDER_FACTORY.setFeature(EXTERNAL_GENERAL_ENTITIES, false); - DOCUMENT_BUILDER_FACTORY.setFeature(EXTERNAL_PARAMETER_ENTITIES, false); + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setNamespaceAware(true); + documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + documentBuilderFactory.setFeature(EXTERNAL_GENERAL_ENTITIES, false); + documentBuilderFactory.setFeature(EXTERNAL_PARAMETER_ENTITIES, false); + DOCUMENT_BUILDER = documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } - DOCUMENT_BUILDER = new UnsafeGenericObjectPool<>(new BasePooledObjectFactory<>() - { - @Override - public DocumentBuilder create() throws ParserConfigurationException - { - return DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); - } - - @Override - public PooledObject wrap(DocumentBuilder obj) - { - return new DefaultPooledObject<>(obj); - } - }); } private XmlUtils() { } - public static Document convertToDocument(String xml) + public static Document convertToDocument(String xml) throws SAXException, IOException { - return DOCUMENT_BUILDER.apply(documentBuilder -> + synchronized (DOCUMENT_BUILDER) { - try - { - return documentBuilder.parse(createInputSource(xml)); - } - catch (SAXException | IOException e) - { - throw new IllegalStateException(e.getMessage(), e); - } - }); + return DOCUMENT_BUILDER.parse(createInputSource(xml)); + } } /** @@ -119,24 +93,19 @@ public static Document convertToDocument(String xml) * @param xml XML * @param xpath xpath * @return Search result + * @throws XPathExpressionException If an XPath expression error has occurred */ - public static Optional getXmlByXpath(String xml, String xpath) + public static Optional getXmlByXpath(String xml, String xpath) throws XPathExpressionException { - return XPATH_FACTORY.apply(xPathFactory -> { - try - { - InputSource source = createInputSource(xml); - NodeList nodeList = (NodeList) xPathFactory.newXPath().evaluate(xpath, source, XPathConstants.NODESET); - Node singleNode = nodeList.item(0); - Properties outputProperties = new Properties(); - outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, YES); - return transform(new DOMSource(singleNode), outputProperties); - } - catch (XPathExpressionException e) - { - throw new IllegalStateException(e.getMessage(), e); - } - }); + synchronized (XPATH_FACTORY) + { + InputSource source = createInputSource(xml); + NodeList nodeList = (NodeList) XPATH_FACTORY.newXPath().evaluate(xpath, source, XPathConstants.NODESET); + Node singleNode = nodeList.item(0); + Properties outputProperties = new Properties(); + outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, YES); + return transform(new DOMSource(singleNode), outputProperties); + } } public static void validateXmlAgainstXsd(String xml, String xsd) throws SAXException, IOException @@ -152,11 +121,12 @@ public static void transform(String xml, String xslt, Consumer transform { StreamSource xmlSource = createStreamSource(xml); StreamSource xsltSource = createStreamSource(xslt); - TRANSFORMER_FACTORY.accept(transformerFactory -> + + synchronized (TRANSFORMER_FACTORY) { try { - Transformer transformer = transformerFactory.newTransformer(xsltSource); + Transformer transformer = TRANSFORMER_FACTORY.newTransformer(xsltSource); String transformedXml = transform(xmlSource, transformer); transformedXmlConsumer.accept(transformedXml); } @@ -164,7 +134,7 @@ public static void transform(String xml, String xslt, Consumer transform { transformerExceptionConsumer.accept(e); } - }); + } } /** @@ -192,10 +162,11 @@ public static Optional format(String xml) private static Optional transform(Source xmlSource, Properties outputProperties) { - return TRANSFORMER_FACTORY.apply(transformerFactory -> { + synchronized (TRANSFORMER_FACTORY) + { try { - Transformer transformer = transformerFactory.newTransformer(); + Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); transformer.setOutputProperties(outputProperties); String result = transform(xmlSource, transformer); return Optional.of(result); @@ -204,7 +175,7 @@ private static Optional transform(Source xmlSource, Properties outputPro { return Optional.empty(); } - }); + } } private static String transform(Source xmlSource, Transformer transformer) throws TransformerException diff --git a/vividus-plugin-xml/src/test/java/org/vividus/steps/xml/XmlStepsTests.java b/vividus-plugin-xml/src/test/java/org/vividus/steps/xml/XmlStepsTests.java index fe7230b554..8d57d713d7 100644 --- a/vividus-plugin-xml/src/test/java/org/vividus/steps/xml/XmlStepsTests.java +++ b/vividus-plugin-xml/src/test/java/org/vividus/steps/xml/XmlStepsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,11 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; +import java.io.IOException; import java.util.Set; import javax.xml.transform.TransformerException; +import javax.xml.xpath.XPathExpressionException; import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; @@ -37,6 +39,7 @@ import org.vividus.variable.VariableScope; import org.w3c.dom.Document; import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; @ExtendWith(MockitoExtension.class) class XmlStepsTests @@ -54,7 +57,7 @@ class XmlStepsTests private XmlSteps xmlValidationSteps; @Test - void shouldSaveDataByXpathIntoScopeVariable() + void shouldSaveDataByXpathIntoScopeVariable() throws XPathExpressionException { Set scopes = Set.of(VariableScope.STEP); String name = "name"; @@ -64,7 +67,7 @@ void shouldSaveDataByXpathIntoScopeVariable() @Test @SuppressWarnings("unchecked") - void shouldValidateXmlElementExistenceByXpath() + void shouldValidateXmlElementExistenceByXpath() throws SAXException, IOException { Document doc = XmlUtils.convertToDocument(XML); softAssert.assertThat(eq("XML has element with XPath: " + XPATH), eq(doc), any(Matcher.class)); @@ -114,6 +117,20 @@ void shouldRecordFailedAssertionOnTransformationException() verify(softAssert).recordFailedAssertion(any(TransformerException.class)); } + @Test + void shouldRecordFailedAssertionIfXmlIsNotWellFormed() + { + xmlValidationSteps.validateXmlIsWellFormed("xslt test"); + verify(softAssert).recordFailedAssertion(any(SAXParseException.class)); + } + + @Test + void shouldPassIfXMLDocumentIsWellFormed() + { + xmlValidationSteps.validateXmlIsWellFormed(XML); + verify(softAssert).recordPassedAssertion("The XML document is well formed"); + } + private String loadXsd() { return loadResource("test.xsd"); diff --git a/vividus-plugin-xml/src/test/java/org/vividus/util/xml/XmlUtilsTests.java b/vividus-plugin-xml/src/test/java/org/vividus/util/xml/XmlUtilsTests.java index f340e6ce7a..fb1037b779 100644 --- a/vividus-plugin-xml/src/test/java/org/vividus/util/xml/XmlUtilsTests.java +++ b/vividus-plugin-xml/src/test/java/org/vividus/util/xml/XmlUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 the original author or authors. + * Copyright 2019-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import java.util.stream.Stream; import javax.xml.transform.TransformerException; +import javax.xml.xpath.XPathExpressionException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -36,6 +37,7 @@ import org.vividus.util.ResourceUtils; import org.w3c.dom.Document; import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; class XmlUtilsTests { @@ -47,7 +49,7 @@ class XmlUtilsTests + ""; @Test - void shouldReturnXmlByXpath() + void shouldReturnXmlByXpath() throws XPathExpressionException { assertEquals(Optional.of("value1"), XmlUtils.getXmlByXpath(XML, "//data/text()")); } @@ -55,11 +57,11 @@ void shouldReturnXmlByXpath() @Test void shouldThrowExceptionInCaseOfInvalidXpath() { - assertThrows(IllegalStateException.class, () -> XmlUtils.getXmlByXpath(XML, "")); + assertThrows(XPathExpressionException.class, () -> XmlUtils.getXmlByXpath(XML, "")); } @Test - void shouldConvertXmlStringToDocument() + void shouldConvertXmlStringToDocument() throws SAXException, IOException { assertThat(XmlUtils.convertToDocument(XML), instanceOf(Document.class)); } @@ -67,7 +69,7 @@ void shouldConvertXmlStringToDocument() @Test void shouldThrowExceptionInCaseOfInvalidXmlOnConversion() { - assertThrows(IllegalStateException.class, () -> XmlUtils.convertToDocument("")); + assertThrows(SAXParseException.class, () -> XmlUtils.convertToDocument("")); } @Test diff --git a/vividus-util/build.gradle b/vividus-util/build.gradle index 66f1209f26..9eefa79ce9 100644 --- a/vividus-util/build.gradle +++ b/vividus-util/build.gradle @@ -46,7 +46,6 @@ test { } dependencies { - api(group: 'org.apache.commons', name: 'commons-pool2', version: '2.12.0') api(group: 'org.freemarker', name: 'freemarker', version: '2.3.34') api platform(group: 'com.fasterxml.jackson', name: 'jackson-bom', version: '2.18.2') api(group: 'com.fasterxml.jackson.core', name: 'jackson-core') diff --git a/vividus-util/src/main/java/org/vividus/util/pool/SuppliedPooledObjectFactory.java b/vividus-util/src/main/java/org/vividus/util/pool/SuppliedPooledObjectFactory.java deleted file mode 100644 index 34587b1b66..0000000000 --- a/vividus-util/src/main/java/org/vividus/util/pool/SuppliedPooledObjectFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2019-2020 the original author or authors. - * - * Licensed 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 - * - * https://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.vividus.util.pool; - -import java.util.function.Supplier; - -import org.apache.commons.pool2.BasePooledObjectFactory; -import org.apache.commons.pool2.PooledObject; -import org.apache.commons.pool2.impl.DefaultPooledObject; - -public class SuppliedPooledObjectFactory extends BasePooledObjectFactory -{ - private final Supplier supplier; - - public SuppliedPooledObjectFactory(Supplier supplier) - { - this.supplier = supplier; - } - - @Override - public T create() - { - return supplier.get(); - } - - @Override - public PooledObject wrap(T obj) - { - return new DefaultPooledObject<>(obj); - } -} diff --git a/vividus-util/src/main/java/org/vividus/util/pool/UnsafeGenericObjectPool.java b/vividus-util/src/main/java/org/vividus/util/pool/UnsafeGenericObjectPool.java deleted file mode 100644 index ae359a089e..0000000000 --- a/vividus-util/src/main/java/org/vividus/util/pool/UnsafeGenericObjectPool.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2019-2021 the original author or authors. - * - * Licensed 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 - * - * https://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.vividus.util.pool; - -import java.time.Duration; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; - -import org.apache.commons.pool2.PooledObjectFactory; -import org.apache.commons.pool2.impl.GenericObjectPool; - -public class UnsafeGenericObjectPool extends GenericObjectPool -{ - private static final Duration MAX_WAIT_DURATION = Duration.ofMinutes(5); - - public UnsafeGenericObjectPool(PooledObjectFactory factory) - { - super(factory); - setMaxWait(MAX_WAIT_DURATION); - } - - public UnsafeGenericObjectPool(Supplier supplier) - { - this(new SuppliedPooledObjectFactory<>(supplier)); - } - - public R apply(Function function) - { - try - { - T obj = borrowObject(); - try - { - return function.apply(obj); - } - finally - { - returnObject(obj); - } - } - catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception e) - { - throw new IllegalStateException(e); - } - } - - public void accept(Consumer consumer) - { - apply(obj -> - { - consumer.accept(obj); - return null; - }); - } -} diff --git a/vividus-util/src/test/java/org/vividus/util/pool/SuppliedPooledObjectFactoryTests.java b/vividus-util/src/test/java/org/vividus/util/pool/SuppliedPooledObjectFactoryTests.java deleted file mode 100644 index ac275a66c7..0000000000 --- a/vividus-util/src/test/java/org/vividus/util/pool/SuppliedPooledObjectFactoryTests.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2019-2020 the original author or authors. - * - * Licensed 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 - * - * https://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.vividus.util.pool; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.apache.commons.pool2.PooledObject; -import org.apache.commons.pool2.impl.DefaultPooledObject; -import org.junit.jupiter.api.Test; - -class SuppliedPooledObjectFactoryTests -{ - private static final String VALUE = "value"; - - private final SuppliedPooledObjectFactory factory = new SuppliedPooledObjectFactory<>(() -> VALUE); - - @Test - void testCreate() - { - assertEquals(VALUE, factory.create()); - } - - @Test - void testWrap() - { - PooledObject actual = factory.wrap(VALUE); - assertThat(actual, instanceOf(DefaultPooledObject.class)); - assertEquals(VALUE, actual.getObject()); - } -} diff --git a/vividus-util/src/test/java/org/vividus/util/pool/UnsafeGenericObjectPoolTests.java b/vividus-util/src/test/java/org/vividus/util/pool/UnsafeGenericObjectPoolTests.java deleted file mode 100644 index e787d8d9cf..0000000000 --- a/vividus-util/src/test/java/org/vividus/util/pool/UnsafeGenericObjectPoolTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2019-2021 the original author or authors. - * - * Licensed 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 - * - * https://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.vividus.util.pool; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.time.Duration; -import java.util.function.Consumer; - -import org.junit.jupiter.api.Test; - -class UnsafeGenericObjectPoolTests -{ - private static final String VALUE = "value"; - - @Test - void shouldSetMaxWait() - { - testSimplePool(pool -> assertEquals(Duration.ofMinutes(5), pool.getMaxWaitDuration())); - } - - @Test - void testApply() - { - testSimplePool(pool -> assertEquals(VALUE, pool.apply(pooled -> pooled))); - } - - @Test - void testAccept() - { - testSimplePool(pool -> pool.accept(pooled -> assertEquals(VALUE, pooled))); - } - - @Test - void testApplyWithExceptionAtCreation() - { - var exception = new IllegalArgumentException(); - try (var pool = new UnsafeGenericObjectPool<>(() -> { throw exception; })) - { - var actual = assertThrows(IllegalStateException.class, () -> pool.apply(pooled -> pooled)); - assertEquals(exception, actual.getCause()); - } - } - - @Test - void testApplyWithExceptionAtAppliance() - { - testSimplePool(pool -> - { - IllegalArgumentException exception = new IllegalArgumentException(); - var actual = assertThrows(IllegalStateException.class, () -> pool.apply(pooled -> { - throw exception; - })); - assertEquals(exception, actual.getCause()); - }); - } - - private void testSimplePool(Consumer> test) - { - try (var pool = new UnsafeGenericObjectPool<>(() -> VALUE)) - { - test.accept(pool); - } - } -}