1
+ package fr.free.nrw.commons.logging
2
+
3
+ import android.util.Log
4
+ import com.nhaarman.mockitokotlin2.times
5
+ import com.nhaarman.mockitokotlin2.verify
6
+ import org.junit.Before
7
+ import org.junit.Test
8
+ import org.junit.jupiter.api.Assertions.assertEquals
9
+ import org.mockito.Mock
10
+ import org.mockito.MockitoAnnotations
11
+ import org.powermock.reflect.Whitebox
12
+ import org.slf4j.Logger
13
+ import java.lang.reflect.Method
14
+ import java.util.concurrent.Executor
15
+
16
+ class FileLoggingTreeUnitTest {
17
+
18
+ private lateinit var fileLoggingTree: FileLoggingTree
19
+
20
+ @Mock
21
+ private lateinit var executor: Executor
22
+
23
+ @Mock
24
+ private lateinit var logger: Logger
25
+
26
+ @Before
27
+ fun setUp () {
28
+ MockitoAnnotations .initMocks(this )
29
+ fileLoggingTree = FileLoggingTree (
30
+ Log .VERBOSE ,
31
+ " test" ,
32
+ " test" ,
33
+ 1000 ,
34
+ executor
35
+ )
36
+ Whitebox .setInternalState(fileLoggingTree, " logger" , logger)
37
+ }
38
+
39
+ @Test
40
+ fun testSetLogLevel () {
41
+ var logLevel = Log .VERBOSE
42
+ Whitebox .setInternalState(fileLoggingTree, " logLevel" , logLevel)
43
+ fileLoggingTree.setLogLevel(Log .DEBUG )
44
+ logLevel = Log .DEBUG
45
+ assertEquals(logLevel, logLevel)
46
+ }
47
+
48
+ @Test
49
+ fun testLog () {
50
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
51
+ " log" , Int ::class .java, String ::class .java, String ::class .java, Throwable ::class .java
52
+ )
53
+ method.isAccessible = true
54
+ method.invoke(fileLoggingTree, 0 , " test" , " test" , Throwable ())
55
+ }
56
+
57
+ @Test
58
+ fun testLogMessageCaseVERBOSE () {
59
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
60
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
61
+ )
62
+ method.isAccessible = true
63
+ method.invoke(fileLoggingTree, Log .VERBOSE , " test" , " test" )
64
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
65
+ verify(logger, times(1 )).trace(messageWithTag)
66
+ }
67
+
68
+ @Test
69
+ fun testLogMessageCaseDEBUG () {
70
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
71
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
72
+ )
73
+ method.isAccessible = true
74
+ method.invoke(fileLoggingTree, Log .DEBUG , " test" , " test" )
75
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
76
+ verify(logger, times(1 )).debug(messageWithTag)
77
+ }
78
+
79
+ @Test
80
+ fun testLogMessageCaseINFO () {
81
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
82
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
83
+ )
84
+ method.isAccessible = true
85
+ method.invoke(fileLoggingTree, Log .INFO , " test" , " test" )
86
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
87
+ verify(logger, times(1 )).info(messageWithTag)
88
+ }
89
+
90
+ @Test
91
+ fun testLogMessageCaseWARN () {
92
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
93
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
94
+ )
95
+ method.isAccessible = true
96
+ method.invoke(fileLoggingTree, Log .WARN , " test" , " test" )
97
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
98
+ verify(logger, times(1 )).warn(messageWithTag)
99
+ }
100
+
101
+ @Test
102
+ fun testLogMessageCaseERROR () {
103
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
104
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
105
+ )
106
+ method.isAccessible = true
107
+ method.invoke(fileLoggingTree, Log .ERROR , " test" , " test" )
108
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
109
+ verify(logger, times(1 )).error(messageWithTag)
110
+ }
111
+
112
+ @Test
113
+ fun testLogMessageCaseASSERT () {
114
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
115
+ " logMessage" , Int ::class .java, String ::class .java, String ::class .java
116
+ )
117
+ method.isAccessible = true
118
+ method.invoke(fileLoggingTree, Log .ASSERT , " test" , " test" )
119
+ val messageWithTag = String .format(" [%s] : %s" , " test" , " test" )
120
+ verify(logger, times(1 )).error(messageWithTag)
121
+ }
122
+
123
+ @Test
124
+ fun testIsLoggableCaseTrue () {
125
+ Whitebox .setInternalState(fileLoggingTree, " logLevel" , Log .VERBOSE )
126
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
127
+ " isLoggable" , Int ::class .java
128
+ )
129
+ method.isAccessible = true
130
+ assertEquals(method.invoke(fileLoggingTree, Log .ASSERT ), true )
131
+ }
132
+
133
+ @Test
134
+ fun testIsLoggableCaseFalse () {
135
+ Whitebox .setInternalState(fileLoggingTree, " logLevel" , Log .ASSERT )
136
+ val method: Method = FileLoggingTree ::class .java.getDeclaredMethod(
137
+ " isLoggable" , Int ::class .java
138
+ )
139
+ method.isAccessible = true
140
+ assertEquals(method.invoke(fileLoggingTree, Log .VERBOSE ), false )
141
+ }
142
+
143
+ }
0 commit comments