@@ -18,144 +18,162 @@ import org.junit.runner.RunWith
1818
1919@RunWith(AndroidJUnit4 ::class )
2020class SentryLogcatAdapterTest {
21- private val tag = " my-tag"
22- private val commonMsg = " SentryLogcatAdapter"
23- private val throwable = RuntimeException (" Test Exception" )
21+ private val tag = " my-tag"
22+ private val commonMsg = " SentryLogcatAdapter"
23+ private val throwable = RuntimeException (" Test Exception" )
2424
2525 class Fixture {
26- val breadcrumbs = mutableListOf<Breadcrumb >()
27- val logs = mutableListOf<SentryLogEvent >()
26+ val breadcrumbs = mutableListOf<Breadcrumb >()
27+ val logs = mutableListOf<SentryLogEvent >()
2828
29- fun initSut (options : Sentry .OptionsConfiguration <SentryAndroidOptions >? = null) {
29+ fun initSut (options : Sentry .OptionsConfiguration <SentryAndroidOptions >? = null) {
3030 val metadata =
3131 Bundle ().apply { putString(ManifestMetadataReader .DSN , " https://key@sentry.io/123" ) }
3232 val mockContext = ContextUtilsTestHelper .mockMetaData(metaData = metadata)
33- initForTest(mockContext) {
34- it.beforeBreadcrumb = SentryOptions .BeforeBreadcrumbCallback { breadcrumb, _ ->
35- breadcrumbs.add(breadcrumb)
36- breadcrumb
37- }
38- it.logs.isEnabled = true
39- it.logs.beforeSend = SentryOptions .Logs .BeforeSendLogCallback { logEvent ->
40- logs.add(logEvent)
41- logEvent
42- }
43- options?.configure(it)
33+ initForTest(mockContext) {
34+ it.beforeBreadcrumb =
35+ SentryOptions .BeforeBreadcrumbCallback { breadcrumb, _ ->
36+ breadcrumbs.add(breadcrumb)
37+ breadcrumb
4438 }
39+ it.logs.isEnabled = true
40+ it.logs.beforeSend =
41+ SentryOptions .Logs .BeforeSendLogCallback { logEvent ->
42+ logs.add(logEvent)
43+ logEvent
44+ }
45+ options?.configure(it)
4546 }
4647 }
4748 }
4849
4950 private val fixture = Fixture ()
5051
51- @AfterTest
52- fun `clean up` () {
53- AppStartMetrics .getInstance().clear()
54- ContextUtils .resetInstance()
55- Sentry .close()
56- fixture.breadcrumbs.clear()
57- fixture.logs.clear()
58- }
59-
52+ @AfterTest
53+ fun `clean up` () {
54+ AppStartMetrics .getInstance().clear()
55+ ContextUtils .resetInstance()
56+ Sentry .close()
57+ fixture.breadcrumbs.clear()
58+ fixture.logs.clear()
59+ }
6060
61- @Test
62- fun `verbose log message has expected content` () {
61+ @Test
62+ fun `verbose log message has expected content` () {
6363 fixture.initSut()
6464 SentryLogcatAdapter .v(tag, " $commonMsg verbose" )
6565 fixture.breadcrumbs.first().assert (tag, " $commonMsg verbose" , SentryLevel .DEBUG )
6666 fixture.logs.first().assert (" $commonMsg verbose" , SentryLogLevel .TRACE )
67- }
67+ }
6868
69- @Test
70- fun `info log message has expected content` () {
69+ @Test
70+ fun `info log message has expected content` () {
7171 fixture.initSut()
7272 SentryLogcatAdapter .i(tag, " $commonMsg info" )
7373 fixture.breadcrumbs.first().assert (tag, " $commonMsg info" , SentryLevel .INFO )
7474 fixture.logs.first().assert (" $commonMsg info" , SentryLogLevel .INFO )
75- }
75+ }
7676
77- @Test
78- fun `debug log message has expected content` () {
77+ @Test
78+ fun `debug log message has expected content` () {
7979 fixture.initSut()
8080 SentryLogcatAdapter .d(tag, " $commonMsg debug" )
8181 fixture.breadcrumbs.first().assert (tag, " $commonMsg debug" , SentryLevel .DEBUG )
8282 fixture.logs.first().assert (" $commonMsg debug" , SentryLogLevel .DEBUG )
83- }
83+ }
8484
85- @Test
86- fun `warning log message has expected content` () {
85+ @Test
86+ fun `warning log message has expected content` () {
8787 fixture.initSut()
8888 SentryLogcatAdapter .w(tag, " $commonMsg warning" )
8989 fixture.breadcrumbs.first().assert (tag, " $commonMsg warning" , SentryLevel .WARNING )
9090 fixture.logs.first().assert (" $commonMsg warning" , SentryLogLevel .WARN )
91- }
91+ }
9292
93- @Test
94- fun `error log message has expected content` () {
93+ @Test
94+ fun `error log message has expected content` () {
9595 fixture.initSut()
9696 SentryLogcatAdapter .e(tag, " $commonMsg error" )
9797 fixture.breadcrumbs.first().assert (tag, " $commonMsg error" , SentryLevel .ERROR )
9898 fixture.logs.first().assert (" $commonMsg error" , SentryLogLevel .ERROR )
99- }
99+ }
100100
101- @Test
102- fun `wtf log message has expected content` () {
101+ @Test
102+ fun `wtf log message has expected content` () {
103103 fixture.initSut()
104104 SentryLogcatAdapter .wtf(tag, " $commonMsg wtf" )
105105 fixture.breadcrumbs.first().assert (tag, " $commonMsg wtf" , SentryLevel .ERROR )
106106 fixture.logs.first().assert (" $commonMsg wtf" , SentryLogLevel .FATAL )
107- }
107+ }
108108
109- @Test
110- fun `e log throwable has expected content` () {
109+ @Test
110+ fun `e log throwable has expected content` () {
111111 fixture.initSut()
112112 SentryLogcatAdapter .e(tag, " $commonMsg error exception" , throwable)
113113 fixture.breadcrumbs.first().assert (tag, " $commonMsg error exception" , SentryLevel .ERROR )
114- fixture.logs.first().assert (" $commonMsg error exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .ERROR )
115- }
114+ fixture.logs
115+ .first()
116+ .assert (" $commonMsg error exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .ERROR )
117+ }
116118
117- @Test
118- fun `v log throwable has expected content` () {
119+ @Test
120+ fun `v log throwable has expected content` () {
119121 fixture.initSut()
120122 SentryLogcatAdapter .v(tag, " $commonMsg verbose exception" , throwable)
121123 fixture.breadcrumbs.first().assert (tag, " $commonMsg verbose exception" , SentryLevel .DEBUG )
122- fixture.logs.first().assert (" $commonMsg verbose exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .TRACE )
123- }
124+ fixture.logs
125+ .first()
126+ .assert (
127+ " $commonMsg verbose exception\n ${throwable.stackTraceToString()} " ,
128+ SentryLogLevel .TRACE ,
129+ )
130+ }
124131
125- @Test
126- fun `i log throwable has expected content` () {
132+ @Test
133+ fun `i log throwable has expected content` () {
127134 fixture.initSut()
128135 SentryLogcatAdapter .i(tag, " $commonMsg info exception" , throwable)
129136 fixture.breadcrumbs.first().assert (tag, " $commonMsg info exception" , SentryLevel .INFO )
130- fixture.logs.first().assert (" $commonMsg info exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .INFO )
131- }
137+ fixture.logs
138+ .first()
139+ .assert (" $commonMsg info exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .INFO )
140+ }
132141
133- @Test
134- fun `d log throwable has expected content` () {
142+ @Test
143+ fun `d log throwable has expected content` () {
135144 fixture.initSut()
136145 SentryLogcatAdapter .d(tag, " $commonMsg debug exception" , throwable)
137146 fixture.breadcrumbs.first().assert (tag, " $commonMsg debug exception" , SentryLevel .DEBUG )
138- fixture.logs.first().assert (" $commonMsg debug exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .DEBUG )
139- }
147+ fixture.logs
148+ .first()
149+ .assert (" $commonMsg debug exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .DEBUG )
150+ }
140151
141- @Test
142- fun `w log throwable has expected content` () {
152+ @Test
153+ fun `w log throwable has expected content` () {
143154 fixture.initSut()
144155 SentryLogcatAdapter .w(tag, " $commonMsg warning exception" , throwable)
145156 fixture.breadcrumbs.first().assert (tag, " $commonMsg warning exception" , SentryLevel .WARNING )
146- fixture.logs.first().assert (" $commonMsg warning exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .WARN )
147- }
157+ fixture.logs
158+ .first()
159+ .assert (
160+ " $commonMsg warning exception\n ${throwable.stackTraceToString()} " ,
161+ SentryLogLevel .WARN ,
162+ )
163+ }
148164
149- @Test
150- fun `wtf log throwable has expected content` () {
165+ @Test
166+ fun `wtf log throwable has expected content` () {
151167 fixture.initSut()
152168 SentryLogcatAdapter .wtf(tag, " $commonMsg wtf exception" , throwable)
153169 fixture.breadcrumbs.first().assert (tag, " $commonMsg wtf exception" , SentryLevel .ERROR )
154- fixture.logs.first().assert (" $commonMsg wtf exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .FATAL )
155- }
170+ fixture.logs
171+ .first()
172+ .assert (" $commonMsg wtf exception\n ${throwable.stackTraceToString()} " , SentryLogLevel .FATAL )
173+ }
156174
157- @Test
158- fun `do not send logs if logs is disabled` () {
175+ @Test
176+ fun `do not send logs if logs is disabled` () {
159177 fixture.initSut { it.logs.isEnabled = false }
160178
161179 SentryLogcatAdapter .v(tag, " $commonMsg verbose" )
@@ -172,10 +190,10 @@ fun `do not send logs if logs is disabled`() {
172190 SentryLogcatAdapter .wtf(tag, " $commonMsg wtf exception" , throwable)
173191
174192 assertTrue(fixture.logs.isEmpty())
175- }
193+ }
176194
177- @Test
178- fun `logs add correct number of breadcrumb` () {
195+ @Test
196+ fun `logs add correct number of breadcrumb` () {
179197 fixture.initSut()
180198 SentryLogcatAdapter .v(tag, commonMsg)
181199 SentryLogcatAdapter .d(tag, commonMsg)
@@ -184,29 +202,24 @@ fun `logs add correct number of breadcrumb`() {
184202 SentryLogcatAdapter .e(tag, commonMsg)
185203 SentryLogcatAdapter .wtf(tag, commonMsg)
186204 assertEquals(
187- 6 ,
188- fixture.breadcrumbs.filter {
189- it.message?.contains(" SentryLogcatAdapter" ) ? : false
190- }.size
205+ 6 ,
206+ fixture.breadcrumbs.filter { it.message?.contains(" SentryLogcatAdapter" ) ? : false }.size,
191207 )
192- }
208+ }
193209
194- private fun Breadcrumb.assert (
210+ private fun Breadcrumb.assert (
195211 expectedTag : String ,
196212 expectedMessage : String ,
197- expectedLevel : SentryLevel
198- ) {
213+ expectedLevel : SentryLevel ,
214+ ) {
199215 assertEquals(expectedMessage, message)
200216 assertEquals(expectedTag, data[" tag" ])
201217 assertEquals(expectedLevel, level)
202218 assertEquals(" Logcat" , category)
203- }
219+ }
204220
205- private fun SentryLogEvent.assert (
206- expectedMessage : String ,
207- expectedLevel : SentryLogLevel
208- ) {
221+ private fun SentryLogEvent.assert (expectedMessage : String , expectedLevel : SentryLogLevel ) {
209222 assertEquals(expectedMessage, body)
210223 assertEquals(expectedLevel, level)
211- }
224+ }
212225}
0 commit comments