You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2021/11/22 19:14:14 UTC
[commons-jcs] 01/07: Fix possible NPE in LateralCache
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit dfe1b4eeda2b71f2fa9f374f2d8e7de962868f2a
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Mon Nov 22 17:46:49 2021 +0100
Fix possible NPE in LateralCache
---
.../jcs3/auxiliary/lateral/LateralCache.java | 10 ++-
.../jcs3/engine/ZombieCacheServiceNonLocal.java | 2 +-
.../lateral/LateralCacheNoWaitFacadeUnitTest.java | 52 +++++---------
.../tcp/LateralTCPDiscoveryListenerUnitTest.java | 81 ++++++++--------------
4 files changed, 54 insertions(+), 91 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCache.java
index 34e9540..0688fde 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCache.java
@@ -77,11 +77,13 @@ public class LateralCache<K, V>
* Constructor for the LateralCache object
* <p>
* @param cattr
+ *
+ * @deprecated Causes NPE
*/
+ @Deprecated
public LateralCache( final ILateralCacheAttributes cattr )
{
- this.cacheName = cattr.getCacheName();
- this.lateralCacheAttributes = cattr;
+ this(cattr, null, null);
}
/**
@@ -240,10 +242,6 @@ public class LateralCache<K, V>
{
log.debug( "Disposing of lateral cache" );
- ///* HELP: This section did nothing but generate compilation warnings.
- // TODO: may limit this functionality. It is dangerous.
- // asmuts -- Added functionality to help with warnings. I'm not getting
- // any.
try
{
lateralCacheService.dispose( this.lateralCacheAttributes.getCacheName() );
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/ZombieCacheServiceNonLocal.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/ZombieCacheServiceNonLocal.java
index 6f6c098..8c16847 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/ZombieCacheServiceNonLocal.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/ZombieCacheServiceNonLocal.java
@@ -69,8 +69,8 @@ public class ZombieCacheServiceNonLocal<K, V>
*/
public ZombieCacheServiceNonLocal( final int maxQueueSize )
{
+ this();
this.maxQueueSize = maxQueueSize;
- queue = new ConcurrentLinkedQueue<>();
}
/**
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheNoWaitFacadeUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheNoWaitFacadeUnitTest.java
index fc4b589..5bc0b19 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheNoWaitFacadeUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheNoWaitFacadeUnitTest.java
@@ -3,7 +3,8 @@ package org.apache.commons.jcs3.auxiliary.lateral;
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.jcs3.auxiliary.lateral.behavior.ILateralCacheAttributes;
+import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes;
+import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -32,20 +33,29 @@ import junit.framework.TestCase;
public class LateralCacheNoWaitFacadeUnitTest
extends TestCase
{
- /**
- * Verify that we can remove an item.
- */
- public void testAddThenRemoveNoWait_InList()
+ private LateralCacheNoWaitFacade<String, String> facade;
+ private LateralCache<String, String> cache;
+
+ @Override
+ protected void setUp() throws Exception
{
// SETUP
List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
+ TCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
cattr.setCacheName( "testCache1" );
+ cattr.setTcpServer("localhost:7890");
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ cache = new LateralCache<>(cattr, new ZombieCacheServiceNonLocal<>(), null);
+ }
+
+ /**
+ * Verify that we can remove an item.
+ */
+ public void testAddThenRemoveNoWait_InList()
+ {
+ LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
// DO WORK
facade.addNoWait( noWait );
@@ -66,14 +76,6 @@ public class LateralCacheNoWaitFacadeUnitTest
*/
public void testAddThenRemoveNoWait_InListSize2()
{
- // SETUP
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( "testCache1" );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
-
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
noWait.setIdentityKey("1234");
final LateralCacheNoWait<String, String> noWait2 = new LateralCacheNoWait<>( cache );
@@ -102,14 +104,6 @@ public class LateralCacheNoWaitFacadeUnitTest
*/
public void testAdd_InList()
{
- // SETUP
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( "testCache1" );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
-
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
// DO WORK
@@ -126,14 +120,6 @@ public class LateralCacheNoWaitFacadeUnitTest
*/
public void testAddThenRemoveNoWait_NotInList()
{
- // SETUP
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( "testCache1" );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
-
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
// DO WORK
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerUnitTest.java
index be7615d..4a2a60f 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerUnitTest.java
@@ -4,11 +4,10 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.jcs3.auxiliary.lateral.LateralCache;
-import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheAttributes;
import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheNoWait;
import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheNoWaitFacade;
-import org.apache.commons.jcs3.auxiliary.lateral.behavior.ILateralCacheAttributes;
import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
+import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
import org.apache.commons.jcs3.engine.control.CompositeCacheManager;
import org.apache.commons.jcs3.engine.logging.MockCacheEventLogger;
@@ -70,6 +69,24 @@ public class LateralTCPDiscoveryListenerUnitTest
cacheEventLogger, elementSerializer );
}
+ private LateralCacheNoWaitFacade<String, String> setupFacade(final String cacheName)
+ {
+ List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
+ final ITCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
+ cattr.setCacheName( cacheName );
+
+ return new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ }
+
+ private LateralCacheNoWait<String, String> setupNoWait(final String cacheName)
+ {
+ final ITCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
+ cattr.setCacheName( cacheName );
+
+ final LateralCache<String, String> cache = new LateralCache<>(cattr, new ZombieCacheServiceNonLocal<>(), null);
+ return new LateralCacheNoWait<>( cache );
+ }
+
/**
* Add a no wait facade.
*/
@@ -77,12 +94,7 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testAddNoWaitFacade_NotInList";
-
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
// DO WORK
listener.addNoWaitFacade( cacheName, facade );
@@ -98,16 +110,10 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testAddNoWaitFacade_FacadeInList";
-
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
listener.addNoWaitFacade( cacheName, facade );
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
// DO WORK
final boolean result = listener.addNoWait( noWait );
@@ -123,11 +129,7 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testAddNoWaitFacade_FacadeInList";
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
// DO WORK
final boolean result = listener.addNoWait( noWait );
@@ -143,11 +145,7 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testRemoveNoWaitFacade_FacadeNotInList";
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
// DO WORK
final boolean result = listener.removeNoWait( noWait );
@@ -163,16 +161,10 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testAddNoWaitFacade_FacadeInList";
-
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
listener.addNoWaitFacade( cacheName, facade );
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
// DO WORK
final boolean result = listener.removeNoWait( noWait );
@@ -188,16 +180,10 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testRemoveNoWaitFacade_FacadeInListNoWaitIs";
-
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new LateralCacheAttributes();
- cattr.setCacheName( cacheName );
-
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
listener.addNoWaitFacade( cacheName, facade );
- final LateralCache<String, String> cache = new LateralCache<>( cattr );
- final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
+ final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
listener.addNoWait( noWait );
// DO WORK
@@ -214,7 +200,6 @@ public class LateralTCPDiscoveryListenerUnitTest
{
// SETUP
final String cacheName = "testAddDiscoveredService_FacadeInList_NoWaitNot";
-
final ArrayList<String> cacheNames = new ArrayList<>();
cacheNames.add( cacheName );
@@ -232,10 +217,7 @@ public class LateralTCPDiscoveryListenerUnitTest
cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
cacheMgr.registryFacPut(factory);
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new TCPLateralCacheAttributes();
- cattr.setCacheName( cacheName );
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
listener.addNoWaitFacade( cacheName, facade );
// DO WORK
@@ -270,10 +252,7 @@ public class LateralTCPDiscoveryListenerUnitTest
cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
cacheMgr.registryFacPut(factory);
- List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
- final ILateralCacheAttributes cattr = new TCPLateralCacheAttributes();
- cattr.setCacheName( cacheName );
- final LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
+ final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
listener.addNoWaitFacade( cacheName, facade );
listener.addDiscoveredService( service );