package com.octo.android.robospice.persistence.string;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;

import android.app.Application;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;

import com.octo.android.robospice.persistence.DurationInMillis;

@MediumTest
public class InFileStringObjectPersisterTest extends AndroidTestCase {

    private static final long FIVE_SECONDS = 5 * DurationInMillis.ONE_SECOND;

    private static final String TEST_CACHE_KEY = "TEST_CACHE_KEY";

    private InFileStringObjectPersister inFileStringObjectPersister;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Application application = (Application) getContext().getApplicationContext();
        inFileStringObjectPersister = new InFileStringObjectPersister(application);
    }

    public void testSaveDataToCacheAndReturnData() throws Exception {
        inFileStringObjectPersister.saveDataToCacheAndReturnData("coucou", TEST_CACHE_KEY);

        File cachedFile = inFileStringObjectPersister.getCacheFile(TEST_CACHE_KEY);
        assertTrue(cachedFile.exists());

        @SuppressWarnings("unchecked")
        List<String> actual = IOUtils.readLines(new FileInputStream(cachedFile), CharEncoding.UTF_8);
        assertEquals(1, actual.size());
        assertEquals("coucou", actual.get(0));
    }

    public void testLoadDataFromCache_no_expiracy() throws Exception {
        File cachedFile = inFileStringObjectPersister.getCacheFile(TEST_CACHE_KEY);
        IOUtils.write("coucou", new FileOutputStream(cachedFile), CharEncoding.UTF_8);

        String actual = inFileStringObjectPersister.loadDataFromCache(TEST_CACHE_KEY, DurationInMillis.ALWAYS_RETURNED);
        assertEquals("coucou", actual);
    }

    public void testLoadDataFromCache_not_expired() throws Exception {
        File cachedFile = inFileStringObjectPersister.getCacheFile(TEST_CACHE_KEY);
        IOUtils.write("coucou", new FileOutputStream(cachedFile), CharEncoding.UTF_8);

        String actual = inFileStringObjectPersister.loadDataFromCache(TEST_CACHE_KEY, DurationInMillis.ONE_SECOND);
        assertEquals("coucou", actual);
    }

    public void testLoadDataFromCache_expired() throws Exception {
        File cachedFile = inFileStringObjectPersister.getCacheFile(TEST_CACHE_KEY);
        IOUtils.write("coucou", new FileOutputStream(cachedFile), CharEncoding.UTF_8);
        cachedFile.setLastModified(System.currentTimeMillis() - FIVE_SECONDS);

        String actual = inFileStringObjectPersister.loadDataFromCache(TEST_CACHE_KEY, DurationInMillis.ONE_SECOND);
        assertNull(actual);
    }

    @Override
    protected void tearDown() throws Exception {
        inFileStringObjectPersister.removeAllDataFromCache();
    }

}

@@ -1671,32 +1671,33 @@
 CACHE_KEY);%0A
+%0A
         
 IOUtils.writ
@@ -1684,39 +1684,41 @@
         
-IOUtils.write(%22coucou%22,
+FileOutputStream output =
  new Fil
@@ -1742,36 +1742,114 @@
 hedFile)
-, CharEncoding.UTF_8
+;%0A        IOUtils.write(%22coucou%22, output, CharEncoding.UTF_8);%0A        IOUtils.closeQuietly(output
 );%0A%0A    
@@ -2175,39 +2175,41 @@
         
-IOUtils.write(%22coucou%22,
+FileOutputStream output =
  new Fil
@@ -2233,36 +2233,114 @@
 hedFile)
-, CharEncoding.UTF_8
+;%0A        IOUtils.write(%22coucou%22, output, CharEncoding.UTF_8);%0A        IOUtils.closeQuietly(output
 );%0A%0A    
@@ -2657,39 +2657,41 @@
         
-IOUtils.write(%22coucou%22,
+FileOutputStream output =
  new Fil
@@ -2715,36 +2715,114 @@
 hedFile)
-, CharEncoding.UTF_8
+;%0A        IOUtils.write(%22coucou%22, output, CharEncoding.UTF_8);%0A        IOUtils.closeQuietly(output
 );%0A     

Correct test in InFileStringObjectPersisterTest. Files were not closed