You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by GitBox <gi...@apache.org> on 2021/03/09 02:13:03 UTC

[GitHub] [tomee] cesarhernandezgt commented on a change in pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

cesarhernandezgt commented on a change in pull request #765:
URL: https://github.com/apache/tomee/pull/765#discussion_r589886825



##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/resources/arquillian.xml
##########
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+-->
+<arquillian xmlns="http://jboss.org/schema/arquillian"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="
+              http://jboss.org/schema/arquillian
+              http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
+    <container qualifier="tomee" default="true">
+        <configuration>
+            <property name="serverXml">src/main/conf/server.xml</property>
+            <property name="httpPort">8081</property>

Review comment:
       I recall fixing port numbers can lead to concurrency issues during test executions. 
   The port definition in arquillian.xml can be dynamic. 
   
   Example
   ```      <property name="httpPort">-1</property>
         <property name="stopPort">-1</property>
   ```
   https://github.com/apache/tomee/blob/master/examples/mp-custom-healthcheck/src/test/resources/arquillian.xml#L22-L23 
   
   

##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/org/superbiz/websockets/WebSocketResourceTest.java
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.superbiz.websockets;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.RunAsClient;
+import org.jboss.arquillian.junit5.ArquillianExtension;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import javax.websocket.*;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@RunAsClient
+@ExtendWith(ArquillianExtension.class)
+public class WebSocketResourceTest {
+
+    private static final int PORT = 8081;
+
+    @ArquillianResource()
+    private URL base;
+
+    @Deployment(testable = false)
+    public static final WebArchive app() {
+        return ShrinkWrap.create(WebArchive.class, "demo.war")
+                .addClasses(WebSocketResource.class)
+                .addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml"), "web.xml")
+                .addAsWebInfResource(new File("src/main/resources/META-INF/beans.xml"), "beans.xml");
+    }
+
+    @Test
+    public void testConnectAndReceiveMessage() throws Exception {
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    @Test
+    public void testConnectAndSendPayload() throws Exception {
+
+        String payload = "I am the payload sent to this resource.";
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.getBasicRemote().sendText(payload);
+
+        assertTrue(MyWebSocketClientObject.payloadLatch.await(2, TimeUnit.SECONDS));
+        assertEquals("Received: " + payload, MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    /**
+     * Method used to supply connection to the server by passing the naming of
+     * the websocket endpoint
+     */
+    public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
+        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
+        assertNotNull(container);
+        return container.connectToServer(endpoint, new URI("ws", base.getUserInfo(), "localhost", PORT,"/api/socket",null, null));

Review comment:
       Once arquillian dinamicaly allocate an available port (check my other comment) you can obtain the webTarget with `base.toExternalForm()`
   Excample
   https://github.com/apache/tomee/blob/master/examples/mp-custom-healthcheck/src/test/java/org/superbiz/test/WeatherServiceTest.java#L68




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org