Enable LoggingTest with robolectric

This commit is contained in:
cpfeiffer 2016-08-28 23:51:10 +02:00
parent c93186cc56
commit bcb07ccacd
3 changed files with 28 additions and 24 deletions

View File

@ -6,10 +6,7 @@ def ABORT_ON_CHECK_FAILURE=false
tasks.withType(Test) { systemProperty 'MiFirmwareDir', System.getProperty('MiFirmwareDir', null) } tasks.withType(Test) { systemProperty 'MiFirmwareDir', System.getProperty('MiFirmwareDir', null) }
// sourceSets.test.runtimeClasspath += File('src/main/assets')
android { android {
compileOptions { compileOptions {
// for KitKat // for KitKat
sourceCompatibility JavaVersion.VERSION_1_7 sourceCompatibility JavaVersion.VERSION_1_7

View File

@ -138,7 +138,7 @@ public class FileUtils {
File[] dirs; File[] dirs;
try { try {
dirs = context.getExternalFilesDirs(null); dirs = context.getExternalFilesDirs(null);
} catch (NullPointerException ex) { } catch (NullPointerException | UnsupportedOperationException ex) {
// workaround for robolectric 3.1.2 not implementinc getExternalFilesDirs() // workaround for robolectric 3.1.2 not implementinc getExternalFilesDirs()
// https://github.com/robolectric/robolectric/issues/2531 // https://github.com/robolectric/robolectric/issues/2531
File dir = context.getExternalFilesDir(null); File dir = context.getExternalFilesDir(null);

View File

@ -2,48 +2,55 @@ package nodomain.freeyourgadget.gadgetbridge.test;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import junit.framework.AssertionFailedError;
import org.junit.After; import org.junit.After;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.Logging; import nodomain.freeyourgadget.gadgetbridge.Logging;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static junit.framework.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static junit.framework.Assert.fail; import static org.junit.Assert.fail;
/** /**
* Tests dynamic enablement and disablement of file appenders. * Tests dynamic enablement and disablement of file appenders.
* Test is currently disabled because logback-android does not work * Test is currently disabled because logback-android does not work
* inside a plain junit test. * inside a plain junit test.
*/ */
@Ignore @RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
// need sdk 19 because "WITHOUT ROWID" is not supported in robolectric/sqlite4java
public class LoggingTest { public class LoggingTest {
private static File logFilesDir;
public LoggingTest() throws Exception {
}
@BeforeClass @BeforeClass
public static void setupSuite() { public static void setupSuite() throws Exception {
System.setProperty("logback.configurationFile", "logback.xml"); logFilesDir = FileUtils.createTempDir("logfiles");
System.setProperty(Logging.PROP_LOGFILES_DIR, logFilesDir.getAbsolutePath());
File workingDir = new File(System.getProperty("user.dir"));
File configFile = new File(workingDir, "src/main/assets/logback.xml");
System.out.println(configFile.getAbsolutePath());
System.setProperty("logback.configurationFile", configFile.getAbsolutePath());
} }
private Logging logging = new Logging() { private Logging logging = new Logging() {
@Override @Override
protected String createLogDirectory() throws IOException { protected String createLogDirectory() throws IOException {
File dir = ensureLogFilesDir(); return logFilesDir.getAbsolutePath();
return dir.getAbsolutePath();
}
@NonNull
private File ensureLogFilesDir() throws IOException {
return FileUtils.createTempDir("logfiles");
} }
}; };
@ -67,7 +74,7 @@ public class LoggingTest {
public void testToggleLogging() { public void testToggleLogging() {
try { try {
File dir = getLogFilesDir(); File dir = getLogFilesDir();
} catch (AssertionFailedError ignored) { } catch (AssertionError ignored) {
// expected, as not yet set up // expected, as not yet set up
} }
@ -85,7 +92,7 @@ public class LoggingTest {
logging.setupLogging(true); logging.setupLogging(true);
assertNotNull(logging.getFileLogger()); assertNotNull(logging.getFileLogger());
assertTrue(logging.getFileLogger().isStarted()); assertTrue(logging.getFileLogger().isStarted());
} catch (AssertionFailedError ex) { } catch (AssertionError ex) {
logging.debugLoggingConfiguration(); logging.debugLoggingConfiguration();
System.err.println(System.getProperty("java.class.path")); System.err.println(System.getProperty("java.class.path"));
throw ex; throw ex;