diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java index 54ed321c0138dba59c251bbdd6db038806fac145..482f492349f98649eb65b812033553cb84faa537 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java @@ -17,7 +17,6 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.HashMap; @@ -237,6 +236,8 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi return; } + checkIfCanBeContainer(samplePE, containerPE); + if (containerPE == null) { relationshipService.removeSampleFromContainer(session, samplePE); @@ -270,6 +271,24 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi replaceParents(childPE, parentPEs); } + /** + * check if the given candidate for the container is not already contained in the sample. + */ + private void checkIfCanBeContainer(final SamplePE sample, final SamplePE container) + { + SamplePE containerCandidate = container; + + while (containerCandidate != null) + { + if (sample.equals(containerCandidate)) + { + throw UserFailureException.fromTemplate("'%s' cannot be it's own container.", + sample.getIdentifier()); + } + containerCandidate = containerCandidate.getContainer(); + } + } + /** * depth-first search through all parents of parent candidate in search of a childPE. If it's * found - the exception is being thrown diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java index 45766938eb8f35b14817e2422d38f90138a8f8af..47bb54a2e7a267ab8448d9bd21de7258f5e31218 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java @@ -119,14 +119,13 @@ public class UpdateSampleContainmentTest extends BaseTest perform(anUpdateOf(subComponent).toHaveContainer(component)); } - @Test + @Test(expectedExceptions = + { UserFailureException.class }) public void sampleCanContainItself() throws Exception { Sample sample = create(aSample().inSpace(space)); perform(anUpdateOf(sample).toHaveContainer(sample)); - - assertThat(sample, hasContainer(sample)); } Space containerSpace;