From a23ded6a94697adad66b79fcb61e64ddc965926c Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Mon, 31 Oct 2016 16:18:58 +0100 Subject: [PATCH] [TEST] Fix NullPointerException in AzureStorageServiceMock Makes the code safe against concurrent modifications of the underlying hashmap. --- .../cloud/azure/storage/AzureStorageServiceMock.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceMock.java b/plugins/repository-azure/src/test/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceMock.java index 51b5eae57ae..ba2011c276e 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceMock.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceMock.java @@ -43,7 +43,7 @@ import java.util.concurrent.ConcurrentHashMap; */ public class AzureStorageServiceMock extends AbstractComponent implements AzureStorageService { - protected Map blobs = new ConcurrentHashMap<>(); + protected final Map blobs = new ConcurrentHashMap<>(); public AzureStorageServiceMock() { super(Settings.EMPTY); @@ -94,7 +94,7 @@ public class AzureStorageServiceMock extends AbstractComponent implements AzureS @Override public Map listBlobsByPrefix(String account, LocationMode mode, String container, String keyPath, String prefix) { MapBuilder blobsBuilder = MapBuilder.newMapBuilder(); - for (String blobName : blobs.keySet()) { + blobs.forEach((String blobName, ByteArrayOutputStream bos) -> { final String checkBlob; if (keyPath != null && !keyPath.isEmpty()) { // strip off key path from the beginning of the blob name @@ -103,9 +103,9 @@ public class AzureStorageServiceMock extends AbstractComponent implements AzureS checkBlob = blobName; } if (prefix == null || startsWithIgnoreCase(checkBlob, prefix)) { - blobsBuilder.put(blobName, new PlainBlobMetaData(checkBlob, blobs.get(blobName).size())); + blobsBuilder.put(blobName, new PlainBlobMetaData(checkBlob, bos.size())); } - } + }); return blobsBuilder.immutableMap(); }