JAL-2189 apply license
[jalview.git] / test / jalview / workers / AlignCalcManagerTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.workers;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignCalcManagerI;
28 import jalview.api.AlignCalcWorkerI;
29 import jalview.api.FeatureRenderer;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceI;
36 import jalview.gui.AlignFrame;
37
38 import java.util.Collections;
39 import java.util.List;
40
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 public class AlignCalcManagerTest
45 {
46   private AlignFrame alignFrame;
47
48   /**
49    * Test the method that removes a worker associated with an annotation,
50    * provided the worker is marked as 'deletable' (some workers should continue
51    * to run even when their results are no longer displayed)
52    */
53   @Test(groups = "Functional")
54   public void testRemoveWorkerForAnnotation()
55   {
56     AlignCalcManagerI acm = alignFrame.getViewport().getCalcManager();
57     final AlignmentAnnotation ann1 = new AlignmentAnnotation("Ann1",
58             "desc", new Annotation[] {});
59     final AlignmentAnnotation ann2 = new AlignmentAnnotation("Ann2",
60             "desc", new Annotation[] {});
61
62     /*
63      * make two workers for ann1, one deletable, one not
64      * and no worker for ann2
65      */
66     AlignCalcWorkerI worker1 = makeWorker(ann1, true);
67     AlignCalcWorkerI worker2 = makeWorker(ann1, false);
68
69     /*
70      * The new workers will get run each in their own thread.
71      * We can't tell here whether they have finished, or not yet started.
72      * They have to finish to be 'seen' by getRegisteredWorkersOfClass()
73      *   registerWorker adds to the 'restartable' list but
74      *   getRegisteredWorkers reads from the 'canUpdate' list
75      *   (which is only updated after a worker has run) - why?
76      * So just give workers time to start and finish
77      */
78     synchronized (this)
79     {
80       try
81       {
82         wait(100);
83       } catch (InterruptedException e)
84       {
85         //
86       }
87     }
88
89     List<AlignCalcWorkerI> workers = acm
90             .getRegisteredWorkersOfClass(worker1.getClass());
91     assertEquals(2, workers.size());
92     assertTrue(workers.contains(worker1));
93     assertTrue(workers.contains(worker2));
94     assertFalse(acm.isDisabled(worker1));
95     assertFalse(acm.isDisabled(worker2));
96
97     /*
98      * remove workers for ann2 (there aren't any)
99      */
100     acm.removeWorkerForAnnotation(ann2);
101     assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
102             .contains(worker1));
103     assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
104             .contains(worker2));
105     assertFalse(acm.isDisabled(worker1));
106     assertFalse(acm.isDisabled(worker2));
107
108     /*
109      * remove worker2 for ann1
110      * - should delete worker1 but not worker2
111      */
112     acm.removeWorkerForAnnotation(ann1);
113     assertEquals(1, acm.getRegisteredWorkersOfClass(worker1.getClass())
114             .size());
115     assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
116             .contains(worker2));
117     assertFalse(acm.isDisabled(worker1));
118     assertFalse(acm.isDisabled(worker2));
119   }
120
121   /**
122    * Make a worker linked to the given annotation
123    * 
124    * @param ann
125    * @param deletable
126    * @return
127    */
128   AnnotationWorker makeWorker(final AlignmentAnnotation ann,
129           final boolean deletable)
130   {
131     AnnotationProviderI annotationProvider = new AnnotationProviderI()
132     {
133       @Override
134       public List<AlignmentAnnotation> calculateAnnotation(AlignmentI al,
135               FeatureRenderer fr)
136       {
137         return Collections.singletonList(ann);
138       }
139     };
140     return new AnnotationWorker(alignFrame.getViewport(),
141             alignFrame.alignPanel, annotationProvider)
142     {
143       @Override
144       public boolean isDeletable()
145       {
146         return deletable;
147       }
148
149       @Override
150       public boolean involves(AlignmentAnnotation ann1)
151       {
152         return ann == ann1;
153       }
154     };
155   }
156
157   @BeforeMethod(alwaysRun = true)
158   public void setUp()
159   {
160     AlignmentI al = new Alignment(new SequenceI[] { new Sequence("Seq1",
161             "ABC") });
162     al.setDataset(null);
163     alignFrame = new AlignFrame(al, 3, 1);
164   }
165 }