Skip to content

Commit

Permalink
Limit TimeZone setting to individual class instead of build script
Browse files Browse the repository at this point in the history
Fixes #2979.

So tests can be run without using gradle.
  • Loading branch information
TranceLove committed Jan 11, 2022
1 parent ffd6dd1 commit 9198a69
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

/**
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ 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
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])
Expand All @@ -48,13 +51,25 @@ abstract class AbstractCompressedHelperTaskTest {
) = Unit
}

private lateinit var systemTz: TimeZone

/**
* Test setup.
*/
@Before
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -47,6 +46,8 @@ abstract class AbstractExtractorTest {
protected abstract fun extractorClass(): Class<out Extractor>
protected abstract val archiveType: String

private lateinit var systemTz: TimeZone

/**
* Test setup, copy archives to storage space
*/
Expand All @@ -55,6 +56,8 @@ abstract class AbstractExtractorTest {
fun setUp() {
ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED)
copyArchivesToStorage()
systemTz = TimeZone.getDefault()
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
}

/**
Expand All @@ -70,6 +73,7 @@ abstract class AbstractExtractorTest {
.map { obj: Path -> obj.toFile() }
.forEach { obj: File -> obj.delete() }
}
TimeZone.setDefault(systemTz)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -400,5 +405,7 @@ class FileUtilsTest {
assertNotEquals(result1.date, result2.date)
assertEquals(result1.name, result2.name)
assertEquals(result1.path, result2.path)

TimeZone.setDefault(systemTz)
}
}
1 change: 0 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
1 change: 0 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9198a69

Please sign in to comment.