diff --git a/californium-core/src/test/java/org/eclipse/californium/core/coap/TestResource.java b/californium-core/src/test/java/org/eclipse/californium/core/coap/TestResource.java new file mode 100644 index 0000000000..f167d77a16 --- /dev/null +++ b/californium-core/src/test/java/org/eclipse/californium/core/coap/TestResource.java @@ -0,0 +1,63 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + ********************************************************************************/ +package org.eclipse.californium.core.coap; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.californium.core.CoapResource; +import org.eclipse.californium.core.network.Exchange; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test resource to handles junit asserts of the scopes of other thread. + * + * Intended to use assert with {@link #handleRequest(Exchange)} execution. + * + * @since 3.11 + */ +public class TestResource extends CoapResource { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestResource.class); + + private List errors = new ArrayList<>(); + + public TestResource(String name) { + super(name); + } + + @Override + public void handleRequest(final Exchange exchange) { + try { + super.handleRequest(exchange); + } catch (Error e) { + LOGGER.warn("{}", e.getMessage(), e); + errors.add(e); + } + } + + /** + * Report errors catched during execution of + * {@link #handleRequest(Exchange)}. + * + * @throw Errors if occurred execution {@link #handleRequest(Exchange)} + */ + public void report() { + if (!errors.isEmpty()) { + throw errors.get(0); + } + } +} diff --git a/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseBertTransferTest.java b/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseBertTransferTest.java index 59d1daea37..86490ee939 100644 --- a/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseBertTransferTest.java +++ b/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseBertTransferTest.java @@ -32,6 +32,7 @@ import org.eclipse.californium.core.coap.BlockOption; import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.coap.Response; +import org.eclipse.californium.core.coap.TestResource; import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.core.network.Endpoint; @@ -268,8 +269,7 @@ public String getProtocol() { serverEndpoint = builder.build(); serverEndpoint.addInterceptor(interceptor); result.addEndpoint(serverEndpoint); - - result.add(new CoapResource(RESOURCE_TEST) { + TestResource testResource = new TestResource(RESOURCE_TEST) { @Override public void handleGET(final CoapExchange exchange) { @@ -285,7 +285,9 @@ public void handlePOST(final CoapExchange exchange) { assertEquals(payload, LONG_POST_REQUEST); exchange.respond(LONG_POST_RESPONSE); } - }); + }; + cleanup.add(testResource); + result.add(testResource); result.add(new CoapResource(RESOURCE_BIG) { @Override diff --git a/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseTransferTest.java b/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseTransferTest.java index bf60d2b5da..bcbce37117 100644 --- a/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseTransferTest.java +++ b/californium-core/src/test/java/org/eclipse/californium/core/test/BlockwiseTransferTest.java @@ -42,6 +42,7 @@ import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.coap.Response; +import org.eclipse.californium.core.coap.TestResource; import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.core.network.Endpoint; import org.eclipse.californium.core.server.resources.CoapExchange; @@ -398,8 +399,7 @@ private static CoapServer createSimpleServer() { builderStrictBlock2.setConfiguration(configEndpointStrictBlock2Option); serverEndpointStrictBlock2Option = builderStrictBlock2.build(); result.addEndpoint(serverEndpointStrictBlock2Option); - - result.add(new CoapResource(RESOURCE_TEST) { + TestResource testResoure = new TestResource(RESOURCE_TEST) { private boolean isShortRequest(final CoapExchange exchange) { return exchange.getQueryParameter(PARAM_SHORT_REQ) != null; @@ -445,7 +445,9 @@ public void handlePOST(final CoapExchange exchange) { exchange.respond(LONG_POST_RESPONSE); } } - }); + }; + cleanup.add(testResoure); + result.add(testResoure); result.add(new CoapResource(RESOURCE_BIG) { @Override diff --git a/californium-core/src/test/java/org/eclipse/californium/core/test/lockstep/BlockwiseServerSideTest.java b/californium-core/src/test/java/org/eclipse/californium/core/test/lockstep/BlockwiseServerSideTest.java index f3e14e882a..5fd35f6958 100644 --- a/californium-core/src/test/java/org/eclipse/californium/core/test/lockstep/BlockwiseServerSideTest.java +++ b/californium-core/src/test/java/org/eclipse/californium/core/test/lockstep/BlockwiseServerSideTest.java @@ -60,11 +60,11 @@ import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.californium.TestTools; -import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.CoapServer; import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.coap.Response; +import org.eclipse.californium.core.coap.TestResource; import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.core.network.UdpMatcher; import org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter; @@ -158,6 +158,7 @@ public void setup() throws Exception { expectedToken = null; testResource = new MyTestResource(RESOURCE_PATH); testResource.setObservable(true); + cleanup.add(testResource); setupServerAndClient(); } @@ -1163,7 +1164,7 @@ public void testObserveWithBlockwiseResponseEarlyNegotiation() throws Exception } // All tests are made with this resource - private class MyTestResource extends CoapResource { + private class MyTestResource extends TestResource { public AtomicInteger calls = new AtomicInteger(); diff --git a/californium-core/src/test/java/org/eclipse/californium/rule/CoapThreadsRule.java b/californium-core/src/test/java/org/eclipse/californium/rule/CoapThreadsRule.java index 413a72a92e..393cc1a21f 100644 --- a/californium-core/src/test/java/org/eclipse/californium/rule/CoapThreadsRule.java +++ b/californium-core/src/test/java/org/eclipse/californium/rule/CoapThreadsRule.java @@ -21,6 +21,7 @@ import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.CoapServer; +import org.eclipse.californium.core.coap.TestResource; import org.eclipse.californium.core.network.Endpoint; import org.eclipse.californium.core.network.EndpointManager; import org.eclipse.californium.core.test.lockstep.LockstepEndpoint; @@ -76,6 +77,10 @@ public void add(LockstepEndpoint endpoint) { cleanup.add(endpoint); } + public void add(TestResource resource) { + cleanup.add(resource); + } + @Override protected void shutdown() { for (Object resource : cleanup) { @@ -93,6 +98,8 @@ protected void shutdown() { ((LockstepEndpoint) resource).destroy(); } else if (resource instanceof Connector) { ((Connector) resource).destroy(); + } else if (resource instanceof TestResource) { + ((TestResource) resource).report(); } } catch (RuntimeException ex) { LOGGER.warn("shutdown failed!", ex);