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
.lazyloading
;
11 import java
.util
.Iterator
;
12 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.hibernate
.collection
.spi
.PersistentCollection
;
18 import org
.junit
.Assert
;
19 import org
.junit
.BeforeClass
;
20 import org
.junit
.Ignore
;
21 import org
.junit
.Test
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
26 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
32 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
35 * Test class which tests remoting for persistent collections.
40 //FIXME:Remoting need to create a proper dataset for this test
41 //Could be combined with AbstractLazyInitializerTest
43 public class RemotePersistentCollectionTest
extends BaseRemotingTest
{
44 private static final Logger logger
= Logger
.getLogger(RemotePersistentCollectionTest
.class);
46 private static IClassificationService classificationService
;
47 private static ICommonService commonService
;
50 public static void initializeServices() {
51 classificationService
= getRemoteApplicationController().getClassificationService();
52 commonService
= getRemoteApplicationController().getCommonService();
56 * Test method which checks remoting for persistent lists.
60 public void persistentListTest() {
62 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
63 int size
= taxonNodes
.size();
64 logger
.debug("classificationService.getAllNodes() size : " + size
);
65 TaxonNode taxonNode
= null;
67 Assert
.assertFalse(taxonNodes
.isEmpty());
69 taxonNode
= taxonNodes
.get(0);
70 Assert
.assertNotNull(taxonNode
);
71 Assert
.assertTrue(taxonNodes
.contains(taxonNode
));
73 // get the list of child nodes, which will give a
74 // proxy list which is not yet initialised
75 List
<TaxonNode
> childNodes
= taxonNode
.getChildNodes();
76 // this size call will first initialise the list locally by internally
77 // calling the ICommonService.initializeCollection method and then
78 // call size on the initialised list
79 int childCount
= childNodes
.size();
81 // this size call will initialise the list remotely and only return the
83 int remoteChildCount
= commonService
.size((PersistentCollection
)childNodes
);
84 Assert
.assertEquals(childCount
, remoteChildCount
);
86 String firstNodeTaxonTitle
= taxonNode
.getTaxon().getTitleCache();
87 Assert
.assertNotNull(firstNodeTaxonTitle
);
90 Assert
.assertFalse(childNodes
.isEmpty());
91 // this get call will use the already initialised list to get the
93 TaxonNode localTaxonNode
= childNodes
.get(0);
95 // this get call will initialise the list remotely and only return the
96 // 0th element from the list
97 TaxonNode remoteTaxonNode
= (TaxonNode
)commonService
.get((PersistentCollection
)childNodes
,0);
99 // the locally and remotely retrieved taxon node should exist in the
100 // (local and remote) child nodes list, should be not-null and should be equal to each other
101 Assert
.assertTrue(taxonNode
.getChildNodes().contains(localTaxonNode
));
102 Assert
.assertTrue(taxonNode
.getChildNodes().contains(remoteTaxonNode
));
103 Assert
.assertTrue(commonService
.contains((PersistentCollection
)childNodes
, localTaxonNode
));
104 Assert
.assertTrue(commonService
.contains((PersistentCollection
)childNodes
, remoteTaxonNode
));
105 Assert
.assertNotNull(remoteTaxonNode
);
106 Assert
.assertNotNull(localTaxonNode
);
107 Assert
.assertEquals(remoteTaxonNode
,localTaxonNode
);
113 // public void persistentSetTest() {
114 // List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
115 // int size = classifications.size();
117 // Assert.assertFalse(classifications.isEmpty());
119 // Classification classification = classifications.get(0);
120 // Assert.assertNotNull(classification);
121 // Assert.assertTrue(classifications.contains(classification));
123 // TaxonNode rootTaxonNode = classification.getRootNode();
124 // // get the list of child nodes, which will give a
125 // // proxy list which is not yet initialised
126 // List<TaxonNode> childNodes = rootTaxonNode.getChildNodes();
128 // // this size call will initialise the list locally by internally
129 // // calling the ICommonService.initializeCollection method
130 // int childCount = childNodes.size();
132 // if(childCount > 0) {
133 // Assert.assertFalse(childNodes.isEmpty());
135 // // this get call will use the already initialised list to get the
137 // Taxon localTaxon = childNodes.get(0).getTaxon();
138 // Assert.assertNotNull(localTaxon);
140 // TaxonNameBase taxonName = localTaxon.getName();
141 // Assert.assertNotNull(taxonName);
143 // // get the list of taxa, which will give a
144 // // proxy set which is not yet initialised
145 // Set<Taxon> taxa = taxonName.getTaxonBases();
147 // // this size call will initialise the list locally by internally
148 // // calling the ICommonService.initializeCollection method
149 // int taxaCount = taxa.size();
150 // Assert.assertNotEquals(taxaCount,-1);
152 // if(taxaCount > 0) {
153 // Assert.assertFalse(taxa.isEmpty());
154 // // the locally retrieved taxon should exist in the
155 // // (local and remote) taxon list and should be not-null
156 // Assert.assertTrue(taxa.contains(localTaxon));
157 // Assert.assertNotNull(localTaxon);
158 // Assert.assertTrue(commonService.contains((PersistentCollection)taxa, localTaxon));
165 public void persistentMapTest() {
166 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
167 // calling iterator will initialise the collection
168 Iterator
<TaxonNode
> taxonNodesItr
= taxonNodes
.iterator();
169 while(taxonNodesItr
.hasNext()) {
170 TaxonNode taxonNode
= taxonNodesItr
.next();
171 Taxon taxon
= taxonNode
.getTaxon();
174 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
175 Iterator
<TaxonDescription
> descriptionsItr
= descriptions
.iterator();
176 while(descriptionsItr
.hasNext()) {
177 TaxonDescription desc
= descriptionsItr
.next();
179 for (DescriptionElementBase element
: desc
.getElements()){
180 if (element
.isInstanceOf(TextData
.class)){
181 // usually a call to 'get' collections should not initialise the collection,
182 // but the 'getMultilanguageText' call internally calls readSize on the collection
183 // which triggers the initialisation
184 Map
<Language
, LanguageString
> multilanguagetextMap
= ((TextData
)element
).getMultilanguageText();
185 //boolean init = AbstractPersistentCollection.isInitialized(multilanguagetextMap);
186 //Assert.assertTrue(init);
188 if(!multilanguagetextMap
.isEmpty()) {
189 // found a map whcih we can test!
190 logger
.info("Found Non-empty multilanguagetextMap");
191 boolean empty
= commonService
.isEmpty((PersistentCollection
)multilanguagetextMap
);
192 Assert
.assertFalse(empty
);
193 // test retrieval of key set, which should already by initialised
194 Set
<Language
> langKeySet
= multilanguagetextMap
.keySet();
195 Iterator
<Language
> langKeySetItr
= langKeySet
.iterator();
196 while(langKeySetItr
.hasNext()) {
197 Language key
= langKeySetItr
.next();
198 // testing 'containsKey' on locally initialised collection
199 boolean localContainsKey
= multilanguagetextMap
.containsKey(key
);
200 Assert
.assertTrue(localContainsKey
);
201 // testing 'containsKey' on remotely initialised collection
202 boolean remoteContainsKey
=
203 commonService
.containsKey((PersistentCollection
)multilanguagetextMap
, key
);
204 Assert
.assertTrue(remoteContainsKey
);
206 LanguageString value
= multilanguagetextMap
.get(key
);
207 // testing 'containsValue' on locally initialised collection
208 boolean localContainsValue
= multilanguagetextMap
.containsValue(value
);
209 Assert
.assertTrue(localContainsValue
);
210 // testing 'containsValue' on remotely initialised collection
211 boolean remoteContainsValue
=
212 commonService
.containsValue((PersistentCollection
)multilanguagetextMap
, value
);
213 Assert
.assertTrue(remoteContainsValue
);