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
.junit
.Assert
;
18 import org
.junit
.BeforeClass
;
19 import org
.junit
.Ignore
;
20 import org
.junit
.Test
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
25 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
31 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
34 * Test class which tests remoting for persistent collections.
39 //FIXME:Remoting need to create a proper dataset for this test
40 //Could be combined with AbstractLazyInitializerTest
42 public class RemotePersistentCollectionTest
extends BaseRemotingTest
{
43 private static final Logger logger
= Logger
.getLogger(RemotePersistentCollectionTest
.class);
45 private static IClassificationService classificationService
;
46 private static ICommonService commonService
;
49 public static void initializeServices() {
50 classificationService
= getRemoteApplicationController().getClassificationService();
51 commonService
= getRemoteApplicationController().getCommonService();
55 * Test method which checks remoting for persistent lists.
59 public void persistentListTest() {
61 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
62 int size
= taxonNodes
.size();
63 logger
.debug("classificationService.getAllNodes() size : " + size
);
64 TaxonNode taxonNode
= null;
66 Assert
.assertFalse(taxonNodes
.isEmpty());
68 taxonNode
= taxonNodes
.get(0);
69 Assert
.assertNotNull(taxonNode
);
70 Assert
.assertTrue(taxonNodes
.contains(taxonNode
));
72 // get the list of child nodes, which will give a
73 // proxy list which is not yet initialised
74 List
<TaxonNode
> childNodes
= taxonNode
.getChildNodes();
75 // this size call will first initialise the list locally by internally
76 // calling the ICommonService.initializeCollection method and then
77 // call size on the initialised list
78 int childCount
= childNodes
.size();
80 // this size call will initialise the list remotely and only return the
82 int remoteChildCount
= commonService
.size(taxonNode
.getUuid(), "childNodes");
83 Assert
.assertEquals(childCount
, remoteChildCount
);
85 String firstNodeTaxonTitle
= taxonNode
.getTaxon().getTitleCache();
86 Assert
.assertNotNull(firstNodeTaxonTitle
);
89 Assert
.assertFalse(childNodes
.isEmpty());
90 // this get call will use the already initialised list to get the
92 TaxonNode localTaxonNode
= childNodes
.get(0);
94 // this get call will initialise the list remotely and only return the
95 // 0th element from the list
96 TaxonNode remoteTaxonNode
= (TaxonNode
)commonService
.get(taxonNode
.getUuid(), "childNodes",0);
98 // the locally and remotely retrieved taxon node should exist in the
99 // (local and remote) child nodes list, should be not-null and should be equal to each other
100 Assert
.assertTrue(taxonNode
.getChildNodes().contains(localTaxonNode
));
101 Assert
.assertTrue(taxonNode
.getChildNodes().contains(remoteTaxonNode
));
102 Assert
.assertTrue(commonService
.contains(taxonNode
.getUuid(), "childNodes", localTaxonNode
));
103 Assert
.assertTrue(commonService
.contains(taxonNode
.getUuid(), "childNodes", remoteTaxonNode
));
104 Assert
.assertNotNull(remoteTaxonNode
);
105 Assert
.assertNotNull(localTaxonNode
);
106 Assert
.assertEquals(remoteTaxonNode
,localTaxonNode
);
112 // public void persistentSetTest() {
113 // List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
114 // int size = classifications.size();
116 // Assert.assertFalse(classifications.isEmpty());
118 // Classification classification = classifications.get(0);
119 // Assert.assertNotNull(classification);
120 // Assert.assertTrue(classifications.contains(classification));
122 // TaxonNode rootTaxonNode = classification.getRootNode();
123 // // get the list of child nodes, which will give a
124 // // proxy list which is not yet initialised
125 // List<TaxonNode> childNodes = rootTaxonNode.getChildNodes();
127 // // this size call will initialise the list locally by internally
128 // // calling the ICommonService.initializeCollection method
129 // int childCount = childNodes.size();
131 // if(childCount > 0) {
132 // Assert.assertFalse(childNodes.isEmpty());
134 // // this get call will use the already initialised list to get the
136 // Taxon localTaxon = childNodes.get(0).getTaxon();
137 // Assert.assertNotNull(localTaxon);
139 // TaxonNameBase taxonName = localTaxon.getName();
140 // Assert.assertNotNull(taxonName);
142 // // get the list of taxa, which will give a
143 // // proxy set which is not yet initialised
144 // Set<Taxon> taxa = taxonName.getTaxonBases();
146 // // this size call will initialise the list locally by internally
147 // // calling the ICommonService.initializeCollection method
148 // int taxaCount = taxa.size();
149 // Assert.assertNotEquals(taxaCount,-1);
151 // if(taxaCount > 0) {
152 // Assert.assertFalse(taxa.isEmpty());
153 // // the locally retrieved taxon should exist in the
154 // // (local and remote) taxon list and should be not-null
155 // Assert.assertTrue(taxa.contains(localTaxon));
156 // Assert.assertNotNull(localTaxon);
157 // Assert.assertTrue(commonService.contains((PersistentCollection)taxa, localTaxon));
164 public void persistentMapTest() {
165 List
<TaxonNode
> taxonNodes
= classificationService
.getAllNodes();
166 // calling iterator will initialise the collection
167 Iterator
<TaxonNode
> taxonNodesItr
= taxonNodes
.iterator();
168 while(taxonNodesItr
.hasNext()) {
169 TaxonNode taxonNode
= taxonNodesItr
.next();
170 Taxon taxon
= taxonNode
.getTaxon();
173 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
174 Iterator
<TaxonDescription
> descriptionsItr
= descriptions
.iterator();
175 while(descriptionsItr
.hasNext()) {
176 TaxonDescription desc
= descriptionsItr
.next();
178 for (DescriptionElementBase element
: desc
.getElements()){
179 if (element
.isInstanceOf(TextData
.class)){
180 // usually a call to 'get' collections should not initialise the collection,
181 // but the 'getMultilanguageText' call internally calls readSize on the collection
182 // which triggers the initialisation
183 Map
<Language
, LanguageString
> multilanguagetextMap
= ((TextData
)element
).getMultilanguageText();
184 //boolean init = AbstractPersistentCollection.isInitialized(multilanguagetextMap);
185 //Assert.assertTrue(init);
187 if(!multilanguagetextMap
.isEmpty()) {
188 // found a map whcih we can test!
189 logger
.info("Found Non-empty multilanguagetextMap");
190 boolean empty
= commonService
.isEmpty(element
.getUuid(), "multilanguageText");
191 Assert
.assertFalse(empty
);
192 // test retrieval of key set, which should already by initialised
193 Set
<Language
> langKeySet
= multilanguagetextMap
.keySet();
194 Iterator
<Language
> langKeySetItr
= langKeySet
.iterator();
195 while(langKeySetItr
.hasNext()) {
196 Language key
= langKeySetItr
.next();
197 // testing 'containsKey' on locally initialised collection
198 boolean localContainsKey
= multilanguagetextMap
.containsKey(key
);
199 Assert
.assertTrue(localContainsKey
);
200 // testing 'containsKey' on remotely initialised collection
201 boolean remoteContainsKey
=
202 commonService
.containsKey(element
.getUuid(), "multilanguageText", key
);
203 Assert
.assertTrue(remoteContainsKey
);
205 LanguageString value
= multilanguagetextMap
.get(key
);
206 // testing 'containsValue' on locally initialised collection
207 boolean localContainsValue
= multilanguagetextMap
.containsValue(value
);
208 Assert
.assertTrue(localContainsValue
);
209 // testing 'containsValue' on remotely initialised collection
210 boolean remoteContainsValue
=
211 commonService
.containsValue(element
.getUuid(), "multilanguageText", value
);
212 Assert
.assertTrue(remoteContainsValue
);