Android Open Source - slf4android Logger Adapter Tests






From Project

Back to project page slf4android.

License

The source code is released under:

MIT License

If you think the Android project slf4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.brightinventions.slf4android.androidTest;
//from  w w w. ja  v  a 2  s  .  com
import android.test.AndroidTestCase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.LogManager;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class LoggerAdapterTests extends AndroidTestCase {

    public static final String LOGGER_NAME = "ApplicationTest";

    @Override
    public void setUp() throws Exception {
        super.setUp();
        LogManager.getLogManager().getLogger("").setLevel(Level.FINEST);
        clearLogcat();
    }

    private void clearLogcat() {
        try {
            Runtime.getRuntime().exec("logcat -c").waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void test_debug_message() throws Exception {
        getLogger().debug("Hello debug {} {}", 1, 2);
        assertThat(getLastMessage(), containsString("Hello debug 1 2"));
    }

    private Logger getLogger() {
        return LoggerFactory.getLogger(LOGGER_NAME);
    }

    private String getLastMessage() {
        return getLastMessage(LOGGER_NAME);
    }

    private String getLastMessage(String tag) {
        String result = null;
        try {
            Process process = Runtime.getRuntime().exec(String.format("logcat -d %s:*", tag));
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }
            result = log.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public void test_debug_message_multi_arg() throws Exception {
        getLogger().debug("Message {} {} {} {}", 1, 2, 3, 4);
        assertThat(getLastMessage(), containsString("Message 1 2 3 4"));
    }

    public void test_info_message() throws Exception {
        getLogger().info("With string '{}'", "string arg");
        assertThat(getLastMessage(), containsString("With string 'string arg'"));
    }

    public void test_warning_message() throws Exception {
        getLogger().warn("warning message", new NullPointerException("Bad"));
        String lastMessage = getLastMessage();
        assertThat(lastMessage, containsString("warning message"));
        assertThat(lastMessage, containsString(getClass().getName()));
    }

    public void test_error_message() throws Exception {
        getLogger().error("error message", new IllegalArgumentException("Wrong argument"));
        String lastMessage = getLastMessage();

        assertThat(lastMessage, containsString("error message"));
        assertThat(lastMessage, containsString("IllegalArgumentException"));
    }
}




Java Source Code List

com.squareup.seismic.ShakeDetector.java
org.slf4j.impl.StaticLoggerBinder.java
pl.brightinventions.slf4android.ActivityStateListener.java
pl.brightinventions.slf4android.AndroidLoggerAdapter.java
pl.brightinventions.slf4android.AndroidLoggerFactory.java
pl.brightinventions.slf4android.AtLeastFilter.java
pl.brightinventions.slf4android.ConstLoggerValueSupplier.java
pl.brightinventions.slf4android.DateValueSupplier.java
pl.brightinventions.slf4android.Disposable.java
pl.brightinventions.slf4android.EmailErrorReport.java
pl.brightinventions.slf4android.FileHandlerExpose.java
pl.brightinventions.slf4android.FileLogHandlerConfiguration.java
pl.brightinventions.slf4android.FileLogHandler.java
pl.brightinventions.slf4android.HandlerFormatterCompiler.java
pl.brightinventions.slf4android.LevelValueSupplier.java
pl.brightinventions.slf4android.ListLogRecordFormatter.java
pl.brightinventions.slf4android.Lists.java
pl.brightinventions.slf4android.LogLevel.java
pl.brightinventions.slf4android.LogRecordFormatterUtilFormatterAdapter.java
pl.brightinventions.slf4android.LogRecordFormatter.java
pl.brightinventions.slf4android.LogRecord.java
pl.brightinventions.slf4android.LogcatHandler.java
pl.brightinventions.slf4android.LoggerConfiguration.java
pl.brightinventions.slf4android.LoggerNameValueSupplier.java
pl.brightinventions.slf4android.LoggerPatternConfiguration.java
pl.brightinventions.slf4android.LoggerPatternValueSupplier.java
pl.brightinventions.slf4android.LoggerPattern.java
pl.brightinventions.slf4android.MakeScreenShotAsyncTask.java
pl.brightinventions.slf4android.MessageValueSupplier.java
pl.brightinventions.slf4android.NotifyDeveloperDialogDisplayActivity.java
pl.brightinventions.slf4android.NotifyDeveloperHandler.java
pl.brightinventions.slf4android.ReadLogcatEntriesAsyncTask.java
pl.brightinventions.slf4android.ThreadValueSupplier.java
pl.brightinventions.slf4android.androidTest.FileHandlerTests.java
pl.brightinventions.slf4android.androidTest.LoggerAdapterTests.java
pl.brightinventions.slf4android.androidTest.NotifyDeveloperHandlerTests.java
pl.brightinventions.slf4android.androidTest.TestActivity.java
pl.brightinventions.slf4android.roboelectric.HandlerFormatterCompilerTests.java
pl.brightinventions.slf4android.roboelectric.MessageValueSupplierTests.java
pl.brightinventions.slf4android.roboelectric.RoboelectricTest.java
pl.brightinventions.slf4android.roboelectric.RobolectricTestRunner.java