-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from linean/feature/allow-user-to-provide-cus…
…tom-logger Allow users to provide custom logging logic
- Loading branch information
Showing
5 changed files
with
303 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/LogUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.prebid.mobile; | ||
|
||
import static org.prebid.mobile.LogUtil.DEBUG; | ||
import static org.prebid.mobile.LogUtil.ERROR; | ||
import static org.prebid.mobile.LogUtil.INFO; | ||
import static org.prebid.mobile.LogUtil.VERBOSE; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.prebid.mobile.testutils.TestLogger; | ||
|
||
public class LogUtilTest { | ||
|
||
private final String DEFAULT_TAG = "PrebidMobile"; | ||
private final TestLogger testLogger = new TestLogger(); | ||
|
||
@Before | ||
public void setup() { | ||
LogUtil.setLogLevel(VERBOSE); | ||
LogUtil.setLogger(testLogger); | ||
} | ||
|
||
@Test | ||
public void testErrorLogWithMessage() { | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.error(testMessage); | ||
|
||
testLogger.assertLogPrinted(ERROR, DEFAULT_TAG, testMessage); | ||
} | ||
|
||
@Test | ||
public void testErrorLogWithMessageAndTag() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.error(testTag, testMessage); | ||
|
||
testLogger.assertLogPrinted(ERROR, testTag, testMessage); | ||
} | ||
|
||
@Test | ||
public void testErrorLogWithMessageTagAndThrowable() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
Throwable testThrowable = new Throwable("Test throwable"); | ||
|
||
LogUtil.error(testTag, testMessage, testThrowable); | ||
|
||
testLogger.assertErrorLogged(testTag, testMessage, testThrowable); | ||
} | ||
|
||
@Test | ||
public void testVerboseLogWithMessage() { | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.verbose(testMessage); | ||
|
||
testLogger.assertLogPrinted(VERBOSE, DEFAULT_TAG, testMessage); | ||
} | ||
|
||
@Test | ||
public void testVerboseLogWithMessageAndTag() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.verbose(testTag, testMessage); | ||
|
||
testLogger.assertLogPrinted(VERBOSE, testTag, testMessage); | ||
} | ||
|
||
@Test | ||
public void testInfoLogWithMessage() { | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.info(testMessage); | ||
|
||
testLogger.assertLogPrinted(INFO, DEFAULT_TAG, testMessage); | ||
} | ||
|
||
@Test | ||
public void testInfoLogWithMessageAndTag() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.info(testTag, testMessage); | ||
|
||
testLogger.assertLogPrinted(INFO, testTag, testMessage); | ||
} | ||
|
||
@Test | ||
public void testWarningLogWithMessage() { | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.warning(testMessage); | ||
|
||
testLogger.assertLogPrinted(INFO, DEFAULT_TAG, testMessage); | ||
} | ||
|
||
@Test | ||
public void testWarningLogWithMessageAndTag() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.warning(testTag, testMessage); | ||
|
||
testLogger.assertLogPrinted(INFO, testTag, testMessage); | ||
} | ||
|
||
@Test | ||
public void testDebugLogWithMessage() { | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.warning(testMessage); | ||
|
||
testLogger.assertLogPrinted(DEBUG, DEFAULT_TAG, testMessage); | ||
} | ||
|
||
@Test | ||
public void testDebugLogWithMessageAndTag() { | ||
String testTag = "Test tag"; | ||
String testMessage = "Test error message"; | ||
|
||
LogUtil.warning(testTag, testMessage); | ||
|
||
testLogger.assertLogPrinted(DEBUG, testTag, testMessage); | ||
} | ||
} | ||
|
126 changes: 126 additions & 0 deletions
126
PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/testutils/TestLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.prebid.mobile.testutils; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.prebid.mobile.LogUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import kotlin.jvm.internal.Intrinsics; | ||
|
||
|
||
public final class TestLogger implements LogUtil.PrebidLogger { | ||
|
||
private final List<PrintLog> printLogs = new ArrayList<>(); | ||
private final List<ErrorLog> errorLogs = new ArrayList<>(); | ||
|
||
public void println(int messagePriority, @NotNull String tag, @NotNull String message) { | ||
printLogs.add(new PrintLog(messagePriority, tag, message)); | ||
} | ||
|
||
public void e(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) { | ||
errorLogs.add(new ErrorLog(tag, message, throwable)); | ||
} | ||
|
||
public void assertErrorLogged(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) { | ||
ErrorLog expectedLog = new ErrorLog(tag, message, throwable); | ||
Iterator<ErrorLog> iterator = errorLogs.iterator(); | ||
|
||
ErrorLog actualLog = null; | ||
while (iterator.hasNext()) { | ||
actualLog = iterator.next(); | ||
if (!Intrinsics.areEqual(expectedLog, actualLog)) { | ||
continue; | ||
} | ||
break; | ||
} | ||
|
||
assertNotNull("Expected error log not captured. Recorded logs: " + this.errorLogs, actualLog); | ||
} | ||
|
||
public void assertLogPrinted(int messagePriority, @NotNull String tag, @NotNull String message) { | ||
PrintLog expectedLog = new PrintLog(messagePriority, tag, message); | ||
Iterator<PrintLog> iterator = printLogs.iterator(); | ||
|
||
PrintLog actualLog = null; | ||
while (iterator.hasNext()) { | ||
actualLog = iterator.next(); | ||
if (!Intrinsics.areEqual(expectedLog, actualLog)) { | ||
continue; | ||
} | ||
break; | ||
} | ||
|
||
assertNotNull("Expected log not captured. Recorded logs: " + this.printLogs, actualLog); | ||
} | ||
|
||
private static final class ErrorLog { | ||
@NotNull | ||
private final String tag; | ||
@NotNull | ||
private final String message; | ||
@NotNull | ||
private final Throwable throwable; | ||
|
||
public ErrorLog(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) { | ||
this.tag = tag; | ||
this.message = message; | ||
this.throwable = throwable; | ||
} | ||
|
||
@NotNull | ||
public String toString() { | ||
return "ErrorLog(tag=" + this.tag + ", message=" + this.message + ", throwable=" + this.throwable + ")"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ErrorLog errorLog = (ErrorLog) o; | ||
return tag.equals(errorLog.tag) && message.equals(errorLog.message) && throwable.equals(errorLog.throwable); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(tag, message, throwable); | ||
} | ||
} | ||
|
||
private static final class PrintLog { | ||
private final int messagePriority; | ||
@NotNull | ||
private final String tag; | ||
@NotNull | ||
private final String message; | ||
|
||
|
||
public PrintLog(int messagePriority, @NotNull String tag, @NotNull String message) { | ||
this.messagePriority = messagePriority; | ||
this.tag = tag; | ||
this.message = message; | ||
} | ||
|
||
@NotNull | ||
public String toString() { | ||
return "PrintLog(messagePriority=" + this.messagePriority + ", tag=" + this.tag + ", message=" + this.message + ")"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
PrintLog printLog = (PrintLog) o; | ||
return messagePriority == printLog.messagePriority && tag.equals(printLog.tag) && message.equals(printLog.message); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(messagePriority, tag, message); | ||
} | ||
} | ||
} |