@Override public boolean hasConstrainedParameters() { if ( crossParameterDescriptor.hasConstraints() ) { return true; } for ( ParameterDescriptor oneParameter : parameters ) { if ( oneParameter.hasConstraints() || oneParameter.isCascaded() ) { return true; } } return false; }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "h") public void testMethodDescriptorCanBeRetrievedAlsoIfValidateExecutableIsSetToNONEOnTypeLevel() { MethodDescriptor descriptor = getMethodDescriptor( OrderLine.class, "setItem", String.class ); assertNotNull( descriptor ); assertEquals( descriptor.getName(), "setItem" ); assertEquals( descriptor.getParameterDescriptors().get( 0 ).getConstraintDescriptors().size(), 1 ); }
private Set<List<Class<?>>> getParameterTypes(Set<ConstructorDescriptor> constructorDescriptors) { Set<List<Class<?>>> parameterTypes = new HashSet<List<Class<?>>>(); for ( ConstructorDescriptor constructorDescriptor : constructorDescriptors ) { List<Class<?>> types = new ArrayList<Class<?>>(); for ( ParameterDescriptor parameterDescriptor : constructorDescriptor.getParameterDescriptors() ) { types.add( parameterDescriptor.getElementClass() ); } parameterTypes.add( types ); } return parameterTypes; } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testIsCascadedForMethod() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedMethod() .getParameterDescriptors(); assertFalse( parameters.get( 0 ).isCascaded(), "Should not be cascaded" ); parameters = Executables.cascadedParameterMethod().getParameterDescriptors(); assertTrue( parameters.get( 0 ).isCascaded(), "Should be cascaded" ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "l"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "m"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "n"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "p") }) public void testIgnoreAnnotationsOnMethodLevel() { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( IgnoreAnnotations.class, "foobar", String.class, String.class ); CrossParameterDescriptor crossParameterDescriptor = descriptor.getCrossParameterDescriptor(); assertFalse( crossParameterDescriptor.hasConstraints(), "Cross parameter constraints should be ignored." ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertFalse( returnValueDescriptor.hasConstraints(), "Return value constraints should be ignored." ); assertTrue( returnValueDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored" ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertFalse( parameterDescriptor.hasConstraints(), "First parameter constraints should be ignored." ); assertTrue( parameterDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored" ); parameterDescriptor = descriptor.getParameterDescriptors().get( 1 ); assertTrue( parameterDescriptor.hasConstraints(), "Second parameter constraints should be applied." ); assertEquals( parameterDescriptor.getGroupConversions().size(), 2, "All group conversions should be combined" ); } }
@Test @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "k") public void testIgnoreAnnotationsOnReturnValueParameterAndCrossParameter() { ConstructorDescriptor descriptor = TestUtil.getConstructorDescriptor( IgnoreAnnotations.class, String.class, String.class ); CrossParameterDescriptor crossParameterDescriptor = descriptor.getCrossParameterDescriptor(); assertFalse( crossParameterDescriptor.hasConstraints(), "Cross parameter constraints should be ignored." ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertFalse( returnValueDescriptor.hasConstraints(), "Return value constraints should be ignored." ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertFalse( parameterDescriptor.hasConstraints(), "First parameter constraints should be ignored." ); parameterDescriptor = descriptor.getParameterDescriptors().get( 1 ); assertTrue( parameterDescriptor.hasConstraints(), "Second parameter constraints should be applied." ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "b") public void testGetGroupConversionsReturnsEmptySetForConstructorParameter() { ParameterDescriptor parameterDescriptor = Executables.parameterConstrainedConstructor() .getParameterDescriptors() .get( 0 ); Set<GroupConversionDescriptor> groupConversions = parameterDescriptor.getGroupConversions(); assertNotNull( groupConversions ); assertTrue( groupConversions.isEmpty() ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_PARAMETERDESCRIPTOR, id = "b") public void testGetNameForConstructor() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedConstructor() .getParameterDescriptors(); assertEquals( parameters.get( 0 ).getName(), "firstName", "Wrong parameter name" ); assertEquals( parameters.get( 1 ).getName(), "lastName", "Wrong parameter name" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_PARAMETERDESCRIPTOR, id = "a") public void testGetIndexForConstructor() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedConstructor() .getParameterDescriptors(); assertEquals( parameters.get( 0 ).getIndex(), 0, "Wrong parameter index" ); assertEquals( parameters.get( 1 ).getIndex(), 1, "Wrong parameter index" ); }
private void checkContainerElementMetaDataOnParameterDescriptor(ParameterDescriptor parameterDescriptor) { Set<ContainerElementTypeDescriptor> containerElementTypes = parameterDescriptor.getConstrainedContainerElementTypes();
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testIsCascadedForConstructor() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedConstructor() .getParameterDescriptors(); assertFalse( parameters.get( 0 ).isCascaded(), "Should not be cascaded" ); parameters = Executables.cascadedParameterConstructor().getParameterDescriptors(); assertTrue( parameters.get( 0 ).isCascaded(), "Should be cascaded" ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "k"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "l"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "m"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "o") }) public void testIgnoreAnnotationsOnConstructorLevel() { ConstructorDescriptor descriptor = TestUtil.getConstructorDescriptor( IgnoreAnnotations.class, String.class, String.class ); CrossParameterDescriptor crossParameterDescriptor = descriptor.getCrossParameterDescriptor(); assertFalse( crossParameterDescriptor.hasConstraints(), "Cross parameter constraints should be ignored." ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertFalse( returnValueDescriptor.hasConstraints(), "Return value constraints should be ignored." ); assertTrue( returnValueDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored" ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertFalse( parameterDescriptor.hasConstraints(), "First parameter constraints should be ignored." ); assertTrue( parameterDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored" ); parameterDescriptor = descriptor.getParameterDescriptors().get( 1 ); assertTrue( parameterDescriptor.hasConstraints(), "Second parameter constraints should be applied." ); assertEquals( parameterDescriptor.getGroupConversions().size(), 2, "All group conversions should be combined" ); } }
@Test @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "l") public void testIgnoreAnnotationsOnReturnValueParameterAndCrossParameter() { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( IgnoreAnnotations.class, "foobar", String.class, String.class ); CrossParameterDescriptor crossParameterDescriptor = descriptor.getCrossParameterDescriptor(); assertFalse( crossParameterDescriptor.hasConstraints(), "Cross parameter constraints should be ignored." ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertFalse( returnValueDescriptor.hasConstraints(), "Return value constraints should be ignored." ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertFalse( parameterDescriptor.hasConstraints(), "First parameter constraints should be ignored." ); parameterDescriptor = descriptor.getParameterDescriptors().get( 1 ); assertTrue( parameterDescriptor.hasConstraints(), "Second parameter constraints should be applied." ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "b") public void testGetGroupConversionsReturnsEmptySetForMethodParameter() { ParameterDescriptor parameterDescriptor = Executables.parameterConstrainedMethod() .getParameterDescriptors() .get( 0 ); Set<GroupConversionDescriptor> groupConversions = parameterDescriptor.getGroupConversions(); assertNotNull( groupConversions ); assertTrue( groupConversions.isEmpty() ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_PARAMETERDESCRIPTOR, id = "b") public void testGetNameForMethod() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedMethod() .getParameterDescriptors(); assertEquals( parameters.get( 0 ).getName(), "firstName", "Wrong parameter name" ); assertEquals( parameters.get( 1 ).getName(), "lastName", "Wrong parameter name" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_PARAMETERDESCRIPTOR, id = "a") public void testGetIndexForMethod() { List<ParameterDescriptor> parameters = Executables.parameterConstrainedMethod() .getParameterDescriptors(); assertEquals( parameters.get( 0 ).getIndex(), 0, "Wrong parameter index" ); assertEquals( parameters.get( 1 ).getIndex(), 1, "Wrong parameter index" ); }
@Override public boolean hasConstrainedParameters() { if ( crossParameterDescriptor.hasConstraints() ) { return true; } for ( ParameterDescriptor oneParameter : parameters ) { if ( oneParameter.hasConstraints() || oneParameter.isCascaded() ) { return true; } } return false; }
@Test @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "o") public void testValidaAnnotationIsApplied() throws Exception { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( Cascaded.class, "cascade", String.class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertTrue( returnValueDescriptor.isCascaded(), "Cascaded validation should be applied" ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertTrue( parameterDescriptor.isCascaded(), "Cascaded validation should be applied" ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "d") public void testConvertGroupDefaultFromForConstructorParameter() { ParameterDescriptor descriptor = getConstructorDescriptor( BeanService.class, Bean.class ) .getParameterDescriptors().iterator().next(); Set<GroupConversionDescriptor> groupConversionDescriptors = descriptor.getGroupConversions(); GroupConversionDescriptor groupConversionDescriptor = getGroupConversionDescriptorByFrom( groupConversionDescriptors, Default.class ); assertEquals( groupConversionDescriptor.getTo(), ComplexChecks.class ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "h") public void testConstructorDescriptorCanBeRetrievedAlsoIfValidateExecutableIsSetToNONEOnTypeLevel() { ConstructorDescriptor descriptor = getConstructorDescriptor( OrderLine.class, String.class ); assertNotNull( descriptor ); assertEquals( descriptor.getName(), "OrderLine" ); assertEquals( descriptor.getParameterDescriptors().get( 0 ).getConstraintDescriptors().size(), 1 ); }