diff --git a/logger/build.gradle b/logger/build.gradle index cb3f3a7..88ff226 100644 --- a/logger/build.gradle +++ b/logger/build.gradle @@ -25,7 +25,8 @@ android { } dependencies { - + testImplementation 'junit:junit:4.13.2' + testImplementation 'org.mockito:mockito-core:3.12.4' } afterEvaluate { diff --git a/logger/src/main/java/m/co/rh/id/alogger/CompositeLogger.java b/logger/src/main/java/m/co/rh/id/alogger/CompositeLogger.java index 30d0f33..fb62617 100644 --- a/logger/src/main/java/m/co/rh/id/alogger/CompositeLogger.java +++ b/logger/src/main/java/m/co/rh/id/alogger/CompositeLogger.java @@ -25,6 +25,35 @@ public void setLogLevel(int logLevel) { } } + /** + * Set log level to all clazz type + * + * @param clazz logger type + * @param logLevel log level to be set + */ + public void setLogLevel(Class clazz, int logLevel) { + if (clazz == null) { + return; + } + for (ILogger iLogger : mILoggerList) { + if (clazz.isInstance(iLogger)) { + iLogger.setLogLevel(logLevel); + } + } + } + + public L getLogger(Class clazz) { + if (clazz == null) { + return null; + } + for (ILogger iLogger : mILoggerList) { + if (clazz.isInstance(iLogger)) { + return (L) iLogger; + } + } + return null; + } + @Override public void v(String tag, String message) { for (ILogger iLogger : mILoggerList) { diff --git a/logger/src/test/java/m/co/rh/id/alogger/CompositeLoggerTest.java b/logger/src/test/java/m/co/rh/id/alogger/CompositeLoggerTest.java new file mode 100644 index 0000000..4939126 --- /dev/null +++ b/logger/src/test/java/m/co/rh/id/alogger/CompositeLoggerTest.java @@ -0,0 +1,54 @@ +package m.co.rh.id.alogger; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +public class CompositeLoggerTest { + + @Test + public void getLogger_gettingExactLoggerType() { + AndroidLogger androidLogger = Mockito.mock(AndroidLogger.class); + FileLogger fileLogger = Mockito.mock(FileLogger.class); + ToastLogger toastLogger = Mockito.mock(ToastLogger.class); + List iLoggerList = new ArrayList<>(); + iLoggerList.add(androidLogger); + iLoggerList.add(fileLogger); + iLoggerList.add(toastLogger); + + CompositeLogger compositeLogger = new CompositeLogger(iLoggerList); + Assert.assertSame(compositeLogger.getLogger(AndroidLogger.class), androidLogger); + Assert.assertSame(compositeLogger.getLogger(FileLogger.class), fileLogger); + Assert.assertSame(compositeLogger.getLogger(ToastLogger.class), toastLogger); + } + + @Test + public void setLogLevel_settingBaseOnLoggerType() { + AndroidLogger androidLogger = Mockito.mock(AndroidLogger.class); + FileLogger fileLogger = Mockito.mock(FileLogger.class); + ToastLogger toastLogger = Mockito.mock(ToastLogger.class); + List iLoggerList = new ArrayList<>(); + iLoggerList.add(androidLogger); + iLoggerList.add(fileLogger); + iLoggerList.add(toastLogger); + + CompositeLogger compositeLogger = new CompositeLogger(iLoggerList); + compositeLogger.setLogLevel(AndroidLogger.class, ILogger.ERROR); + Mockito.verify(androidLogger, Mockito.times(1)).setLogLevel(ILogger.ERROR); + compositeLogger.setLogLevel(FileLogger.class, ILogger.DEBUG); + Mockito.verify(fileLogger, Mockito.times(1)).setLogLevel(ILogger.DEBUG); + compositeLogger.setLogLevel(ToastLogger.class, ILogger.INFO); + Mockito.verify(toastLogger, Mockito.times(1)).setLogLevel(ILogger.INFO); + + Mockito.clearInvocations(androidLogger, fileLogger, toastLogger); + + compositeLogger.setLogLevel(ILogger.class, ILogger.VERBOSE); + Mockito.verify(androidLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE); + Mockito.verify(fileLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE); + Mockito.verify(toastLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE); + + } +}