JAL-3253 jalview.bin.Instance streamlining
[jalview.git] / src / jalview / ws / SequenceFetcher.java
index 4f82619..2b3b1ee 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.ws;
 
+import jalview.bin.Instance;
 import jalview.datamodel.DBRefSource;
 import jalview.ext.ensembl.EnsemblGene;
 import jalview.ws.dbsources.Uniprot;
@@ -31,15 +32,35 @@ import java.util.Collections;
 import java.util.List;
 
 /**
- * This implements the run-time discovery of sequence database clients.
+ * Thread safe construction of database proxies. This implements the run-time
+ * discovery of sequence database clients.
  * 
+ * TODO: extend to a configurable database plugin mechanism where classes are
+ * instantiated by reflection and queried for their DbRefSource and version
+ * association.
  */
 public class SequenceFetcher extends ASequenceFetcher
 {
+
+  /**
+   * Returns a new SequenceFetcher singleton, or a mock object if one has been
+   * set. Used by CrossRef and java.gui.SequenceFetcher.
+   * 
+   * @return
+   */
+  public static SequenceFetcher getInstance()
+  {
+    Instance j = Instance.getInstance();
+    return (j.sequenceFetcher == null
+            ? j.sequenceFetcher = new SequenceFetcher()
+            : j.sequenceFetcher);
+  }
+
   /**
-   * Thread safe construction of database proxies TODO: extend to a configurable
-   * database plugin mechanism where classes are instantiated by reflection and
-   * queried for their DbRefSource and version association.
+   * 
+   * This public constructor is only is for use by testng to anonymously
+   * subclass SequenceFetcher.
+   * 
    * 
    */
   public SequenceFetcher()
@@ -47,6 +68,19 @@ public class SequenceFetcher extends ASequenceFetcher
     addAllDatabases();
   }
 
+  /**
+   * Set the instance object to use (intended for unit testing with mock
+   * objects).
+   * 
+   * Be sure to reset to null in the tearDown method of any tests!
+   * 
+   * @param sf
+   */
+  public static void setSequenceFetcher(SequenceFetcher sf)
+  {
+    Instance.getInstance().sequenceFetcher = sf;
+  }
+
   public void addAllDatabases()
   {
     addDBRefSourceImpl(EnsemblGene.class); // includes EnsemblGenomes.class