Project

General

Profile

Download (8.77 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 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
package eu.etaxonomy.taxeditor.lazyloading;
10

    
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15

    
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;
22

    
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;
33

    
34
/**
35
 * Test class which tests remoting for persistent collections.
36
 *
37
 * @author c.mathew
38
 * @created 13.03.2014
39
 */
40
//FIXME:Remoting need to create a proper dataset for this test
41
//Could be combined with AbstractLazyInitializerTest
42
@Ignore
43
public class RemotePersistentCollectionTest extends BaseRemotingTest {
44
	private static final Logger logger = Logger.getLogger(RemotePersistentCollectionTest.class);
45

    
46
	private static IClassificationService classificationService;
47
	private static ICommonService commonService;
48

    
49
	@BeforeClass
50
	public static void initializeServices() {
51
		classificationService = getRemoteApplicationController().getClassificationService();
52
		commonService = getRemoteApplicationController().getCommonService();
53
	}
54

    
55
	/**
56
	 * Test method which checks remoting for persistent lists.
57
	 *
58
	 */
59
	@Test
60
	public void persistentListTest() {
61

    
62
		List<TaxonNode> taxonNodes = classificationService.getAllNodes();
63
		int size = taxonNodes.size();
64
		logger.debug("classificationService.getAllNodes() size : " + size);
65
		TaxonNode taxonNode = null;
66
		if(size > 0) {
67
			Assert.assertFalse(taxonNodes.isEmpty());
68

    
69
			taxonNode = taxonNodes.get(0);
70
			Assert.assertNotNull(taxonNode);
71
			Assert.assertTrue(taxonNodes.contains(taxonNode));
72

    
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();
80

    
81
			// this size call will initialise the list remotely and only return the
82
			// size of the list
83
			int remoteChildCount = commonService.size((PersistentCollection)childNodes);
84
			Assert.assertEquals(childCount, remoteChildCount);
85

    
86
			String firstNodeTaxonTitle = taxonNode.getTaxon().getTitleCache();
87
			Assert.assertNotNull(firstNodeTaxonTitle);
88

    
89
			if(childCount > 0) {
90
				Assert.assertFalse(childNodes.isEmpty());
91
				// this get call will use the already initialised list to get the
92
				// 0th element
93
				TaxonNode localTaxonNode = childNodes.get(0);
94

    
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);
98

    
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);
108
			}
109
		}
110
	}
111

    
112
//	@Test
113
//	public void persistentSetTest() {
114
//		List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
115
//		int size = classifications.size();
116
//		if(size > 0) {
117
//			Assert.assertFalse(classifications.isEmpty());
118
//
119
//			Classification classification = classifications.get(0);
120
//			Assert.assertNotNull(classification);
121
//			Assert.assertTrue(classifications.contains(classification));
122
//
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();
127
//
128
//			// this size call will initialise the list locally by internally
129
//			// calling the ICommonService.initializeCollection method
130
//			int childCount = childNodes.size();
131
//
132
//			if(childCount > 0) {
133
//				Assert.assertFalse(childNodes.isEmpty());
134
//
135
//				// this get call will use the already initialised list to get the
136
//				// 0th element
137
//				Taxon localTaxon = childNodes.get(0).getTaxon();
138
//				Assert.assertNotNull(localTaxon);
139
//
140
//				TaxonNameBase taxonName = localTaxon.getName();
141
//				Assert.assertNotNull(taxonName);
142
//
143
//				// get the list of taxa, which will give a
144
//				// proxy set which is not yet initialised
145
//				Set<Taxon> taxa = taxonName.getTaxonBases();
146
//
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);
151
//
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));
159
//				}
160
//			}
161
//		}
162
//	}
163

    
164
	@Test
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();
172

    
173
			if(taxon != null) {
174
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
175
				Iterator<TaxonDescription> descriptionsItr = descriptions.iterator();
176
				while(descriptionsItr.hasNext()) {
177
					TaxonDescription desc = descriptionsItr.next();
178
					if(desc != null) {
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);
187

    
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);
205

    
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);
214

    
215
									}
216
									return;
217
								}
218
							}
219
						}
220
					}
221
				}
222
			}
223
		}
224
	}
225
}
(4-4/4)