From e56c2897c3c0cd2bea07bfac175e8b59e4e06d1b Mon Sep 17 00:00:00 2001 From: jlemes Date: Fri, 30 May 2014 05:59:35 -0300 Subject: [PATCH 1/2] Update OgnlParametersProviderTestVRaptorTests.java --- .../vraptor/OgnlParametersProviderTestVRaptorTests.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/br/com/caelum/iogi/vraptor/OgnlParametersProviderTestVRaptorTests.java b/test/br/com/caelum/iogi/vraptor/OgnlParametersProviderTestVRaptorTests.java index fb5e891..be2f10f 100644 --- a/test/br/com/caelum/iogi/vraptor/OgnlParametersProviderTestVRaptorTests.java +++ b/test/br/com/caelum/iogi/vraptor/OgnlParametersProviderTestVRaptorTests.java @@ -107,6 +107,14 @@ public void removeFromTheCollectionIfAnElementIsCreatedWithinACollectionButNoFie assertThat(house.extraCats, hasSize(1)); assertThat(house.extraCats.get(0).id, is(equalTo("guilherme"))); } + + @Test + public void returnNullWhenNoParameters() { + final Target target = Target.create(Cat.class, "cat"); + final Parameter parameter = new Parameter("cats.id", "1"); + final Cat cat = iogi.instantiate(target, parameter); + assertEquals(cat, null); + } @Test public void removeFromTheCollectionIfAnElementIsCreatedWithinAnArrayButNoFieldIsSet() { From 4165b8f59e04e94f5cfb004c64113672558c1472 Mon Sep 17 00:00:00 2001 From: jlemes Date: Fri, 30 May 2014 06:00:14 -0300 Subject: [PATCH 2/2] Update ObjectInstantiator.java --- .../com/caelum/iogi/ObjectInstantiator.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/br/com/caelum/iogi/ObjectInstantiator.java b/src/br/com/caelum/iogi/ObjectInstantiator.java index 51411e6..df235ff 100644 --- a/src/br/com/caelum/iogi/ObjectInstantiator.java +++ b/src/br/com/caelum/iogi/ObjectInstantiator.java @@ -25,14 +25,18 @@ public boolean isAbleToInstantiate(final Target target) { public Object instantiate(final Target target, final Parameters parameters) { expectingAConcreteTarget(target); - final Parameters parametersForTarget = parameters.focusedOn(target); - - return target - .constructors(parameterNamesProvider, dependenciesInjector) - .compatibleWith(parametersForTarget) - .largest() - .instantiate(argumentInstantiator) - .valueWithPropertiesSet(); + if(parameters.hasRelatedTo(target)){ + final Parameters parametersForTarget = parameters.focusedOn(target); + + return target + .constructors(parameterNamesProvider, dependenciesInjector) + .compatibleWith(parametersForTarget) + .largest() + .instantiate(argumentInstantiator) + .valueWithPropertiesSet(); + } else { + return null; + } } private void expectingAConcreteTarget(final Target target) {