private static List<FacetedMethodParameter> getParameters(final Method actionMethod) { final Class<?>[] parameterTypes = actionMethod.getParameterTypes(); final List<FacetedMethodParameter> actionParams = Lists.newArrayList(); for (final Class<?> parameterType : parameterTypes) { actionParams.add(new FacetedMethodParameter(parameterType)); } return Collections.unmodifiableList(actionParams); }
/** * 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())); } }
@Test public void addsAMustSatisfySpecificationFacetIfAnnotated() { context.checking(new Expectations() { { oneOf(mockFacetedMethodParameter).addFacet(with(anInstanceOf(MustSatisfySpecificationFacetForMustSatisfyAnnotationOnParameter.class))); } }); facetFactory.processParamsMustSatisfy(new ProcessParameterContext(Customer.class, changeLastNameMethodWith, 0, null, mockFacetedMethodParameter)); }
@Before public void setUp() throws Exception { stubConfiguration = new IsisConfigurationDefault(); context.checking(new Expectations() {{ allowing(mockServicesInjector).lookupService(TranslationService.class); will(returnValue(mockTranslationService)); allowing(mockServicesInjector).getConfigurationServiceInternal(); will(returnValue(stubConfiguration)); allowing(mockServicesInjector).injectServicesInto(with(any(List.class))); }}); domainObjectClassWithoutAnnotation = DomainObjectWithoutMustSatisfyAnnotations.class; domainObjectClassWithAnnotation = DomainObjectWithMustSatisfyAnnotations.class; changeLastNameMethodWithout = domainObjectClassWithoutAnnotation.getMethod("changeLastName", String.class); changeLastNameMethodWith = domainObjectClassWithAnnotation.getMethod("changeLastName", String.class); context.checking(new Expectations() { { allowing(mockFacetedMethodParameter).getIdentifier(); will(returnValue(Identifier.actionIdentifier(Customer.class, "foo"))); } }); facetFactory = new ParameterAnnotationFacetFactory(); facetFactory.setServicesInjector(mockServicesInjector); }
@Test public void doesNotAddsAMustSatisfySpecificationFacetIfNotAnnotated() { context.checking(new Expectations() { { never(mockFacetedMethodParameter).addFacet(with(anInstanceOf(MustSatisfySpecificationFacetForMustSatisfyAnnotationOnParameter.class))); } }); facetFactory.processParamsMustSatisfy(new ProcessParameterContext(Customer.class, changeLastNameMethodWithout, 0, null, mockFacetedMethodParameter)); }
public void testParameterLayoutAnnotationNamedEscapedFalse() { final ParameterLayoutFacetFactory facetFactory = new ParameterLayoutFacetFactory(); class Customer { @SuppressWarnings("unused") public void someAction(@ParameterLayout(named = NAME, namedEscaped = false) final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[]{String.class}); facetFactory.processParams(new FacetFactory.ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final NamedFacet facet = facetedMethodParameter.getFacet(NamedFacet.class); assertThat(facet, is(notNullValue())); assertThat(facet, is(instanceOf(NamedFacetForParameterLayoutAnnotation.class))); assertThat(facet.value(), is(equalTo(NAME))); assertThat(facet.escaped(), is(false)); }
@Override protected void setUp() throws Exception { Logger.getRootLogger().setLevel(Level.OFF); super.setUp(); BasicConfigurator.configure(); reflector = new ProgrammableReflector(); facetHolder = new FacetHolderImpl(); facetedMethod = FacetedMethod.createProperty(Customer.class, "firstName"); facetedMethodParameter = new FacetedMethodParameter(String.class); methodRemover = new ProgrammableMethodRemover(); }
@Test public void addsAMustSatisfySpecificationFacetIfAnnotated() { final MustSatisfySpecificationOnParameterFacetFactory facetFactory = new MustSatisfySpecificationOnParameterFacetFactory(); mockery.checking(new Expectations() { { one(mockFacetedMethodParameter).addFacet(with(anInstanceOf(MustSatisfySpecificationOnParameterFacet.class))); } }); facetFactory.processParams(new ProcessParameterContext(changeLastNameMethodWith, 0, mockFacetedMethodParameter)); }
public void testParameterLayoutAnnotationNamed() { final ParameterLayoutFacetFactory facetFactory = new ParameterLayoutFacetFactory(); class Customer { @SuppressWarnings("unused") public void someAction(@ParameterLayout(named = NAME) final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[]{String.class}); facetFactory.processParams(new FacetFactory.ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final NamedFacet facet = facetedMethodParameter.getFacet(NamedFacet.class); assertThat(facet, is(notNullValue())); assertThat(facet, is(instanceOf(NamedFacetForParameterLayoutAnnotation.class))); assertThat(facet.value(), is(equalTo(NAME))); assertThat(facet.escaped(), is(true)); }
Identifier.propertyOrCollectionIdentifier(Customer.class, "firstName")); facetedMethod = FacetedMethod.createForProperty(Customer.class, "firstName"); facetedMethodParameter = new FacetedMethodParameter( FeatureType.ACTION_PARAMETER_SCALAR, facetedMethod.getOwningType(), facetedMethod.getMethod(), String.class );
@Test public void doesNotAddsAMustSatisfySpecificationFacetIfNotAnnotated() { final MustSatisfySpecificationOnParameterFacetFactory facetFactory = new MustSatisfySpecificationOnParameterFacetFactory(); mockery.checking(new Expectations() { { never(mockFacetedMethodParameter).addFacet(with(anInstanceOf(MustSatisfySpecificationOnParameterFacet.class))); } }); facetFactory.processParams(new ProcessParameterContext(changeLastNameMethodWithout, 0, mockFacetedMethodParameter)); }
public void testParameterLayoutAnnotationPickedUp() { final ParameterLayoutFacetFactory facetFactory = new ParameterLayoutFacetFactory(); class Customer { @SuppressWarnings("unused") public void someAction(@ParameterLayout(labelPosition = LabelPosition.LEFT) final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { String.class }); facetFactory.processParams(new FacetFactory.ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(LabelAtFacet.class); assertThat(facet, is(notNullValue())); assertThat(facet, is(instanceOf(LabelAtFacetForParameterLayoutAnnotation.class))); final LabelAtFacetForParameterLayoutAnnotation layoutAnnotation = (LabelAtFacetForParameterLayoutAnnotation) facet; assertThat(layoutAnnotation.label(), is(LabelPosition.LEFT)); } }
@Before public void setUpFacetedMethodAndParameter() throws Exception { context.checking(new Expectations() {{ allowing(mockServicesInjector).getDeploymentCategoryProvider(); will(returnValue(mockDeploymentCategoryProvider)); allowing(mockServicesInjector).lookupService(DeploymentCategoryProvider.class); will(returnValue(mockDeploymentCategoryProvider)); allowing(mockDeploymentCategoryProvider).getDeploymentCategory(); will(returnValue(DeploymentCategory.PRODUCTION)); allowing(mockServicesInjector).getConfigurationServiceInternal(); will(returnValue(mockConfiguration)); allowing(mockServicesInjector).getPersistenceSessionServiceInternal(); will(returnValue(mockPersistenceSessionServiceInternal)); allowing(mockServicesInjector).lookupService(TranslationService.class); will(returnValue(mockTranslationService)); allowing(mockServicesInjector).getAuthenticationSessionProvider(); will(returnValue(mockAuthenticationSessionProvider)); allowing(mockServicesInjector).lookupService(AuthenticationSessionProvider.class); will(returnValue(mockAuthenticationSessionProvider)); allowing(mockServicesInjector).getSpecificationLoader(); will(returnValue(mockSpecificationLoader)); }}); facetHolder = new AbstractFacetFactoryTest.IdentifiedHolderImpl(Identifier.propertyOrCollectionIdentifier(Customer.class, "firstName")); facetedMethod = FacetedMethod.createForProperty(AbstractFacetFactoryTest.Customer.class, "firstName"); facetedMethodParameter = new FacetedMethodParameter(FeatureType.ACTION_PARAMETER_SCALAR, facetedMethod.getOwningType(), facetedMethod.getMethod(), String.class ); }
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")); }
public void testOptionalAnnotationPickedUpOnActionParameter() { class Customer { @SuppressWarnings("unused") public void someAction(@Optional final String foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { String.class }); facetFactory.processParamsOptional(new ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); final Facet facet = facetedMethodParameter.getFacet(MandatoryFacet.class); assertNotNull(facet); assertTrue(facet instanceof MandatoryFacetInvertedByOptionalAnnotationOnParameter); }
public void testMaskAnnotationNotIgnoredForPrimitiveOnActionParameter() { final MaskFacetOnParameterAnnotationFactory facetFactory = new MaskFacetOnParameterAnnotationFactory(); facetFactory.setServicesInjector(stubServicesInjector); class Customer { @SuppressWarnings("unused") public void someAction(@Mask("###") final int foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { int.class }); facetFactory.processParams(new ProcessParameterContext(Customer.class, method, 0, null, facetedMethodParameter)); assertNotNull(facetedMethodParameter.getFacet(MaskFacet.class)); }
public void testMaskAnnotationNotIgnoredForPrimitiveOnActionParameter() { final MaskAnnotationForParameterFacetFactory facetFactory = new MaskAnnotationForParameterFacetFactory(); facetFactory.setSpecificationLookup(reflector); class Customer { @SuppressWarnings("unused") public void someAction(@Mask("###") final int foo) { } } final Method method = findMethod(Customer.class, "someAction", new Class[] { int.class }); facetFactory.processParams(new ProcessParameterContext(method, 0, facetedMethodParameter)); assertNotNull(facetedMethodParameter.getFacet(MaskFacet.class)); }
@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); }
@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); }
@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); }