@@ -3,8 +3,15 @@ package com.bugsnag.android.ndk
33import org.junit.After
44import org.junit.Assert.assertEquals
55import org.junit.Before
6+ import org.junit.Ignore
67import org.junit.Test
78import java.io.File
9+ import java.text.SimpleDateFormat
10+ import java.util.Calendar
11+ import java.util.Date
12+ import java.util.GregorianCalendar
13+ import java.util.Locale
14+ import java.util.TimeZone
815
916class NativeJsonSerializeTest {
1017
@@ -16,6 +23,8 @@ class NativeJsonSerializeTest {
1623 }
1724
1825 private val path = File (System .getProperty(" java.io.tmpdir" ), this ::class .simpleName!! )
26+ private val dateFormat = SimpleDateFormat (" yyyy-MM-dd'T'HH:mm:ss'Z'" , Locale .US )
27+ .apply { timeZone = TimeZone .getTimeZone(" UTC" ) }
1928
2029 @Before
2130 fun setupTmpdir () {
@@ -27,13 +36,84 @@ class NativeJsonSerializeTest {
2736 path.deleteRecursively()
2837 }
2938
30- external fun run (outputDir : String ): Int
39+ external fun run (outputDir : String , timestamp : Long ): Int
3140
3241 @Test
33- fun testPassesNativeSuite () {
34- verifyNativeRun(run (path.absolutePath))
42+ fun testPassesNativeSuiteEpoch () {
43+ verifyNativeRun(run (path.absolutePath, 7609 ))
3544 val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
3645 val expected = loadJson(" event_serialization.json" )
3746 assertEquals(expected, jsonFile.readText())
3847 }
48+
49+ @Test
50+ fun testRegression2024 () {
51+ val timestamp = GregorianCalendar (2024 , 11 , 30 , 16 , 0 , 0 ).timeInMillis
52+ val datestamp = dateFormat.format(Date (timestamp))
53+
54+ verifyNativeRun(run (path.absolutePath, timestamp / 1000L ))
55+ val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
56+ val expected = loadJson(" event_serialization.json" )
57+ .replace(" \" 1970-01-01T02:06:49Z\" " , " \" ${datestamp} \" " )
58+ assertEquals(expected, jsonFile.readText())
59+ }
60+
61+ @Test
62+ fun testPassesNativeSuite2024 () {
63+ val timestamp = GregorianCalendar (2024 , 0 , 1 ).timeInMillis
64+ val datestamp = dateFormat.format(Date (timestamp))
65+
66+ verifyNativeRun(run (path.absolutePath, timestamp / 1000L ))
67+ val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
68+ val expected = loadJson(" event_serialization.json" )
69+ .replace(" \" 1970-01-01T02:06:49Z\" " , " \" ${datestamp} \" " )
70+ assertEquals(expected, jsonFile.readText())
71+ }
72+
73+ @Test
74+ fun testPassesNativeSuite2025 () {
75+ val timestamp = GregorianCalendar (2025 , 1 , 1 ).timeInMillis
76+ val datestamp = dateFormat.format(Date (timestamp))
77+
78+ verifyNativeRun(run (path.absolutePath, timestamp / 1000L ))
79+ val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
80+ val expected = loadJson(" event_serialization.json" )
81+ .replace(" \" 1970-01-01T02:06:49Z\" " , " \" ${datestamp} \" " )
82+ assertEquals(expected, jsonFile.readText())
83+ }
84+
85+ @Test
86+ fun testPassesNativeSuiteToday () {
87+ val now = System .currentTimeMillis()
88+ val datestamp = dateFormat.format(Date (now))
89+
90+ verifyNativeRun(run (path.absolutePath, now / 1000L ))
91+ val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
92+ val expected = loadJson(" event_serialization.json" )
93+ .replace(" \" 1970-01-01T02:06:49Z\" " , " \" ${datestamp} \" " )
94+ assertEquals(expected, jsonFile.readText())
95+ }
96+
97+ @Test
98+ @Ignore(" useful when working on the date formatting code" )
99+ fun testDecadesOfDates () {
100+ val calendar = Calendar .getInstance().apply { add(Calendar .YEAR , - 10 ) }
101+ val end = Calendar .getInstance().apply { add(Calendar .YEAR , 10 ) }
102+
103+ while (calendar < end) {
104+ val instant = calendar.timeInMillis
105+ val datestamp = dateFormat.format(Date (instant))
106+
107+ verifyNativeRun(run (path.absolutePath, instant / 1000L ))
108+ val jsonFile = path.listFiles()!! .maxByOrNull { it.lastModified() }!!
109+ val expected = loadJson(" event_serialization.json" )
110+ .replace(" \" 1970-01-01T02:06:49Z\" " , " \" ${datestamp} \" " )
111+ assertEquals(expected, jsonFile.readText())
112+
113+ // move the date along 6 hours at a time
114+ calendar.add(Calendar .HOUR , 6 )
115+
116+ jsonFile.delete()
117+ }
118+ }
39119}
0 commit comments