This is an automated email from the ASF dual-hosted git repository. khowe pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git
commit c855209c98852249caf1013dbcf2a801ea8768a1 Author: Ken Howe <kh...@pivotal.io> AuthorDate: Thu Oct 12 13:34:17 2017 -0700 GEODE-3299: refactor functions to get Cache from FunctionContext This is the 4th set of updates. --- .../internal/cli/commands/CreateRegionCommand.java | 2 +- .../internal/cli/commands/GetCommand.java | 3 +- .../internal/cli/commands/PutCommand.java | 2 +- .../internal/cli/commands/RemoveCommand.java | 2 +- .../cli/functions/DataCommandFunction.java | 62 ++++++++---------- .../functions/FetchRegionAttributesFunction.java | 7 +- .../cli/functions/ListDiskStoresFunction.java | 7 +- .../internal/cli/functions/ListIndexFunction.java | 7 +- .../functions/DataCommandFunctionJUnitTest.java | 8 ++- .../DataCommandFunctionWithPDXJUnitTest.java | 7 +- .../internal/cli/functions/Geode3544JUnitTest.java | 3 +- .../functions/ListDiskStoresFunctionJUnitTest.java | 75 +++++++--------------- .../cli/functions/ListIndexFunctionJUnitTest.java | 31 +++------ .../internal/cli/result/CommandResultTest.java | 18 ++---- 14 files changed, 87 insertions(+), 147 deletions(-) diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateRegionCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateRegionCommand.java index 708c3f1..99c5d0b 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateRegionCommand.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/CreateRegionCommand.java @@ -568,7 +568,7 @@ public class CreateRegionCommand implements GfshCommand { // First check whether the region exists on a this manager, if yes then no // need to use FetchRegionAttributesFunction to fetch RegionAttributes try { - attributes = FetchRegionAttributesFunction.getRegionAttributes(regionPath); + attributes = FetchRegionAttributesFunction.getRegionAttributes(cache, regionPath); } catch (IllegalArgumentException e) { /* region doesn't exist on the manager */ } diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GetCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GetCommand.java index 0ccdfe0..ed7ab26 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GetCommand.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GetCommand.java @@ -84,8 +84,7 @@ public class GetCommand implements GfshCommand { false); } } else { - dataResult = getfn.get(null, key, keyClass, valueClass, regionPath, loadOnCacheMiss, - cache.getSecurityService()); + dataResult = getfn.get(null, key, keyClass, valueClass, regionPath, loadOnCacheMiss, cache); } dataResult.setKeyClass(keyClass); if (valueClass != null) { diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PutCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PutCommand.java index a2e1903..b20aeb1 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PutCommand.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PutCommand.java @@ -81,7 +81,7 @@ public class PutCommand implements GfshCommand { false); } } else { - dataResult = putfn.put(key, value, putIfAbsent, keyClass, valueClass, regionPath); + dataResult = putfn.put(key, value, putIfAbsent, keyClass, valueClass, regionPath, cache); } dataResult.setKeyClass(keyClass); if (valueClass != null) { diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/RemoveCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/RemoveCommand.java index fa5f4b9..1318cdf 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/RemoveCommand.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/RemoveCommand.java @@ -84,7 +84,7 @@ public class RemoveCommand implements GfshCommand { request.setRegionName(regionPath); dataResult = callFunctionForRegion(request, removefn, memberList); } else { - dataResult = removefn.remove(key, keyClass, regionPath, removeAllKeys ? "ALL" : null); + dataResult = removefn.remove(key, keyClass, regionPath, removeAllKeys ? "ALL" : null, cache); } dataResult.setKeyClass(keyClass); diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java index 82a7adf..c8392e4 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java @@ -26,7 +26,6 @@ import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.Logger; import org.json.JSONArray; -import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.DataPolicy; import org.apache.geode.cache.Region; import org.apache.geode.cache.execute.FunctionAdapter; @@ -106,7 +105,7 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti @Override public void execute(FunctionContext functionContext) { try { - InternalCache cache = getCache(); + InternalCache cache = (InternalCache) functionContext.getCache(); DataCommandRequest request = (DataCommandRequest) functionContext.getArguments(); if (logger.isDebugEnabled()) { logger.debug("Executing function : \n{}\n on member {}", request, @@ -114,15 +113,15 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti } DataCommandResult result = null; if (request.isGet()) { - result = get(request, cache.getSecurityService()); + result = get(request, cache); } else if (request.isLocateEntry()) { - result = locateEntry(request); + result = locateEntry(request, cache); } else if (request.isPut()) { - result = put(request); + result = put(request, cache); } else if (request.isRemove()) { - result = remove(request); + result = remove(request, cache); } else if (request.isSelect()) { - result = select(request); + result = select(request, cache); } if (logger.isDebugEnabled()) { logger.debug("Result is {}", result); @@ -135,50 +134,46 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti } - private InternalCache getCache() { - return (InternalCache) CacheFactory.getAnyInstance(); - } - - public DataCommandResult remove(DataCommandRequest request) { + public DataCommandResult remove(DataCommandRequest request, InternalCache cache) { String key = request.getKey(); String keyClass = request.getKeyClass(); String regionName = request.getRegionName(); String removeAllKeys = request.getRemoveAllKeys(); - return remove(key, keyClass, regionName, removeAllKeys); + return remove(key, keyClass, regionName, removeAllKeys, cache); } - public DataCommandResult get(DataCommandRequest request, SecurityService securityService) { + public DataCommandResult get(DataCommandRequest request, InternalCache cache) { String key = request.getKey(); String keyClass = request.getKeyClass(); String valueClass = request.getValueClass(); String regionName = request.getRegionName(); Boolean loadOnCacheMiss = request.isLoadOnCacheMiss(); return get(request.getPrincipal(), key, keyClass, valueClass, regionName, loadOnCacheMiss, - securityService); + cache); } - public DataCommandResult locateEntry(DataCommandRequest request) { + public DataCommandResult locateEntry(DataCommandRequest request, InternalCache cache) { String key = request.getKey(); String keyClass = request.getKeyClass(); String valueClass = request.getValueClass(); String regionName = request.getRegionName(); boolean recursive = request.isRecursive(); - return locateEntry(key, keyClass, valueClass, regionName, recursive); + return locateEntry(key, keyClass, valueClass, regionName, recursive, cache); } - public DataCommandResult put(DataCommandRequest request) { + public DataCommandResult put(DataCommandRequest request, InternalCache cache) { String key = request.getKey(); String value = request.getValue(); boolean putIfAbsent = request.isPutIfAbsent(); String keyClass = request.getKeyClass(); String valueClass = request.getValueClass(); String regionName = request.getRegionName(); - return put(key, value, putIfAbsent, keyClass, valueClass, regionName); + return put(key, value, putIfAbsent, keyClass, valueClass, regionName, cache); } - public DataCommandResult select(DataCommandRequest request) { + public DataCommandResult select(DataCommandRequest request, InternalCache cache) { String query = request.getQuery(); - return select(request.getPrincipal(), query); + return select(cache, request.getPrincipal(), query); } /** @@ -197,9 +192,8 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti } @SuppressWarnings("rawtypes") - private DataCommandResult select(Object principal, String queryString) { + private DataCommandResult select(InternalCache cache, Object principal, String queryString) { - InternalCache cache = getCache(); AtomicInteger nestedObjectCount = new AtomicInteger(0); if (StringUtils.isEmpty(queryString)) { return DataCommandResult.createSelectInfoResult(null, null, -1, null, @@ -227,7 +221,7 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti queryObserver.reset2(); } if (results instanceof SelectResults) { - select_SelectResults((SelectResults) results, principal, list, nestedObjectCount); + select_SelectResults((SelectResults) results, principal, list, nestedObjectCount, cache); } else { select_NonSelectResults(results, list); } @@ -270,10 +264,11 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti } private void select_SelectResults(SelectResults selectResults, Object principal, - List<SelectResultRow> list, AtomicInteger nestedObjectCount) throws GfJsonException { + List<SelectResultRow> list, AtomicInteger nestedObjectCount, InternalCache cache) + throws GfJsonException { for (Object object : selectResults) { // Post processing - object = getCache().getSecurityService().postProcess(principal, null, null, object, false); + object = cache.getSecurityService().postProcess(principal, null, null, object, false); if (object instanceof Struct) { StructImpl impl = (StructImpl) object; @@ -344,9 +339,7 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti @SuppressWarnings({"rawtypes"}) public DataCommandResult remove(String key, String keyClass, String regionName, - String removeAllKeys) { - - InternalCache cache = getCache(); + String removeAllKeys, InternalCache cache) { if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createRemoveResult(key, null, null, @@ -410,9 +403,9 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti @SuppressWarnings({"rawtypes"}) public DataCommandResult get(Object principal, String key, String keyClass, String valueClass, - String regionName, Boolean loadOnCacheMiss, SecurityService securityService) { + String regionName, Boolean loadOnCacheMiss, InternalCache cache) { - InternalCache cache = getCache(); + SecurityService securityService = cache.getSecurityService(); if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createGetResult(key, null, null, @@ -480,9 +473,7 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti @SuppressWarnings({"unchecked", "rawtypes"}) public DataCommandResult locateEntry(String key, String keyClass, String valueClass, - String regionPath, boolean recursive) { - - InternalCache cache = getCache(); + String regionPath, boolean recursive, InternalCache cache) { if (StringUtils.isEmpty(regionPath)) { return DataCommandResult.createLocateEntryResult(key, null, null, @@ -596,7 +587,7 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti @SuppressWarnings({"rawtypes"}) public DataCommandResult put(String key, String value, boolean putIfAbsent, String keyClass, - String valueClass, String regionName) { + String valueClass, String regionName, InternalCache cache) { if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createPutResult(key, null, null, @@ -613,7 +604,6 @@ public class DataCommandFunction extends FunctionAdapter implements InternalEnti false); } - InternalCache cache = getCache(); Region region = cache.getRegion(regionName); if (region == null) { return DataCommandResult.createPutResult(key, null, null, diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/FetchRegionAttributesFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/FetchRegionAttributesFunction.java index 1173568..5ff5a81 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/FetchRegionAttributesFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/FetchRegionAttributesFunction.java @@ -20,7 +20,6 @@ import org.apache.logging.log4j.Logger; import org.apache.geode.cache.AttributesFactory; import org.apache.geode.cache.Cache; -import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.CacheListener; import org.apache.geode.cache.CacheLoader; import org.apache.geode.cache.CacheWriter; @@ -52,12 +51,13 @@ public class FetchRegionAttributesFunction extends FunctionAdapter { @Override public void execute(FunctionContext context) { try { + Cache cache = context.getCache(); String regionPath = (String) context.getArguments(); if (regionPath == null) { throw new IllegalArgumentException( CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_REGION_PATH); } - FetchRegionAttributesFunctionResult<?, ?> result = getRegionAttributes(regionPath); + FetchRegionAttributesFunctionResult<?, ?> result = getRegionAttributes(cache, regionPath); context.getResultSender().lastResult(result); } catch (IllegalArgumentException e) { if (logger.isDebugEnabled()) { @@ -68,9 +68,8 @@ public class FetchRegionAttributesFunction extends FunctionAdapter { } @SuppressWarnings("deprecation") - public static <K, V> FetchRegionAttributesFunctionResult<K, V> getRegionAttributes( + public static <K, V> FetchRegionAttributesFunctionResult<K, V> getRegionAttributes(Cache cache, String regionPath) { - Cache cache = CacheFactory.getAnyInstance(); Region<K, V> foundRegion = cache.getRegion(regionPath); if (foundRegion == null) { diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunction.java index 80c84b5..c98f585 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunction.java @@ -20,7 +20,6 @@ import java.util.Properties; import java.util.Set; import org.apache.geode.cache.Cache; -import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.DiskStore; import org.apache.geode.cache.execute.FunctionAdapter; import org.apache.geode.cache.execute.FunctionContext; @@ -52,15 +51,11 @@ public class ListDiskStoresFunction extends FunctionAdapter implements InternalE return getClass().getName(); } - protected Cache getCache() { - return CacheFactory.getAnyInstance(); - } - public void execute(final FunctionContext context) { final Set<DiskStoreDetails> memberDiskStores = new HashSet<DiskStoreDetails>(); try { - final Cache cache = getCache(); + final Cache cache = context.getCache(); if (cache instanceof InternalCache) { final InternalCache gemfireCache = (InternalCache) cache; diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListIndexFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListIndexFunction.java index f8d302c..0a44c76 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListIndexFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/ListIndexFunction.java @@ -19,7 +19,6 @@ import java.util.HashSet; import java.util.Set; import org.apache.geode.cache.Cache; -import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.execute.FunctionAdapter; import org.apache.geode.cache.execute.FunctionContext; import org.apache.geode.cache.query.Index; @@ -43,10 +42,6 @@ import org.apache.geode.management.internal.cli.domain.IndexDetails; @SuppressWarnings("unused") public class ListIndexFunction extends FunctionAdapter implements InternalEntity { - protected Cache getCache() { - return CacheFactory.getAnyInstance(); - } - public String getId() { return ListIndexFunction.class.getName(); } @@ -55,7 +50,7 @@ public class ListIndexFunction extends FunctionAdapter implements InternalEntity try { final Set<IndexDetails> indexDetailsSet = new HashSet<IndexDetails>(); - final Cache cache = getCache(); + final Cache cache = context.getCache(); final DistributedMember member = cache.getDistributedSystem().getDistributedMember(); for (final Index index : cache.getQueryService().getIndexes()) { diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionJUnitTest.java index 5323258..b2d65f0 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionJUnitTest.java @@ -15,6 +15,7 @@ package org.apache.geode.management.internal.cli.functions; import org.apache.geode.cache.*; +import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.management.internal.cli.domain.DataCommandResult; import org.apache.geode.test.junit.categories.IntegrationTest; import org.junit.AfterClass; @@ -101,8 +102,9 @@ public class DataCommandFunctionJUnitTest { public void testLocateKeyIsObject() throws Exception { DataCommandFunction dataCmdFn = new DataCommandFunction(); - DataCommandResult result = dataCmdFn.locateEntry("{'cheese': 'key_1'}", - StringCheese.class.getName(), String.class.getName(), PARTITIONED_REGION, false); + DataCommandResult result = + dataCmdFn.locateEntry("{'cheese': 'key_1'}", StringCheese.class.getName(), + String.class.getName(), PARTITIONED_REGION, false, (InternalCache) cache); assertNotNull(result); result.aggregate(null); @@ -115,7 +117,7 @@ public class DataCommandFunctionJUnitTest { DataCommandFunction dataCmdFn = new DataCommandFunction(); DataCommandResult result = dataCmdFn.locateEntry("key_2", String.class.getName(), - String.class.getName(), PARTITIONED_REGION, false); + String.class.getName(), PARTITIONED_REGION, false, (InternalCache) cache); assertNotNull(result); result.aggregate(null); diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java index c26a931..64cc18a 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java @@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionShortcut; +import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.management.internal.cli.domain.DataCommandRequest; import org.apache.geode.management.internal.cli.domain.DataCommandResult; import org.apache.geode.management.internal.cli.json.GfJsonArray; @@ -49,6 +50,7 @@ public class DataCommandFunctionWithPDXJUnitTest { private Customer bob; private CustomerWithPhone charlie; private CustomerWithPhone dan; + private InternalCache cache; @Before public void setup() { @@ -57,7 +59,8 @@ public class DataCommandFunctionWithPDXJUnitTest { charlie = new CustomerWithPhone("2", "Charlie", "Chaplin", "(222) 222-2222"); dan = new CustomerWithPhone("3", "Dan", "Dickinson", "(333) 333-3333"); - Region region = server.getCache().getRegion(PARTITIONED_REGION); + cache = server.getCache(); + Region region = cache.getRegion(PARTITIONED_REGION); region.put(0, alice); region.put(1, bob); region.put(2, charlie); @@ -131,7 +134,7 @@ public class DataCommandFunctionWithPDXJUnitTest { private TabularResultData getTableFromQuery(String query) { DataCommandRequest request = new DataCommandRequest(); request.setQuery(query); - DataCommandResult result = new DataCommandFunction().select(request); + DataCommandResult result = new DataCommandFunction().select(request, cache); CompositeResultData r = result.toSelectCommandResult(); return r.retrieveSectionByIndex(0).retrieveTableByIndex(0); } diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/Geode3544JUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/Geode3544JUnitTest.java index 0e21146..a35dfa9 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/Geode3544JUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/Geode3544JUnitTest.java @@ -27,6 +27,7 @@ import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionFactory; import org.apache.geode.cache.RegionShortcut; +import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.management.internal.cli.domain.DataCommandResult; import org.apache.geode.test.junit.categories.IntegrationTest; import org.junit.AfterClass; @@ -155,7 +156,7 @@ public class Geode3544JUnitTest { DataCommandFunction dataCmdFn = new DataCommandFunction(); DataCommandResult result = dataCmdFn.locateEntry(emp_key, EmpData.class.getName(), - String.class.getName(), PARTITIONED_REGION, false); + String.class.getName(), PARTITIONED_REGION, false, (InternalCache) cache); assertNotNull(result); result.aggregate(null); diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunctionJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunctionJUnitTest.java index a8b9193..bb54d12 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunctionJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListDiskStoresFunctionJUnitTest.java @@ -38,7 +38,6 @@ import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheClosedException; import org.apache.geode.cache.execute.FunctionContext; import org.apache.geode.cache.execute.ResultSender; -import org.apache.geode.distributed.DistributedMember; import org.apache.geode.distributed.internal.membership.InternalDistributedMember; import org.apache.geode.internal.cache.DiskStoreImpl; import org.apache.geode.internal.cache.InternalCache; @@ -63,6 +62,10 @@ import org.apache.geode.test.junit.categories.UnitTest; public class ListDiskStoresFunctionJUnitTest { private Mockery mockContext; + private InternalCache mockCache; + private FunctionContext mockFunctionContext; + + @Before public void setup() { @@ -72,6 +75,9 @@ public class ListDiskStoresFunctionJUnitTest { setThreadingPolicy(new Synchroniser()); } }; + + mockCache = mockContext.mock(InternalCache.class, "Cache"); + mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext"); } @After @@ -85,10 +91,6 @@ public class ListDiskStoresFunctionJUnitTest { return new DiskStoreDetails(id, name, memberId, memberName); } - private ListDiskStoresFunction createListDiskStoresFunction(final Cache cache) { - return new TestListDiskStoresFunction(cache); - } - @Test @SuppressWarnings("unchecked") public void testExecute() throws Throwable { @@ -99,8 +101,6 @@ public class ListDiskStoresFunctionJUnitTest { final String memberId = "mockMemberId"; final String memberName = "mockMemberName"; - final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache"); - final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember"); @@ -115,9 +115,6 @@ public class ListDiskStoresFunctionJUnitTest { mockDiskStores.add(mockDiskStoreTwo); mockDiskStores.add(mockDiskStoreThree); - final FunctionContext mockFunctionContext = - mockContext.mock(FunctionContext.class, "FunctionContext"); - final TestResultSender testResultSender = new TestResultSender(); mockContext.checking(new Expectations() { @@ -142,12 +139,14 @@ public class ListDiskStoresFunctionJUnitTest { will(returnValue(mockDiskStoreThreeId)); oneOf(mockDiskStoreThree).getName(); will(returnValue("ds-persistence")); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache); + final ListDiskStoresFunction function = new ListDiskStoresFunction(); function.execute(mockFunctionContext); @@ -171,18 +170,14 @@ public class ListDiskStoresFunctionJUnitTest { final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "MockFunctionContext"); - final ListDiskStoresFunction testListDiskStoresFunction = - new TestListDiskStoresFunction(mockContext.mock(Cache.class, "MockCache")) { - @Override - protected Cache getCache() { - throw new CacheClosedException("Expected"); - } - }; + final ListDiskStoresFunction testListDiskStoresFunction = new ListDiskStoresFunction(); final TestResultSender testResultSender = new TestResultSender(); mockContext.checking(new Expectations() { { + oneOf(mockFunctionContext).getCache(); + will(throwException(new CacheClosedException("Expected"))); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } @@ -201,14 +196,9 @@ public class ListDiskStoresFunctionJUnitTest { @Test @SuppressWarnings("unchecked") public void testExecuteOnMemberHavingNoDiskStores() throws Throwable { - final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache"); - final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember"); - final FunctionContext mockFunctionContext = - mockContext.mock(FunctionContext.class, "FunctionContext"); - final TestResultSender testResultSender = new TestResultSender(); mockContext.checking(new Expectations() { @@ -217,12 +207,14 @@ public class ListDiskStoresFunctionJUnitTest { will(returnValue(mockMember)); oneOf(mockCache).listDiskStoresIncludingRegionOwned(); will(returnValue(Collections.emptyList())); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache); + final ListDiskStoresFunction function = new ListDiskStoresFunction(); function.execute(mockFunctionContext); @@ -240,21 +232,20 @@ public class ListDiskStoresFunctionJUnitTest { @Test @SuppressWarnings("unchecked") public void testExecuteOnMemberWithANonGemFireCache() throws Throwable { - final Cache mockCache = mockContext.mock(Cache.class, "Cache"); - - final FunctionContext mockFunctionContext = - mockContext.mock(FunctionContext.class, "FunctionContext"); - final TestResultSender testResultSender = new TestResultSender(); + final Cache mockNonGemCache = mockContext.mock(Cache.class, "NonGemCache"); + mockContext.checking(new Expectations() { { + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockNonGemCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache); + final ListDiskStoresFunction function = new ListDiskStoresFunction(); function.execute(mockFunctionContext); @@ -271,14 +262,9 @@ public class ListDiskStoresFunctionJUnitTest { @Test(expected = RuntimeException.class) public void testExecuteThrowsRuntimeException() throws Throwable { - final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache"); - final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember"); - final FunctionContext mockFunctionContext = - mockContext.mock(FunctionContext.class, "FunctionContext"); - final TestResultSender testResultSender = new TestResultSender(); mockContext.checking(new Expectations() { @@ -287,12 +273,14 @@ public class ListDiskStoresFunctionJUnitTest { will(returnValue(mockMember)); oneOf(mockCache).listDiskStoresIncludingRegionOwned(); will(throwException(new RuntimeException("expected"))); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache); + final ListDiskStoresFunction function = new ListDiskStoresFunction(); function.execute(mockFunctionContext); @@ -305,21 +293,6 @@ public class ListDiskStoresFunctionJUnitTest { } } - private static class TestListDiskStoresFunction extends ListDiskStoresFunction { - - private final Cache cache; - - public TestListDiskStoresFunction(final Cache cache) { - assert cache != null : "The Cache cannot be null!"; - this.cache = cache; - } - - @Override - protected Cache getCache() { - return cache; - } - } - private static class TestResultSender implements ResultSender { private final List<Object> results = new LinkedList<Object>(); diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListIndexFunctionJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListIndexFunctionJUnitTest.java index 01b934e..69e07a1 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListIndexFunctionJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/ListIndexFunctionJUnitTest.java @@ -158,10 +158,6 @@ public class ListIndexFunctionJUnitTest { return indexStatisticsDetails; } - private ListIndexFunction createListIndexFunction(final Cache cache) { - return new TestListIndexFunction(cache); - } - private Index createMockIndex(final IndexDetails indexDetails) { final Index mockIndex = mockContext.mock(Index.class, "Index " + indexDetails.getIndexName() + " " + mockCounter.getAndIncrement()); @@ -287,12 +283,14 @@ public class ListIndexFunctionJUnitTest { will(returnValue( Arrays.asList(createMockIndex(indexDetailsOne), createMockIndex(indexDetailsTwo), createMockIndex(indexDetailsThree), createMockIndex(indexDetailsFour)))); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListIndexFunction function = createListIndexFunction(mockCache); + final ListIndexFunction function = new ListIndexFunction(); function.execute(mockFunctionContext); @@ -347,12 +345,14 @@ public class ListIndexFunctionJUnitTest { will(returnValue(mockDistributedMember)); oneOf(mockQueryService).getIndexes(); will(returnValue(Collections.emptyList())); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListIndexFunction function = createListIndexFunction(mockCache); + final ListIndexFunction function = new ListIndexFunction(); function.execute(mockFunctionContext); @@ -393,12 +393,14 @@ public class ListIndexFunctionJUnitTest { will(returnValue(mockDistributedMember)); oneOf(mockQueryService).getIndexes(); will(throwException(new RuntimeException("expected"))); + oneOf(mockFunctionContext).getCache(); + will(returnValue(mockCache)); oneOf(mockFunctionContext).getResultSender(); will(returnValue(testResultSender)); } }); - final ListIndexFunction function = createListIndexFunction(mockCache); + final ListIndexFunction function = new ListIndexFunction(); function.execute(mockFunctionContext); @@ -411,21 +413,6 @@ public class ListIndexFunctionJUnitTest { } } - private static class TestListIndexFunction extends ListIndexFunction { - - private final Cache cache; - - protected TestListIndexFunction(final Cache cache) { - assert cache != null : "The Cache cannot be null!"; - this.cache = cache; - } - - @Override - protected Cache getCache() { - return this.cache; - } - } - private static class TestResultSender implements ResultSender { private final List<Object> results = new LinkedList<Object>(); diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/result/CommandResultTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/result/CommandResultTest.java index 579d081..e320b7e 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/result/CommandResultTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/result/CommandResultTest.java @@ -19,21 +19,20 @@ import static org.assertj.core.api.Assertions.assertThat; import java.nio.file.Path; import java.nio.file.Paths; - +import org.apache.geode.test.junit.categories.UnitTest; +import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.apache.geode.test.junit.categories.UnitTest; - -@Category(UnitTest.class) +@Category({UnitTest.class}) public class CommandResultTest { + public CommandResultTest() {} @Test public void emptyResultHasOneEmptyLine() { CommandResult commandResult = new CommandResult(new InfoResultData()); - - assertThat(commandResult.nextLine()).isEqualTo(""); - assertThat(commandResult.hasNextLine()).isFalse(); + Assertions.assertThat(commandResult.nextLine()).isEqualTo(""); + Assertions.assertThat(commandResult.hasNextLine()).isFalse(); } @Test @@ -59,8 +58,7 @@ public class CommandResultTest { @Test public void emptyResultDoesNotHaveFileToDownload() { CommandResult commandResult = new CommandResult(new InfoResultData()); - - assertThat(commandResult.hasFileToDownload()).isFalse(); + Assertions.assertThat(commandResult.hasFileToDownload()).isFalse(); } @Test @@ -72,6 +70,4 @@ public class CommandResultTest { assertThat(commandResult.nextLine()).isEqualTo(fileToDownload.toString() + LINE_SEPARATOR); assertThat(commandResult.getFileToDownload()).isEqualTo(fileToDownload); } - - } -- To stop receiving notification emails like this one, please contact "commits@geode.apache.org" <commits@geode.apache.org>.