JAL-1793 update spike branch to latest
[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
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
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[] { "CSQ" }, new String[] { "csq" }), 0);
56
57     assertTrue(comp.compare(new String[] { "CSQ", "a" },
58             new String[] { "csq" }) > 0);
59
60     assertTrue(comp.compare(new String[] { "CSQ" }, new String[] { "csq",
61         "b" }) < 0);
62
63     assertTrue(comp.compare(new String[] { "CSQ", "AF" }, new String[] {
64         "csq", "ac" }) > 0);
65
66     assertTrue(comp.compare(new String[] { "CSQ", "ac" }, new String[] {
67         "csq", "AF" }) < 0);
68   }
69
70   @Test(groups = "Functional")
71   public void testGetMinMax()
72   {
73     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
74             "group");
75     FeatureAttributes fa = FeatureAttributes.getInstance();
76     assertNull(fa.getMinMax("Pfam", "kd"));
77     sf.setValue("domain", "xyz");
78     assertNull(fa.getMinMax("Pfam", "kd"));
79     sf.setValue("kd", "some text");
80     assertNull(fa.getMinMax("Pfam", "kd"));
81     sf.setValue("kd", "1.3");
82     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { 1.3f, 1.3f });
83     sf.setValue("kd", "-2.6");
84     assertEquals(fa.getMinMax("Pfam", "kd"), new float[] { -2.6f, 1.3f });
85     Map<String, String> csq = new HashMap<>();
86     csq.put("AF", "-3");
87     sf.setValue("CSQ", csq);
88     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
89             new float[]
90             { -3f, -3f });
91     csq.put("AF", "4");
92     sf.setValue("CSQ", csq);
93     assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
94             new float[]
95             { -3f, 4f });
96   }
97
98   /**
99    * Test the method that returns an attribute description, provided it is
100    * recorded and unique
101    */
102   @Test(groups = "Functional")
103   public void testGetDescription()
104   {
105     FeatureAttributes fa = FeatureAttributes.getInstance();
106     // with no description returns null
107     assertNull(fa.getDescription("Pfam", "kd"));
108     // with a unique description, returns that value
109     fa.addDescription("Pfam", "desc1", "kd");
110     assertEquals(fa.getDescription("Pfam", "kd"), "desc1");
111     // with ambiguous description, returns null
112     fa.addDescription("Pfam", "desc2", "kd");
113     assertNull(fa.getDescription("Pfam", "kd"));
114   }
115
116   @Test(groups = "Functional")
117   public void testDatatype()
118   {
119     FeatureAttributes fa = FeatureAttributes.getInstance();
120     assertNull(fa.getDatatype("Pfam", "kd"));
121     SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
122             "group");
123     sf.setValue("kd", "-1");
124     sf.setValue("domain", "Metal");
125     sf.setValue("phase", "1");
126     sf.setValue("phase", "reverse");
127     assertEquals(fa.getDatatype("Pfam", "kd"), Datatype.Number);
128     assertEquals(fa.getDatatype("Pfam", "domain"), Datatype.Character);
129     assertEquals(fa.getDatatype("Pfam", "phase"), Datatype.Mixed);
130   }
131 }