From 9198a69d6528f1be2363d2dbf7a3025d635d2cd6 Mon Sep 17 00:00:00 2001 From: Raymond Lai Date: Tue, 11 Jan 2022 00:24:00 +0800 Subject: [PATCH] Limit TimeZone setting to individual class instead of build script Fixes #2979. So tests can be run without using gradle. --- ...AbstractCompressedHelperTaskArchiveTest.kt | 78 +++++++++---------- .../AbstractCompressedHelperTaskTest.kt | 15 ++++ .../extractcontents/AbstractExtractorTest.kt | 10 ++- .../filesystem/files/FileUtilsTest.kt | 7 ++ gradlew | 1 - gradlew.bat | 1 - 6 files changed, 68 insertions(+), 44 deletions(-) diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskArchiveTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskArchiveTest.kt index 81a3e195d1..b637409d17 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskArchiveTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskArchiveTest.kt @@ -20,7 +20,7 @@ package com.amaze.filemanager.asynchronous.asynctasks.compress -import org.junit.Assert +import org.junit.Assert.assertEquals import org.junit.Test import java.time.ZoneId import java.time.ZonedDateTime @@ -46,9 +46,9 @@ abstract class AbstractCompressedHelperTaskArchiveTest : AbstractCompressedHelpe open fun testRoot() { val task = createTask("") val result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("test-archive", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("test-archive", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) } /** @@ -58,57 +58,57 @@ abstract class AbstractCompressedHelperTaskArchiveTest : AbstractCompressedHelpe open fun testSublevels() { var task = createTask("test-archive") var result = task.doInBackground() - Assert.assertEquals(5, result.result.size.toLong()) - Assert.assertEquals("1", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) - Assert.assertEquals("2", result.result[1].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[1].date) - Assert.assertEquals("3", result.result[2].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[2].date) - Assert.assertEquals("4", result.result[3].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[3].date) - Assert.assertEquals("a", result.result[4].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[4].date) + assertEquals(5, result.result.size.toLong()) + assertEquals("1", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals("2", result.result[1].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[1].date) + assertEquals("3", result.result[2].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[2].date) + assertEquals("4", result.result[3].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[3].date) + assertEquals("a", result.result[4].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[4].date) task = createTask("test-archive/1") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("8", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("8", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/2") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("7", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("7", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/3") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("6", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("6", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/4") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("5", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("5", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/a") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("b", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("b", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/a/b") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("c", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("c", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/a/b/c") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("d", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("d", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) task = createTask("test-archive/a/b/c/d") result = task.doInBackground() - Assert.assertEquals(1, result.result.size.toLong()) - Assert.assertEquals("lipsum.bin", result.result[0].name) - Assert.assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) + assertEquals(1, result.result.size.toLong()) + assertEquals("lipsum.bin", result.result[0].name) + assertEquals(EXPECTED_TIMESTAMP, result.result[0].date) // assertEquals(512, result.get(0).size); } diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskTest.kt index bc1e136fff..3e312733c5 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/compress/AbstractCompressedHelperTaskTest.kt @@ -29,6 +29,7 @@ import com.amaze.filemanager.adapters.data.CompressedObjectParcelable import com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult import com.amaze.filemanager.shadows.ShadowMultiDex import com.amaze.filemanager.utils.OnAsyncTaskFinished +import org.junit.After import org.junit.Before import org.junit.runner.RunWith import org.robolectric.annotation.Config @@ -36,6 +37,8 @@ import org.robolectric.shadows.ShadowEnvironment import java.io.File import java.io.FileInputStream import java.io.FileOutputStream +import java.util.* +import kotlin.collections.ArrayList @RunWith(AndroidJUnit4::class) @Config(shadows = [ShadowMultiDex::class], sdk = [JELLY_BEAN, KITKAT, P]) @@ -48,6 +51,8 @@ abstract class AbstractCompressedHelperTaskTest { ) = Unit } + private lateinit var systemTz: TimeZone + /** * Test setup. */ @@ -55,6 +60,16 @@ abstract class AbstractCompressedHelperTaskTest { fun setUp() { ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED) copyArchivesToStorage() + systemTz = TimeZone.getDefault() + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) + } + + /** + * Post test cleanup. + */ + @After + fun tearDown() { + TimeZone.setDefault(systemTz) } private fun copyArchivesToStorage() { diff --git a/app/src/test/java/com/amaze/filemanager/filesystem/compressed/extractcontents/AbstractExtractorTest.kt b/app/src/test/java/com/amaze/filemanager/filesystem/compressed/extractcontents/AbstractExtractorTest.kt index e46d1b939b..8cb807c376 100644 --- a/app/src/test/java/com/amaze/filemanager/filesystem/compressed/extractcontents/AbstractExtractorTest.kt +++ b/app/src/test/java/com/amaze/filemanager/filesystem/compressed/extractcontents/AbstractExtractorTest.kt @@ -27,9 +27,7 @@ import android.os.Environment import androidx.test.ext.junit.runners.AndroidJUnit4 import com.amaze.filemanager.file_operations.filesystem.compressed.ArchivePasswordCache import com.amaze.filemanager.shadows.ShadowMultiDex -import org.junit.After -import org.junit.Before -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import org.robolectric.annotation.Config import org.robolectric.shadows.ShadowEnvironment @@ -39,6 +37,7 @@ import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths +import java.util.* @RunWith(AndroidJUnit4::class) @Config(shadows = [ShadowMultiDex::class], sdk = [JELLY_BEAN, KITKAT, P]) @@ -47,6 +46,8 @@ abstract class AbstractExtractorTest { protected abstract fun extractorClass(): Class protected abstract val archiveType: String + private lateinit var systemTz: TimeZone + /** * Test setup, copy archives to storage space */ @@ -55,6 +56,8 @@ abstract class AbstractExtractorTest { fun setUp() { ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED) copyArchivesToStorage() + systemTz = TimeZone.getDefault() + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) } /** @@ -70,6 +73,7 @@ abstract class AbstractExtractorTest { .map { obj: Path -> obj.toFile() } .forEach { obj: File -> obj.delete() } } + TimeZone.setDefault(systemTz) } /** diff --git a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt index 14c73b041b..c9da970e2c 100644 --- a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt +++ b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt @@ -30,6 +30,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.Config import org.robolectric.annotation.LooperMode +import java.util.* @RunWith(AndroidJUnit4::class) @LooperMode(LooperMode.Mode.PAUSED) @@ -366,6 +367,10 @@ class FileUtilsTest { */ @Test fun testParseStringForHybridFileParcelable() { + + val systemTz = TimeZone.getDefault() + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) + // ls val a = "-rwxr-x--- 1 root shell 29431 2009-01-01 08:00 init.rc" val b = "lrw-r--r-- 1 root root 15 2009-01-01 08:00 product -> /system/product" @@ -400,5 +405,7 @@ class FileUtilsTest { assertNotEquals(result1.date, result2.date) assertEquals(result1.name, result2.name) assertEquals(result1.path, result2.path) + + TimeZone.setDefault(systemTz) } } diff --git a/gradlew b/gradlew index d7380dd90b..89708a6a04 100755 --- a/gradlew +++ b/gradlew @@ -8,7 +8,6 @@ # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS="" -TZ=UTC APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` diff --git a/gradlew.bat b/gradlew.bat index ffa0873d88..42d6f213a7 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -10,7 +10,6 @@ if "%OS%"=="Windows_NT" setlocal @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS= -set TZ=UTC set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0