Skip to content
Snippets Groups Projects
Commit e31861fe authored by felmer's avatar felmer
Browse files

SP-482, BIS-318: Throw exception MaterialIdentifier.tryCreate() has inconsistent arguments.

SVN: 28578
parent a5c3f189
No related branches found
No related tags found
No related merge requests found
...@@ -93,29 +93,35 @@ public final class MaterialIdentifier implements Serializable ...@@ -93,29 +93,35 @@ public final class MaterialIdentifier implements Serializable
* full identifier is specified. * full identifier is specified.
* *
* @return <code>null</code> if no full identifier specified and material type is unknown. * @return <code>null</code> if no full identifier specified and material type is unknown.
* @throw {@link IllegalArgumentException} if material type of full identifier doesn't match
* specified material type.
*/ */
public static MaterialIdentifier tryCreate(String codeOrIdentifierOrNull, public static MaterialIdentifier tryCreate(String codeOrIdentifierOrNull,
ICodeHolder materialTypeCodeHolderOrNull) ICodeHolder materialTypeCodeHolderOrNull)
{ {
MaterialIdentifier materialIdentifier = MaterialIdentifier materialIdentifier =
MaterialIdentifier.tryParseIdentifier(codeOrIdentifierOrNull); MaterialIdentifier.tryParseIdentifier(codeOrIdentifierOrNull);
if (materialIdentifier == null) if (materialIdentifier != null)
{ {
// if the material type of the property is fixed, then we accept when only material
// code is specified and its type is skipped (we know what the type should be)
if (materialTypeCodeHolderOrNull != null if (materialTypeCodeHolderOrNull != null
&& StringUtils.isBlank(codeOrIdentifierOrNull) == false) && materialIdentifier.getTypeCode().equals(
materialTypeCodeHolderOrNull.getCode()) == false)
{ {
materialIdentifier = throw new IllegalArgumentException("Material identified by '" + materialIdentifier
new MaterialIdentifier(codeOrIdentifierOrNull, + "' has to be of type " + materialTypeCodeHolderOrNull.getCode() + ".");
materialTypeCodeHolderOrNull.getCode());
} else
{
// identifier is invalid or null
return null;
} }
return materialIdentifier;
}
// if the material type of the property is fixed, then we accept when only material
// code is specified and its type is skipped (we know what the type should be)
if (materialTypeCodeHolderOrNull != null
&& StringUtils.isBlank(codeOrIdentifierOrNull) == false)
{
return new MaterialIdentifier(codeOrIdentifierOrNull,
materialTypeCodeHolderOrNull.getCode());
} }
return materialIdentifier; // identifier is invalid or null
return null;
} }
/** /**
......
...@@ -357,15 +357,21 @@ class EntityExistenceChecker ...@@ -357,15 +357,21 @@ class EntityExistenceChecker
{ {
String value = property.getValue(); String value = property.getValue();
MaterialTypePE materialType = propertyTypePE.getMaterialType(); MaterialTypePE materialType = propertyTypePE.getMaterialType();
MaterialIdentifier materialIdentifier = try
MaterialIdentifier.tryCreate(value, materialType);
if (materialIdentifier == null)
{ {
errors.add("Material identifier not in the form '<material code> (<material type code>)': " MaterialIdentifier materialIdentifier =
+ value); MaterialIdentifier.tryCreate(value, materialType);
} else if (materialIdentifier == null)
{
errors.add("Material identifier not in the form '<material code> (<material type code>)': "
+ value);
} else
{
materialExistenceManager.exists(materialIdentifier);
}
} catch (Exception ex)
{ {
materialExistenceManager.exists(materialIdentifier); errors.add(ex.getMessage());
} }
} }
} }
......
...@@ -53,11 +53,26 @@ public class MaterialIdentifierTest extends AssertJUnit ...@@ -53,11 +53,26 @@ public class MaterialIdentifierTest extends AssertJUnit
@Test @Test
public void testTryCreateWithFullIdentifierAndType() public void testTryCreateWithFullIdentifierAndType()
{ {
MaterialType materialType = new MaterialTypeBuilder().code("MY_M").getMaterialType(); MaterialType materialType = new MaterialTypeBuilder().code("MY_MATERIAL").getMaterialType();
assertEquals("ABC (MY_MATERIAL)", assertEquals("ABC (MY_MATERIAL)",
MaterialIdentifier.tryCreate("ABC (MY_MATERIAL)", materialType).toString()); MaterialIdentifier.tryCreate("ABC (MY_MATERIAL)", materialType).toString());
} }
@Test
public void testTryCreateWithFullIdentifierAndInconsistentType()
{
MaterialType materialType = new MaterialTypeBuilder().code("MY_M").getMaterialType();
try
{
MaterialIdentifier.tryCreate("ABC (MY_MATERIAL)", materialType);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Material identified by 'ABC (MY_MATERIAL)' has to be of type MY_M.",
ex.getMessage());
}
}
@Test @Test
public void testTryCreateWithCodeAndType() public void testTryCreateWithCodeAndType()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment