package net.straininfo2.grs.idloader.bioproject.xmlparsing;

import net.straininfo2.grs.idloader.bioproject.bindings.TypePackage;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import static junit.framework.Assert.assertEquals;

public class DocumentChunkerTest {

    /**
     * Utility function that reads the bioproject XML into binding objects.
     */
    public static List<TypePackage> parseBioProjectFile() throws
            JAXBException, SAXException, ParserConfigurationException, IOException {
        JAXBContext context = JAXBContext.newInstance(TypePackage.class);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader reader = factory.newSAXParser().getXMLReader();
        final List<TypePackage> packageList = new LinkedList<>();
        DocumentChunker chunker = new DocumentChunker(context, new PackageProcessor() {
            @Override
            public void processPackage(TypePackage nextPackage) {
                packageList.add(nextPackage);
            }
        });
        reader.setContentHandler(chunker);
        reader.parse(DocumentChunkerTest.class.getClassLoader().getResource("bioproject.xml").toExternalForm());
        return packageList;
    }

    /**
     * Pass a bioproject XML file to chunker, checks if chunking works.
     */
    @Test
    public void parseBioProjectXml() throws Exception {
        assertEquals(4, parseBioProjectFile().size());
    }
}

@@ -400,32 +400,53 @@
 io.IOException;%0A
+import java.net.URL;%0A
 import java.util
@@ -1429,21 +1429,21 @@
     
-reader.parse(
+URL xmlUrl = 
 Docu
@@ -1510,16 +1510,178 @@
 ct.xml%22)
+;%0A        if (xmlUrl == null) %7B%0A            throw new RuntimeException(%22Unable to fetch bioproject.xml%22);%0A        %7D%0A        else %7B%0A            reader.parse(xmlUrl
 .toExter
@@ -1700,16 +1700,20 @@
         
+  
+  
 return p
@@ -1724,16 +1724,26 @@
 geList;%0A
+        %7D%0A
     %7D%0A%0A 

Return more useful exception when bioproject.xml is not found