Is there a simple example for validation of method parameters with Seam Validation? - seam

I'm stuck...
I know https://github.com/gunnarmorling/methodvalidation-integration/tree/master/methodvalidation-cdi, which works fine, and I know that there's Seam Validation that should help me validate my method parameters within Hibernate Validator 4.2.0
I refactored the example above to use Seam Validation:
Replaced AutoValidating and ValidationInterceptor with Seam Validation's classes
Changed beans.xml to use org.jboss.seam.validation.ValidationInterceptor
Changed CdiMethodValidationTest to use Seam Validation's ValidationInterceptor
Added seam-validation-api and seam-validation 3.1.0.Final to the Maven POM
Changed arquillian-weld-se-embedded-1 to arquillian-weld-ee-embedded-1.1 in the POM
All I get is
java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.api.Bootstrap.parse(Ljava/lang/Iterable;)Lorg/jboss/weld/bootstrap/spi/BeansXml;
at org.jboss.arquillian.container.weld.ee.embedded_1_1.mock.TestContainer.<init>(TestContainer.java:215)
at org.jboss.arquillian.container.weld.ee.embedded_1_1.WeldEEMockContainer.deploy(WeldEEMockContainer.java:76)
at org.jboss.arquillian.impl.handler.ContainerDeployer.callback(ContainerDeployer.java:62)
at org.jboss.arquillian.impl.handler.ContainerDeployer.callback(ContainerDeployer.java:50)
at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
at org.jboss.arquillian.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:96)
at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:162)
at org.jboss.arquillian.junit.Arquillian$3$1.evaluate(Arquillian.java:186)
at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:182)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
What am I doing wrong?

Related

Dependency injection using PRISM Xamarin Forms doesn't work

I have a problem with dependency injection in my project. I use PRISM framework in my project and I chose Ioc container when create it. Link -> https://github.com/blackpantific/InstagroomEX
In my app.xaml file I associate the class and interface
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation();
containerRegistry.RegisterForNavigation<WelcomeView, WelcomeViewModel>();
containerRegistry.RegisterForNavigation<RegistrationView, RegistrationViewModel>();
containerRegistry.RegisterForNavigation<LoginView, LoginViewModel>();
//regestering
containerRegistry.RegisterSingleton<IValidationService, ValidationService>();
}
But my page after initializing() doesn't appear at the screen. This is ViewModel constructor
public RegistrationViewModel(INavigationService navigationService, IUserDataService userDataService,
IValidationService validationService) :
base(navigationService)
{
_userDataService = userDataService;
_validationService = validationService;
}
Something wrong here. When I pass to the RegistrationViewModel() constructor another parameters besides INavigationService navigationService the RegistrationView page doesn't not displayed. What am I doing wrong?
Assuming that WelcomePage is displayed correctly and RegistrationPage is not, I think that Andres Castro is correct: Prism tries to resolve the IUserDataService which is not possible. In the debug output you should see a message that IUserDataService cannot be resolved.
(Note: The following is based on my experiences with Prism and Unity. This might hold for other Dependency Injection frameworks, but it's possible that other frameworks behave differently.)
Prism relies on a Dependency Injection (DI) framework, for example Microsofts Unity application block. What happens when you try to navigate to RegistrationPage is, that Prism (if you are using ViewModelLocator.AutowireViewModel="True") tries to determine the type of the viewmodel (by convention) and asks the underlying DI framework to resolve this type (i.e. create an instance). For each of the required constructors parameters of this type, then again the DI framework tries to resolve them. If the parameters require concrete types it will then try to resolve those. If the parametery require interface (or abstract) types, the DI framework will look at its registrations and if the types have been registered create instances according to this registrations (which in turn might involve resolving other types).
If - however - the parameter requires an interface type which has not been registered, the DI framework does not know how to handle the situation. It coulld of course assume null, but this might lead to errors that might be way harder to track down, hence it will throw an exception (which is swallowed and Logged by Prism).
Now how can you handle the situation?
You'll need to tell the DI framework how to resolve IUserDataInterface. Since RegistrationPageViewModel does not actually use it, you could register null
containerRegistry.RegisterInstance<IValidationService>(null);
Could be irritating later, though. I'd rather remove the dependency of RegistrationPageViewModel to IUserDataService altogether - for now and add it later when it's actually used - or create a mock/stub that implements IUserDataService and is used as long there is no real implementation. This would be registered with the DI framework as you did with IValidationService.

java : Is it possible to dynamically modify the code of a servlet's method (like doPost)

I'm trying to write a Framework like Spring MVC.
I'm looking for a way to modify the code of the doPost/doGet method of a servlet 3.0 deployed on tomcat using javassist or reflection or whatever so the doPost can call a service method dynamically defined .
doPost(...){
ServiceClassName.methodeName(); // dynamic line of code
}
Thanks
It is possible using javassist. Refer this to write your Transformer class.
Although I have never tried it before, have a look at http://www.bytebuddy.net for a bytecode creation/manipulation library.

adding constraints dynamically to hibernate validator in a spring mvc application

I am using hibernate validator in my spring mvc app. With the following simple steps I am able to get the validation happen.
pom.xml changes to add hibernate validator.
Annotated the beans with some constraints.
Annotated the controller method parameter with #Valid.
Handled the MethodArgumentNotValidException
I don't want to add the constraints to the beans, instead want to add them to the beans dynamically. This dynamic addition need to happen during application startup and just once.
In the hibernate validator documentation, I see the following snippet.
constraintMapping.type( Car.class ).property( "manufacturer", FIELD ).constraint( new NotNullDef() )
My question is, how do I get a handle to this ConstraintMapping instance ? I see that Spring instantiates the hibernate validator through the LocalValidatorFactoryBean.
thanks for your help.
You get it from the HibernateValidatorConfiguration - http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html_single/#section-programmatic-api
HibernateValidatorConfiguration configuration = Validation
        .byProvider( HibernateValidator.class )
        .configure();
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping
    .type( Car.class )
        .property( "manufacturer", FIELD )
            .constraint( new NotNullDef() )
        .property( "licensePlate", FIELD )
            .ignoreAnnotations()
            .constraint( new NotNullDef() )
            .constraint( new SizeDef().min( 2 ).max( 14 ) )
    .type( RentalCar.class )
        .property( "rentalStation", METHOD )
            .constraint( new NotNullDef() );
Validator validator = configuration.addMapping( constraintMapping )
        .buildValidatorFactory()
        .getValidator();
This mean, however, that you probably cannot use LocalValidatorFactoryBean, but you probably can just write your own.  

CDI using an interceptor for a decorator

I would like to know if it is possible by definition, that a decorator may have an interceptor for a method or not. I am using owb and it does not work!
EDIT: Just tested it with WELD and it's the same! Why does a decorator may not have an interceptor?
IMO: Methods in the decorator are never called directly through a contextual reference, therefore must not interceptable. These are container invocations of methods.

Seam component init method after request parameters are set

I'm using an event-scope component as a view controller. I need to have an "init" method on that controller, where I can do authorization checks, load some entities from JPA, etc.
Problem is, if I choose to do that in a #Create method, parameters defined in page.xml are not yet set (via GET). #RequestParameter fields get set, but they are less flexible - can't use them in inner classes of the component, or just somewhere deeper down in a bean path. For example, I can direct name=abc to #{controller.user.name}, but can only use a component-level setter with #RequestParameter.
Is there another way to do an "init" method, where all request parameters are set, then?
Couldn't find a solution, after all. Moving project to Spring MVC.
maybe u can try this in u page.xml
<param name="xxx" value="#{component.xxx}"/>
<action execute="#{component.init}"/>

Resources