working through mocking logger

This commit is contained in:
Greg DiCristofaro 2020-10-01 08:27:53 -04:00
parent 131ee65dbb
commit 37edfc630d
2 changed files with 27 additions and 19 deletions

View File

@ -92,7 +92,6 @@ public class UserActivitySummaryTests {
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
private static BlackboardArtifact.Type ACCOUNT_TYPE = new BlackboardArtifact.Type(ARTIFACT_TYPE.TSK_ACCOUNT); private static BlackboardArtifact.Type ACCOUNT_TYPE = new BlackboardArtifact.Type(ARTIFACT_TYPE.TSK_ACCOUNT);
private static BlackboardAttribute.Type TYPE_ACCOUNT_TYPE = new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE); private static BlackboardAttribute.Type TYPE_ACCOUNT_TYPE = new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE);
@ -123,18 +122,14 @@ public class UserActivitySummaryTests {
private <T> void testMinCount(DataFunction<T> funct, String id) private <T> void testMinCount(DataFunction<T> funct, String id)
throws TskCoreException, NoServiceProviderException, TranslationException, SleuthkitCaseProviderException { throws TskCoreException, NoServiceProviderException, TranslationException, SleuthkitCaseProviderException {
for (int nonPositiveValue : new int[]{ -1, 0}) {
Pair<SleuthkitCase, Blackboard> tskPair = getArtifactsTSKMock(null); Pair<SleuthkitCase, Blackboard> tskPair = getArtifactsTSKMock(null);
UserActivitySummary summary = getTestClass(tskPair.getLeft(), false, null); UserActivitySummary summary = getTestClass(tskPair.getLeft(), false, null);
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class);
funct.retrieve(summary, TskMockUtils.mockDataSource(1), -1); funct.retrieve(summary, TskMockUtils.mockDataSource(1), nonPositiveValue);
verify(tskPair.getRight(), never()).getArtifacts(anyInt(), anyInt());
Pair<SleuthkitCase, Blackboard> tskPair2 = getArtifactsTSKMock(null);
UserActivitySummary summary2 = getTestClass(tskPair.getLeft(), false, null);
thrown.expect(IllegalArgumentException.class);
funct.retrieve(summary2, TskMockUtils.mockDataSource(1), -1);
verify(tskPair.getRight(), never()).getArtifacts(anyInt(), anyInt()); verify(tskPair.getRight(), never()).getArtifacts(anyInt(), anyInt());
} }
}
@Test @Test
public void testMinCountInvariant() public void testMinCountInvariant()

View File

@ -18,15 +18,14 @@
*/ */
package org.sleuthkit.autopsy.testutils; package org.sleuthkit.autopsy.testutils;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
@ -126,11 +125,25 @@ public class TskMockUtils {
return translationService; return translationService;
} }
public static Logger getTSKLogger() {
Logger logger = mock(Logger.class); /**
doNothing().when(logger).log(any(Level.class), anyString(), any(Throwable.class)); *
doNothing().when(logger).log(any(Level.class), anyString()); * @param loggerName
return logger; * @return
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Logger getTSKLogger(String loggerName)
throws InstantiationException, NoSuchMethodException, SecurityException {
// The logger doesn't appear to respond well to mocking with mockito.
// It appears that the issue may have to do with mocking methods in the java.* packages
// since the autopsy logger extends the java.util.logging.Logger class:
// https://javadoc.io/static/org.mockito/mockito-core/3.5.13/org/mockito/Mockito.html#39
Constructor<Logger> constructor = Logger.class.getConstructor(String.class, String.class);
constructor.setAccessible(true);
return constructor.newInstance(loggerName, null);
} }
private TskMockUtils() { private TskMockUtils() {