Skip to content

Commit 75a01e8

Browse files
committedJan 15, 2014
ConceptNumeric(concept) incorrectly copies collections - TRUNK-3974
1 parent b130616 commit 75a01e8

File tree

2 files changed

+159
-5
lines changed

2 files changed

+159
-5
lines changed
 

‎api/src/main/java/org/openmrs/ConceptNumeric.java

+36-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
package org.openmrs;
1515

16+
import java.util.HashSet;
17+
import java.util.TreeSet;
18+
1619
import org.simpleframework.xml.Attribute;
1720

1821
/**
@@ -63,25 +66,53 @@ public ConceptNumeric(Integer conceptId) {
6366
* Note: This cannot copy over numeric specific values
6467
*
6568
* @param c
69+
* @should make deep copy of collections
70+
* @should change reference to the parent object for objects in answers collection
71+
* @should change reference to the parent object for objects in conceptSets collection
72+
* @should change reference to the parent object for objects in names collection
73+
* @should change reference to the parent object for objects in descriptions collection
74+
* @should change reference to the parent object for objects in conceptMappings collection
6675
*/
6776
public ConceptNumeric(Concept c) {
68-
this.setAnswers(c.getAnswers(true));
6977
this.setChangedBy(c.getChangedBy());
7078
this.setConceptClass(c.getConceptClass());
7179
this.setConceptId(c.getConceptId());
72-
this.setConceptSets(c.getConceptSets());
7380
this.setCreator(c.getCreator());
7481
this.setDatatype(c.getDatatype());
7582
this.setDateChanged(c.getDateChanged());
7683
this.setDateCreated(c.getDateCreated());
7784
this.setSet(c.isSet());
78-
this.setNames(c.getNames());
79-
this.setDescriptions(c.getDescriptions());
80-
this.setConceptMappings(c.getConceptMappings());
8185
this.setRetired(c.isRetired());
86+
this.setRetiredBy(c.getRetiredBy());
87+
this.setRetireReason(c.getRetireReason());
8288
this.setVersion(c.getVersion());
8389
this.setUuid(c.getUuid());
8490

91+
this.setNames(new HashSet<ConceptName>(c.getNames()));
92+
for (ConceptName cName : this.getNames()) {
93+
cName.setConcept(this);
94+
}
95+
96+
this.setAnswers(new HashSet<ConceptAnswer>(c.getAnswers(true)));
97+
for (ConceptAnswer cAnswer : this.getAnswers()) {
98+
cAnswer.setConcept(this);
99+
}
100+
101+
this.setConceptSets(new TreeSet<ConceptSet>(c.getConceptSets()));
102+
for (ConceptSet cSet : this.getConceptSets()) {
103+
cSet.setConcept(this);
104+
}
105+
106+
this.setDescriptions(new HashSet<ConceptDescription>(c.getDescriptions()));
107+
for (ConceptDescription cDescription : this.getDescriptions()) {
108+
cDescription.setConcept(this);
109+
}
110+
111+
this.setConceptMappings(new HashSet<ConceptMap>(c.getConceptMappings()));
112+
for (ConceptMap cMap : this.getConceptMappings()) {
113+
cMap.setConcept(this);
114+
}
115+
85116
this.hiAbsolute = null;
86117
this.hiCritical = null;
87118
this.hiNormal = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs;
15+
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.openmrs.test.Verifies;
19+
20+
/**
21+
* Tests the {@link ConceptNumeric} object
22+
*/
23+
public class ConceptNumericTest {
24+
25+
/**
26+
* Regression test for TRUNK-82 (old TRAC-1511)
27+
*
28+
* @see {@link ConceptNumeric#equals(Object)}
29+
*/
30+
@Test
31+
@Verifies(value = "should not return true if obj is concept", method = "equals(Object)")
32+
public void equals_shouldNotReturnTrueIfObjIsConcept() throws Exception {
33+
ConceptNumeric cn = new ConceptNumeric(123);
34+
Concept c = new Concept(123);
35+
36+
Assert.assertNotSame(c, cn);
37+
}
38+
39+
/**
40+
* Tests for TRUNK-3974
41+
*
42+
*
43+
* @throws Exception
44+
*/
45+
46+
@Test
47+
@Verifies(value = "should make deep copie of collections", method = "ConceptNumeric(Concept)")
48+
public void equals_shouldNotBeTheSameReference() throws Exception {
49+
Concept c = new Concept(123);
50+
ConceptNumeric cn = new ConceptNumeric(c);
51+
52+
Assert.assertNotSame(c.getAnswers(), cn.getAnswers());
53+
Assert.assertNotSame(c.getConceptSets(), cn.getConceptSets());
54+
Assert.assertNotSame(c.getNames(), cn.getNames());
55+
Assert.assertNotSame(c.getConceptMappings(), cn.getConceptMappings());
56+
Assert.assertNotSame(c.getDescriptions(), cn.getDescriptions());
57+
}
58+
59+
@Test
60+
@Verifies(value = "should change reference to the parent object for objects in answers collection", method = "ConceptNumeric(Concept)")
61+
public void shouldChangeConceptAnswerReferenceToParentConcept() throws Exception {
62+
Concept c = new Concept(123);
63+
c.addAnswer(new ConceptAnswer(1));
64+
c.addAnswer(new ConceptAnswer(2));
65+
ConceptNumeric cn = new ConceptNumeric(c);
66+
67+
for (ConceptAnswer cAnswer : cn.getAnswers()) {
68+
Assert.assertSame(cn, cAnswer.getConcept());
69+
}
70+
}
71+
72+
@Test
73+
@Verifies(value = "should change reference to the parent object for objects in conceptSets collection", method = "ConceptNumeric(Concept)")
74+
public void shouldChangeConceptSetReferenceToParentConcept() throws Exception {
75+
Concept c = new Concept(123);
76+
c.addSetMember(new Concept(1));
77+
c.addSetMember(new Concept(2));
78+
ConceptNumeric cn = new ConceptNumeric(c);
79+
80+
for (ConceptSet cSet : cn.getConceptSets()) {
81+
Assert.assertSame(cn, cSet.getConcept());
82+
}
83+
}
84+
85+
@Test
86+
@Verifies(value = "should change reference to the parent object for objects in names collection", method = "ConceptNumeric(Concept)")
87+
public void shouldChangeConceptNameReferenceToParentConcept() throws Exception {
88+
Concept c = new Concept(123);
89+
c.addName(new ConceptName(1));
90+
c.addName(new ConceptName(2));
91+
ConceptNumeric cn = new ConceptNumeric(c);
92+
93+
for (ConceptName cName : cn.getNames()) {
94+
Assert.assertSame(cn, cName.getConcept());
95+
}
96+
}
97+
98+
@Test
99+
@Verifies(value = "should change reference to the parent object for objects in descriptions collection", method = "ConceptNumeric(Concept)")
100+
public void shouldChangeConceptDescriptionReferenceToParentConcept() throws Exception {
101+
Concept c = new Concept(123);
102+
c.addDescription(new ConceptDescription(1));
103+
c.addDescription(new ConceptDescription(2));
104+
ConceptNumeric cn = new ConceptNumeric(c);
105+
106+
for (ConceptDescription cDesc : cn.getDescriptions()) {
107+
Assert.assertSame(cn, cDesc.getConcept());
108+
}
109+
}
110+
111+
@Test
112+
@Verifies(value = "should change reference to the parent object for objects in conceptMappings collection", method = "ConceptNumeric(Concept)")
113+
public void shouldChangeConceptMapReferenceToParentConcept() throws Exception {
114+
Concept c = new Concept(123);
115+
c.getConceptMappings().add(new ConceptMap(1));
116+
c.getConceptMappings().add(new ConceptMap(2));
117+
ConceptNumeric cn = new ConceptNumeric(c);
118+
119+
for (ConceptMap cMap : cn.getConceptMappings()) {
120+
Assert.assertSame(cn, cMap.getConcept());
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)
Please sign in to comment.