You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/06/15 12:51:42 UTC

[camel] 03/05: (chores) camel-mina: cleaned up assertion testing for throws/does not throw

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 07b4ef30edfcd4ed7c7d00e5976e811021ec73c5
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Jun 15 11:04:41 2022 +0200

    (chores) camel-mina: cleaned up assertion testing for throws/does not throw
---
 .../camel/component/mina/MinaComponentTest.java    | 29 +++++-----
 .../camel/component/mina/MinaCustomCodecTest.java  | 27 +++++-----
 .../camel/component/mina/MinaEncodingTest.java     | 61 +++++++---------------
 .../mina/MinaExchangeDefaultTimeOutTest.java       | 14 ++---
 .../component/mina/MinaSendToProcessorTest.java    | 39 +++++---------
 5 files changed, 62 insertions(+), 108 deletions(-)

diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaComponentTest.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaComponentTest.java
index 91d77e9608e..9c2a76ac344 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaComponentTest.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaComponentTest.java
@@ -21,8 +21,8 @@ import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 /**
  * For testing various minor holes that hasn't been covered by other unit tests.
@@ -31,24 +31,21 @@ public class MinaComponentTest extends CamelTestSupport {
 
     @Test
     public void testUnknownProtocol() {
-        try {
-            template.sendBody("mina:xxx://localhost:8080", "mina:xxx://localhost:8080");
-            fail("Should have thrown a ResolveEndpointFailedException");
-        } catch (ResolveEndpointFailedException e) {
-            assertTrue(e.getCause() instanceof IllegalArgumentException, "Should be an IAE exception");
-            assertEquals("Unrecognised MINA protocol: xxx for uri: mina://xxx://localhost:8080", e.getCause().getMessage());
-        }
+        Exception e = assertThrows(ResolveEndpointFailedException.class,
+                () -> template.sendBody("mina:xxx://localhost:8080", "mina:xxx://localhost:8080"),
+                "Should have thrown a ResolveEndpointFailedException");
+
+        assertTrue(e.getCause() instanceof IllegalArgumentException, "Should be an IAE exception");
+        assertEquals("Unrecognised MINA protocol: xxx for uri: mina://xxx://localhost:8080", e.getCause().getMessage());
     }
 
     @Test
     public void testMistypedProtocol() {
-        try {
-            // the protocol is mistyped as a colon is missing after tcp
-            template.sendBody("mina:tcp//localhost:8080", "mina:tcp//localhost:8080");
-            fail("Should have thrown a ResolveEndpointFailedException");
-        } catch (ResolveEndpointFailedException e) {
-            assertTrue(e.getCause() instanceof IllegalArgumentException, "Should be an IAE exception");
-            assertEquals("Unrecognised MINA protocol: null for uri: mina://tcp//localhost:8080", e.getCause().getMessage());
-        }
+        Exception e = assertThrows(ResolveEndpointFailedException.class,
+                () -> template.sendBody("mina:tcp//localhost:8080", "mina:tcp//localhost:8080"),
+                "Should have thrown a ResolveEndpointFailedException");
+
+        assertTrue(e.getCause() instanceof IllegalArgumentException, "Should be an IAE exception");
+        assertEquals("Unrecognised MINA protocol: null for uri: mina://tcp//localhost:8080", e.getCause().getMessage());
     }
 }
diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaCustomCodecTest.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaCustomCodecTest.java
index 397dd9406e6..797d88d3376 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaCustomCodecTest.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaCustomCodecTest.java
@@ -35,7 +35,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 /**
  * Unit test with custom codec.
@@ -66,21 +65,19 @@ public class MinaCustomCodecTest extends BaseMinaTest {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
 
-        try {
-            template.requestBody(String.format("mina:tcp://localhost:%1$s?sync=true&codec=#failingCodec", getPort()),
-                    "Hello World");
-            fail("Expecting that decode of result fails");
-        } catch (Exception e) {
-            assertTrue(e instanceof CamelExecutionException);
-            assertNotNull(e.getCause());
-            Throwable rootCause = e;
-            while (rootCause.getCause() != null) {
-                rootCause = rootCause.getCause();
-            }
-            assertTrue(rootCause instanceof IllegalArgumentException);
-            assertTrue(rootCause.getMessage().contains("Something went wrong in decode"));
-        }
+        Exception e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody(String.format("mina:tcp://localhost:%1$s?sync=true&codec=#failingCodec", getPort()),
+                        "Hello World"),
+                "Expecting that decode of result fails");
 
+        assertTrue(e instanceof CamelExecutionException);
+        assertNotNull(e.getCause());
+        Throwable rootCause = e;
+        while (rootCause.getCause() != null) {
+            rootCause = rootCause.getCause();
+        }
+        assertTrue(rootCause instanceof IllegalArgumentException);
+        assertTrue(rootCause.getMessage().contains("Something went wrong in decode"));
     }
 
     @Test
diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaEncodingTest.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaEncodingTest.java
index 937162436d9..188d18c357c 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaEncodingTest.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaEncodingTest.java
@@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  * Unit testing using different encodings with the TCP protocol.
@@ -35,12 +35,7 @@ public class MinaEncodingTest extends BaseMinaTest {
     @Test
     public void testTCPEncodeUTF8InputIsBytes() throws Exception {
         final String uri = String.format("mina:tcp://localhost:%1$s?encoding=UTF-8&sync=false", getPort());
-        context.addRoutes(new RouteBuilder() {
-
-            public void configure() {
-                from(uri).to("mock:result");
-            }
-        });
+        context.addRoutes(getBuilder(uri));
 
         MockEndpoint endpoint = getMockEndpoint("mock:result");
 
@@ -57,12 +52,7 @@ public class MinaEncodingTest extends BaseMinaTest {
     @Test
     public void testTCPEncodeUTF8InputIsString() throws Exception {
         final String uri = String.format("mina:tcp://localhost:%1$s?encoding=UTF-8&sync=false", getPort());
-        context.addRoutes(new RouteBuilder() {
-
-            public void configure() {
-                from(uri).to("mock:result");
-            }
-        });
+        context.addRoutes(getBuilder(uri));
 
         MockEndpoint endpoint = getMockEndpoint("mock:result");
 
@@ -79,12 +69,7 @@ public class MinaEncodingTest extends BaseMinaTest {
     @Test
     public void testTCPEncodeUTF8TextLineInputIsString() throws Exception {
         final String uri = String.format("mina:tcp://localhost:%1$s?textline=true&encoding=UTF-8&sync=false", getPort());
-        context.addRoutes(new RouteBuilder() {
-
-            public void configure() {
-                from(uri).to("mock:result");
-            }
-        });
+        context.addRoutes(getBuilder(uri));
 
         MockEndpoint endpoint = getMockEndpoint("mock:result");
 
@@ -103,12 +88,7 @@ public class MinaEncodingTest extends BaseMinaTest {
     @Test
     public void testUDPEncodeUTF8InputIsBytes() throws Exception {
         final String uri = String.format("mina:udp://localhost:%1$s?encoding=UTF-8&sync=false", getPort());
-        context.addRoutes(new RouteBuilder() {
-
-            public void configure() {
-                from(uri).to("mock:result");
-            }
-        });
+        context.addRoutes(getBuilder(uri));
 
         MockEndpoint endpoint = getMockEndpoint("mock:result");
 
@@ -125,12 +105,7 @@ public class MinaEncodingTest extends BaseMinaTest {
     @Test
     public void testUDPEncodeUTF8InputIsString() throws Exception {
         final String uri = String.format("mina:udp://localhost:%1$s?encoding=UTF-8&sync=false", getPort());
-        context.addRoutes(new RouteBuilder() {
-
-            public void configure() {
-                from(uri).to("mock:result");
-            }
-        });
+        context.addRoutes(getBuilder(uri));
 
         MockEndpoint endpoint = getMockEndpoint("mock:result");
 
@@ -184,17 +159,17 @@ public class MinaEncodingTest extends BaseMinaTest {
     public void testInvalidEncoding() {
         final String uri = String.format("mina:tcp://localhost:%1$s?textline=true&encoding=XXX&sync=false", getPort());
 
-        try {
-            context.addRoutes(new RouteBuilder() {
-
-                public void configure() {
-                    from(uri).to("mock:result");
-                }
-            });
-            fail("Should have thrown a ResolveEndpointFailedException due invalid encoding parameter");
-        } catch (Exception e) {
-            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("The encoding: XXX is not supported", iae.getMessage());
-        }
+        Exception e = assertThrows(Exception.class, () -> context.addRoutes(getBuilder(uri)),
+                "Should have thrown a ResolveEndpointFailedException due invalid encoding parameter");
+        IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertEquals("The encoding: XXX is not supported", iae.getMessage());
+    }
+
+    private RouteBuilder getBuilder(String uri) {
+        return new RouteBuilder() {
+            public void configure() {
+                from(uri).to("mock:result");
+            }
+        };
     }
 }
diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaExchangeDefaultTimeOutTest.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaExchangeDefaultTimeOutTest.java
index c556558d13d..bd5235315b1 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaExchangeDefaultTimeOutTest.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaExchangeDefaultTimeOutTest.java
@@ -16,12 +16,11 @@
  */
 package org.apache.camel.component.mina;
 
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
 
 /**
  * To test timeout.
@@ -30,13 +29,10 @@ public class MinaExchangeDefaultTimeOutTest extends BaseMinaTest {
 
     @Test
     public void testDefaultTimeOut() {
-        try {
-            String result = (String) template
-                    .requestBody(String.format("mina:tcp://localhost:%1$s?textline=true&sync=true", getPort()), "Hello World");
-            assertEquals("Okay I will be faster in the future", result);
-        } catch (RuntimeCamelException e) {
-            fail("Should not get a RuntimeCamelException");
-        }
+        String result = (String) assertDoesNotThrow(() -> template
+                .requestBody(String.format("mina:tcp://localhost:%1$s?textline=true&sync=true", getPort()), "Hello World"),
+                "Should not get a RuntimeCamelException");
+        assertEquals("Okay I will be faster in the future", result);
     }
 
     @Override
diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaSendToProcessorTest.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaSendToProcessorTest.java
index 010d6c162a0..2382470fb47 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaSendToProcessorTest.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaSendToProcessorTest.java
@@ -19,45 +19,34 @@ package org.apache.camel.component.mina;
 import org.apache.camel.builder.RouteBuilder;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class MinaSendToProcessorTest extends BaseMinaTest {
 
-    @Test
-    public void testConnectionOnStartupTest() throws Exception {
-        context.addRoutes(new RouteBuilder() {
+    private RouteBuilder getRouteBuilder(String uri) {
+        return new RouteBuilder() {
 
             @Override
             public void configure() {
                 from("direct:start")
-                        .toF("mina:tcp://localhost:%1$s?sync=false&lazySessionCreation=false", getPort());
+                        .toF(uri, getPort());
             }
-        });
-
-        try {
-            context.start();
-            fail("Should have thrown an exception");
-        } catch (Exception e) {
-            // expected
-        }
+        };
     }
 
     @Test
-    public void testConnectionOnSendMessage() throws Exception {
-        context.addRoutes(new RouteBuilder() {
+    public void testConnectionOnStartupTest() throws Exception {
+        context.addRoutes(getRouteBuilder("mina:tcp://localhost:%1$s?sync=false&lazySessionCreation=false"));
 
-            @Override
-            public void configure() {
-                from("direct:start").toF("mina:tcp://localhost:%1$s?sync=false", getPort());
-            }
-        });
+        assertThrows(Exception.class, () -> context.start(), "Should have thrown an exception");
+    }
 
-        try {
-            context.start();
-        } catch (Exception e) {
-            fail("Should not have thrown an exception");
-        }
+    @Test
+    public void testConnectionOnSendMessage() throws Exception {
+        context.addRoutes(getRouteBuilder("mina:tcp://localhost:%1$s?sync=false"));
 
+        assertDoesNotThrow(() -> context.start(), "Should not have thrown an exception");
     }
 
     @Override