--- /dev/null
+/**
+ * Copyright (C) 2014 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
+
+package eu.etaxonomy.taxeditor.lazyloading;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.name.BotanicalName;
+import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
+
+
+
+/**
+ * Test class which tests remoting for persistent cdm entities.
+ *
+ * FIXME:Remoting saving tests are ignored until the merge development is underway
+ * @author c.mathew
+ *
+ */
+//@DataSet
+public class RemoteLazyLoadingTest extends BaseRemotingTest {
+
+ private static final Logger logger = Logger.getLogger(RemoteLazyLoadingTest.class);
+
+ private static IClassificationService classificationService;
+ private static ITaxonService taxonService;
+
+ private static List<TaxonNode> taxonNodes;
+
+ private final UUID taxonUuid1 = UUID.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
+ private final UUID taxonUuid2 = UUID.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
+
+
+ @BeforeClass
+ public static void initializeRemoteLazyLoading() {
+
+ Logger.getRootLogger().setLevel(Level.DEBUG);
+ taxonService = getRemoteApplicationController().getTaxonService();
+
+ classificationService= getRemoteApplicationController().getClassificationService();
+ //List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
+// Assert.assertFalse(classifications.isEmpty());
+//
+// Classification classification = classifications.get(0);
+// Assert.assertNotNull(classification);
+ taxonNodes = classificationService.getAllNodes();
+ Assert.assertFalse(taxonNodes.isEmpty());
+
+ }
+
+
+
+ @Test
+ public void testCDMEntityGet() {
+ Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
+ int maxcount = 30;
+ int count = 0;
+ while(taxonNodeItr.hasNext() && count <= maxcount) {
+ TaxonNode taxonNode = taxonNodeItr.next();
+ Assert.assertNotNull(taxonNode);
+
+ Taxon taxon = taxonNode.getTaxon();
+ Assert.assertNotNull(taxon);
+
+ String taxonTitle = taxon.getTitleCache();
+ logger.info("Taxon : " + taxonTitle);
+
+ TaxonNameBase name = taxon.getName();
+ Assert.assertNotNull(name);
+
+ String nameTitle = name.getTitleCache();
+ logger.info("Taxon Name : " + nameTitle);
+
+ count++;
+ }
+
+
+ }
+
+ @Test
+ public void test() {
+
+ }
+
+ @Test
+ public void testCDMEntitySaveEager() {
+ Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+ String oldTitleCache = taxon.getTitleCache();
+
+ System.out.println("Taxon title : " + oldTitleCache);
+
+ taxon.setTitleCache(oldTitleCache + ":updated");
+ taxonService.merge(taxon);
+
+ Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+ System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
+
+ Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,taxonNew.getTitleCache());
+
+ taxonNew.setTitleCache(oldTitleCache);
+ taxonService.merge(taxonNew);
+
+ Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+ System.out.println("Old Taxon Title : " + taxonOld.getTitleCache());
+
+ Assert.assertEquals("Title caches should be equal",oldTitleCache,taxonOld.getTitleCache());
+
+ }
+
+
+ @Test
+ public void testCDMEntityUpdate() {
+
+ Team combAuthor = Team.NewInstance();
+ combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
+ BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
+ name.setCombinationAuthorTeam(combAuthor);
+ Taxon taxon = Taxon.NewInstance(name, null);
+ UUID taxonUuid = taxonService.save(taxon);
+
+ // Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+ // NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ // String oldTitleCache = nvn.getTitleCache();
+ // System.out.println("Taxon Name Title : " + oldTitleCache);
+ // nvn.setTitleCache(oldTitleCache + ":updated", true);
+ //
+ // taxon.setTitleCache(oldTitleCache + ":updated",true);
+ // try {
+ // taxonService.update(taxon);
+ // } catch (LazyInitializationException lie) {
+ // lie.printStackTrace();
+ // }
+
+ List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String[] {
+ "name"
+ });
+ Taxon taxonNew = (Taxon)taxonService.findTaxonByUuid(taxonUuid,TAXON_INIT_STRATEGY);
+ NonViralName nvn = CdmBase.deproxy(taxonNew.getName(),NonViralName.class);
+ Team team = CdmBase.deproxy(nvn.getCombinationAuthorTeam(),Team.class);
+ String oldTitleCache = nvn.getTitleCache();
+ System.out.println("Taxon Name Title : " + oldTitleCache);
+ nvn.setTitleCache(oldTitleCache + ":updated", true);
+ taxonService.update(taxonNew);
+
+ }
+
+
+ @Test
+ public void testCDMEntitySaveLazy() {
+ Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+ NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ String oldTitleCache = nvn.getTitleCache();
+ System.out.println("Taxon Name Title : " + oldTitleCache);
+ nvn.setTitleCache(oldTitleCache + ":updated", true);
+ taxonService.update(taxon);
+
+ // Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+ // NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ // System.out.println("New Taxon Name Title : " + nvnNew.getTitleCache());
+ //
+ // Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
+ //
+ // nvnNew.setTitleCache(oldTitleCache, true);
+ // taxonService.update(taxon);
+ //
+ // Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+ // NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ // System.out.println("Old Taxon Name Title : " + nvnNew.getTitleCache());
+ //
+ // Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
+ }
+
+ @Test
+ public void testCDMEntitySaveLazyNew() {
+ Team combAuthor = Team.NewInstance();
+ combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
+ BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
+ name.setCombinationAuthorTeam(combAuthor);
+ Taxon tax1 = Taxon.NewInstance(name, null);
+ UUID taxonUuid1 = taxonService.save(tax1);
+
+ Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+ NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ String oldTitleCache = nvn.getTitleCache();
+ logger.info("Taxon Name Title : " + oldTitleCache);
+ nvn.setTitleCache(oldTitleCache + ":updated",false);
+ taxonService.update(taxon);
+
+ Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+ NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
+
+ Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
+
+ nvnNew.setTitleCache(oldTitleCache, true);
+ taxonService.update(taxon);
+
+ Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+ NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+ logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
+
+ Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
+ }
+ @Ignore
+ @Test
+ public void testCDMEntitySaveCollection() {
+ Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+ Set<SynonymRelationship> synRelations = taxon.getSynonymRelations();
+ Set<String> relToTitles = new HashSet<String>();
+ Iterator<SynonymRelationship> srItr = synRelations.iterator();
+ while(srItr.hasNext()) {
+ SynonymRelationship sr = srItr.next();
+ System.out.println("Synonym Title Cache : " + sr.getSynonym().getTitleCache());
+ relToTitles.add(sr.getSynonym().getTitleCache());
+ sr.getSynonym().setTitleCache(sr.getSynonym().getTitleCache() + ":updated");
+
+ }
+ taxonService.merge(taxon);
+
+ Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+ Set<SynonymRelationship> synRelationsNew = taxonNew.getSynonymRelations();
+
+ Iterator<SynonymRelationship> srItrNew = synRelationsNew.iterator();
+ Iterator<String> relToTitlesItr = relToTitles.iterator();
+ while(srItrNew.hasNext() && relToTitlesItr.hasNext()) {
+ SynonymRelationship srNew = srItrNew.next();
+ String relToTitle = relToTitlesItr.next();
+ System.out.println("New Synonym Title Cache: " + srNew.getSynonym().getTitleCache());
+ Assert.assertNotEquals("Synonym Title caches should not be equal", srNew.getSynonym().getTitleCache(), relToTitle);
+ srNew.getSynonym().setTitleCache(relToTitle);
+ }
+
+ Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+
+ Set<SynonymRelationship> synRelationsOld = taxonNew.getSynonymRelations();
+ Iterator<SynonymRelationship> srItrOld = synRelationsOld.iterator();
+ relToTitlesItr = relToTitles.iterator();
+ while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
+ SynonymRelationship srOld = srItrOld.next();
+ String relToTitle = relToTitlesItr.next();
+ System.out.println("New Synonym Title Cache: " + srOld.getSynonym().getTitleCache());
+ Assert.assertEquals("Synonym Title caches should be equal", srOld.getSynonym().getTitleCache(), relToTitle);
+
+ }
+ }
+}