I have a solution to get all the singleton (I have improve the test too):
@RunWith(MockitoJUnitRunner.class)
public class GuiceBindingScopingVisitorTest {
@Mock
Supplier<Object> supplier;
@Test
public void testScopingVisitor() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Key.get(MyInterface.class,
Names.named("toto1"))).to(MyClass.class);
bind(Key.get(MyInterface.class,
Names.named("toto2"))).to(MyClass.class).in(com.google.inject.Singleton.class);
bind(Key.get(MyInterface.class,
Names.named("toto3"))).to(MyClass.class).in(Singleton.class);
bind(Key.get(MyInterface.class,
Names.named("toto4"))).to(MyClass.class).in(Scopes.SINGLETON);
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type,
TypeEncounter<I> encounter) {
Class<? super I> rawType = type.getRawType();
if (rawType.isAnnotationPresent(Singleton.class) ||
rawType.isAnnotationPresent(com.google.inject.Singleton.class)) {
supplier.get();
}
}
});
}
@Provides @Singleton @Named("toto5")
public MyInterface getMyInterface() {
return new MyClass();
}
});
for (Binding<?> binding : injector.getBindings().values()) {
binding.acceptScopingVisitor(new
DefaultBindingScopingVisitor<Object>() {
@Override
public Object visitScope(Scope scope) {
if (scope == Scopes.SINGLETON) {
supplier.get();
}
return scope;
}
});
}
verify(supplier, times(5)).get();
}
public static interface MyInterface {
}
@Singleton
public static class MyClass implements MyInterface {
}
}
But I'm more and more convince that there is a bug here. My solution is a
work around...
Thanks,
Romain.
Le mercredi 17 avril 2013 23:19:43 UTC+2, Romain Gilles a écrit :
>
> Maybe I'm wrong when I say it could be a bug. So a better question is how
> can I get all the singleton even if they are annotated and not
> bind()...in(...)?
> Thanks,
>
> Romain
>
> Le mercredi 17 avril 2013 23:12:10 UTC+2, Romain Gilles a écrit :
>>
>> Hi all,
>> It is strange when I'm writing this unit test to validate my
>> comprehension of BindingScopingVisitor I don't get my expecting result...
>> Is somebody can help me please?
>> Thanks in advance,
>>
>> The code:
>> import static org.hamcrest.MatcherAssert.assertThat;
>> import static org.mockito.Mockito.times;
>> import static org.mockito.Mockito.verify;
>>
>> import javax.inject.Singleton;
>>
>> import org.junit.Test;
>> import org.junit.runner.RunWith;
>> import org.mockito.Mock;
>> import org.mockito.runners.MockitoJUnitRunner;
>>
>> import com.google.common.base.Supplier;
>> import com.google.inject.*;
>> import com.google.inject.name.Names;
>> import com.google.inject.spi.DefaultBindingScopingVisitor;
>>
>> /**
>> * @author Romain Gilles Date: 4/17/13 Time: 10:40 PM
>> */
>> @RunWith(MockitoJUnitRunner.class)
>> public class GuiceBindingScopingVisitorTest {
>>
>> @Mock
>> Supplier<Object> supplier;
>>
>> @Test
>> public void testScopingVisitor() {
>> Injector injector = Guice.createInjector(new AbstractModule() {
>> @Override
>> protected void configure() {
>> bind(MyInterface.class).to(MyClass.class);
>> bind(Key.get(MyInterface.class,
>> Names.named("toto"))).to(MyClass.class).in(com.google.inject.Singleton.class);
>> bind(Key.get(MyInterface.class,
>> Names.named("toto1"))).to(MyClass.class).in(Singleton.class);
>> bind(Key.get(MyInterface.class,
>> Names.named("toto2"))).to(MyClass.class).in(Scopes.SINGLETON);
>> }
>> });
>> assertThat(injector.getScopeBindings(), is(not(nullValue())));
>> for (Binding<?> binding : injector.getBindings().values()) {
>> binding.acceptScopingVisitor(new
>> DefaultBindingScopingVisitor<Object>() {
>> @Override
>> public Object visitScope(Scope scope) {
>> if (scope == Scopes.SINGLETON) {
>> supplier.get();
>> }
>> return scope;
>> }
>> });
>> }
>> verify(supplier, times(4)).get();
>> }
>>
>> public static interface MyInterface {
>> }
>>
>> @Singleton
>> public static class MyClass implements MyInterface {
>> }
>> }
>>
>> The result:
>> org.mockito.exceptions.verification.TooLittleActualInvocations:
>> supplier.get();
>> Wanted 4 times:
>> -> at
>> test.GuiceBindingScopingVisitorTest.testScopingVisitor(GuiceBindingScopingVisitorTest.java:54)
>> But was 3 times:
>> -> at
>> test.GuiceBindingScopingVisitorTest$2.visitScope(GuiceBindingScopingVisitorTest.java:48)
>>
>>
>> My conclusion:
>> It seems that @Singleton annotation is not handle as an Scope but I think
>> it is a bug not?
>>
>> Regards,
>>
>> Romain
>>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.