/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.datamodel; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; import org.testng.annotations.Test; public class SequenceFeatureTest { @Test(groups = { "Functional" }) public void testCopyConstructor() { SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33, 12.5f, "group"); sf1.setValue("STRAND", "+"); sf1.setValue("Note", "Testing"); Integer count = new Integer(7); sf1.setValue("Count", count); SequenceFeature sf2 = new SequenceFeature(sf1); assertEquals("type", sf2.getType()); assertEquals("desc", sf2.getDescription()); assertEquals(22, sf2.getBegin()); assertEquals(33, sf2.getEnd()); assertEquals("+", sf2.getValue("STRAND")); assertEquals("Testing", sf2.getValue("Note")); // shallow clone of otherDetails map - contains the same object values! assertSame(count, sf2.getValue("Count")); } /** * Tests for retrieving a 'miscellaneous details' property value, with or * without a supplied default */ @Test(groups = { "Functional" }) public void testGetValue() { SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33, 12.5f, "group"); sf1.setValue("STRAND", "+"); assertEquals("+", sf1.getValue("STRAND")); assertNull(sf1.getValue("strand")); // case-sensitive assertEquals(".", sf1.getValue("unknown", ".")); Integer i = new Integer(27); assertSame(i, sf1.getValue("Unknown", i)); } /** * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other */ @Test(groups = { "Functional" }) public void testGetStrand() { SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f, "group"); assertEquals(0, sf.getStrand()); sf.setValue("STRAND", "+"); assertEquals(1, sf.getStrand()); sf.setValue("STRAND", "-"); assertEquals(-1, sf.getStrand()); sf.setValue("STRAND", "."); assertEquals(0, sf.getStrand()); } /** * Tests for equality, and that equal objects have the same hashCode */ @Test(groups = { "Functional" }) public void testEqualsAndHashCode() { SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33, 12.5f, "group"); sf1.setValue("ID", "id"); sf1.setValue("Name", "name"); sf1.setValue("Parent", "parent"); sf1.setStrand("+"); sf1.setPhase("1"); SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33, 12.5f, "group"); sf2.setValue("ID", "id"); sf2.setValue("Name", "name"); sf2.setValue("Parent", "parent"); sf2.setStrand("+"); sf2.setPhase("1"); assertFalse(sf1.equals(null)); assertTrue(sf1.equals(sf2)); assertTrue(sf2.equals(sf1)); assertEquals(sf1.hashCode(), sf2.hashCode()); // changing type breaks equals: sf2.setType("Type"); assertFalse(sf1.equals(sf2)); // changing description breaks equals: sf2.setType("type"); sf2.setDescription("Desc"); assertFalse(sf1.equals(sf2)); // changing start position breaks equals: sf2.setDescription("desc"); sf2.setBegin(21); assertFalse(sf1.equals(sf2)); // changing end position breaks equals: sf2.setBegin(22); sf2.setEnd(32); assertFalse(sf1.equals(sf2)); // changing feature group breaks equals: sf2.setEnd(33); sf2.setFeatureGroup("Group"); assertFalse(sf1.equals(sf2)); // changing ID breaks equals: sf2.setFeatureGroup("group"); sf2.setValue("ID", "id2"); assertFalse(sf1.equals(sf2)); // changing Name breaks equals: sf2.setValue("ID", "id"); sf2.setValue("Name", "Name"); assertFalse(sf1.equals(sf2)); // changing Parent breaks equals: sf2.setValue("Name", "name"); sf1.setValue("Parent", "Parent"); assertFalse(sf1.equals(sf2)); // changing strand breaks equals: sf1.setValue("Parent", "parent"); sf2.setStrand("-"); assertFalse(sf1.equals(sf2)); // changing phase breaks equals: sf2.setStrand("+"); sf1.setPhase("2"); assertFalse(sf1.equals(sf2)); // restore equality as sanity check: sf1.setPhase("1"); assertTrue(sf1.equals(sf2)); assertTrue(sf2.equals(sf1)); assertEquals(sf1.hashCode(), sf2.hashCode()); // changing status doesn't change equals: sf1.setStatus("new"); assertTrue(sf1.equals(sf2)); } }