package focusedCrawler.util.parser;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Scanner;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PaginaURLTest {

    public static final Logger logger = LoggerFactory.getLogger(PaginaURLTest.class);

    @Test
    public void linksShouldNotContainFragments() throws UnsupportedEncodingException {

        String path = PaginaURLTest.class.getResource("PaginaURL/paginaURLTest").getPath();
        File URLdirecory = new File(path);

        File[] allDirectories = URLdirecory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.getName().equals(".DS_Store"))
                    return false;
                return true;
            }
        });

        for (File eachDirectory : allDirectories) {

            File[] htmlFiles = eachDirectory.listFiles();

            for (File eachtmlFile : htmlFiles) {
                URL fileUrl;
                try {
                    fileUrl = new URL(URLDecoder.decode(eachtmlFile.getName(), "UTF-8"));
                    String source = readContentsOfFile(eachtmlFile);
                    PaginaURL pageParser = new PaginaURL(fileUrl, 0, 0, source.length(), source,
                            null);
                    Object[] extractedLinks = pageParser.links();
                    assertEquals("This file has some fragments: " + eachtmlFile.getName(),
                                 false, hasFragments(extractedLinks));
                } catch (MalformedURLException e) {
                    logger.error("URL of input file not in proper format.", e);
                }
            }
        }
    }

    private boolean hasFragments(Object[] urls) {
        for (Object url : urls) {
            if (url.toString().contains("#"))
                return true;
        }
        return false;
    }

    private String readContentsOfFile(File fileUrl) {

        StringBuilder source = new StringBuilder();
        try {
            Scanner fileScanner = new Scanner(fileUrl);
            while (fileScanner.hasNext()) {
                source.append(fileScanner.nextLine() + "\n");
            }
            fileScanner.close();
        } catch (FileNotFoundException e) {
            logger.error("Unable to find file!", e);
        }
        return source.toString();
    }

}

@@ -65,9 +65,20 @@
 ert.
-*
+assertEquals
 ;%0A%0Ai
@@ -101,35 +101,8 @@
 le;%0A
-import java.io.FileFilter;%0A
 impo
@@ -244,36 +244,8 @@
 RL;%0A
-import java.net.URLDecoder;%0A
 impo
@@ -562,25 +562,24 @@
 ption %7B%0A
-%0A
         
 String p
@@ -574,427 +574,137 @@
     
-S
 tr
-ing path = PaginaURLTest.class.getResource(%22PaginaURL/paginaURLTest%22).getPath();%0A        File URLdirecory = new File(path);%0A%0A        File%5B%5D allDirectories = URLdirecory.listFiles(new FileFilter() %7B%0A            @Override%0A            public boolean accept(File pathname) %7B%0A                if (pathname.getName().equals(%22.DS_Store%22))%0A                    return false;%0A                return true;%0A     
+y %7B%0A            String testString = createTestPage();%0A            PaginaURL pageParser = new PaginaURL(new URL(%0A
        
-%7D%0A
         
 %7D);%0A
@@ -703,524 +703,156 @@
     
-%7D);%0A%0A        for (File eachDirectory : allDirectories) %7B%0A%0A            File%5B%5D htmlFiles = eachDirectory.listFiles();%0A%0A            for (File eachtmlFile : htmlFiles) %7B%0A                URL fileUrl;%0A                try %7B%0A                    fileUrl = new URL(URLDecoder.decode(eachtmlFile.getName(), %22UTF-8%22));%0A                    String source = readContentsOfFile(eachtmlFile);%0A                    PaginaURL pageParser = new PaginaURL(fileUrl, 0, 0, source.length(), source,%0A                            null);%0A        
+     %22http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic_document%22), 0,%0A                    0, testString.length(), testString, null);%0A%0A
     
@@ -917,24 +917,16 @@
         
-        
 assertEq
@@ -935,97 +935,41 @@
 ls(%22
-This file has some fragments: %22 + eachtmlFile.getName(),%0A                                
+Fragment detector test failed. %22,
  fal
@@ -1003,30 +1003,25 @@
 inks));%0A
-      
+%0A
         
   %7D catc
@@ -1008,26 +1008,24 @@
 );%0A%0A        
-  
 %7D catch (Mal
@@ -1044,32 +1044,24 @@
 eption e) %7B%0A
-        
             
@@ -1124,40 +1124,8 @@
 e);%0A
-                %7D%0A            %7D%0A
     
@@ -1358,44 +1358,27 @@
 ing 
+c
 rea
-dContentsOfFile(File fileUrl
+teTestPage(
 ) %7B%0A
-%0A
     
@@ -1393,19 +1393,20 @@
 ngBu
-ilder sourc
+ffer testPag
 e = 
@@ -1421,17 +1421,17 @@
 ngBu
-ild
+ff
 er();%0A
+%0A
     
@@ -1439,171 +1439,163 @@
    t
-ry %7B%0A            Scanner fileScanner = new Scanner(fileUrl);%0A            while (fileScanner.hasNext()) %7B%0A                source.append(fileScanner.nextLine() + %22%5Cn
+estPage.append(%22%3C!DOCTYPE html%3E%22);%0A        testPage.append(%22%3Chtml%3E%22);%0A        testPage.append(%22%3Cbody%3E%22);%0A        testPage.append(%22%3Ch1%3EMy First Heading%3C/h1%3E
 %22);%0A
@@ -1606,153 +1606,202 @@
     
- 
-   %7D%0A            fileScanner.close();%0A        %7D catch (FileNotFoundException e) %7B%0A            logger.error(%22Unable to find file!%22, e);%0A        %7D
+testPage.append(%22%3Ca href = %5C%22https://en.wikipedia.org/wiki/Mouse_(computing)#Mechanical_mice%5C%22%3EMy first paragraph.%3C/a%3E%22);%0A        testPage.append(%22%3C/body%3E%22);%0A        testPage.append(%22%3C/html%3E%22);%0A
 %0A   
@@ -1816,13 +1816,15 @@
 urn 
-sourc
+testPag
 e.to
@@ -1839,11 +1839,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

change test method, test one page generate by code