taxonomic view and node in model
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / model / taxon / TaxonomicViewTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.model.taxon;
12
13 import static org.junit.Assert.*;
14
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.junit.After;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23
24 import eu.etaxonomy.cdm.model.name.BotanicalName;
25 import eu.etaxonomy.cdm.model.name.Rank;
26 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.reference.Journal;
28 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
29
30 /**
31 * @author a.mueller
32 * @created 01.04.2009
33 * @version 1.0
34 */
35 public class TaxonomicViewTest {
36 @SuppressWarnings("unused")
37 private static final Logger logger = Logger.getLogger(TaxonomicViewTest.class);
38
39 private static String viewName1;
40 private static TaxonomicView taxonomicView1;
41 private static TaxonNode taxonNode1;
42 private static TaxonNode taxonNode2;
43 private static TaxonNode taxonNode3;
44 private static Taxon taxon1;
45 private static Taxon taxon2;
46 private static TaxonNameBase<?,?> taxonName1;
47 private static ReferenceBase ref1;
48
49
50
51 /**
52 * @throws java.lang.Exception
53 */
54 @BeforeClass
55 public static void setUpBeforeClass() throws Exception {
56 }
57
58 /**
59 * @throws java.lang.Exception
60 */
61 @AfterClass
62 public static void tearDownAfterClass() throws Exception {
63 }
64
65 /**
66 * @throws java.lang.Exception
67 */
68 @Before
69 public void setUp() throws Exception {
70 viewName1 = "Greuther, 1993";
71 taxonomicView1 = TaxonomicView.NewInstance(viewName1);
72 taxonName1 = BotanicalName.NewInstance(Rank.SPECIES());
73 ref1 = Journal.NewInstance();
74 taxon1 = Taxon.NewInstance(taxonName1, ref1);
75 //taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
76 }
77
78 /**
79 * @throws java.lang.Exception
80 */
81 @After
82 public void tearDown() throws Exception {
83 }
84
85 //****************************** TESTS *****************************************/
86
87 /**
88 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicView#addRoot(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.reference.ReferenceBase, java.lang.String, eu.etaxonomy.cdm.model.taxon.Synonym)}.
89 */
90 @Test
91 public void testAddRoot() {
92 TaxonNameBase<?,?> synonymName = BotanicalName.NewInstance(Rank.SPECIES());
93 Synonym synonym = Synonym.NewInstance(synonymName, ref1);
94 TaxonNode taxonNode1 = taxonomicView1.addRoot(taxon1, synonym);
95
96 //test root node
97 Set<TaxonNode> rootNodes = taxonomicView1.getRootNodes();
98 assertFalse("List of root nodes should not be empty", rootNodes.isEmpty());
99 assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
100 TaxonNode root = rootNodes.iterator().next();
101 assertEquals(taxon1, root.getTaxon());
102 assertSame(taxonNode1, root);
103 assertNull(root.getReferenceForParentChildRelation());
104 assertNull(root.getMicroReferenceForParentChildRelation());
105 assertEquals(synonym, root.getSynonymToBeUsed());
106
107 //any node
108 Set<TaxonNode> allNodes = taxonomicView1.getRootNodes();
109 assertFalse("List of root nodes should not be empty", allNodes.isEmpty());
110 assertEquals("Number of root nodes should be 1", 1, allNodes.size());
111 TaxonNode anyNode = allNodes.iterator().next();
112 assertSame("Taxon for TaxonNode should be the same added to the view", taxon1, anyNode.getTaxon());
113 assertSame("TaxonNode should be the same added to the view", taxonNode1, anyNode);
114 }
115
116 /**
117 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicView#isTaxonInView(eu.etaxonomy.cdm.model.taxon.Taxon)}.
118 */
119 @Test
120 public void testIsTaxonInView() {
121 taxonomicView1.addRoot(taxon1, null);
122
123 assertTrue(taxonomicView1.isTaxonInView(taxon1));
124 Taxon anyTaxon = Taxon.NewInstance(null, null);
125 assertFalse(taxonomicView1.isTaxonInView(anyTaxon));
126 }
127
128
129 /**
130 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicView#makeRootChildOfOtherNode(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.reference.ReferenceBase, java.util.String)}.
131 */
132 @Test
133 public void testMakeRootChildOfOtherNode() {
134 TaxonNode root1 = taxonomicView1.addRoot(taxon1, null);
135 TaxonNode root2 = taxonomicView1.addRoot(taxon2, null);
136 Taxon taxon3 = Taxon.NewInstance(null, null);
137 root2.addChild(taxon3);
138 String microRef = "p55";
139
140 assertFalse("Root1 must not yet be child of root 2", root2.getChildNodes().contains(root1));
141 assertNotSame("Root2 must not yet be parent of root 1", root2, root1.getParent());
142 assertEquals("view must contain 3 nodes", 3, taxonomicView1.getAllNodes().size());
143 assertEquals("view must still contain 2 root", 2, taxonomicView1.getRootNodes().size());
144 assertEquals("root2 must have 1 child", 1, root2.getChildNodes().size());
145
146 taxonomicView1.makeRootChildOfOtherNode(root1, root2, ref1, microRef);
147 assertTrue("Root1 must be child of root 2", root2.getChildNodes().contains(root1));
148 assertSame("Root2 must be parent of root 1", root2, root1.getParent());
149 assertEquals("view must contain 3 nodes", 3, taxonomicView1.getAllNodes().size());
150 assertEquals("view must contain 1 root", 1, taxonomicView1.getRootNodes().size());
151 assertEquals("new child node must have the expected reference for parent child relationship", ref1, root1.getReferenceForParentChildRelation());
152 assertEquals("new child node must have the expected micro reference for parent child relationship", microRef, root1.getMicroReferenceForParentChildRelation());
153 assertEquals("root2 must have 2 children", 2, root2.getChildNodes().size());
154
155 }
156
157 /**
158 * Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicView#generateTitle()}.
159 */
160 @Test
161 public void testGenerateTitle() {
162 TaxonomicView taxonomicViewLocal = TaxonomicView.NewInstance(viewName1);
163 //Maybe changed if title cache is generated in a different way
164 assertEquals(viewName1, taxonomicViewLocal.getTitleCache());
165 }
166
167 }