@Test public void whenHasAnnotation() { class Customer { public void someAction( @Parameter( regexPattern = "[123].*", regexPatternFlags = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final RegExFacet regExFacet = facetedMethodParameter.getFacet(RegExFacet.class); Assert.assertNotNull(regExFacet); Assert.assertTrue(regExFacet instanceof RegExFacetForParameterAnnotation); assertThat(regExFacet.caseSensitive(), is(false)); assertThat(regExFacet.validation(), is("[123].*")); }
/** * If there is a {@link DefaultedFacet} on any of the action's parameter * types, then installs a {@link ActionDefaultsFacet} for the action. */ @Override public void processParams(final ProcessParameterContext processParameterContext) { // don't overwrite any defaults already picked up if (processParameterContext.getFacetHolder().getFacet(ActionDefaultsFacet.class) != null) { return; } // try to infer defaults from any of the parameter's underlying types final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes(); final DefaultedFacet[] parameterTypeDefaultedFacets = new DefaultedFacet[parameterTypes.length]; boolean hasAtLeastOneDefault = false; for (int i = 0; i < parameterTypes.length; i++) { final Class<?> paramType = parameterTypes[i]; parameterTypeDefaultedFacets[i] = getDefaultedFacet(paramType); hasAtLeastOneDefault = hasAtLeastOneDefault | (parameterTypeDefaultedFacets[i] != null); } if (hasAtLeastOneDefault) { FacetUtil.addFacet(new ActionParameterDefaultFacetDerivedFromTypeFacets(parameterTypeDefaultedFacets, processParameterContext.getFacetHolder())); } }
@Override public void processParams(final ProcessParameterContext processParameterContext) { final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes(); if (processParameterContext.getParamNum() >= parameterTypes.length) { // ignore return; } if (!isString(parameterTypes[processParameterContext.getParamNum()])) { return; } final Annotation[] parameterAnnotations = getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()]; for (final Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation instanceof MultiLine) { final MultiLine annotation = (MultiLine) parameterAnnotation; FacetUtil.addFacet(create(annotation, processParameterContext.getFacetHolder())); return; } } }
@Test public void whenOptionalityIsFalse() { class Customer { public void someAction( @Parameter( optionality = Optionality.MANDATORY ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MandatoryFacet mandatoryFacet = facetedMethodParameter.getFacet(MandatoryFacet.class); Assert.assertNotNull(mandatoryFacet); Assert.assertTrue(mandatoryFacet instanceof MandatoryFacetForParameterAnnotation.Required); }
@Test public void withAnnotation() { class Customer { public void someAction( @Parameter( mustSatisfy = {NotTooHot.class, NotTooCold.class} ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // expecting context.ignoring(mockServicesInjector); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MustSatisfySpecificationFacet mustSatisfySpecificationFacet = facetedMethodParameter.getFacet(MustSatisfySpecificationFacet.class); Assert.assertNotNull(mustSatisfySpecificationFacet); Assert.assertTrue(mustSatisfySpecificationFacet instanceof MustSatisfySpecificationFacetForParameterAnnotation); MustSatisfySpecificationFacetForParameterAnnotation mustSatisfySpecificationFacetImpl = (MustSatisfySpecificationFacetForParameterAnnotation) mustSatisfySpecificationFacet; List<Specification> specifications = mustSatisfySpecificationFacetImpl.getSpecifications(); assertThat(specifications.size(), is(2)); assertTrue(specifications.get(0) instanceof NotTooHot); assertTrue(specifications.get(1) instanceof NotTooCold); }
@Test public void whenNone() { class Customer { public void someAction( @Parameter( ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final RegExFacet regExFacet = facetedMethodParameter.getFacet(RegExFacet.class); Assert.assertNull(regExFacet); }
/** * Attaches all facets applicable to the provided * {@link FeatureType#ACTION_PARAMETER parameter}), to the supplied * {@link FacetHolder}. * * <p> * Delegates to {@link FacetFactory#processParams(ProcessParameterContext)} * for each appropriate factory. * * @see FacetFactory#processParams(ProcessParameterContext) * * @param method * - action method to process * @param paramNum * - 0-based * @param facetedMethodParameter * - holder to attach facets to. */ public void processParams(final Method method, final int paramNum, final FacetedMethodParameter facetedMethodParameter) { final List<FacetFactory> factoryList = getFactoryListByFeatureType(FeatureType.ACTION_PARAMETER); for (final FacetFactory facetFactory : factoryList) { facetFactory.processParams(new ProcessParameterContext(method, paramNum, facetedMethodParameter)); } }
@Override public void processParams(final ProcessParameterContext processParameterContext) { final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes(); if (processParameterContext.getParamNum() >= parameterTypes.length) { // ignore return; } if (!isString(parameterTypes[processParameterContext.getParamNum()])) { return; } final Annotation[] parameterAnnotations = getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()]; for (final Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation instanceof RegEx) { final RegEx annotation = (RegEx) parameterAnnotation; addRegexFacetAndCorrespondingTitleFacet(processParameterContext.getFacetHolder(), annotation); return; } } }
@Test public void whenOptionalityIsTrue() { class Customer { public void someAction( @Parameter( optionality = Optionality.OPTIONAL ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MandatoryFacet mandatoryFacet = facetedMethodParameter.getFacet(MandatoryFacet.class); Assert.assertNotNull(mandatoryFacet); Assert.assertTrue(mandatoryFacet instanceof MandatoryFacetForParameterAnnotation.Optional); }
@Test public void withAnnotation() { class Customer { public void someAction( @Parameter( maxLength = 30 ) final String name) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MaxLengthFacet maxLengthFacet = facetedMethodParameter.getFacet(MaxLengthFacet.class); Assert.assertNotNull(maxLengthFacet); Assert.assertTrue(maxLengthFacet instanceof MaxLengthFacetForParameterAnnotation); assertThat(maxLengthFacet.value(), is(30)); } }
@Test public void whenOptionalityIsDefault() { class Customer { public void someAction( @Parameter( optionality = Optionality.DEFAULT ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MandatoryFacet mandatoryFacet = facetedMethodParameter.getFacet(MandatoryFacet.class); Assert.assertNull(mandatoryFacet); }
@Test public void whenNotAnnotatedOnStringParameter() { class Customer { public void someAction( @Parameter( regexPattern = "[123].*" ) final int name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{int.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final RegExFacet regExFacet = facetedMethodParameter.getFacet(RegExFacet.class); Assert.assertNull(regExFacet); }
public void testNamedAnnotationPickedUpOnActionParameter() { final NamedFacetOnParameterAnnotationFactory facetFactory = new NamedFacetOnParameterAnnotationFactory(); class Customer { @SuppressWarnings("unused") public void someAction(@Named("some name") final int x) { } } final Method actionMethod = findMethod(Customer.class, "someAction", new Class[] { int.class }); expectNoMethodsRemoved(); facetFactory.processParams(new ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(NamedFacet.class); assertThat(facet, is(not(nullValue()))); assertThat(facet instanceof NamedFacetAbstract, is(true)); final NamedFacetAbstract namedFacetAbstract = (NamedFacetAbstract) facet; assertThat(namedFacetAbstract.value(), equalTo("some name")); }
@Override public void processParams(final ProcessParameterContext processParameterContext) { final int paramNum = processParameterContext.getParamNum(); final Class<?> parameterType = processParameterContext.getMethod().getParameterTypes()[paramNum]; final Annotation[] parameterAnnotations = getParameterAnnotations(processParameterContext.getMethod())[paramNum]; for (final Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation instanceof DescribedAs) { FacetUtil.addFacet(create((DescribedAs) parameterAnnotation, processParameterContext.getFacetHolder())); return; } } // otherwise, fall back to a description on the parameter's type, if // available final DescribedAsFacet parameterTypeDescribedAsFacet = getDescribedAsFacet(parameterType); if (parameterTypeDescribedAsFacet != null) { FacetUtil.addFacet(new DescribedAsFacetForParameterDerivedFromType(parameterTypeDescribedAsFacet, processParameterContext.getFacetHolder())); return; } }
public void testAnnotationPickedUpOnActionParameter() { final BigDecimalFacetOnParameterFromJavaxValidationAnnotationFactory facetFactory = new BigDecimalFacetOnParameterFromJavaxValidationAnnotationFactory(); class Order { @SuppressWarnings("unused") public void updateCost( @javax.validation.constraints.Digits(integer=14, fraction=4) final BigDecimal cost) { } } final Method method = findMethod(Order.class, "updateCost", new Class[] { BigDecimal.class }); facetFactory.processParams(new ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(BigDecimalValueFacet.class); assertNotNull(facet); assertTrue(facet instanceof BigDecimalFacetOnParameterFromJavaxValidationDigitsAnnotation); final BigDecimalFacetOnParameterFromJavaxValidationDigitsAnnotation annotation = (BigDecimalFacetOnParameterFromJavaxValidationDigitsAnnotation) facet; assertEquals(new Integer(18), annotation.getLength()); assertEquals(new Integer(4), annotation.getScale()); }
@Test public void whenEmptyString() { class Customer { public void someAction( @Parameter( regexPattern = "" ) final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final RegExFacet regExFacet = facetedMethodParameter.getFacet(RegExFacet.class); Assert.assertNull(regExFacet); }
public void testRegExAnnotationPickedUpOnActionParameter() { class Customer { @SuppressWarnings("unused") public void someAction(@RegEx(validation = "^A.*", caseSensitive = false) final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { String.class }); facetFactory.processParams(new ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(RegExFacet.class); assertNotNull(facet); assertTrue(facet instanceof RegExFacetFromRegExAnnotationOnParameter); final RegExFacetFromRegExAnnotationOnParameter regExFacet = (RegExFacetFromRegExAnnotationOnParameter) facet; assertEquals("^A.*", regExFacet.validation()); assertEquals(false, regExFacet.caseSensitive()); }
@Test public void whenNone() { class Customer { public void someAction( final String name ) { } } // given actionMethod = findMethod(Customer.class, "someAction", new Class[]{String.class} ); // when final FacetFactory.ProcessParameterContext processParameterContext = new FacetFactory.ProcessParameterContext(Customer.class, actionMethod, 0, null, facetedMethodParameter); facetFactory.processParams(processParameterContext); // then final MandatoryFacet mandatoryFacet = facetedMethodParameter.getFacet(MandatoryFacet.class); Assert.assertNull(mandatoryFacet); }
@Override public void processParams(final ProcessParameterContext processParameterContext) { final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes(); if (processParameterContext.getParamNum() >= parameterTypes.length) { // ignore return; } if (parameterTypes[processParameterContext.getParamNum()].isPrimitive()) { return; } final Annotation[] parameterAnnotations = getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()]; for (final Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation instanceof Optional) { FacetUtil.addFacet(new MandatoryFacetInvertedByOptionalForParameter(processParameterContext.getFacetHolder())); return; } } }
public void testMultiLineAnnotationPickedUpOnActionParameter() { final MultiLineAnnotationOnParameterFacetFactory facetFactory = new MultiLineAnnotationOnParameterFacetFactory(); class Customer { @SuppressWarnings("unused") public void someAction(@MultiLine(numberOfLines = 8, preventWrapping = false) final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { String.class }); facetFactory.processParams(new ProcessParameterContext(method, 0, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(MultiLineFacet.class); assertNotNull(facet); assertTrue(facet instanceof MultiLineFacetAnnotationOnParameter); final MultiLineFacetAnnotationOnParameter multiLineFacetAnnotation = (MultiLineFacetAnnotationOnParameter) facet; assertEquals(8, multiLineFacetAnnotation.numberOfLines()); assertEquals(false, multiLineFacetAnnotation.preventWrapping()); }