|
Java example source code file (ProvisionListenerTest.java)
The ProvisionListenerTest.java Java example source code
/**
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.inject;
import static com.google.common.collect.ImmutableList.of;
import static com.google.inject.Asserts.assertContains;
import static com.google.inject.name.Names.named;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.matcher.Matcher;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Named;
import com.google.inject.spi.DependencyAndSource;
import com.google.inject.spi.InstanceBinding;
import com.google.inject.spi.ProvisionListener;
import com.google.inject.util.Providers;
import junit.framework.TestCase;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* Tests for {@link Binder#bindListener(Matcher, ProvisionListener...)}
*
* @author sameb@google.com (Sam Berlin)
*/
// TODO(sameb): Add some tests for private modules & child injectors.
public class ProvisionListenerTest extends TestCase {
public void testExceptionInListenerBeforeProvisioning() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new FailBeforeProvision());
}
});
try {
injector.getInstance(Foo.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error notifying ProvisionListener " + FailBeforeProvision.class.getName()
+ " of " + Foo.class.getName(),
"Reason: java.lang.RuntimeException: boo",
"while locating " + Foo.class.getName());
assertEquals("boo", pe.getCause().getMessage());
}
}
public void testExceptionInListenerAfterProvisioning() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new FailAfterProvision());
}
});
try {
injector.getInstance(Foo.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error notifying ProvisionListener " + FailAfterProvision.class.getName()
+ " of " + Foo.class.getName(),
"Reason: java.lang.RuntimeException: boo",
"while locating " + Foo.class.getName());
assertEquals("boo", pe.getCause().getMessage());
}
}
public void testExceptionInProvisionExplicitlyCalled() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new JustProvision());
}
});
try {
injector.getInstance(FooBomb.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error injecting constructor, java.lang.RuntimeException: Retry, Abort, Fail",
" at " + FooBomb.class.getName(),
" while locating " + FooBomb.class.getName());
assertEquals("Retry, Abort, Fail", pe.getCause().getMessage());
}
}
public void testExceptionInProvisionAutomaticallyCalled() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new NoProvision());
}
});
try {
injector.getInstance(FooBomb.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error injecting constructor, java.lang.RuntimeException: Retry, Abort, Fail",
" at " + FooBomb.class.getName(),
" while locating " + FooBomb.class.getName());
assertEquals("Retry, Abort, Fail", pe.getCause().getMessage());
}
}
public void testExceptionInFieldProvision() throws Exception {
final CountAndCaptureExceptionListener listener = new CountAndCaptureExceptionListener();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
bindListener(new AbstractMatcher<Binding>>() {
@Override public boolean matches(Binding<?> binding) {
return binding.getKey().getRawType().equals(DependsOnFooBombInField.class);
}
}, listener);
}
});
assertEquals(0, listener.beforeProvision);
String expectedMsg = null;
try {
injector.getInstance(DependsOnFooBombInField.class);
fail();
} catch (ProvisionException expected) {
assertEquals(1, expected.getErrorMessages().size());
expectedMsg = expected.getMessage();
assertContains(listener.capture.get().getMessage(),
"1) Error injecting constructor, java.lang.RuntimeException: Retry, Abort, Fail",
" at " + FooBomb.class.getName(),
" while locating " + FooBomb.class.getName(),
" while locating " + DependsOnFooBombInField.class.getName());
}
assertEquals(1, listener.beforeProvision);
assertEquals(expectedMsg, listener.capture.get().getMessage());
assertEquals(0, listener.afterProvision);
}
public void testExceptionInCxtorProvision() throws Exception {
final CountAndCaptureExceptionListener listener = new CountAndCaptureExceptionListener();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
bindListener(new AbstractMatcher<Binding>>() {
@Override public boolean matches(Binding<?> binding) {
return binding.getKey().getRawType().equals(DependsOnFooBombInCxtor.class);
}
}, listener);
}
});
assertEquals(0, listener.beforeProvision);
String expectedMsg = null;
try {
injector.getInstance(DependsOnFooBombInCxtor.class);
fail();
} catch (ProvisionException expected) {
assertEquals(1, expected.getErrorMessages().size());
expectedMsg = expected.getMessage();
assertContains(listener.capture.get().getMessage(),
"1) Error injecting constructor, java.lang.RuntimeException: Retry, Abort, Fail",
" at " + FooBomb.class.getName(),
" while locating " + FooBomb.class.getName(),
" while locating " + DependsOnFooBombInCxtor.class.getName());
}
assertEquals(1, listener.beforeProvision);
assertEquals(expectedMsg, listener.capture.get().getMessage());
assertEquals(0, listener.afterProvision);
}
public void testListenerCallsProvisionTwice() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new ProvisionTwice());
}
});
try {
injector.getInstance(Foo.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error notifying ProvisionListener " + ProvisionTwice.class.getName()
+ " of " + Foo.class.getName(),
"Reason: java.lang.IllegalStateException: Already provisioned in this listener.",
"while locating " + Foo.class.getName());
assertEquals("Already provisioned in this listener.", pe.getCause().getMessage());
}
}
public void testCachedInScopePreventsProvisionNotify() {
final Counter count1 = new Counter();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), count1);
bind(Foo.class).in(Scopes.SINGLETON);
}
});
Foo foo = injector.getInstance(Foo.class);
assertNotNull(foo);
assertEquals(1, count1.count);
// not notified the second time because nothing is provisioned
// (it's cached in the scope)
count1.count = 0;
assertSame(foo, injector.getInstance(Foo.class));
assertEquals(0, count1.count);
}
public void testCombineAllBindListenerCalls() {
final Counter count1 = new Counter();
final Counter count2 = new Counter();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), count1);
bindListener(Matchers.any(), count2);
}
});
assertNotNull(injector.getInstance(Foo.class));
assertEquals(1, count1.count);
assertEquals(1, count2.count);
}
public void testNotifyEarlyListenersIfFailBeforeProvision() {
final Counter count1 = new Counter();
final Counter count2 = new Counter();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), count1, new FailBeforeProvision(), count2);
}
});
try {
injector.getInstance(Foo.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error notifying ProvisionListener " + FailBeforeProvision.class.getName()
+ " of " + Foo.class.getName(),
"Reason: java.lang.RuntimeException: boo",
"while locating " + Foo.class.getName());
assertEquals("boo", pe.getCause().getMessage());
assertEquals(1, count1.count);
assertEquals(0, count2.count);
}
}
public void testNotifyLaterListenersIfFailAfterProvision() {
final Counter count1 = new Counter();
final Counter count2 = new Counter();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), count1, new FailAfterProvision(), count2);
}
});
try {
injector.getInstance(Foo.class);
fail();
} catch(ProvisionException pe) {
assertEquals(1, pe.getErrorMessages().size());
assertContains(pe.getMessage(),
"1) Error notifying ProvisionListener " + FailAfterProvision.class.getName()
+ " of " + Foo.class.getName(),
"Reason: java.lang.RuntimeException: boo",
"while locating " + Foo.class.getName());
assertEquals("boo", pe.getCause().getMessage());
assertEquals(1, count1.count);
assertEquals(1, count2.count);
}
}
public void testNotifiedKeysOfAllBindTypes() {
final Capturer capturer = new Capturer();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), capturer);
bind(Foo.class).annotatedWith(named("pk")).toProvider(FooP.class);
try {
bind(Foo.class).annotatedWith(named("cxtr")).toConstructor(Foo.class.getDeclaredConstructor());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
bind(LinkedFoo.class).to(Foo.class);
bind(Interface.class).toInstance(new Implementation());
bindConstant().annotatedWith(named("constant")).to("MyConstant");
}
@Provides @Named("pi") Foo provideFooBar() {
return new Foo();
}
});
// toInstance & constant bindings are notified in random order, at the very beginning.
assertEquals(
ImmutableSet.of(Key.get(Interface.class), Key.get(String.class, named("constant"))),
capturer.getAsSetAndClear());
// simple binding
assertNotNull(injector.getInstance(Foo.class));
assertEquals(of(Key.get(Foo.class)), capturer.getAndClear());
// provider key binding -- notifies about provider & the object, always
assertNotNull(injector.getInstance(Key.get(Foo.class, named("pk"))));
assertEquals(of(Key.get(FooP.class), Key.get(Foo.class, named("pk"))), capturer.getAndClear());
assertNotNull(injector.getInstance(Key.get(Foo.class, named("pk"))));
assertEquals(of(Key.get(FooP.class), Key.get(Foo.class, named("pk"))), capturer.getAndClear());
// JIT provider key binding -- notifies about provider & the object, always
assertNotNull(injector.getInstance(JitFoo2.class));
assertEquals(of(Key.get(JitFoo2P.class), Key.get(JitFoo2.class)), capturer.getAndClear());
assertNotNull(injector.getInstance(JitFoo2.class));
assertEquals(of(Key.get(JitFoo2P.class), Key.get(JitFoo2.class)), capturer.getAndClear());
// provider instance binding -- just the object (not the provider)
assertNotNull(injector.getInstance(Key.get(Foo.class, named("pi"))));
assertEquals(of(Key.get(Foo.class, named("pi"))), capturer.getAndClear());
// toConstructor binding
assertNotNull(injector.getInstance(Key.get(Foo.class, named("cxtr"))));
assertEquals(of(Key.get(Foo.class, named("cxtr"))), capturer.getAndClear());
// linked binding -- notifies about the target (that's what's provisioned), not the link
assertNotNull(injector.getInstance(LinkedFoo.class));
assertEquals(of(Key.get(Foo.class)), capturer.getAndClear());
// JIT linked binding -- notifies about the target (that's what's provisioned), not the link
assertNotNull(injector.getInstance(JitFoo.class));
assertEquals(of(Key.get(Foo.class)), capturer.getAndClear());
}
public void testSingletonMatcher() {
final Counter counter = new Counter();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(new AbstractMatcher<Binding>>() {
@Override
public boolean matches(Binding<?> t) {
return Scopes.isSingleton(t);
}
}, counter);
}
});
assertEquals(0, counter.count);
// no increment for getting Many.
injector.getInstance(Many.class);
assertEquals(0, counter.count);
// but an increment for getting Sole, since it's a singleton.
injector.getInstance(Sole.class);
assertEquals(1, counter.count);
}
public void testCallingBindingDotGetProviderDotGet() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new ProvisionListener() {
@Override
public <T> void onProvision(ProvisionInvocation
Other Java examples (source code examples)Here is a short list of links related to this Java ProvisionListenerTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.