Android Open Source - slf4android File Handler 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.j  ava  2s . c om*/
import android.test.AndroidTestCase;

import org.fest.util.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.GregorianCalendar;

import pl.brightinventions.slf4android.FileLogHandlerConfiguration;
import pl.brightinventions.slf4android.LoggerConfiguration;

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

public class FileHandlerTests extends AndroidTestCase {
    private Logger LOG;
    private FileLogHandlerConfiguration handler;

    @Override
    protected void setUp() throws Exception {
        LoggerConfiguration.resetConfigurationToDefault();
        handler = LoggerConfiguration.fileLogHandler(getContext());
        LoggerConfiguration.configuration().addHandlerToLogger(getClass().getSimpleName(), handler);
        LOG = LoggerFactory.getLogger(getClass().getSimpleName());

        super.setUp();
    }

    public void test_can_log_debug_into_file() throws Exception {
        LOG.debug("Hello {}", "my friend!");

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString("Hello my friend!"));
    }

    private String readAndClearFileEntries() throws IOException {
        handler.flush();
        String currentFileName = handler.getCurrentFileName();
        String contents = Files.contentOf(new File(currentFileName), Charset.forName("UTF-8"));
        FileChannel outChan = new FileOutputStream(currentFileName, true).getChannel();
        outChan.truncate(0);
        outChan.close();
        return contents;
    }

    public void test_can_log_info_with_level_into_file() throws Exception {
        LOG.info("Hello {}", "my friend!");

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString("INFO"));
    }

    public void test_can_log_warn_with_excpetion_into_file() throws Exception {
        LOG.warn("Hello {}", "my friend!", new RuntimeException("A test"));

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString("RuntimeException"));
    }

    public void test_can_log_logger_name_into_file() throws Exception {
        LOG.error("Hello {}", "my friend!");

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString(LOG.getName()));
    }

    public void test_can_log_thread_name_into_file() throws Exception {
        LOG.error("Hello {}", "my friend!");

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString(Thread.currentThread().getName()));
    }

    public void test_can_log_date_into_file() throws Exception {
        LOG.error("Hello {}", "my friend!");

        String entries = readAndClearFileEntries();

        assertThat(entries, containsString(String.valueOf(new GregorianCalendar().get(Calendar.YEAR))));
    }
}




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