2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.remoting
;
11 import java
.util
.Iterator
;
12 import java
.util
.List
;
16 import org
.apache
.log4j
.Level
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.collection
.internal
.AbstractPersistentCollection
;
19 import org
.hibernate
.collection
.spi
.PersistentCollection
;
20 import org
.junit
.Assert
;
21 import org
.junit
.BeforeClass
;
22 import org
.junit
.Test
;
23 import org
.unitils
.UnitilsJUnit4
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
40 * Test class which tests remoting for persistent collections.
45 public class RemotePersistentCollectionTest
extends RemoteApplicationConfigurationTest
{
46 private static final Logger logger
= Logger
.getLogger(RemotePersistentCollectionTest
.class);
48 private static IClassificationService classificationService
;
51 public static void initializeServices() {
52 Logger
.getRootLogger().setLevel(Level
.INFO
);
53 classificationService
= applicationController
.getClassificationService();
57 * Test method which checks remoting for persistent lists.
61 public void persistentListTest() {
63 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
64 int size
= taxonNodes
.size();
65 logger
.debug("classificationService.getAllNodes() size : " + size
);
66 TaxonNode taxonNode
= null;
68 Assert
.assertFalse(taxonNodes
.isEmpty());
70 taxonNode
= taxonNodes
.get(0);
71 Assert
.assertNotNull(taxonNode
);
72 Assert
.assertTrue(taxonNodes
.contains(taxonNode
));
74 // get the list of child nodes, which will give a
75 // proxy list which is not yet initialised
76 List
<TaxonNode
> childNodes
= taxonNode
.getChildNodes();
77 // this size call will first initialise the list locally by internally
78 // calling the ICommonService.initializeCollection method and then
79 // call size on the initialised list
80 int childCount
= childNodes
.size();
82 // this size call will initialise the list remotely and only return the
84 int remoteChildCount
= applicationController
.getCommonService().size((PersistentCollection
)childNodes
);
85 Assert
.assertEquals(childCount
, remoteChildCount
);
87 String firstNodeTaxonTitle
= taxonNode
.getTaxon().getTitleCache();
88 Assert
.assertNotNull(firstNodeTaxonTitle
);
91 Assert
.assertFalse(childNodes
.isEmpty());
92 // this get call will use the already initialised list to get the
94 TaxonNode localTaxonNode
= childNodes
.get(0);
96 // this get call will initialise the list remotely and only return the
97 // 0th element from the list
98 TaxonNode remoteTaxonNode
= (TaxonNode
)applicationController
.getCommonService().get((PersistentCollection
)childNodes
,0);
100 // the locally and remotely retrieved taxon node should exist in the
101 // (local and remote) child nodes list, should be not-null and should be equal to each other
102 Assert
.assertTrue(taxonNode
.getChildNodes().contains(localTaxonNode
));
103 Assert
.assertTrue(taxonNode
.getChildNodes().contains(remoteTaxonNode
));
104 Assert
.assertTrue(applicationController
.getCommonService().contains((PersistentCollection
)childNodes
, localTaxonNode
));
105 Assert
.assertTrue(applicationController
.getCommonService().contains((PersistentCollection
)childNodes
, remoteTaxonNode
));
106 Assert
.assertNotNull(remoteTaxonNode
);
107 Assert
.assertNotNull(localTaxonNode
);
108 Assert
.assertEquals(remoteTaxonNode
,localTaxonNode
);
114 public void persistentSetTest() {
115 List
<Classification
> classifications
= classificationService
.listClassifications(1,0,null,null);
116 int size
= classifications
.size();
118 Assert
.assertFalse(classifications
.isEmpty());
120 Classification classification
= classifications
.get(0);
121 Assert
.assertNotNull(classification
);
122 Assert
.assertTrue(classifications
.contains(classification
));
124 TaxonNode rootTaxonNode
= classification
.getRootNode();
125 // get the list of child nodes, which will give a
126 // proxy list which is not yet initialised
127 List
<TaxonNode
> childNodes
= rootTaxonNode
.getChildNodes();
129 // this size call will initialise the list locally by internally
130 // calling the ICommonService.initializeCollection method
131 int childCount
= childNodes
.size();
134 Assert
.assertFalse(childNodes
.isEmpty());
136 // this get call will use the already initialised list to get the
138 Taxon localTaxon
= childNodes
.get(0).getTaxon();
139 Assert
.assertNotNull(localTaxon
);
141 TaxonNameBase taxonName
= localTaxon
.getName();
142 Assert
.assertNotNull(taxonName
);
144 // get the list of taxa, which will give a
145 // proxy set which is not yet initialised
146 Set
<Taxon
> taxa
= taxonName
.getTaxonBases();
148 // this size call will initialise the list locally by internally
149 // calling the ICommonService.initializeCollection method
150 int taxaCount
= taxa
.size();
151 Assert
.assertNotEquals(taxaCount
,-1);
154 Assert
.assertFalse(taxa
.isEmpty());
155 // the locally retrieved taxon should exist in the
156 // (local and remote) taxon list and should be not-null
157 Assert
.assertTrue(taxa
.contains(localTaxon
));
158 Assert
.assertNotNull(localTaxon
);
159 Assert
.assertTrue(applicationController
.getCommonService().contains((PersistentCollection
)taxa
, localTaxon
));
166 public void persistentMapTest() {
167 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
168 // calling iterator will initialise the collection
169 Iterator
<TaxonNode
> taxonNodesItr
= taxonNodes
.iterator();
170 while(taxonNodesItr
.hasNext()) {
171 TaxonNode taxonNode
= taxonNodesItr
.next();
172 Taxon taxon
= taxonNode
.getTaxon();
175 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
176 Iterator
<TaxonDescription
> descriptionsItr
= descriptions
.iterator();
177 while(descriptionsItr
.hasNext()) {
178 TaxonDescription desc
= descriptionsItr
.next();
180 for (DescriptionElementBase element
: desc
.getElements()){
181 if (element
.isInstanceOf(TextData
.class)){
182 // usually a call to 'get' collections should not initialise the collection,
183 // but the 'getMultilanguageText' call internally calls readSize on the collection
184 // which triggers the initialisation
185 Map
<Language
, LanguageString
> multilanguagetextMap
= ((TextData
)element
).getMultilanguageText();
186 boolean init
= AbstractPersistentCollection
.isInitialized(multilanguagetextMap
);
187 Assert
.assertTrue(init
);
189 if(!multilanguagetextMap
.isEmpty()) {
190 // found a map whcih we can test!
191 logger
.info("Found Non-empty multilanguagetextMap");
192 boolean empty
= applicationController
.getCommonService().isEmpty((PersistentCollection
)multilanguagetextMap
);
193 Assert
.assertFalse(empty
);
194 // test retrieval of key set, which should already by initialised
195 Set
<Language
> langKeySet
= multilanguagetextMap
.keySet();
196 Iterator
<Language
> langKeySetItr
= langKeySet
.iterator();
197 while(langKeySetItr
.hasNext()) {
198 Language key
= langKeySetItr
.next();
199 // testing 'containsKey' on locally initialised collection
200 boolean localContainsKey
= multilanguagetextMap
.containsKey(key
);
201 Assert
.assertTrue(localContainsKey
);
202 // testing 'containsKey' on remotely initialised collection
203 boolean remoteContainsKey
=
204 applicationController
.getCommonService().containsKey((PersistentCollection
)multilanguagetextMap
, key
);
205 Assert
.assertTrue(remoteContainsKey
);
207 LanguageString value
= multilanguagetextMap
.get(key
);
208 // testing 'containsValue' on locally initialised collection
209 boolean localContainsValue
= multilanguagetextMap
.containsValue(value
);
210 Assert
.assertTrue(localContainsValue
);
211 // testing 'containsValue' on remotely initialised collection
212 boolean remoteContainsValue
=
213 applicationController
.getCommonService().containsValue((PersistentCollection
)multilanguagetextMap
, value
);
214 Assert
.assertTrue(remoteContainsValue
);