You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2019/05/24 15:33:18 UTC

[aries-jax-rs-whiteboard] 02/02: [ARIES-1916] Add test

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

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-jax-rs-whiteboard.git

commit daed1608a2118205d9d7f59f3cb66bb131e89051
Author: Carlos Sierra <cs...@apache.org>
AuthorDate: Fri May 24 17:30:35 2019 +0200

    [ARIES-1916] Add test
---
 jax-rs.itests/src/main/java/test/JaxrsTest.java    | 31 ++++++++++++++
 .../main/java/test/types/PerRequestTestFilter.java | 49 ++++++++++++++++++++++
 .../java/test/types/PerRequestTestResource.java    | 36 ++++++++++++++++
 .../src/main/java/test/types/TestHelper.java       | 40 ++++++++++++++++++
 4 files changed, 156 insertions(+)

diff --git a/jax-rs.itests/src/main/java/test/JaxrsTest.java b/jax-rs.itests/src/main/java/test/JaxrsTest.java
index 1f61953..63db600 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -75,6 +75,8 @@ import test.types.ConfigurationAwareResource;
 import test.types.CxfExtensionTestAddon;
 import test.types.ExtensionA;
 import test.types.ExtensionB;
+import test.types.PerRequestTestFilter;
+import test.types.PerRequestTestResource;
 import test.types.SSEResource;
 import test.types.TestAddon;
 import test.types.TestAddonConflict;
@@ -96,6 +98,7 @@ import javax.ws.rs.HttpMethod;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.InvocationCallback;
 import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.container.ContainerRequestFilter;
 import javax.ws.rs.container.ContainerResponseFilter;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Feature;
@@ -1909,6 +1912,34 @@ public class JaxrsTest extends TestHelper {
     }
 
     @Test
+    public void testPerRequestLifecycleAddon() {
+        WebTarget webTarget = createDefaultTarget().path("state");
+
+        assertEquals(0, getRuntimeDTO().applicationDTOs.length);
+        assertEquals(0, getRuntimeDTO().failedApplicationDTOs.length);
+
+        ServiceRegistration<?> serviceRegistration = registerAddon(
+            new PerRequestTestResource());
+
+        assertEquals("original", webTarget.request().get(String.class));
+
+        registerExtension(
+            ContainerRequestFilter.class,
+            new PerRequestTestFilter(), "perrequestfilter");
+
+        assertEquals("original-changed", webTarget.request().get(String.class));
+        assertEquals(
+            "original-changed-changed", webTarget.request().get(String.class));
+
+        serviceRegistration.unregister();
+
+        registerAddonPrototype(PerRequestTestResource::new);
+
+        assertEquals("original-changed", webTarget.request().get(String.class));
+        assertEquals("original-changed", webTarget.request().get(String.class));
+    }
+
+    @Test
     public void testRegisterApplicationWithOnlyExtensions() {
         ServiceRegistration<Application> serviceRegistration =
             registerApplication(new Application() {
diff --git a/jax-rs.itests/src/main/java/test/types/PerRequestTestFilter.java b/jax-rs.itests/src/main/java/test/types/PerRequestTestFilter.java
new file mode 100644
index 0000000..307b3a4
--- /dev/null
+++ b/jax-rs.itests/src/main/java/test/types/PerRequestTestFilter.java
@@ -0,0 +1,49 @@
+/*
+ * 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 test.types;
+
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ResourceContext;
+import javax.ws.rs.core.Context;
+import java.io.IOException;
+
+public class PerRequestTestFilter implements ContainerRequestFilter {
+
+    @Override
+    public void filter(ContainerRequestContext requestContext)
+        throws IOException {
+
+        Class<?> matchedResource = (Class<?>)requestContext.
+            getUriInfo().
+            getMatchedResources().
+            get(0);
+
+        Object resource = _resourceContext.getResource(matchedResource);
+
+        if (resource instanceof PerRequestTestResource) {
+            PerRequestTestResource perRequestTestResource =
+                (PerRequestTestResource) resource;
+
+            perRequestTestResource.setState(perRequestTestResource.getState() + "-changed");
+        }
+    }
+
+    @Context
+    private ResourceContext _resourceContext;
+}
diff --git a/jax-rs.itests/src/main/java/test/types/PerRequestTestResource.java b/jax-rs.itests/src/main/java/test/types/PerRequestTestResource.java
new file mode 100644
index 0000000..ac1f5a6
--- /dev/null
+++ b/jax-rs.itests/src/main/java/test/types/PerRequestTestResource.java
@@ -0,0 +1,36 @@
+/*
+ * 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 test.types;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+public class PerRequestTestResource {
+
+    private String _state = "original";
+
+    @GET
+    @Path("/state")
+    public String getState() {
+        return _state;
+    }
+
+    public void setState(String state) {
+        _state = state;
+    }
+}
diff --git a/jax-rs.itests/src/main/java/test/types/TestHelper.java b/jax-rs.itests/src/main/java/test/types/TestHelper.java
index e4bd084..7dc069f 100644
--- a/jax-rs.itests/src/main/java/test/types/TestHelper.java
+++ b/jax-rs.itests/src/main/java/test/types/TestHelper.java
@@ -53,6 +53,7 @@ import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.function.Supplier;
 
 public class TestHelper {
 
@@ -245,6 +246,45 @@ public class TestHelper {
         return serviceRegistration;
     }
 
+    protected ServiceRegistration<?> registerAddonPrototype(
+        Supplier<?> supplier, Object... keyValues) {
+
+        Dictionary<String, Object> properties = new Hashtable<>();
+
+        properties.put(JAX_RS_RESOURCE, "true");
+
+        for (int i = 0; i < keyValues.length; i = i + 2) {
+            properties.put(keyValues[i].toString(), keyValues[i + 1]);
+        }
+
+        PrototypeServiceFactory<Object> prototypeServiceFactory =
+            new PrototypeServiceFactory<Object>() {
+                @Override
+                public Object getService(
+                    Bundle bundle,
+                    ServiceRegistration<Object> registration) {
+
+                    return supplier.get();
+                }
+
+                @Override
+                public void ungetService(
+                    Bundle bundle, ServiceRegistration<Object> registration,
+                    Object service) {
+
+                }
+            };
+
+        ServiceRegistration<?> serviceRegistration =
+            bundleContext.registerService(
+                Object.class, (ServiceFactory<?>) prototypeServiceFactory,
+                properties);
+
+        _registrations.add(serviceRegistration);
+
+        return serviceRegistration;
+    }
+
     protected ServiceRegistration<?> registerAddonLifecycle(
         boolean singleton, Object... keyValues) {