Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 71bad29

Browse files
committed
POLYGENE-122 : Adding name to Null constraint on set() method on Property of Transient instance.
1 parent ecee06b commit 71bad29

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

core/api/src/main/java/org/apache/polygene/api/constraint/ConstraintViolationException.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,17 @@ public String localizedMessageFrom( ResourceBundle bundle )
190190
: instanceTypes.stream()
191191
.map( this::nameOf )
192192
.collect( Collectors.joining( "," ) );
193-
String name;
194-
if( longNames )
193+
String name = "";
194+
if( primaryType != null )
195195
{
196-
name = primaryType.getName();
197-
}
198-
else
199-
{
200-
name = primaryType.getSimpleName();
196+
if( longNames )
197+
{
198+
name = primaryType.getName();
199+
}
200+
else
201+
{
202+
name = primaryType.getSimpleName();
203+
}
201204
}
202205
Object[] args = new Object[]{ name, instanceToString, identity, types };
203206
MessageFormat formatter = new MessageFormat( compositePattern, locale );

core/runtime/src/main/java/org/apache/polygene/runtime/injection/DependencyModel.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ public void bind( Resolution resolution )
261261

262262
if( injectionProvider == null && !optional )
263263
{
264-
String message =
265-
"[Module " + resolution.module()
266-
.name() + "] Non-optional @" + rawInjectionClass.getName() + " was not bound in " + injectedClass
267-
.getName();
264+
String message = "[Module " + resolution.module().name() + "] Non-optional @" + rawInjectionClass.getName() + " was not bound in " + injectedClass.getName();
268265
throw new ConstructionException( message );
269266
}
270267
}
@@ -301,9 +298,7 @@ public Object inject( InjectionContext context )
301298
if( injectedValue == null && !optional )
302299
{
303300
String simpleName = injectionAnnotation.annotationType().getSimpleName();
304-
String message = "[Module " + context.module().name() + "] Non-optional @" +
305-
simpleName + " " + injectionType.toString() +
306-
" was null in " + injectedClass.getName();
301+
String message = "[Module " + context.module().name() + "] Non-optional @" + simpleName + " " + injectionType.toString() + " was null in " + injectedClass.getName();
307302
if( simpleName.toLowerCase().contains( "service" )
308303
&& !isServiceInjectionProvider() )
309304
{

core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyInstance.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.Set;
3131
import org.apache.polygene.api.composite.Composite;
32+
import org.apache.polygene.api.constraint.ConstraintViolationException;
3233
import org.apache.polygene.api.property.Property;
3334
import org.apache.polygene.api.property.PropertyDescriptor;
3435
import org.apache.polygene.api.property.PropertyWrapper;
@@ -97,7 +98,15 @@ public void set( T aNewValue )
9798
throw new IllegalStateException( "Property [" + model.qualifiedName() + "] is immutable." );
9899
}
99100

100-
model.checkConstraints( aNewValue );
101+
try
102+
{
103+
model.checkConstraints( aNewValue );
104+
}
105+
catch( ConstraintViolationException e )
106+
{
107+
e.setInstanceString( model.qualifiedName().toString() );
108+
throw e;
109+
}
101110

102111
value = aNewValue;
103112
}

core/runtime/src/test/java/org/apache/polygene/api/common/OptionalTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
/**
4343
* Tests for @Optional
4444
*/
45-
public class OptionalTest
46-
extends AbstractPolygeneTest
45+
public class OptionalTest extends AbstractPolygeneTest
4746
{
4847
public void assemble( ModuleAssembly module )
4948
throws AssemblyException
@@ -100,6 +99,19 @@ public void givenMandatoryPropertyWhenMandatoryMissingThenException()
10099
assertThrows( ConstraintViolationException.class, () -> transientBuilderFactory.newTransient( TestComposite2.class ) );
101100
}
102101

102+
@Test
103+
public void givenMandatoryPropertyWhenSettingPropertyToNullOnBuiltInstanceThenException()
104+
{
105+
TransientBuilder<TestComposite2> builder = transientBuilderFactory.newTransientBuilder( TestComposite2.class );
106+
builder.prototype().mandatoryProperty().set( "Hello" );
107+
builder.prototype().optionalProperty().set( "World" );
108+
TestComposite2 testComposite2 = builder.newInstance();
109+
testComposite2.optionalProperty().set( null );
110+
assertThrows( ConstraintViolationException.class, () -> {
111+
testComposite2.mandatoryProperty().set( null );
112+
} );
113+
}
114+
103115
@Test
104116
public void givenOptionalAssociationWhenOptionalMissingThenNoException()
105117
throws Exception

0 commit comments

Comments
 (0)