ExceptionPersister { public Map<String, StoredException> loadAll(){ Map<String, StoredException> storedExceptions = new HashMap<>(); if(context != null) { String[] fileNames = context.fileList(); if (fileNames != null) { for (String fileName : fileNames) { if (utils.isNotBlank(fileName) && fileName.startsWith("FoamStoredException")) { StoredException storedException = loadStoredExceptionData(fileName); if (storedException != null) { storedExceptions.put(fileName, storedException); } } } } } return storedExceptions; } ExceptionPersister(Context context); Map<String, StoredException> loadAll(); void store(StoredException storedException);  }
@Test public void testLoadAll() { Context mockContext = mock(Context.class); when(mockContext.fileList()).thenReturn(new String[]{ "FoamStoredException1.log", "ShouldNotBeProcessed.log", "FoamStoredException2.log", "", null, "null.log", "BadFoamStoredException99.log", }); StoredException storedException1 = new StoredException(); StoredException storedException2 = new StoredException(); final List<StoredException> storedExceptions = new ArrayList<>(); storedExceptions.add(storedException1); storedExceptions.add(storedException2); ExceptionPersister exceptionPersister = new ExceptionPersister(mockContext){ @Nullable @Override StoredException loadStoredExceptionData(String fileName) { return storedExceptions.remove(0); } }; Map<String, StoredException> result = exceptionPersister.loadAll(); assertNotNull(result); assertEquals(2, result.size()); assertSame(storedException1, result.get("FoamStoredException1.log")); assertSame(storedException2, result.get("FoamStoredException2.log")); }
@Test public void testLoadAllNoFiles() { Context mockContext = mock(Context.class); when(mockContext.fileList()).thenReturn(null); ExceptionPersister exceptionPersister = new ExceptionPersister(mockContext); Map<String, StoredException> result = exceptionPersister.loadAll(); assertNotNull(result); assertEquals(0, result.size()); }