JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / io / SequenceAnnotationReportTest.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.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
25 import jalview.datamodel.SequenceFeature;
26 import jalview.gui.JvOptionPane;
27
28 import java.util.Hashtable;
29 import java.util.Map;
30
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
33
34 public class SequenceAnnotationReportTest
35 {
36
37   @BeforeClass(alwaysRun = true)
38   public void setUpJvOptionPane()
39   {
40     JvOptionPane.setInteractiveMode(false);
41     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42   }
43
44   @Test(groups = "Functional")
45   public void testAppendFeature_disulfideBond()
46   {
47     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
48     StringBuilder sb = new StringBuilder();
49     sb.append("123456");
50     SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
51             3, 1.2f, "group");
52
53     // residuePos == 2 does not match start or end of feature, nothing done:
54     sar.appendFeature(sb, 2, null, sf);
55     assertEquals("123456", sb.toString());
56
57     // residuePos == 1 matches start of feature, text appended (but no <br>)
58     // feature score is not included
59     sar.appendFeature(sb, 1, null, sf);
60     assertEquals("123456disulfide bond 1:3", sb.toString());
61
62     // residuePos == 3 matches end of feature, text appended
63     // <br> is prefixed once sb.length() > 6
64     sar.appendFeature(sb, 3, null, sf);
65     assertEquals("123456disulfide bond 1:3<br>disulfide bond 1:3",
66             sb.toString());
67   }
68
69   @Test(groups = "Functional")
70   public void testAppendFeature_status()
71   {
72     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
73     StringBuilder sb = new StringBuilder();
74     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
75             Float.NaN, "group");
76     sf.setStatus("Confirmed");
77
78     sar.appendFeature(sb, 1, null, sf);
79     assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
80   }
81
82   @Test(groups = "Functional")
83   public void testAppendFeature_withScore()
84   {
85     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
86     StringBuilder sb = new StringBuilder();
87     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
88             "group");
89
90     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
91     sar.appendFeature(sb, 1, minmax, sf);
92     /*
93      * map has no entry for this feature type - score is not shown:
94      */
95     assertEquals("METAL 1 3; Fe2-S", sb.toString());
96
97     /*
98      * map has entry for this feature type - score is shown:
99      */
100     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
101     sar.appendFeature(sb, 1, minmax, sf);
102     // <br> is appended to a buffer > 6 in length
103     assertEquals("METAL 1 3; Fe2-S<br>METAL 1 3; Fe2-S Score=1.3",
104             sb.toString());
105
106     /*
107      * map has min == max for this feature type - score is not shown:
108      */
109     minmax.put("METAL", new float[][] { { 2f, 2f }, null });
110     sb.setLength(0);
111     sar.appendFeature(sb, 1, minmax, sf);
112     assertEquals("METAL 1 3; Fe2-S", sb.toString());
113   }
114
115   @Test(groups = "Functional")
116   public void testAppendFeature_noScore()
117   {
118     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
119     StringBuilder sb = new StringBuilder();
120     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
121             Float.NaN, "group");
122
123     sar.appendFeature(sb, 1, null, sf);
124     assertEquals("METAL 1 3; Fe2-S", sb.toString());
125   }
126
127   @Test(groups = "Functional")
128   public void testAppendFeature_clinicalSignificance()
129   {
130     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
131     StringBuilder sb = new StringBuilder();
132     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
133             Float.NaN, "group");
134     sf.setValue("clinical_significance", "Benign");
135
136     sar.appendFeature(sb, 1, null, sf);
137     assertEquals("METAL 1 3; Fe2-S; Benign", sb.toString());
138   }
139
140   @Test(groups = "Functional")
141   public void testAppendFeature_withScoreStatusClinicalSignificance()
142   {
143     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
144     StringBuilder sb = new StringBuilder();
145     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
146             "group");
147     sf.setStatus("Confirmed");
148     sf.setValue("clinical_significance", "Benign");
149     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
150     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
151     sar.appendFeature(sb, 1, minmax, sf);
152
153     assertEquals("METAL 1 3; Fe2-S Score=1.3; (Confirmed); Benign",
154             sb.toString());
155   }
156
157   @Test(groups = "Functional")
158   public void testAppendFeature_DescEqualsType()
159   {
160     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
161     StringBuilder sb = new StringBuilder();
162     SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
163             Float.NaN, "group");
164
165     // description is not included if it duplicates type:
166     sar.appendFeature(sb, 1, null, sf);
167     assertEquals("METAL 1 3", sb.toString());
168
169     sb.setLength(0);
170     sf.setDescription("Metal");
171     // test is case-sensitive:
172     sar.appendFeature(sb, 1, null, sf);
173     assertEquals("METAL 1 3; Metal", sb.toString());
174   }
175
176   @Test(groups = "Functional")
177   public void testAppendFeature_stripHtml()
178   {
179     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
180     StringBuilder sb = new StringBuilder();
181     SequenceFeature sf = new SequenceFeature("METAL",
182             "<html><body>hello<em>world</em></body></html>", 1, 3,
183             Float.NaN, "group");
184
185     sar.appendFeature(sb, 1, null, sf);
186     // !! strips off </body> but not <body> ??
187     assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
188
189     sb.setLength(0);
190     sf.setDescription("<br>&kHD>6");
191     sar.appendFeature(sb, 1, null, sf);
192     // if no <html> tag, html-encodes > and < (only):
193     assertEquals("METAL 1 3; &lt;br&gt;&kHD&gt;6", sb.toString());
194   }
195 }