Merge branch 'develop' into task/JAL-2196pdbeProperties
[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
35 //import org.testng.Assert;
36 import org.testng.annotations.AfterMethod;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39
40 public class PDBEntryTest
41 {
42
43   @BeforeMethod(alwaysRun = true)
44   public void setUp() throws Exception
45   {
46   }
47
48   @AfterMethod(alwaysRun = true)
49   public void tearDown() throws Exception
50   {
51   }
52
53   @Test(groups = { "Functional" })
54   public void testEquals()
55   {
56     PDBEntry pdbEntry = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB,
57             "x/y/z/File");
58
59     // id comparison is not case sensitive
60     PDBEntry case1 = new PDBEntry("1XYZ", "A", PDBEntry.Type.PDB,
61             "x/y/z/File");
62     // chain code comparison is not case sensitive
63     PDBEntry case2 = new PDBEntry("1xyz", "a", PDBEntry.Type.PDB,
64             "x/y/z/File");
65     // different type
66     PDBEntry case3 = new PDBEntry("1xyz", "A", PDBEntry.Type.FILE,
67             "x/y/z/File");
68     // different everything
69     PDBEntry case4 = new PDBEntry(null, null, null, null);
70     // null id
71     PDBEntry case5 = new PDBEntry(null, "A", PDBEntry.Type.PDB,
72             "x/y/z/File");
73     // null chain
74     PDBEntry case6 = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
75             "x/y/z/File");
76     // null type
77     PDBEntry case7 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
78     // null file
79     PDBEntry case8 = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB, null);
80     // identical to case7
81     PDBEntry case9 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
82     // different file only
83     PDBEntry case10 = new PDBEntry("1xyz", "A", null, "a/b/c/File");
84
85     /*
86      * assertEquals will invoke PDBEntry.equals()
87      */
88     assertFalse(pdbEntry.equals(null));
89     assertFalse(pdbEntry.equals("a"));
90     assertEquals(case1, pdbEntry);
91     assertEquals(case2, pdbEntry);
92     assertNotEquals(case3, pdbEntry);
93     assertNotEquals(case4, pdbEntry);
94     assertNotEquals(case5, pdbEntry);
95     assertNotEquals(case6, pdbEntry);
96     assertNotEquals(case7, pdbEntry);
97     assertNotEquals(case8, pdbEntry);
98     assertEquals(case7, case9);
99     assertNotEquals(case9, case10);
100
101     // add properties
102     case7.setProperty("hello", "world");
103     assertNotEquals(case7, case9);
104     case9.setProperty("hello", "world");
105     assertEquals(case7, case9);
106     case9.setProperty("hello", "WORLD");
107     assertNotEquals(case7, case9);
108
109     /*
110      * change string wrapper property to string...
111      */
112     case1.setProperty("chain_code", "a");
113     assertFalse(pdbEntry.equals(case1));
114     assertFalse(case1.equals(pdbEntry));
115   }
116
117   @Test(groups = { "Functional" })
118   public void testSetChainCode()
119   {
120     PDBEntry pdbEntry = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
121             "x/y/z/File");
122     assertNull(pdbEntry.getChainCode());
123
124     pdbEntry.setChainCode("a");
125     assertEquals("a", pdbEntry.getChainCode());
126
127     pdbEntry.setChainCode(null);
128     assertNull(pdbEntry.getChainCode());
129   }
130
131   @Test(groups = { "Functional" })
132   public void testGetType()
133   {
134     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("FILE"));
135     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("File"));
136     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file"));
137     assertNotSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file "));
138   }
139
140   @Test(groups = { "Functional" })
141   public void testTypeMatches()
142   {
143     // TODO Type.matches() is not used - delete?
144     assertTrue(PDBEntry.Type.FILE.matches("FILE"));
145     assertTrue(PDBEntry.Type.FILE.matches("File"));
146     assertTrue(PDBEntry.Type.FILE.matches("file"));
147     assertFalse(PDBEntry.Type.FILE.matches("FILE "));
148   }
149
150   @Test(groups = { "Functional" })
151   public void testUpdateFrom()
152   {
153     PDBEntry pdb1 = new PDBEntry("3A6S", null, null, null);
154     PDBEntry pdb2 = new PDBEntry("3A6S", null, null, null);
155     assertTrue(pdb1.updateFrom(pdb2));
156
157     /*
158      * mismatch of pdb id not allowed
159      */
160     pdb2 = new PDBEntry("1A70", "A", null, null);
161     assertFalse(pdb1.updateFrom(pdb2));
162     assertNull(pdb1.getChainCode());
163
164     /*
165      * match of pdb id is not case sensitive
166      */
167     pdb2 = new PDBEntry("3a6s", "A", null, null);
168     assertTrue(pdb1.updateFrom(pdb2));
169     assertEquals(pdb1.getChainCode(), "A");
170     assertEquals(pdb1.getId(), "3A6S");
171
172     /*
173      * add chain - with differing case for id
174      */
175     pdb1 = new PDBEntry("3A6S", null, null, null);
176     pdb2 = new PDBEntry("3a6s", "A", null, null);
177     assertTrue(pdb1.updateFrom(pdb2));
178     assertEquals(pdb1.getChainCode(), "A");
179
180     /*
181      * change of chain is not allowed
182      */
183     pdb2 = new PDBEntry("3A6S", "B", null, null);
184     assertFalse(pdb1.updateFrom(pdb2));
185     assertEquals(pdb1.getChainCode(), "A");
186
187     /*
188      * change chain from null
189      */
190     pdb1 = new PDBEntry("3A6S", null, null, null);
191     pdb2 = new PDBEntry("3A6S", "B", null, null);
192     assertTrue(pdb1.updateFrom(pdb2));
193     assertEquals(pdb1.getChainCode(), "B");
194
195     /*
196      * set file and type
197      */
198     pdb2 = new PDBEntry("3A6S", "B", Type.FILE, "filePath");
199     assertTrue(pdb1.updateFrom(pdb2));
200     assertEquals(pdb1.getFile(), "filePath");
201     assertEquals(pdb1.getType(), Type.FILE.toString());
202
203     /*
204      * change of file is not allowed
205      */
206     pdb1 = new PDBEntry("3A6S", null, null, "file1");
207     pdb2 = new PDBEntry("3A6S", "A", null, "file2");
208     assertFalse(pdb1.updateFrom(pdb2));
209     assertNull(pdb1.getChainCode());
210     assertEquals(pdb1.getFile(), "file1");
211
212     /*
213      * set type without change of file
214      */
215     pdb1 = new PDBEntry("3A6S", null, null, "file1");
216     pdb2 = new PDBEntry("3A6S", null, Type.PDB, "file1");
217     assertTrue(pdb1.updateFrom(pdb2));
218     assertEquals(pdb1.getType(), Type.PDB.toString());
219
220     /*
221      * set file with differing case of id and chain code
222      */
223     pdb1 = new PDBEntry("3A6S", "A", null, null);
224     pdb2 = new PDBEntry("3a6s", "a", Type.PDB, "file1");
225     assertTrue(pdb1.updateFrom(pdb2));
226     assertEquals(pdb1.getType(), Type.PDB.toString());
227     assertEquals(pdb1.getId(), "3A6S"); // unchanged
228     assertEquals(pdb1.getFile(), "file1"); // updated
229     assertEquals(pdb1.getChainCode(), "A"); // unchanged
230
231     /*
232      * changing nothing returns true
233      */
234     pdb1 = new PDBEntry("3A6S", "A", Type.PDB, "file1");
235     pdb2 = new PDBEntry("3A6S", null, null, null);
236     assertTrue(pdb1.updateFrom(pdb2));
237     assertEquals(pdb1.getChainCode(), "A");
238     assertEquals(pdb1.getType(), Type.PDB.toString());
239     assertEquals(pdb1.getFile(), "file1");
240
241     /*
242      * add and update properties only
243      */
244     pdb1 = new PDBEntry("3A6S", null, null, null);
245     pdb2 = new PDBEntry("3A6S", null, null, null);
246     pdb1.setProperty("destination", "mars");
247     pdb1.setProperty("hello", "world");
248     pdb2.setProperty("hello", "moon");
249     pdb2.setProperty("goodbye", "world");
250     assertTrue(pdb1.updateFrom(pdb2));
251     assertEquals(pdb1.getProperty("destination"), "mars");
252     assertEquals(pdb1.getProperty("hello"), "moon");
253     assertEquals(pdb1.getProperty("goodbye"), "world");
254
255     /*
256      * add properties only
257      */
258     pdb1 = new PDBEntry("3A6S", null, null, null);
259     pdb2 = new PDBEntry("3A6S", null, null, null);
260     pdb2.setProperty("hello", "moon");
261     assertTrue(pdb1.updateFrom(pdb2));
262     assertEquals(pdb1.getProperty("hello"), "moon");
263   }
264
265   @Test(groups = { "Functional" })
266   public void testConstructor_fromDbref()
267   {
268     PDBEntry pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70"));
269     assertEquals(pdb.getId(), "1A70");
270     assertNull(pdb.getChainCode());
271     assertNull(pdb.getType());
272     assertNull(pdb.getFile());
273
274     /*
275      * from dbref with chain code appended
276      */
277     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70B"));
278     assertEquals(pdb.getId(), "1A70");
279     assertEquals(pdb.getChainCode(), "B");
280
281     /*
282      * from dbref with overlong accession
283      */
284     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70BC"));
285     assertEquals(pdb.getId(), "1A70BC");
286     assertNull(pdb.getChainCode());
287
288     /*
289      * from dbref which is not for PDB
290      */
291     try
292     {
293       pdb = new PDBEntry(new DBRefEntry("PDBe", "0", "1A70"));
294       fail("Expected exception");
295     } catch (IllegalArgumentException e)
296     {
297       // expected;
298     }
299   }
300
301 }