JAL-3956 - PDBEntry objects constructed from 3D-Beacons have authoritative IDs overri...
[jalview.git] / test / jalview / datamodel / PDBEntryTest.java
1 /*
2     assertEquals(case7, case9);
3  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4  * Copyright (C) $$Year-Rel$$ The Jalview Authors
5  * 
6  * This file is part of Jalview.
7  * 
8  * Jalview is free software: you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License 
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *  
13  * Jalview is distributed in the hope that it will be useful, but 
14  * WITHOUT ANY WARRANTY; without even the implied warranty 
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
16  * PURPOSE.  See the GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
20  * The Jalview Authors are detailed in the 'AUTHORS' file.
21  */
22 package jalview.datamodel;
23
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertFalse;
26 import static org.testng.Assert.assertNotEquals;
27 import static org.testng.Assert.assertNotSame;
28 import static org.testng.Assert.assertNull;
29 import static org.testng.Assert.assertSame;
30 import static org.testng.Assert.assertTrue;
31 import static org.testng.Assert.fail;
32
33 import jalview.datamodel.PDBEntry.Type;
34 import jalview.gui.JvOptionPane;
35
36 //import org.testng.Assert;
37 import org.testng.annotations.AfterMethod;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 public class PDBEntryTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   @BeforeMethod(alwaysRun = true)
53   public void setUp() throws Exception
54   {
55   }
56
57   @AfterMethod(alwaysRun = true)
58   public void tearDown() throws Exception
59   {
60   }
61
62   @Test(groups = { "Functional" })
63   public void testEquals()
64   {
65     PDBEntry pdbEntry = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB,
66             "x/y/z/File");
67
68     // id comparison is not case sensitive
69     PDBEntry case1 = new PDBEntry("1XYZ", "A", PDBEntry.Type.PDB,
70             "x/y/z/File");
71     // chain code comparison is not case sensitive
72     PDBEntry case2 = new PDBEntry("1xyz", "a", PDBEntry.Type.PDB,
73             "x/y/z/File");
74     // different type
75     PDBEntry case3 = new PDBEntry("1xyz", "A", PDBEntry.Type.FILE,
76             "x/y/z/File");
77     // different everything
78     PDBEntry case4 = new PDBEntry(null, null, null, null);
79     // null id
80     PDBEntry case5 = new PDBEntry(null, "A", PDBEntry.Type.PDB,
81             "x/y/z/File");
82     // null chain
83     PDBEntry case6 = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
84             "x/y/z/File");
85     // null type
86     PDBEntry case7 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
87     // null file
88     PDBEntry case8 = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB, null);
89     // identical to case7
90     PDBEntry case9 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
91     // different file only
92     PDBEntry case10 = new PDBEntry("1xyz", "A", null, "a/b/c/File");
93     
94     /*
95      * assertEquals will invoke PDBEntry.equals()
96      */
97     assertFalse(pdbEntry.equals(null));
98     assertFalse(pdbEntry.equals("a"));
99     assertEquals(case1, pdbEntry);
100     assertEquals(case2, pdbEntry);
101     assertNotEquals(case3, pdbEntry);
102     assertNotEquals(case4, pdbEntry);
103     assertNotEquals(case5, pdbEntry);
104     assertNotEquals(case6, pdbEntry);
105     assertNotEquals(case7, pdbEntry);
106     assertNotEquals(case8, pdbEntry);
107     assertEquals(case7, case9);
108     assertNotEquals(case9, case10);
109
110     // add properties
111     case7.setProperty("hello", "world");
112     assertNotEquals(case7, case9);
113     case9.setProperty("hello", "world");
114     assertEquals(case7, case9);
115     case9.setProperty("hello", "WORLD");
116     assertNotEquals(case7, case9);
117
118     /*
119      * change string wrapper property to string...
120      */
121     case1.setProperty("chain_code", "a");
122     assertFalse(pdbEntry.equals(case1));
123     assertFalse(case1.equals(pdbEntry));
124   }
125
126   @Test(groups = { "Functional" })
127   public void testSetChainCode()
128   {
129     PDBEntry pdbEntry = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
130             "x/y/z/File");
131     assertNull(pdbEntry.getChainCode());
132
133     pdbEntry.setChainCode("a");
134     assertEquals("a", pdbEntry.getChainCode());
135
136     pdbEntry.setChainCode(null);
137     assertNull(pdbEntry.getChainCode());
138   }
139
140   @Test(groups = { "Functional" })
141   public void testGetType()
142   {
143     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("FILE"));
144     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("File"));
145     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file"));
146     assertNotSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file "));
147   }
148
149   @Test(groups = { "Functional" })
150   public void testTypeMatches()
151   {
152     // TODO Type.matches() is not used - delete?
153     assertTrue(PDBEntry.Type.FILE.matches("FILE"));
154     assertTrue(PDBEntry.Type.FILE.matches("File"));
155     assertTrue(PDBEntry.Type.FILE.matches("file"));
156     assertFalse(PDBEntry.Type.FILE.matches("FILE "));
157   }
158
159   @Test(groups = { "Functional" })
160   public void testUpdateFrom()
161   {
162     PDBEntry pdb1 = new PDBEntry("3A6S", null, null, null);
163     PDBEntry pdb2 = new PDBEntry("3A6S", null, null, null);
164     assertTrue(pdb1.updateFrom(pdb2));
165
166     /*
167      * mismatch of pdb id not allowed
168      */
169     pdb2 = new PDBEntry("1A70", "A", null, null);
170     assertFalse(pdb1.updateFrom(pdb2));
171     assertNull(pdb1.getChainCode());
172
173     /*
174      * match of pdb id is not case sensitive
175      */
176     pdb2 = new PDBEntry("3a6s", "A", null, null);
177     assertTrue(pdb1.updateFrom(pdb2));
178     assertEquals(pdb1.getChainCode(), "A");
179     assertEquals(pdb1.getId(), "3A6S");
180
181     /*
182      * add chain - with differing case for id
183      */
184     pdb1 = new PDBEntry("3A6S", null, null, null);
185     pdb2 = new PDBEntry("3a6s", "A", null, null);
186     assertTrue(pdb1.updateFrom(pdb2));
187     assertEquals(pdb1.getChainCode(), "A");
188
189     /*
190      * change of chain is not allowed
191      */
192     pdb2 = new PDBEntry("3A6S", "B", null, null);
193     assertFalse(pdb1.updateFrom(pdb2));
194     assertEquals(pdb1.getChainCode(), "A");
195
196     /*
197      * change chain from null
198      */
199     pdb1 = new PDBEntry("3A6S", null, null, null);
200     pdb2 = new PDBEntry("3A6S", "B", null, null);
201     assertTrue(pdb1.updateFrom(pdb2));
202     assertEquals(pdb1.getChainCode(), "B");
203
204     /*
205      * set file and type
206      */
207     pdb2 = new PDBEntry("3A6S", "B", Type.FILE, "filePath");
208     assertTrue(pdb1.updateFrom(pdb2));
209     assertEquals(pdb1.getFile(), "filePath");
210     assertEquals(pdb1.getType(), Type.FILE.toString());
211     assertEquals(pdb1.getChainCode(),"B");
212     /*
213      * change of file is not allowed
214      */
215     pdb1 = new PDBEntry("3A6S", null, null, "file1");
216     pdb2 = new PDBEntry("3A6S", "A", null, "file2");
217     assertFalse(pdb1.updateFrom(pdb2));
218     assertNull(pdb1.getChainCode());
219     assertEquals(pdb1.getFile(), "file1");
220
221     /*
222      * set type without change of file
223      */
224     pdb1 = new PDBEntry("3A6S", null, null, "file1");
225     pdb2 = new PDBEntry("3A6S", null, Type.PDB, "file1");
226     assertTrue(pdb1.updateFrom(pdb2));
227     assertEquals(pdb1.getType(), Type.PDB.toString());
228
229     /*
230      * set file with differing case of id and chain code
231      */
232     pdb1 = new PDBEntry("3A6S", "A", null, null);
233     pdb2 = new PDBEntry("3a6s", "a", Type.PDB, "file1");
234     assertTrue(pdb1.updateFrom(pdb2));
235     assertEquals(pdb1.getType(), Type.PDB.toString());
236     assertEquals(pdb1.getId(), "3A6S"); // unchanged
237     assertEquals(pdb1.getFile(), "file1"); // updated
238     assertEquals(pdb1.getChainCode(), "A"); // unchanged
239
240     /*
241      * changing nothing returns true
242      */
243     pdb1 = new PDBEntry("3A6S", "A", Type.PDB, "file1");
244     pdb2 = new PDBEntry("3A6S", null, null, null);
245     assertTrue(pdb1.updateFrom(pdb2));
246     assertEquals(pdb1.getChainCode(), "A");
247     assertEquals(pdb1.getType(), Type.PDB.toString());
248     assertEquals(pdb1.getFile(), "file1");
249
250     /*
251      * add and update properties only
252      */
253     pdb1 = new PDBEntry("3A6S", null, null, null);
254     pdb2 = new PDBEntry("3A6S", null, null, null);
255     pdb1.setProperty("destination", "mars");
256     pdb1.setProperty("hello", "world");
257     pdb2.setProperty("hello", "moon");
258     pdb2.setProperty("goodbye", "world");
259     assertTrue(pdb1.updateFrom(pdb2));
260     assertEquals(pdb1.getProperty("destination"), "mars");
261     assertEquals(pdb1.getProperty("hello"), "moon");
262     assertEquals(pdb1.getProperty("goodbye"), "world");
263
264     /*
265      * add properties only
266      */
267     pdb1 = new PDBEntry("3A6S", null, null, null);
268     pdb2 = new PDBEntry("3A6S", null, null, null);
269     pdb2.setProperty("hello", "moon");
270     assertTrue(pdb1.updateFrom(pdb2));
271     assertEquals(pdb1.getProperty("hello"), "moon");
272     
273     /*
274     * different id but authoritative
275     */
276     pdb1 = new PDBEntry("af:1xyz", "A", null, "a/b/c/File");
277     pdb2 = new PDBEntry("af-1xyz", "A", null, "a/b/c/File");
278     pdb1.setAuthoritative(true);
279
280     assertTrue(pdb1.isAuthoritative());
281     assertFalse(pdb2.isAuthoritative());
282     // can update pdb1 (authoritative) from pdb2 (non-authoritative)
283     assertTrue(pdb1.updateFrom(pdb2));
284     // but the ID must remain the same
285     assertEquals(pdb1.getId(),"af:1xyz");
286     
287   }
288
289   @Test(groups = { "Functional" })
290   public void testConstructor_fromDbref()
291   {
292     PDBEntry pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70"));
293     assertEquals(pdb.getId(), "1A70");
294     assertNull(pdb.getChainCode());
295     assertNull(pdb.getType());
296     assertNull(pdb.getFile());
297
298     /*
299      * from dbref with chain code appended
300      */
301     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70B"));
302     assertEquals(pdb.getId(), "1A70");
303     assertEquals(pdb.getChainCode(), "B");
304
305     /*
306      * from dbref with overlong accession
307      */
308     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70BC"));
309     assertEquals(pdb.getId(), "1A70BC");
310     assertNull(pdb.getChainCode());
311
312     /*
313      * from dbref which is not for PDB
314      */
315     try
316     {
317       pdb = new PDBEntry(new DBRefEntry("PDBe", "0", "1A70"));
318       fail("Expected exception");
319     } catch (IllegalArgumentException e)
320     {
321       // expected;
322     }
323   }
324
325 }