Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / test / java / eu / etaxonomy / taxeditor / editor / name / operation / SwapSynonymAndAcceptedOperationTest.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name.operation;
11
12 import junit.framework.Assert;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18
19 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
20 import eu.etaxonomy.cdm.model.common.ICdmBase;
21 import eu.etaxonomy.cdm.model.description.TaxonDescription;
22 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
23 import eu.etaxonomy.cdm.model.name.NonViralName;
24 import eu.etaxonomy.cdm.model.taxon.Synonym;
25 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
28 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
29 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
30 import eu.etaxonomy.taxeditor.store.operations.AbstractTaxeditorOperationTestBase;
31
32 /**
33 * @author n.hoffmann
34 * @created 02.04.2009
35 * @version 1.0
36 */
37 public class SwapSynonymAndAcceptedOperationTest extends AbstractTaxeditorOperationTestBase {
38 private static final Logger logger = Logger
39 .getLogger(SwapSynonymAndAcceptedOperationTest.class);
40
41 private static AbstractPostOperation operation;
42
43 private static Taxon taxon;
44
45 private static Taxon parentTaxon;
46
47 private static Synonym homotypicSynonym;
48
49 private static Synonym oldHeterotypicSynonym;
50
51 private static HomotypicalGroup heteroypicalGroup;
52
53 private static Taxon misapplication;
54
55 private static Taxon concept;
56
57 private static TaxonRelationshipType conceptRelationshipType;
58
59 private static Taxon childTaxon;
60
61 private static TaxonDescription description;
62
63 private static NonViralName<?> oldSynonymName;
64
65 private static NonViralName<?> oldTaxonName;
66
67
68 /**
69 * @throws java.lang.Exception
70 */
71 @BeforeClass
72 public static void setUpBeforeClass() throws Exception {
73
74 (new DefaultTermInitializer()).initialize();
75
76 // Create the original accepted taxon
77 oldTaxonName = NonViralName.NewInstance(null);
78 taxon = Taxon.NewInstance(oldTaxonName, null);
79
80 // Create its parent taxon
81 parentTaxon = Taxon.NewInstance(NonViralName.NewInstance(null), null);
82 parentTaxon.addTaxonomicChild(taxon, null, null);
83
84 // Give it a child taxon
85 childTaxon = Taxon.NewInstance(NonViralName.NewInstance(null), null);
86 childTaxon.setTaxonomicParent(taxon, null, null);
87
88 // Create a homotypic synonym for the accepted taxon
89 taxon.addHomotypicSynonymName(NonViralName.NewInstance(null), null, null);
90 // homotypicSynonym = Synonym.NewInstance(NonViralName.NewInstance(null), null);
91 // HomotypicalGroup acceptedHomotypicalGroup = HomotypicalGroup.NewInstance();
92 // acceptedHomotypicalGroup.addTypifiedName(oldTaxon.getName());
93 // acceptedHomotypicalGroup.addTypifiedName(homotypicSynonym.getName());
94
95 // Create a heterotypic synonym that will be used to create the new accepted taxon
96 oldSynonymName = NonViralName.NewInstance(null);
97 oldHeterotypicSynonym = Synonym.NewInstance(oldSynonymName, null);
98 // oldTaxon.addS .addHeterotypicSynonym(oldHeterotypicSynonym, null, null);
99 heteroypicalGroup = HomotypicalGroup.NewInstance();
100 heteroypicalGroup.addTypifiedName(oldHeterotypicSynonym.getName());
101 taxon.addSynonym(oldHeterotypicSynonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
102
103 // Create a misapplication
104 misapplication = Taxon.NewInstance(NonViralName.NewInstance(null), null);
105 taxon.addMisappliedName(misapplication, null, null);
106
107 // Create a concept relation
108 concept = Taxon.NewInstance(NonViralName.NewInstance(null), null);
109 conceptRelationshipType = TaxonRelationshipType.CONGRUENT_TO();
110 concept.addTaxonRelation(taxon, conceptRelationshipType, null, null);
111
112 // Create a description
113 description = TaxonDescription.NewInstance();
114 taxon.addDescription(description);
115
116 operation = new SwapSynonymAndAcceptedOperation
117 (null, undoContext, taxon, oldHeterotypicSynonym, postOperation, cdmEntitySessionEnabled);
118 }
119
120
121 /**
122 * Test method for {@link eu.etaxonomy.taxeditor.editor.name.operation.ChangeHomotypicGroupOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
123 * @throws ExecutionException
124 */
125 @Test
126 public void testExecute() throws ExecutionException {
127 operation.execute(null, null);
128
129 // New taxon has correct name?
130 Assert.assertEquals(taxon.getName(), oldSynonymName);
131
132 // New taxon has correct parent?
133 Assert.assertEquals(taxon.getTaxonomicParent(), parentTaxon);
134
135 // New taxon has correct child?
136 Assert.assertTrue(taxon.getTaxonomicChildren().contains(childTaxon));
137
138 // New taxon has 2 synonyms?
139 Assert.assertEquals(taxon.getSynonyms().size(), 2);
140
141 // New taxon has a synonym with the name of the previous accepted taxon?
142 Assert.assertTrue(taxon.getSynonymNames().contains(oldTaxonName));
143
144 // New taxon has misapplication?
145 Assert.assertTrue(taxon.getMisappliedNames().contains(misapplication));
146
147 // New taxon has 1 concept relation?
148 int conceptRelCount = 0;
149 for (TaxonRelationship relation : taxon.getTaxonRelations()) {
150 if (relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) ||
151 relation.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())) {
152 continue;
153 }
154 conceptRelCount++;
155 }
156 Assert.assertEquals(conceptRelCount, 1);
157
158 // New taxon has description?
159 taxon.getDescriptions().contains(description);
160 }
161
162 /**
163 * Test method for {@link eu.etaxonomy.taxeditor.editor.name.operation.ChangeHomotypicGroupOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
164 * @throws ExecutionException
165 */
166 @Test
167 public void testUndo() throws ExecutionException {
168 operation.undo(null, null);
169
170 // Assert.assertEquals(oldHomotypicalGroup, synonym.getHomotypicGroup());
171 }
172
173 /**
174 * Test method for {@link eu.etaxonomy.taxeditor.editor.name.operation.ChangeHomotypicGroupOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
175 * @throws ExecutionException
176 */
177 @Test
178 public void testRedo() throws ExecutionException {
179 operation.redo(null, null);
180
181 // Assert.assertEquals(newHomotypicalGroup, synonym.getHomotypicGroup());
182 }
183
184 /**
185 * Test method for {@link eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation#postExecute(ICdmBase)}.
186 */
187 @Test
188 public void testPostExecute() {
189 // TODO there is not post operation functionality for this class
190 // at the moment. Implement test when there is.
191 logger.warn("No post operation functionality for this class");
192 }
193 }