1 package jalview.datamodel.features;
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;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.features.FeatureAttributes.Datatype;
11 import java.util.Comparator;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.regex.Pattern;
17 import org.testng.annotations.AfterMethod;
18 import org.testng.annotations.BeforeClass;
19 import org.testng.annotations.Test;
21 import junit.extensions.PA;
23 public class FeatureAttributesTest
27 * clear down attributes map before tests
29 @BeforeClass(alwaysRun = true)
32 FeatureAttributes fa = FeatureAttributes.getInstance();
33 ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
37 * clear down attributes map after tests
39 @AfterMethod(alwaysRun = true)
40 public void tearDown()
42 FeatureAttributes fa = FeatureAttributes.getInstance();
43 ((Map<?, ?>) PA.getValue(fa, "attributes")).clear();
47 * Test the method that keeps attribute names in non-case-sensitive order,
48 * including handling of 'compound' names
50 @Test(groups="Functional")
51 public void testAttributeNameComparator()
53 FeatureAttributes fa = FeatureAttributes.getInstance();
54 Comparator<String[]> comp = (Comparator<String[]>) PA.getValue(fa,
58 comp.compare(new String[] { "CSQ" }, new String[] { "csq" }), 0);
60 assertTrue(comp.compare(new String[] { "CSQ", "a" },
61 new String[] { "csq" }) > 0);
63 assertTrue(comp.compare(new String[] { "CSQ" }, new String[] { "csq",
66 assertTrue(comp.compare(new String[] { "CSQ", "AF" }, new String[] {
69 assertTrue(comp.compare(new String[] { "CSQ", "ac" }, new String[] {
73 @Test(groups = "Functional")
74 public void testGetMinMax()
76 SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
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"));
90 Map<String, String> csq = new HashMap<>();
92 sf.setValue("CSQ", csq);
93 assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
97 sf.setValue("CSQ", csq);
98 assertEquals(fa.getMinMax("Pfam", "CSQ", "AF"),
104 * Test the method that returns an attribute description, provided it is
105 * recorded and unique
107 @Test(groups = "Functional")
108 public void testGetDescription()
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"));
121 @Test(groups = "Functional")
122 public void testDatatype()
124 FeatureAttributes fa = FeatureAttributes.getInstance();
125 assertNull(fa.getDatatype("Pfam", "kd"));
126 SequenceFeature sf = new SequenceFeature("Pfam", "desc", 10, 20,
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);
137 @Test(groups = "Functional")
138 public void testGetFieldMatchers()
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);
145 // first pattern .* matches anything
146 assertTrue(matchers.get(0).matcher("xyz").matches());
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());
155 // third matches CLIN_SIG
156 assertTrue(matchers.get(2).matcher("CLIN_SIG").matches());
157 assertTrue(matchers.get(2).matcher("clin_sig").matches());
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());