For testing: add rule to mock timezone.

Used by ExportImportTest.csv_export_only()
This commit is contained in:
Dennis Guse
2022-04-23 14:42:19 +02:00
parent 574bd38a35
commit b44bab1ab5
2 changed files with 42 additions and 0 deletions
@@ -0,0 +1,36 @@
package de.dennisguse.opentracks;
import androidx.annotation.NonNull;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.util.TimeZone;
public class TimezoneRule implements TestRule {
private final TimeZone mTimeZone;
private TimeZone mDeviceTimeZone;
public TimezoneRule(@NonNull TimeZone timeZone) {
mTimeZone = timeZone;
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
mDeviceTimeZone = TimeZone.getDefault();
TimeZone.setDefault(mTimeZone);
base.evaluate();
} finally {
TimeZone.setDefault(mDeviceTimeZone);
}
}
};
}
}