diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/DefaultOgnlCacheFactory.java b/core/src/main/java/com/opensymphony/xwork2/ognl/DefaultOgnlCacheFactory.java index 8bd9a2099..bc14dcd15 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/DefaultOgnlCacheFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/DefaultOgnlCacheFactory.java @@ -32,6 +32,11 @@ public class DefaultOgnlCacheFactory implements OgnlCacheFactory { private final AtomicBoolean useLRUCache = new AtomicBoolean(false); private final AtomicInteger cacheMaxSize = new AtomicInteger(25000); + @Override + public OgnlCache buildOgnlCache() { + return buildOgnlCache(getCacheMaxSize(), 16, 0.75f, getUseLRUCache()); + } + @Override public OgnlCache buildOgnlCache(int evictionLimit, int initialCapacity, float loadFactor, boolean lruCache) { if (lruCache) { diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCacheFactory.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCacheFactory.java index eabb1a955..a3791dac5 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCacheFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCacheFactory.java @@ -23,6 +23,7 @@ package com.opensymphony.xwork2.ognl; * @param The type for the cache value entries */ public interface OgnlCacheFactory { + OgnlCache buildOgnlCache(); OgnlCache buildOgnlCache(int evictionLimit, int initialCapacity, float loadFactor, boolean lruCache); int getCacheMaxSize(); boolean getUseLRUCache(); diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java index 6854926f5..cbcc777ee 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java @@ -80,7 +80,10 @@ public class OgnlUtil { /** * Construct a new OgnlUtil instance for use with the framework + * + * @deprecated It is recommended to utilize the {@link OgnlUtil#OgnlUtil(com.opensymphony.xwork2.ognl.OgnlCacheFactory, com.opensymphony.xwork2.ognl.OgnlCacheFactory) method instead. */ + @Deprecated public OgnlUtil() { this(null, null); // Instantiate default Expression and BeanInfo caches (null factories) } @@ -108,12 +111,12 @@ public class OgnlUtil { this.ognlBeanInfoCacheFactory = ognlBeanInfoCacheFactory; if (ognlExpressionCacheFactory != null) { - this.expressionCache = ognlExpressionCacheFactory.buildOgnlCache(ognlExpressionCacheFactory.getCacheMaxSize(), 16, 0.75f, ognlExpressionCacheFactory.getUseLRUCache()); + this.expressionCache = ognlExpressionCacheFactory.buildOgnlCache(); } else { this.expressionCache = new OgnlDefaultCache<>(25000, 16, 0.75f); } if (ognlBeanInfoCacheFactory != null) { - this.beanInfoCache = ognlBeanInfoCacheFactory.buildOgnlCache(ognlBeanInfoCacheFactory.getCacheMaxSize(), 16, 0.75f, ognlBeanInfoCacheFactory.getUseLRUCache()); + this.beanInfoCache = ognlBeanInfoCacheFactory.buildOgnlCache(); } else { this.beanInfoCache = new OgnlDefaultCache<>(25000, 16, 0.75f); } diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java index 9c250ba51..0fbd83095 100644 --- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java @@ -1856,6 +1856,32 @@ public class OgnlUtilTest extends XWorkTestCase { assertEquals("LRU cache not empty after clear ?", 0, lruCache.size()); } + /** + * Unit test primarily for code coverage + */ + public void testOgnlDefaultCacheFactoryCoverage() { + OgnlCache ognlCache; + DefaultOgnlCacheFactory defaultOgnlCacheFactory = new DefaultOgnlCacheFactory(); + // Normal cache + defaultOgnlCacheFactory.setCacheMaxSize("12"); + defaultOgnlCacheFactory.setUseLRUCache("false"); + ognlCache = defaultOgnlCacheFactory.buildOgnlCache(); + assertNotNull("No param build method result null ?", ognlCache); + assertEquals("Eviction limit for cache mismatches limit for factory ?", 12, ognlCache.getEvictionLimit() ); + ognlCache = defaultOgnlCacheFactory.buildOgnlCache(6, 6, 0.75f, false); + assertNotNull("No param build method result null ?", ognlCache); + assertEquals("Eviction limit for cache mismatches limit for factory ?", 6, ognlCache.getEvictionLimit() ); + // LRU cache + defaultOgnlCacheFactory.setCacheMaxSize("30"); + defaultOgnlCacheFactory.setUseLRUCache("true"); + ognlCache = defaultOgnlCacheFactory.buildOgnlCache(); + assertNotNull("No param build method result null ?", ognlCache); + assertEquals("Eviction limit for cache mismatches limit for factory ?", 30, ognlCache.getEvictionLimit() ); + ognlCache = defaultOgnlCacheFactory.buildOgnlCache(15, 15, 0.75f, false); + assertNotNull("No param build method result null ?", ognlCache); + assertEquals("Eviction limit for cache mismatches limit for factory ?", 15, ognlCache.getEvictionLimit() ); + } + /** * Generate a new OgnlUtil instance (not configured by the {@link ContainerBuilder}) that can be used for * basic tests, with its Expression and BeanInfo factories set to LRU mode. @@ -1867,7 +1893,9 @@ public class OgnlUtilTest extends XWorkTestCase { final DefaultOgnlCacheFactory expressionFactory = new DefaultOgnlExpressionCacheFactory(); final DefaultOgnlCacheFactory beanInfoFactory = new DefaultOgnlBeanInfoCacheFactory, BeanInfo>(); expressionFactory.setUseLRUCache("true"); + expressionFactory.setCacheMaxSize("25"); beanInfoFactory.setUseLRUCache("true"); + beanInfoFactory.setCacheMaxSize("25"); result = new OgnlUtil(expressionFactory, beanInfoFactory); return result; }