efa004c9de66153acc093680c8013d4aa5771287
[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.assertNull;
5 import static org.testng.Assert.assertTrue;
6
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.features.FeatureAttributes.Datatype;
9
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.testng.annotations.AfterMethod;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.Test;
17
18 import junit.extensions.PA;
19
20 public class FeatureAttributesTest
21 {
22
23   /**
24    * clear down attributes map before tests
25    */
26   @BeforeClass(alwaysRun = true)
27   public void setUp()
28   {
29     FeatureAttributes fa = FeatureAttributes.getInstance();
30     ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
31   }
32
33   /**
34    * clear down attributes map after tests
35    */
36   @AfterMethod(alwaysRun = true)
37   public void tearDown()
38   {
39     FeatureAttributes fa = FeatureAttributes.getInstance();
40     ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
41   }
42
43   /**
44    * Test the method that keeps attribute names in non-case-sensitive order,
45    * including handling of 'compound' names
46    */
47   @Test(groups = "Functional")
48   public void testAttributeNameComparator()
49   {
50     FeatureAttributes fa = FeatureAttributes.getInstance();
51     Comparator<String[]> comp = (Comparator<String[]>) PA.getValue(fa,
52             "comparator");
53
54     assertEquals(
55             comp.compare(new String[]
56             { "CSQ" }, new String[] { "csq" }), 0);
57
58     assertTrue(
59             comp.compare(new String[]
60             { "CSQ", "a" }, new String[] { "csq" }) > 0);
61
62     assertTrue(
63             comp.compare(new String[]
64             { "CSQ" }, new String[] { "csq", "b" }) < 0);
65
66     assertTrue(
67             comp.compare(new String[]
68             { "CSQ", "AF" }, new String[] { "csq", "ac" }) > 0);
69
70     assertTrue(
71             comp.compare(new String[]
72             { "CSQ", "ac" }, new String[] { "csq", "AF" }) < 0);
73   }
74
75   @Test(groups = "Functional")
76   public void testGetMinMax()
77   {
78     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
79             "group");
80     FeatureAttributes fa = FeatureAttributes.getInstance();
81     assertNull(fa.getMinMax("Pfam", "kd"));
82     sf.setValue("domain", "xyz");
83     assertNull(fa.getMinMax("Pfam", "kd"));
84     sf.setValue("kd", "1.3");
85     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { 1.3f, 1.3f });
86     sf.setValue("kd", "-2.6");
87     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { -2.6f, 1.3f });
88     // setting 'mixed' character and numeric values wipes the min/max value
89     sf.setValue("kd", "some text");
90     assertNull(fa.getMinMax("Pfam", "kd"));
91
92     Map<String, String> csq = new HashMap<>();
93     csq.put("AF", "-3");
94     sf.setValue("CSQ", csq);
95     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
96             new float[]
97             { -3f, -3f });
98     csq.put("AF", "4");
99     sf.setValue("CSQ", csq);
100     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
101             new float[]
102             { -3f, 4f });
103   }
104
105   /**
106    * Test the method that returns an attribute description, provided it is
107    * recorded and unique
108    */
109   @Test(groups = "Functional")
110   public void testGetDescription()
111   {
112     FeatureAttributes fa = FeatureAttributes.getInstance();
113     // with no description returns null
114     assertNull(fa.getDescription("Pfam", "kd"));
115     // with a unique description, returns that value
116     fa.addDescription("Pfam", "desc1", "kd");
117     assertEquals(fa.getDescription("Pfam", "kd"), "desc1");
118     // with ambiguous description, returns null
119     fa.addDescription("Pfam", "desc2", "kd");
120     assertNull(fa.getDescription("Pfam", "kd"));
121   }
122
123   @Test(groups = "Functional")
124   public void testDatatype()
125   {
126     FeatureAttributes fa = FeatureAttributes.getInstance();
127     assertNull(fa.getDatatype("Pfam", "kd"));
128     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
129             "group");
130     sf.setValue("kd", "-1");
131     sf.setValue("domain", "Metal");
132     sf.setValue("phase", "1");
133     sf.setValue("phase", "reverse");
134     assertEquals(fa.getDatatype("Pfam", "kd"), Datatype.Number);
135     assertEquals(fa.getDatatype("Pfam", "domain"), Datatype.Character);
136     assertEquals(fa.getDatatype("Pfam", "phase"), Datatype.Mixed);
137   }
138 }