Skip to content

Commit

Permalink
Use java 8 time APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
JayNewstrom committed Aug 24, 2020
1 parent 69bf835 commit 5042a28
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ buildscript {
coroutineVersion = '1.3.8'

// Google libraries
coreLibraryDesugaringVersion = '1.0.9'
appCompatVersion = '1.1.0'
constraintLayoutVersion = '1.1.3'
materialComponentsVersion = '1.1.0'
Expand Down
6 changes: 6 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ android {

compileOptions {
kotlinOptions.freeCompilerArgs += ['-module-name', "com.github.ChuckerTeam.Chucker.library"]
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion rootProject.minSdkVersion
versionName VERSION_NAME
versionCode VERSION_CODE.toInteger()
consumerProguardFiles 'proguard-rules.pro'
multiDexEnabled true
}

kotlinOptions {
Expand Down Expand Up @@ -59,6 +63,8 @@ dependencies {
implementation "com.google.code.gson:gson:$gsonVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"

coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:$coreLibraryDesugaringVersion"

testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "junit:junit:$vintageJunitVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.chuckerteam.chucker.internal.data.har

import androidx.annotation.VisibleForTesting
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.google.gson.annotations.SerializedName
import java.text.SimpleDateFormat
import java.util.Date
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale

internal data class Entry(
Expand All @@ -13,11 +13,9 @@ internal data class Entry(
@SerializedName("request") val request: Request?,
@SerializedName("response") val response: Response?
) {
@VisibleForTesting object DateFormat : ThreadLocal<SimpleDateFormat>() {
override fun initialValue(): SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US)
}

companion object {
val DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US)

fun fromHttpTransaction(transaction: HttpTransaction) = Entry(
startedDateTime = transaction.requestDate.harFormatted(),
time = transaction.tookMs ?: 0,
Expand All @@ -26,8 +24,12 @@ internal data class Entry(
)

private fun Long?.harFormatted(): String {
val date = if (this == null) Date() else Date(this)
return DateFormat.get()?.format(date) ?: ""
val date = if (this == null) {
Instant.now().atZone(ZoneId.systemDefault())
} else {
Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault())
}
return DATE_FORMAT.format(date)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package com.chuckerteam.chucker.internal.data.har
import com.chuckerteam.chucker.TestTransactionFactory
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import java.util.Date
import java.time.Instant

internal class EntryTest {
@Test fun fromHttpTransaction_createsEntryWithCorrectStartedDateTime() {
val transaction = TestTransactionFactory.createTransaction("GET")
val entry = Entry.fromHttpTransaction(transaction)

assertThat(Entry.DateFormat.get()!!.parse(entry.startedDateTime)).isEqualTo(Date(transaction.requestDate!!))
assertThat(Entry.DATE_FORMAT.parse(entry.startedDateTime, Instant::from))
.isEqualTo(Instant.ofEpochMilli(transaction.requestDate!!))
assertThat(entry.time).isEqualTo(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.chuckerteam.chucker.internal.data.har.Entry
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
import org.junit.Test
import java.util.Date
import java.time.Instant
import java.time.ZoneId

internal class HarUtilsTest {
@Test fun fromHttpTransactions_createsHarWithMultipleEntries() {
Expand All @@ -31,7 +32,7 @@ internal class HarUtilsTest {
},
"entries": [
{
"startedDateTime": "${Entry.DateFormat.get()!!.format(Date(transaction.requestDate!!))}",
"startedDateTime": "${Instant.ofEpochMilli(transaction.requestDate!!).atZone(ZoneId.systemDefault()).format(Entry.DATE_FORMAT)}",
"time": 1000,
"request": {
"method": "GET",
Expand Down

0 comments on commit 5042a28

Please sign in to comment.