|
Java example source code file (BinderTestSuite.java)
The BinderTestSuite.java Java example source code
/**
* Copyright (C) 2008 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.inject.Asserts.assertContains;
import static com.google.inject.name.Names.named;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;
import com.google.inject.name.Named;
import com.google.inject.util.Providers;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author jessewilson@google.com (Jesse Wilson)
*/
public class BinderTestSuite extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
new Builder()
.name("bind A")
.module(new AbstractModule() {
protected void configure() {
bind(A.class);
}
})
.creationException("No implementation for %s was bound", A.class.getName())
.addToSuite(suite);
new Builder()
.name("bind PlainA named apple")
.module(new AbstractModule() {
protected void configure() {
bind(PlainA.class).annotatedWith(named("apple"));
}
})
.creationException("No implementation for %s annotated with %s was bound",
PlainA.class.getName(), named("apple"))
.addToSuite(suite);
new Builder()
.name("bind A to new PlainA(1)")
.module(new AbstractModule() {
protected void configure() {
bind(A.class).toInstance(new PlainA(1));
}
})
.creationTime(CreationTime.NONE)
.expectedValues(new PlainA(1), new PlainA(1), new PlainA(1))
.addToSuite(suite);
new Builder()
.name("no binding, AWithProvidedBy")
.key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class)
.addToSuite(suite);
new Builder()
.name("no binding, AWithImplementedBy")
.key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class)
.addToSuite(suite);
new Builder()
.name("no binding, ScopedA")
.key(Key.get(ScopedA.class), InjectsScopedA.class)
.expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202))
.addToSuite(suite);
new Builder()
.name("no binding, AWithProvidedBy named apple")
.key(Key.get(AWithProvidedBy.class, named("apple")),
InjectsAWithProvidedByNamedApple.class)
.configurationException("No implementation for %s annotated with %s was bound",
AWithProvidedBy.class.getName(), named("apple"))
.addToSuite(suite);
new Builder()
.name("no binding, AWithImplementedBy named apple")
.key(Key.get(AWithImplementedBy.class, named("apple")),
InjectsAWithImplementedByNamedApple.class)
.configurationException("No implementation for %s annotated with %s was bound",
AWithImplementedBy.class.getName(), named("apple"))
.addToSuite(suite);
new Builder()
.name("no binding, ScopedA named apple")
.key(Key.get(ScopedA.class, named("apple")), InjectsScopedANamedApple.class)
.configurationException("No implementation for %s annotated with %s was bound",
ScopedA.class.getName(), named("apple"))
.addToSuite(suite);
for (final Scoper scoper : Scoper.values()) {
new Builder()
.name("bind PlainA")
.key(Key.get(PlainA.class), InjectsPlainA.class)
.module(new AbstractModule() {
protected void configure() {
AnnotatedBindingBuilder<PlainA> abb = bind(PlainA.class);
scoper.configure(abb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind A to PlainA")
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
scoper.configure(sbb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind A to PlainAProvider.class")
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
scoper.configure(sbb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind A to new PlainAProvider()")
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
scoper.configure(sbb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind AWithProvidedBy")
.key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class)
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
scoper.configure(sbb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind AWithImplementedBy")
.key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class)
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
scoper.configure(sbb);
}
})
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind ScopedA")
.key(Key.get(ScopedA.class), InjectsScopedA.class)
.module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(ScopedA.class);
scoper.configure(sbb);
}
})
.expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202))
.scoper(scoper)
.addToSuite(suite);
new Builder()
.name("bind AWithProvidedBy named apple")
.module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(AWithProvidedBy.class).annotatedWith(named("apple")));
}
})
.creationException("No implementation for %s annotated with %s was bound",
AWithProvidedBy.class.getName(), named("apple"))
.addToSuite(suite);
new Builder()
.name("bind AWithImplementedBy named apple")
.module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(AWithImplementedBy.class).annotatedWith(named("apple")));
}
})
.creationException("No implementation for %s annotated with %s was bound",
AWithImplementedBy.class.getName(), named("apple"))
.addToSuite(suite);
new Builder()
.name("bind ScopedA named apple")
.module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(ScopedA.class).annotatedWith(named("apple")));
}
})
.creationException("No implementation for %s annotated with %s was bound",
ScopedA.class.getName(), named("apple"))
.addToSuite(suite);
}
return suite;
}
enum Scoper {
UNSCOPED {
void configure(ScopedBindingBuilder sbb) {}
void apply(Builder builder) {}
},
EAGER_SINGLETON {
void configure(ScopedBindingBuilder sbb) {
sbb.asEagerSingleton();
}
void apply(Builder builder) {
builder.expectedValues(new PlainA(101), new PlainA(101), new PlainA(101));
builder.creationTime(CreationTime.EAGER);
}
},
SCOPES_SINGLETON {
void configure(ScopedBindingBuilder sbb) {
sbb.in(Scopes.SINGLETON);
}
void apply(Builder builder) {
builder.expectedValues(new PlainA(201), new PlainA(201), new PlainA(201));
}
},
SINGLETON_DOT_CLASS {
void configure(ScopedBindingBuilder sbb) {
sbb.in(Singleton.class);
}
void apply(Builder builder) {
builder.expectedValues(new PlainA(201), new PlainA(201), new PlainA(201));
}
},
TWO_AT_A_TIME_SCOPED_DOT_CLASS {
void configure(ScopedBindingBuilder sbb) {
sbb.in(TwoAtATimeScoped.class);
}
void apply(Builder builder) {
builder.expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202));
}
},
TWO_AT_A_TIME_SCOPE {
void configure(ScopedBindingBuilder sbb) {
sbb.in(new TwoAtATimeScope());
}
void apply(Builder builder) {
builder.expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202));
}
};
abstract void configure(ScopedBindingBuilder sbb);
abstract void apply(Builder builder);
}
/** When Guice creates a value, directly or via a provider */
enum CreationTime {
NONE, EAGER, LAZY
}
public static class Builder {
private String name = "test";
private Key<?> key = Key.get(A.class);
private Class<? extends Injectable> injectsKey = InjectsA.class;
private List<Module> modules = Lists.
Other Java examples (source code examples)Here is a short list of links related to this Java BinderTestSuite.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.