package focusedCrawler.link.frontier;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import focusedCrawler.util.persistence.PersistentHashtable.DB;

public class FrontierTest {

    final Path testPath = Paths.get("frontier_temp/");
    private Frontier frontier;
    
    @Before
    public void setUp() throws IOException {
        
        File file = testPath.toFile();
        if(file.exists()) {
            FileUtils.deleteDirectory(file);
        }
        
        frontier = new Frontier(testPath.toString(), 1000, DB.ROCKSDB);
    }
    
    @After
    public void tearDown() throws IOException {
        frontier.close();
        FileUtils.deleteDirectory(testPath.toFile());
    }

    @Test
    public void shouldInsertUrl() throws Exception {
        // given
        LinkRelevance link1 = new LinkRelevance(new URL("http://www.example1.com/index.html"), 1);
        LinkRelevance link2 = new LinkRelevance(new URL("http://www.example2.com/index.html"), 1);
        
        // when
        frontier.insert(link1);
        
        // then
        assertThat(frontier.exist(link1), is(1));
        assertThat(frontier.exist(link2), is(nullValue()));
    }
    
    @Test
    public void shouldInsertUrlsAndSelectGivenNumberOfUrls() throws Exception {
        // given
        LinkRelevance link1 = new LinkRelevance(new URL("http://www.example1.com/index.html"), 1);
        LinkRelevance link2 = new LinkRelevance(new URL("http://www.example2.com/index.html"), 2);
        
        // when
        frontier.insert(link1);
        frontier.insert(link2);
        
        // then
        assertThat(frontier.exist(link1), is(notNullValue()));
        assertThat(frontier.exist(link1), is(1));
        
        assertThat(frontier.exist(link2), is(notNullValue()));
        assertThat(frontier.exist(link2), is(2));
    }
    
    
    @Test
    public void shouldInsertAndDeleteUrl() throws Exception {
        // given
        LinkRelevance link1 = new LinkRelevance(new URL("http://www.example1.com/index.html"), 1);
        
        // when
        frontier.insert(link1);
        // then
        assertThat(frontier.exist(link1), is(1));
        
        // when
        frontier.delete(link1);
        // then
        assertThat(frontier.exist(link1), is(-1));
    }

}

@@ -216,32 +216,20 @@
 ert.
-*;%0A%0Aimport java.io.File;
+assertThat;%0A
 %0Aimp
@@ -436,20 +436,83 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder
 ;%0A%0Aimpor
@@ -602,63 +602,117 @@
 t %7B%0A
+    
 %0A    
-final Path testPath = Paths.get(%22frontier_temp/%22);
+@Rule%0A    public TemporaryFolder tempFolder = new TemporaryFolder();%0A%0A    public Path testPath;%0A    
 %0A   
@@ -813,139 +813,64 @@
     
-%0A        File file = testPath.toFile();%0A        if(file.exists()) %7B%0A            FileUtils.deleteDirectory(file);%0A        %7D%0A        
+testPath = Paths.get(tempFolder.newFolder().toString());
 %0A   

Use temporary folder in FrontierTest