package org.intermine.bio.dataconversion;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import junit.framework.TestCase;

/**
 * EnsemblIdResolverFactory Unit Tests
 *
 * @author Fengyuan Hu
 *
 */
public class EnsemblIdResolverFactoryTest extends TestCase {
    EnsemblIdResolverFactory factory;
    String ensemblDataFile = "resources/ensembl.data.sample";

    public EnsemblIdResolverFactoryTest() {
    }

    public EnsemblIdResolverFactoryTest(String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        factory = new EnsemblIdResolverFactory();
        IdResolverFactory.resolver = null;
        factory.createIdResolver();
    }

    public void testValidChromosomes() throws Exception {
        Set<String> validedChrs = factory.validChromosomes();
        assertTrue(validedChrs.contains("12" +
                ""));
        assertTrue(validedChrs.contains("X"));
        assertFalse(validedChrs.contains("A"));
        assertFalse(validedChrs.contains("23"));
    }

    public void testCreateFromFile() throws Exception {
        File f = new File(ensemblDataFile);
        if (!f.exists()) {
            fail("data file not found");
        }

        factory.createFromFile(f);
        // IdResolverFactory.resolver.writeToFile(new File("build/ensembl"));
        assertEquals(new LinkedHashSet<String>(Arrays.asList(new String[] {"9606"})), IdResolverFactory.resolver.getTaxons());
        assertTrue(IdResolverFactory.resolver.isPrimaryIdentifier("9606", "pid2"));
        assertEquals("pid3", IdResolverFactory.resolver.resolveId("9606", "pid3").iterator().next());
        assertEquals(Collections.EMPTY_SET, IdResolverFactory.resolver.resolveId("9606", "gene", "pid1"));
    }
}

@@ -57,16 +57,37 @@
 o.File;%0A
+import java.net.URL;%0A
 import j
@@ -103,16 +103,16 @@
 Arrays;%0A
-
 import j
@@ -431,18 +431,8 @@
  = %22
-resources/
 ense
@@ -974,24 +974,8 @@
 2%22 +
-%0A               
  %22%22)
@@ -1184,24 +1184,25 @@
 ption %7B%0A
+%0A
         
 File f =
@@ -1197,41 +1197,215 @@
     
-File f = new File(ensemblDataFile
+ClassLoader cl = getClass().getClassLoader();%0A        URL u = cl.getResource(ensemblDataFile);%0A        if (u == null) %7B%0A            fail(%22data file not found%22);%0A        %7D%0A%0A        File f = new File(u.toURI()
 );%0A 

fix test to work from bio-testall package too