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