Skip to content

Commit

Permalink
ConceptNumeric(concept) incorrectly copies collections - TRUNK-3974
Browse files Browse the repository at this point in the history
  • Loading branch information
dkayiwa committed Jan 15, 2014
1 parent b130616 commit 75a01e8
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 5 deletions.
41 changes: 36 additions & 5 deletions api/src/main/java/org/openmrs/ConceptNumeric.java
Expand Up @@ -13,6 +13,9 @@
*/
package org.openmrs;

import java.util.HashSet;
import java.util.TreeSet;

import org.simpleframework.xml.Attribute;

/**
Expand Down Expand Up @@ -63,25 +66,53 @@ public ConceptNumeric(Integer conceptId) {
* Note: This cannot copy over numeric specific values
*
* @param c
* @should make deep copy of collections
* @should change reference to the parent object for objects in answers collection
* @should change reference to the parent object for objects in conceptSets collection
* @should change reference to the parent object for objects in names collection
* @should change reference to the parent object for objects in descriptions collection
* @should change reference to the parent object for objects in conceptMappings collection
*/
public ConceptNumeric(Concept c) {
this.setAnswers(c.getAnswers(true));
this.setChangedBy(c.getChangedBy());
this.setConceptClass(c.getConceptClass());
this.setConceptId(c.getConceptId());
this.setConceptSets(c.getConceptSets());
this.setCreator(c.getCreator());
this.setDatatype(c.getDatatype());
this.setDateChanged(c.getDateChanged());
this.setDateCreated(c.getDateCreated());
this.setSet(c.isSet());
this.setNames(c.getNames());
this.setDescriptions(c.getDescriptions());
this.setConceptMappings(c.getConceptMappings());
this.setRetired(c.isRetired());
this.setRetiredBy(c.getRetiredBy());
this.setRetireReason(c.getRetireReason());
this.setVersion(c.getVersion());
this.setUuid(c.getUuid());

this.setNames(new HashSet<ConceptName>(c.getNames()));
for (ConceptName cName : this.getNames()) {
cName.setConcept(this);
}

this.setAnswers(new HashSet<ConceptAnswer>(c.getAnswers(true)));
for (ConceptAnswer cAnswer : this.getAnswers()) {
cAnswer.setConcept(this);
}

this.setConceptSets(new TreeSet<ConceptSet>(c.getConceptSets()));
for (ConceptSet cSet : this.getConceptSets()) {
cSet.setConcept(this);
}

this.setDescriptions(new HashSet<ConceptDescription>(c.getDescriptions()));
for (ConceptDescription cDescription : this.getDescriptions()) {
cDescription.setConcept(this);
}

this.setConceptMappings(new HashSet<ConceptMap>(c.getConceptMappings()));
for (ConceptMap cMap : this.getConceptMappings()) {
cMap.setConcept(this);
}

this.hiAbsolute = null;
this.hiCritical = null;
this.hiNormal = null;
Expand Down
123 changes: 123 additions & 0 deletions api/src/test/java/org/openmrs/ConceptNumericTest.java
@@ -0,0 +1,123 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.test.Verifies;

/**
* Tests the {@link ConceptNumeric} object
*/
public class ConceptNumericTest {

/**
* Regression test for TRUNK-82 (old TRAC-1511)
*
* @see {@link ConceptNumeric#equals(Object)}
*/
@Test
@Verifies(value = "should not return true if obj is concept", method = "equals(Object)")
public void equals_shouldNotReturnTrueIfObjIsConcept() throws Exception {
ConceptNumeric cn = new ConceptNumeric(123);
Concept c = new Concept(123);

Assert.assertNotSame(c, cn);
}

/**
* Tests for TRUNK-3974
*
*
* @throws Exception
*/

@Test
@Verifies(value = "should make deep copie of collections", method = "ConceptNumeric(Concept)")
public void equals_shouldNotBeTheSameReference() throws Exception {
Concept c = new Concept(123);
ConceptNumeric cn = new ConceptNumeric(c);

Assert.assertNotSame(c.getAnswers(), cn.getAnswers());
Assert.assertNotSame(c.getConceptSets(), cn.getConceptSets());
Assert.assertNotSame(c.getNames(), cn.getNames());
Assert.assertNotSame(c.getConceptMappings(), cn.getConceptMappings());
Assert.assertNotSame(c.getDescriptions(), cn.getDescriptions());
}

@Test
@Verifies(value = "should change reference to the parent object for objects in answers collection", method = "ConceptNumeric(Concept)")
public void shouldChangeConceptAnswerReferenceToParentConcept() throws Exception {
Concept c = new Concept(123);
c.addAnswer(new ConceptAnswer(1));
c.addAnswer(new ConceptAnswer(2));
ConceptNumeric cn = new ConceptNumeric(c);

for (ConceptAnswer cAnswer : cn.getAnswers()) {
Assert.assertSame(cn, cAnswer.getConcept());
}
}

@Test
@Verifies(value = "should change reference to the parent object for objects in conceptSets collection", method = "ConceptNumeric(Concept)")
public void shouldChangeConceptSetReferenceToParentConcept() throws Exception {
Concept c = new Concept(123);
c.addSetMember(new Concept(1));
c.addSetMember(new Concept(2));
ConceptNumeric cn = new ConceptNumeric(c);

for (ConceptSet cSet : cn.getConceptSets()) {
Assert.assertSame(cn, cSet.getConcept());
}
}

@Test
@Verifies(value = "should change reference to the parent object for objects in names collection", method = "ConceptNumeric(Concept)")
public void shouldChangeConceptNameReferenceToParentConcept() throws Exception {
Concept c = new Concept(123);
c.addName(new ConceptName(1));
c.addName(new ConceptName(2));
ConceptNumeric cn = new ConceptNumeric(c);

for (ConceptName cName : cn.getNames()) {
Assert.assertSame(cn, cName.getConcept());
}
}

@Test
@Verifies(value = "should change reference to the parent object for objects in descriptions collection", method = "ConceptNumeric(Concept)")
public void shouldChangeConceptDescriptionReferenceToParentConcept() throws Exception {
Concept c = new Concept(123);
c.addDescription(new ConceptDescription(1));
c.addDescription(new ConceptDescription(2));
ConceptNumeric cn = new ConceptNumeric(c);

for (ConceptDescription cDesc : cn.getDescriptions()) {
Assert.assertSame(cn, cDesc.getConcept());
}
}

@Test
@Verifies(value = "should change reference to the parent object for objects in conceptMappings collection", method = "ConceptNumeric(Concept)")
public void shouldChangeConceptMapReferenceToParentConcept() throws Exception {
Concept c = new Concept(123);
c.getConceptMappings().add(new ConceptMap(1));
c.getConceptMappings().add(new ConceptMap(2));
ConceptNumeric cn = new ConceptNumeric(c);

for (ConceptMap cMap : cn.getConceptMappings()) {
Assert.assertSame(cn, cMap.getConcept());
}
}
}

0 comments on commit 75a01e8

Please sign in to comment.