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