Re: how to test session/request scopes ?
thanks dhanji. but what other ways do i have for testing ? i wanted to do some integration testing and i need to have everything injected by the container. nicola On Jun 12, 6:32 pm, Dhanji R. Prasanna dha...@gmail.com wrote: You should bind @RequestScoped and @SessionScoped to your own mock Scope implementation. Then Guice will use this instead of its default HTTP scopes. In this case you want to omit the ServletModule in your test. MockRequestScope myMockScope = new MockRequestScope(); bindScope(RequestScoped.class, myMockScope); //after tests myMockScope.assertThatThingsWereScoped(); Btw, this is a very heavyweight way of testing, so I assume this is not something you need to do often. good luck! Dhanji. On Fri, Jun 12, 2009 at 9:18 AM, naaka delia.nic...@gmail.com wrote: hi, i tried out google guice and it looks great. i have tdd-ed a small spike with persistence and singleton/no scopes and every worked fine. now i have added the servlet-guice extension, and changed a class to session scope, but my test complaint: com.google.inject.ProvisionException: Guice provision errors: 1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request. while locating com.naaka.spikes.domain.User for field at com.naaka.spikes.domain.UserTest.defaultUser (UserTest.java:24) while locating com.naaka.spikes.domain.UserTest i already added the servlet module in the setup method: @BeforeClass public void setupInjection() { ... modules.add(declarePersistence); modules.add(configPersistence); modules.add(new ServletModule()); injector = Guice.createInjector(modules); ... } how i can setup a test to work with session modules ? do i have to mock something (servlet request/response/filter) ? is there a base test class or a test i can copy from ? THANKS ! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Wildcards...
Thanks for your suggestion - I have discovered the provides methods (annotated with @Provides) that do the same thing. So for all you out there - just use that annotation - will save a lot of work and the code gets a lot clearer. Btw: Guice is great Btw2: Plz upload to maven central Regards, Johannes je...@swank.ca wrote: On Jun 6, 8:48 am, Johannes Schneider maili...@cedarsoft.com wrote: bind( ( TypeLiteralList? extends B ) TypeLiteral.get( Types.listOf( Types.subtypeOf( new TypeLiteralB?() { }.getType() ) ) ) ).toProvider( BProvider.class ); Not much I can really say - creating models for complex types code- intensive. TypeLiteral works for some wildcards, and for the ones it doesn't you can either use the Types factory class, or you can do something reflective. private final List? extends B? unused = null; private final TypeLiteralList? extends B? type = fieldToType (MyClass.class, unused); private static Type fieldToType(Class? definedIn, String name) { return definedIn.getDeclaredField(name).getGenericType(); } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
AOP interceptor: match only subclass methods
Hi; Has anyone worked with the AOP interceptors in Guice much? I'm used to using full blown AspectJ in my other projects and am having difficulty reproducing some mix-in behaviour using plain AOP. I want to introduce a tracer on instance methods in certain packages. The catch is that I only want the logic applied to methods declared in those packages. That is, skip methods defined in parent classes that reside outside of the target packages. So say class A resides in org.library and class B that extends A resides in my.project. I want to only intercept methods defined in my.project.B and not any inherited method from org.library.A. Right now, I am using subpackageOf() as my class matcher and any() as my method matcher in my bind interceptor declaration. This doesn't do what I want, it will apply to all methods in the subpackage, even inherited methods from classes outside the packages I state. Does anyone have any ideas on how to do this with a custom method matcher? And if it can't be done statically, what about during runtime in the actual interceptor? Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: AOP interceptor: match only subclass methods
Notice the first Matcher takes a ? super Class and it decides if the class is eligible for AOP. The second Matcher takes a ? super Method and it decides if it should intercept the method or not. Nothing is stopping you from inspecting the method e.g. new AbstractMatcherMethod() { public boolean matches(Method method) { return method.getDeclaringClass().getPackage () // decide if you like this method or not } } Cheers Alen On Jun 13, 3:02 pm, tchan tks...@gmail.com wrote: Hi; Has anyone worked with the AOP interceptors in Guice much? I'm used to using full blown AspectJ in my other projects and am having difficulty reproducing some mix-in behaviour using plain AOP. I want to introduce a tracer on instance methods in certain packages. The catch is that I only want the logic applied to methods declared in those packages. That is, skip methods defined in parent classes that reside outside of the target packages. So say class A resides in org.library and class B that extends A resides in my.project. I want to only intercept methods defined in my.project.B and not any inherited method from org.library.A. Right now, I am using subpackageOf() as my class matcher and any() as my method matcher in my bind interceptor declaration. This doesn't do what I want, it will apply to all methods in the subpackage, even inherited methods from classes outside the packages I state. Does anyone have any ideas on how to do this with a custom method matcher? And if it can't be done statically, what about during runtime in the actual interceptor? Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: how to test session/request scopes ?
You can simulate a request by instantiating GuiceFilter() and passing it a mock request and response. If you have previously installed a configured ServletModule, it will execute the servlets + filters registered in the GS2 pipeline. Dhanji. On Sat, Jun 13, 2009 at 5:12 AM, naaka delia.nic...@gmail.com wrote: thanks dhanji. but what other ways do i have for testing ? i wanted to do some integration testing and i need to have everything injected by the container. nicola On Jun 12, 6:32 pm, Dhanji R. Prasanna dha...@gmail.com wrote: You should bind @RequestScoped and @SessionScoped to your own mock Scope implementation. Then Guice will use this instead of its default HTTP scopes. In this case you want to omit the ServletModule in your test. MockRequestScope myMockScope = new MockRequestScope(); bindScope(RequestScoped.class, myMockScope); //after tests myMockScope.assertThatThingsWereScoped(); Btw, this is a very heavyweight way of testing, so I assume this is not something you need to do often. good luck! Dhanji. On Fri, Jun 12, 2009 at 9:18 AM, naaka delia.nic...@gmail.com wrote: hi, i tried out google guice and it looks great. i have tdd-ed a small spike with persistence and singleton/no scopes and every worked fine. now i have added the servlet-guice extension, and changed a class to session scope, but my test complaint: com.google.inject.ProvisionException: Guice provision errors: 1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request. while locating com.naaka.spikes.domain.User for field at com.naaka.spikes.domain.UserTest.defaultUser (UserTest.java:24) while locating com.naaka.spikes.domain.UserTest i already added the servlet module in the setup method: @BeforeClass public void setupInjection() { ... modules.add(declarePersistence); modules.add(configPersistence); modules.add(new ServletModule()); injector = Guice.createInjector(modules); ... } how i can setup a test to work with session modules ? do i have to mock something (servlet request/response/filter) ? is there a base test class or a test i can copy from ? THANKS ! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Issue 271 in google-guice: Scopes.SINGLETON + null doesn't work
Comment #3 on issue 271 by dhanji: Scopes.SINGLETON + null doesn't work http://code.google.com/p/google-guice/issues/detail?id=271 Im not sure this is something we should support at all. I think it should be an error for a @Provides method to return null. -- You received this message because you are listed in the owner or CC fields of this issue, or because you starred this issue. You may adjust your issue notification preferences at: http://code.google.com/hosting/settings --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice-dev group. To post to this group, send email to google-guice-dev@googlegroups.com To unsubscribe from this group, send email to google-guice-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice-dev?hl=en -~--~~~~--~~--~--~---
Issue 271 in google-guice: Scopes.SINGLETON + null doesn't work
Comment #4 on issue 271 by chris.nokleberg: Scopes.SINGLETON + null doesn't work http://code.google.com/p/google-guice/issues/detail?id=271 You mean remove support for @Nullable? -- You received this message because you are listed in the owner or CC fields of this issue, or because you starred this issue. You may adjust your issue notification preferences at: http://code.google.com/hosting/settings --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice-dev group. To post to this group, send email to google-guice-dev@googlegroups.com To unsubscribe from this group, send email to google-guice-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice-dev?hl=en -~--~~~~--~~--~--~---