Moving editor sources back into trunk
[taxeditor.git] / taxeditor-store / src / test / java / eu / etaxonomy / taxeditor / store / operations / 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.store.operations;
11
12 import java.util.Set;
13
14 import junit.framework.Assert;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
23 import eu.etaxonomy.cdm.model.description.TaxonDescription;
24 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
25 import eu.etaxonomy.cdm.model.name.NonViralName;
26 import eu.etaxonomy.cdm.model.taxon.Synonym;
27 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
30 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
31
32 /**
33 * @author n.hoffmann
34 * @created 02.04.2009
35 * @version 1.0
36 */
37 public class SwapSynonymAndAcceptedOperationTest extends AbstractTaxeditorOperationTest {
38 private static final Logger logger = Logger
39 .getLogger(SwapSynonymAndAcceptedOperationTest.class);
40
41 private static AbstractPostOperation operation;
42
43 private static Taxon oldTaxon;
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 oldTaxon = Taxon.NewInstance(oldTaxonName, null);
79
80 // Create its parent taxon
81 parentTaxon = Taxon.NewInstance(NonViralName.NewInstance(null), null);
82 parentTaxon.addTaxonomicChild(oldTaxon, null, null);
83
84 // Give it a child taxon
85 childTaxon = Taxon.NewInstance(NonViralName.NewInstance(null), null);
86 childTaxon.setTaxonomicParent(oldTaxon, null, null);
87
88 // Create a homotypic synonym for the accepted taxon
89 oldTaxon.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 oldTaxon.addSynonym(oldHeterotypicSynonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
102
103 // Create a misapplication
104 misapplication = Taxon.NewInstance(NonViralName.NewInstance(null), null);
105 oldTaxon.addMisappliedName(misapplication, null, null);
106
107 // Create a concept relation
108 concept = Taxon.NewInstance(NonViralName.NewInstance(null), null);
109 conceptRelationshipType = new TaxonRelationshipType();
110 concept.addTaxonRelation(oldTaxon, conceptRelationshipType, null, null);
111
112 // Create a description
113 description = TaxonDescription.NewInstance();
114 oldTaxon.addDescription(description);
115
116 operation = new SwapSynonymAndAcceptedOperation
117 (null, undoContext, oldTaxon, oldHeterotypicSynonym, postOperation);
118 }
119
120 private Taxon newTaxon;
121
122
123 /**
124 * Test method for {@link eu.etaxonomy.taxeditor.store.operations.ChangeHomotypicGroupOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
125 * @throws ExecutionException
126 */
127 @Test
128 public void testExecute() throws ExecutionException {
129 operation.execute(null, null);
130
131 newTaxon = ((SwapSynonymAndAcceptedOperation) operation).getNewTaxon();
132
133 // New taxon has correct name?
134 Assert.assertEquals(newTaxon.getName(), oldSynonymName);
135
136 // New taxon has correct parent?
137 Assert.assertEquals(newTaxon.getTaxonomicParent(), parentTaxon);
138
139 // New taxon has correct child?
140 Assert.assertTrue(newTaxon.getTaxonomicChildren().contains(childTaxon));
141
142 // New taxon has 2 synonyms?
143 Assert.assertEquals(newTaxon.getSynonyms().size(), 2);
144
145 // New taxon has a synonym with the name of the previous accepted taxon?
146 Assert.assertTrue(newTaxon.getSynonymNames().contains(oldTaxonName));
147
148 // New taxon has misapplication?
149 Assert.assertTrue(newTaxon.getMisappliedNames().contains(misapplication));
150
151 // New taxon has 1 concept relation?
152 int conceptRelCount = 0;
153 for (TaxonRelationship relation : newTaxon.getTaxonRelations()) {
154 if (relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) ||
155 relation.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())) {
156 continue;
157 }
158 conceptRelCount++;
159 }
160 Assert.assertEquals(conceptRelCount, 1);
161
162 // New taxon has description?
163 newTaxon.getDescriptions().contains(description);
164 }
165
166 /**
167 * Test method for {@link eu.etaxonomy.taxeditor.store.operations.ChangeHomotypicGroupOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
168 * @throws ExecutionException
169 */
170 @Test
171 public void testUndo() throws ExecutionException {
172 operation.undo(null, null);
173
174 // Assert.assertEquals(oldHomotypicalGroup, synonym.getHomotypicGroup());
175 }
176
177 /**
178 * Test method for {@link eu.etaxonomy.taxeditor.store.operations.ChangeHomotypicGroupOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
179 * @throws ExecutionException
180 */
181 @Test
182 public void testRedo() throws ExecutionException {
183 operation.redo(null, null);
184
185 // Assert.assertEquals(newHomotypicalGroup, synonym.getHomotypicGroup());
186 }
187
188 /**
189 * Test method for {@link eu.etaxonomy.taxeditor.store.operations.AbstractPostOperation#postExecute(CdmBase)}.
190 */
191 @Test
192 public void testPostExecute() {
193 // TODO there is not post operation functionality for this class
194 // at the moment. Implement test when there is.
195 logger.warn("No post operation functionality for this class");
196 }
197 }