@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForCascadedConstructor() { ConstructorDescriptor descriptor = Executables.cascadedParameterConstructor(); assertTrue( descriptor.hasConstrainedParameters(), "Should be constrained on parameters" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "f") public void testIsReturnValueConstrainedForConstrainedConstructor() { ConstructorDescriptor descriptor = Executables.returnValueConstrainedConstructor(); assertTrue( descriptor.hasConstrainedReturnValue(), "Should be constrained on return value" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "g") public void testGetConstraintsForConstructor() { ConstructorDescriptor parameterConstrainedDescriptor = Executables.parameterConstrainedConstructor(); assertTrue( parameterConstrainedDescriptor.getConstraintDescriptors().isEmpty(), "Should have no constraints" ); ConstructorDescriptor returnValueConstrainedDescriptor = Executables.returnValueConstrainedConstructor(); assertTrue( returnValueConstrainedDescriptor.getConstraintDescriptors().isEmpty(), "Should have no constraints" ); ConstructorDescriptor crossParameterConstrainedDescriptor = Executables.crossParameterConstrainedConstructor(); assertTrue( crossParameterConstrainedDescriptor.getConstraintDescriptors().isEmpty(), "Should have no constraints" ); }
@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 ); }
@Test @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "n") public void testValidAnnotationIsIgnored() throws Exception { ConstructorDescriptor descriptor = TestUtil.getConstructorDescriptor( Cascaded.class, String.class ); assertNotNull( descriptor, "the specified constructor should be configured in xml" ); ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor(); assertFalse( returnValueDescriptor.isCascaded(), "Cascaded validation should disabled" ); ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get( 0 ); assertFalse( parameterDescriptor.isCascaded(), "Cascaded validation should disabled" ); } }
@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 @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "c"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "e") }) public void testXmlConfiguredConstructors() throws Exception { ConstructorDescriptor descriptor = TestUtil.getConstructorDescriptor( CustomerRepository.class ); assertNotNull( descriptor, "the specified constructor should be configured in xml" ); assertTrue( descriptor.hasConstrainedReturnValue() ); descriptor = TestUtil.getConstructorDescriptor( CustomerRepository.class, String.class ); assertNotNull( descriptor, "the specified constructor should be configured in xml" ); assertTrue( descriptor.hasConstrainedParameters() ); descriptor = TestUtil.getConstructorDescriptor( CustomerRepository.class, CustomerRepository.class ); assertNotNull( descriptor, "the specified constructor should be configured in xml" ); assertTrue( descriptor.hasConstrainedParameters() ); }
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_EXECUTABLEDESCRIPTOR, id = "c") public void testGetCrossParameterDescriptorForConstructor() { ConstructorDescriptor descriptor = Executables.crossParameterConstrainedConstructor(); assertNotNull( descriptor.getCrossParameterDescriptor(), "Cross-parameter descriptor should not be null" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "d") public void testGetReturnValueDescriptorForConstructor() { ConstructorDescriptor descriptor = Executables.returnValueConstrainedConstructor(); assertNotNull( descriptor.getReturnValueDescriptor(), "Return value descriptor should not be null" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "a") public void testGetElementClassForConstructor() { ConstructorDescriptor descriptor = Executables.returnValueConstrainedConstructor(); assertEquals( descriptor.getElementClass(), CustomerService.class ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "a") public void testGetNameForConstructor() { ConstructorDescriptor descriptor = Executables.parameterConstrainedConstructor(); assertEquals( descriptor.getName(), "CustomerService" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "d") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "g") public void testFindConstraintsForConstructor() { ConstructorDescriptor parameterConstrainedDescriptor = Executables.parameterConstrainedConstructor(); assertTrue( parameterConstrainedDescriptor.findConstraints() .getConstraintDescriptors() .isEmpty(), "Should have no constraints" ); ConstructorDescriptor returnValueConstrainedDescriptor = Executables.returnValueConstrainedConstructor(); assertTrue( returnValueConstrainedDescriptor.findConstraints() .getConstraintDescriptors() .isEmpty(), "Should have no constraints" ); ConstructorDescriptor crossParameterConstrainedDescriptor = Executables.crossParameterConstrainedConstructor(); assertTrue( crossParameterConstrainedDescriptor.findConstraints() .getConstraintDescriptors() .isEmpty(), "Should have no constraints" ); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @AroundConstruct // TODO: see previous one public Object construct(InvocationContext context) throws Exception { final Constructor ctor = context.getConstructor(); if (!isConstructorValidated(ctor)) { return context.proceed(); } final ConstructorDescriptor constraints = validator.getConstraintsForClass(ctor.getDeclaringClass()) .getConstraintsForConstructor(ctor.getParameterTypes()); if (!DescriptorManager.isConstrained(constraints)) { return context.proceed(); } initExecutableValidator(); if (constraints.hasConstrainedParameters()) { final Set<ConstraintViolation<?>> violations = executableValidator.validateConstructorParameters(ctor, context.getParameters()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } final Object result = context.proceed(); if (constraints.hasConstrainedReturnValue()) { final Set<ConstraintViolation<?>> violations = executableValidator.validateConstructorReturnValue(ctor, context.getTarget()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } return result; }
@Test @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_CONSTRUCTORLEVELOVERRIDING, id = "n") public void testValidaAnnotationIsApplied() throws Exception { ConstructorDescriptor descriptor = TestUtil.getConstructorDescriptor( Cascaded.class, String.class ); assertNotNull( descriptor, "the specified constructor 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 @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.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "b") public void testGetParameterDescriptorsForConstructor() { ConstructorDescriptor descriptor = Executables.parameterConstrainedConstructor(); assertEquals( descriptor.getParameterDescriptors().size(), 2, "Size of parameter descriptor list doesn't match constructor parameter count" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "h") public void testConstructorDescriptorCanBeRetrievedAlsoIfValidateExecutableIsSetToNONEOnConstructorLevel() { ConstructorDescriptor descriptor = getConstructorDescriptor( Item.class, String.class ); assertNotNull( descriptor ); assertEquals( descriptor.getName(), "Item" ); assertEquals( descriptor.getParameterDescriptors().get( 0 ).getConstraintDescriptors().size(), 1 ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "c") public void testGetCrossParameterDescriptorForConstructorWithoutCrossParameterConstraints() { ConstructorDescriptor descriptor = Executables.returnValueConstrainedConstructor(); assertNotNull( descriptor.getCrossParameterDescriptor(), "Cross-parameter descriptor should not be null" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "b") public void testGetGroupConversionsReturnsEmptySetForMethodReturnValue() { ReturnValueDescriptor returnValueDescriptor = Executables.cascadedReturnValueConstructor() .getReturnValueDescriptor(); Set<GroupConversionDescriptor> groupConversions = returnValueDescriptor.getGroupConversions(); assertNotNull( groupConversions ); assertTrue( groupConversions.isEmpty() ); }