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

coheigea pushed a commit to branch CXF-7594
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/CXF-7594 by this push:
     new 313b5a8  Fixing systests CachingTest
313b5a8 is described below

commit 313b5a84c1e63e39bede610aff14d50c1e1c0758
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Apr 10 08:31:06 2020 +0100

    Fixing systests CachingTest
---
 .../ws/security/tokenstore/EHCacheTokenStore.java  |  1 +
 .../ws/security/tokenstore/TokenStoreUtils.java    |  3 +-
 .../cxf/sts/cache/EhCacheIdentityCacheTest.java    |  1 +
 .../cxf/systest/sts/caching/CachingTest.java       |  2 -
 .../cxf/systest/sts/caching/ServerCachingTest.java |  2 -
 .../apache/cxf/systest/ws/cache/CachingTest.java   | 89 +++++++++++++++++++---
 .../cxf/systest/ws/cache/per-proxy-cache.xml       | 11 ---
 7 files changed, 83 insertions(+), 26 deletions(-)

diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
index 4ef585c..2d95ea4 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
@@ -58,6 +58,7 @@ public class EHCacheTokenStore implements TokenStore, 
Closeable, BusLifeCycleLis
         try {
             XmlConfiguration xmlConfig = new XmlConfiguration(configFileURL);
 
+            // Exclude the endpoint info bit added in TokenStoreUtils when 
getting the template name
             String template = key;
             if (template.contains("-")) {
                 template = key.substring(0, key.lastIndexOf('-'));
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreUtils.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreUtils.java
index 85e88f3..438e5e7 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreUtils.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreUtils.java
@@ -46,7 +46,8 @@ public final class TokenStoreUtils {
                     
(String)message.getContextualProperty(SecurityConstants.CACHE_IDENTIFIER);
                 if (cacheIdentifier != null) {
                     cacheKey.append('-').append(cacheIdentifier);
-                } else if (info.getName() != null) {
+                }
+                if (info.getName() != null) {
                     int hashcode = info.getName().toString().hashCode();
                     if (hashcode >= 0) {
                         cacheKey.append('-');
diff --git 
a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/EhCacheIdentityCacheTest.java
 
b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/EhCacheIdentityCacheTest.java
index e7948c9..d8eb54c 100644
--- 
a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/EhCacheIdentityCacheTest.java
+++ 
b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/EhCacheIdentityCacheTest.java
@@ -24,6 +24,7 @@ import org.apache.cxf.sts.IdentityMapper;
 
 import org.junit.BeforeClass;
 
[email protected]
 public class EhCacheIdentityCacheTest extends MemoryIdentityCacheTest {
 
     @BeforeClass
diff --git 
a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java
 
b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java
index 140386e..1620810 100644
--- 
a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java
+++ 
b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java
@@ -84,9 +84,7 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
         stopAllServers();
     }
 
-    // @Ignore'd because failing too often on slow Jenkins machines
     @org.junit.Test
-    @org.junit.Ignore
     public void testSTSClientCaching() throws Exception {
 
         SpringBusFactory bf = new SpringBusFactory();
diff --git 
a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/ServerCachingTest.java
 
b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/ServerCachingTest.java
index dab4481..c0b3853 100644
--- 
a/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/ServerCachingTest.java
+++ 
b/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/ServerCachingTest.java
@@ -93,9 +93,7 @@ public class ServerCachingTest extends 
AbstractBusClientServerTestBase {
         stopAllServers();
     }
 
-    // Disabled due to continually failing on Jenkins build
     @org.junit.Test
-    @org.junit.Ignore
     public void testServerSideSAMLTokenCaching() throws Exception {
 
         SpringBusFactory bf = new SpringBusFactory();
diff --git 
a/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/cache/CachingTest.java
 
b/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/cache/CachingTest.java
index 21e3f14..b45c1fe 100644
--- 
a/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/cache/CachingTest.java
+++ 
b/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/cache/CachingTest.java
@@ -22,6 +22,7 @@ package org.apache.cxf.systest.ws.cache;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Random;
 
 import javax.xml.namespace.QName;
 import javax.xml.ws.BindingProvider;
@@ -30,12 +31,14 @@ import javax.xml.ws.Service;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.frontend.ClientProxy;
 import org.apache.cxf.systest.ws.common.SecurityTestUtil;
 import org.apache.cxf.systest.ws.common.TestParam;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.ws.security.SecurityConstants;
+import org.apache.cxf.ws.security.tokenstore.EHCacheTokenStore;
 import org.apache.cxf.ws.security.tokenstore.TokenStore;
 import org.example.contract.doubleit.DoubleItPortType;
 
@@ -45,7 +48,6 @@ import org.junit.runners.Parameterized.Parameters;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -88,6 +90,7 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
         stopAllServers();
     }
 
+    // By default, we have one cache per-proxy
     @org.junit.Test
     public void testSymmetric() throws Exception {
 
@@ -122,7 +125,6 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
         // We expect two tokens as the identifier + SHA-1 are cached
         assertEquals(2, tokenStore.getTokenIdentifiers().size());
 
-
         // Second invocation
         DoubleItPortType port2 = service.getPort(portQName, 
DoubleItPortType.class);
         updateAddressPort(port2, test.getPort());
@@ -140,20 +142,85 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
             );
 
         assertNotNull(tokenStore);
-        // There should now be 4 tokens as both proxies share the same 
TokenStore
-        assertEquals(4, tokenStore.getTokenIdentifiers().size());
+        // We expect two tokens as the identifier + SHA-1 are cached
+        assertEquals(2, tokenStore.getTokenIdentifiers().size());
 
         ((java.io.Closeable)port).close();
-        //port2 is still holding onto the cache, thus, this should still be 4
+        //port2 is still holding onto the cache, thus, this should still be 2
+        assertEquals(2, tokenStore.getTokenIdentifiers().size());
+        ((java.io.Closeable)port2).close();
+
+        bus.shutdown(true);
+    }
+
+    // Here we manually create a cache and share it for both proxies
+    @org.junit.Test
+    public void testSymmetricSharedCache() throws Exception {
+
+        SpringBusFactory bf = new SpringBusFactory();
+        URL busFile = CachingTest.class.getResource("client.xml");
+
+        Bus bus = bf.createBus(busFile.toString());
+        BusFactory.setDefaultBus(bus);
+        BusFactory.setThreadDefaultBus(bus);
+
+        URL wsdl = CachingTest.class.getResource("DoubleItCache.wsdl");
+        Service service = Service.create(wsdl, SERVICE_QNAME);
+        QName portQName = new QName(NAMESPACE, "DoubleItCacheSymmetricPort");
+
+        // First invocation
+        DoubleItPortType port =
+                service.getPort(portQName, DoubleItPortType.class);
+        updateAddressPort(port, test.getPort());
+
+        // Create shared cache
+        String cacheKey = SecurityConstants.TOKEN_STORE_CACHE_INSTANCE + '-' + 
Math.abs(new Random().nextInt());
+        TokenStore tokenStore = new EHCacheTokenStore(cacheKey, bus,
+                ClassLoaderUtils.getResource("cxf-ehcache.xml", 
this.getClass()));
+        Client client = ClientProxy.getClient(port);
+        
client.getEndpoint().getEndpointInfo().setProperty(SecurityConstants.TOKEN_STORE_CACHE_INSTANCE,
 tokenStore);
+
+        if (test.isStreaming()) {
+            SecurityTestUtil.enableStreaming(port);
+        }
+
+        assertEquals(50, port.doubleIt(25));
+
+        // We expect two tokens as the identifier + SHA-1 are cached
+        assertEquals(2, tokenStore.getTokenIdentifiers().size());
+
+        // Second invocation
+        DoubleItPortType port2 = service.getPort(portQName, 
DoubleItPortType.class);
+        updateAddressPort(port2, test.getPort());
+
+        client = ClientProxy.getClient(port2);
+        
client.getEndpoint().getEndpointInfo().setProperty(SecurityConstants.TOKEN_STORE_CACHE_INSTANCE,
 tokenStore);
+
+        if (test.isStreaming()) {
+            SecurityTestUtil.enableStreaming(port2);
+        }
+
+        port2.doubleIt(35);
+
+        client = ClientProxy.getClient(port2);
+        tokenStore =
+                (TokenStore)client.getEndpoint().getEndpointInfo().getProperty(
+                        SecurityConstants.TOKEN_STORE_CACHE_INSTANCE
+                );
+
+        assertNotNull(tokenStore);
+        // We expect four tokens as the identifier + SHA-1 are cached
         assertEquals(4, tokenStore.getTokenIdentifiers().size());
+
+        ((java.io.Closeable)port).close();
         ((java.io.Closeable)port2).close();
-        //port2 is now closed, this should be null
-        assertNull(tokenStore.getTokenIdentifiers());
+
         bus.shutdown(true);
     }
 
+    // Here we supply custom caching configuration
     @org.junit.Test
-    public void testCachePerProxySymmetric() throws Exception {
+    public void testSymmetricCustom() throws Exception {
 
         SpringBusFactory bf = new SpringBusFactory();
         URL busFile = CachingTest.class.getResource("client.xml");
@@ -175,7 +242,8 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
             SecurityConstants.CACHE_IDENTIFIER, "proxy1"
         );
         ((BindingProvider)port).getRequestContext().put(
-            SecurityConstants.CACHE_CONFIG_FILE, "per-proxy-cache.xml"
+            SecurityConstants.CACHE_CONFIG_FILE,
+                ClassLoaderUtils.getResource("per-proxy-cache.xml", 
this.getClass())
         );
 
         if (test.isStreaming()) {
@@ -201,7 +269,8 @@ public class CachingTest extends 
AbstractBusClientServerTestBase {
             SecurityConstants.CACHE_IDENTIFIER, "proxy2"
         );
         ((BindingProvider)port2).getRequestContext().put(
-            SecurityConstants.CACHE_CONFIG_FILE, "per-proxy-cache.xml"
+            SecurityConstants.CACHE_CONFIG_FILE,
+                ClassLoaderUtils.getResource("per-proxy-cache.xml", 
this.getClass())
         );
 
         if (test.isStreaming()) {
diff --git 
a/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/cache/per-proxy-cache.xml
 
b/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/cache/per-proxy-cache.xml
index 7c7327d..dcbf079 100644
--- 
a/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/cache/per-proxy-cache.xml
+++ 
b/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/cache/per-proxy-cache.xml
@@ -1,5 +1,4 @@
 <?xml version="1.0"?>
-<?xml version="1.0"?>
 <config
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xmlns='http://www.ehcache.org/v3'
@@ -23,16 +22,6 @@
          cache is per-endpoint so not "too large" to not consume the
          entire heap.
     -->
-    <cache-template name="org.apache.cxf.ws.security.tokenstore.TokenStore">
-        <key-type>java.lang.String</key-type>
-        
<value-type>org.apache.cxf.ws.security.tokenstore.SecurityToken</value-type>
-        <expiry>
-            <ttl unit="seconds">3600</ttl>
-        </expiry>
-        <resources>
-            <heap unit="entries">10000</heap>
-        </resources>
-    </cache-template>
 
     <cache-template 
name="org.apache.cxf.ws.security.tokenstore.TokenStore-proxy1">
         <key-type>java.lang.String</key-type>

Reply via email to