JAL-3020 cache selected attribute terms for filter value drop-down
[jalview.git] / test / jalview / datamodel / features / FeatureAttributesTest.java
1 package jalview.datamodel.features;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertNull;
6 import static org.testng.Assert.assertTrue;
7
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.features.FeatureAttributes.Datatype;
10
11 import java.util.Comparator;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.regex.Pattern;
16
17 import org.testng.annotations.AfterMethod;
18 import org.testng.annotations.BeforeClass;
19 import org.testng.annotations.Test;
20
21 import junit.extensions.PA;
22
23 public class FeatureAttributesTest
24 {
25
26   /**
27    * clear down attributes map before tests
28    */
29   @BeforeClass(alwaysRun = true)
30   public void setUp()
31   {
32     FeatureAttributes fa = FeatureAttributes.getInstance();
33     ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
34   }
35
36   /**
37    * clear down attributes map after tests
38    */
39   @AfterMethod(alwaysRun = true)
40   public void tearDown()
41   {
42     FeatureAttributes fa = FeatureAttributes.getInstance();
43     ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
44   }
45
46   /**
47    * Test the method that keeps attribute names in non-case-sensitive order,
48    * including handling of 'compound' names
49    */
50   @Test(groups="Functional")
51   public void testAttributeNameComparator()
52   {
53     FeatureAttributes fa = FeatureAttributes.getInstance();
54     Comparator<String[]> comp = (Comparator<String[]>) PA.getValue(fa,
55             "comparator");
56
57     assertEquals(
58             comp.compare(new String[] { "CSQ" }, new String[] { "csq" }), 0);
59
60     assertTrue(comp.compare(new String[] { "CSQ", "a" },
61             new String[] { "csq" }) > 0);
62
63     assertTrue(comp.compare(new String[] { "CSQ" }, new String[] { "csq",
64         "b" }) < 0);
65
66     assertTrue(comp.compare(new String[] { "CSQ", "AF" }, new String[] {
67         "csq", "ac" }) > 0);
68
69     assertTrue(comp.compare(new String[] { "CSQ", "ac" }, new String[] {
70         "csq", "AF" }) < 0);
71   }
72
73   @Test(groups = "Functional")
74   public void testGetMinMax()
75   {
76     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
77             "group");
78     FeatureAttributes fa = FeatureAttributes.getInstance();
79     assertNull(fa.getMinMax("Pfam", "kd"));
80     sf.setValue("domain", "xyz");
81     assertNull(fa.getMinMax("Pfam", "kd"));
82     sf.setValue("kd", "1.3");
83     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { 1.3f, 1.3f });
84     sf.setValue("kd", "-2.6");
85     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { -2.6f, 1.3f });
86     // setting 'mixed' character and numeric values wipes the min/max value
87     sf.setValue("kd", "some text");
88     assertNull(fa.getMinMax("Pfam", "kd"));
89
90     Map<String, String> csq = new HashMap<>();
91     csq.put("AF", "-3");
92     sf.setValue("CSQ", csq);
93     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
94             new float[]
95             { -3f, -3f });
96     csq.put("AF", "4");
97     sf.setValue("CSQ", csq);
98     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
99             new float[]
100             { -3f, 4f });
101   }
102
103   /**
104    * Test the method that returns an attribute description, provided it is
105    * recorded and unique
106    */
107   @Test(groups = "Functional")
108   public void testGetDescription()
109   {
110     FeatureAttributes fa = FeatureAttributes.getInstance();
111     // with no description returns null
112     assertNull(fa.getDescription("Pfam", "kd"));
113     // with a unique description, returns that value
114     fa.addDescription("Pfam", "desc1", "kd");
115     assertEquals(fa.getDescription("Pfam", "kd"), "desc1");
116     // with ambiguous description, returns null
117     fa.addDescription("Pfam", "desc2", "kd");
118     assertNull(fa.getDescription("Pfam", "kd"));
119   }
120
121   @Test(groups = "Functional")
122   public void testDatatype()
123   {
124     FeatureAttributes fa = FeatureAttributes.getInstance();
125     assertNull(fa.getDatatype("Pfam", "kd"));
126     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
127             "group");
128     sf.setValue("kd", "-1");
129     sf.setValue("domain", "Metal");
130     sf.setValue("phase", "1");
131     sf.setValue("phase", "reverse");
132     assertEquals(fa.getDatatype("Pfam", "kd"), Datatype.Number);
133     assertEquals(fa.getDatatype("Pfam", "domain"), Datatype.Character);
134     assertEquals(fa.getDatatype("Pfam", "phase"), Datatype.Mixed);
135   }
136
137   @Test(groups = "Functional")
138   public void testGetFieldMatchers()
139   {
140     // providing a junk property key to ensure the default is used
141     List<Pattern> matchers = FeatureAttributes.getFieldMatchers("^%!",
142             ".*,ABC?,CLIN_SIG,CSQ:P.*");
143     assertEquals(matchers.size(), 4);
144
145     // first pattern .* matches anything
146     assertTrue(matchers.get(0).matcher("xyz").matches());
147
148     // second ABC? matches AB or ABC, not case-sensitive
149     assertTrue(matchers.get(1).matcher("ABC").matches());
150     assertTrue(matchers.get(1).matcher("abc").matches());
151     assertFalse(matchers.get(1).matcher("abc2").matches());
152     assertFalse(matchers.get(1).matcher("xab").matches());
153     assertFalse(matchers.get(1).matcher("xabc").matches());
154
155     // third matches CLIN_SIG
156     assertTrue(matchers.get(2).matcher("CLIN_SIG").matches());
157     assertTrue(matchers.get(2).matcher("clin_sig").matches());
158
159     // fourth matches CSQ:P followed by any characters (or none)
160     assertTrue(matchers.get(3).matcher("CSQ:P").matches());
161     assertTrue(matchers.get(3).matcher("csq:peter").matches());
162     assertFalse(matchers.get(3).matcher("CSQ:Blue Peter").matches());
163   }
164 }