package org.biomoby.shared.test;

import org.biomoby.shared.*;
import org.biomoby.registry.meta.Registry;

import junit.framework.*;

import java.net.URL;

public class MobyNamespaceTestCase extends TestCase{

    private final static String BIOMOBY_NAMESPACE_VALID = "NCBI_gi";
    private final static String BIOMOBY_NAMESPACE_INVALID = "ThisNamespaceShouldntExistInMobyCentral";
    private final static String BIOMOBY_TEST_REGISTRY_URL = "http://bioinfo.icapture.ubc.ca/cgi-bin/mobycentral/MOBY-Central.pl";
    private final static String BIOMOBY_TEST_REGISTRY_NS = "http://bioinfo.icapture.ubc.ca/MOBY/Central";

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

    /**
     * Sees if a namspace that should exist is actually retrievable from Moby Central
     */
    public void testNamespaceRetrieval(){
	MobyNamespace ns = null;
	try{
	    ns = MobyNamespace.getNamespace(BIOMOBY_NAMESPACE_VALID);
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while trying to load namespace '" + BIOMOBY_NAMESPACE_VALID + 
		 "' from the default Moby Central");
	}
	assertNotNull("Got null object when retrieving namespace '" + BIOMOBY_NAMESPACE_VALID +
		      "' from the default Moby Central", ns);
	assertNotNull("Description for the retrieved Moby namespace '" + BIOMOBY_NAMESPACE_VALID +
		      "' was null", ns.getDescription());
	assertTrue("Description for the retrieved Moby namespace '" + BIOMOBY_NAMESPACE_VALID +
		   "' was blank", ns.getDescription().trim().length() != 0);

	Registry testRegistry = new Registry("test", BIOMOBY_TEST_REGISTRY_URL, BIOMOBY_TEST_REGISTRY_NS);
	try{
	    ns = MobyNamespace.getNamespace(BIOMOBY_NAMESPACE_VALID, testRegistry);
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while trying to load namespace '" + BIOMOBY_NAMESPACE_VALID + 
		 "' from the test Moby Central");
	}
	assertNotNull("Got null object when retrieving namespace '" + BIOMOBY_NAMESPACE_VALID +
		      "' from the test Moby Central", ns);
	assertNotNull("Description for the retrieved test Moby namespace '" + BIOMOBY_NAMESPACE_VALID +
		      "' was null", ns.getDescription());
	assertTrue("Description for the retrieved test Moby namespace '" + BIOMOBY_NAMESPACE_VALID +
		   "' was blank", ns.getDescription().trim().length() != 0);

	try{
	    ns = MobyNamespace.getNamespace(BIOMOBY_NAMESPACE_INVALID, testRegistry);
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while trying to load namespace '" + BIOMOBY_NAMESPACE_INVALID + 
		 "' from the test Moby Central.  Should normally just return null instead.");
	}
	assertNull("Got object instead of null when retrieving invalid namespace '" + 
		   BIOMOBY_NAMESPACE_VALID + "' from the test Moby Central", ns);
    }

    /**
     * @return a test suite for all the test methods of this test case.
     */
    public static Test suite() {

	TestSuite suite = new TestSuite();
 	suite.addTest(new MobyNamespaceTestCase("testNamespaceRetrieval"));
        return suite;
    }

    public static void main(String[] args){
	junit.textui.TestRunner.run(suite());
    }
}
