blob: 46313aeea86ef8d4eedcf134c645abfc29189840 [file] [log] [blame]
/**
* Copyright (C) 2006 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 junit.framework.TestCase;
import java.io.IOException;
/**
* @author crazybob@google.com (Bob Lee)
*/
public class ScopesTest extends TestCase {
private final AbstractModule singletonsModule = new AbstractModule() {
protected void configure() {
bind(BoundAsSingleton.class).in(Scopes.SINGLETON);
bind(AnnotatedSingleton.class);
bind(EagerSingleton.class).asEagerSingleton();
bind(LinkedSingleton.class).to(RealLinkedSingleton.class);
bind(DependsOnJustInTimeSingleton.class);
bind(NotASingleton.class);
}
};
@Override protected void setUp() throws Exception {
AnnotatedSingleton.nextInstanceId = 0;
BoundAsSingleton.nextInstanceId = 0;
EagerSingleton.nextInstanceId = 0;
RealLinkedSingleton.nextInstanceId = 0;
JustInTimeSingleton.nextInstanceId = 0;
NotASingleton.nextInstanceId = 0;
}
public void testSingletons() {
Injector injector = Guice.createInjector(singletonsModule);
assertSame(
injector.getInstance(BoundAsSingleton.class),
injector.getInstance(BoundAsSingleton.class));
assertSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
assertSame(
injector.getInstance(EagerSingleton.class),
injector.getInstance(EagerSingleton.class));
assertSame(
injector.getInstance(LinkedSingleton.class),
injector.getInstance(LinkedSingleton.class));
assertSame(
injector.getInstance(JustInTimeSingleton.class),
injector.getInstance(JustInTimeSingleton.class));
assertNotSame(
injector.getInstance(NotASingleton.class),
injector.getInstance(NotASingleton.class));
}
public void testJustInTimeAnnotatedSingleton() {
Injector injector = Guice.createInjector();
assertSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
}
public void testSingletonIsPerInjector() {
assertNotSame(
Guice.createInjector().getInstance(AnnotatedSingleton.class),
Guice.createInjector().getInstance(AnnotatedSingleton.class));
}
public void testOverriddingAnnotation() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(AnnotatedSingleton.class).in(Scopes.NO_SCOPE);
}
});
assertNotSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
}
public void testSingletonsInProductionStage() {
Guice.createInjector(Stage.PRODUCTION, singletonsModule);
assertEquals(1, AnnotatedSingleton.nextInstanceId);
assertEquals(1, BoundAsSingleton.nextInstanceId);
assertEquals(1, EagerSingleton.nextInstanceId);
assertEquals(1, RealLinkedSingleton.nextInstanceId);
assertEquals(1, JustInTimeSingleton.nextInstanceId);
assertEquals(0, NotASingleton.nextInstanceId);
}
public void testSingletonsInDevelopmentStage() {
Guice.createInjector(Stage.DEVELOPMENT, singletonsModule);
assertEquals(0, AnnotatedSingleton.nextInstanceId);
assertEquals(0, BoundAsSingleton.nextInstanceId);
assertEquals(1, EagerSingleton.nextInstanceId);
assertEquals(0, RealLinkedSingleton.nextInstanceId);
assertEquals(0, JustInTimeSingleton.nextInstanceId);
assertEquals(0, NotASingleton.nextInstanceId);
}
public void testSingletonScopeIsNotSerializable() throws IOException {
Asserts.assertNotSerializable(Scopes.SINGLETON);
}
public void testNoScopeIsNotSerializable() throws IOException {
Asserts.assertNotSerializable(Scopes.NO_SCOPE);
}
@Singleton
static class AnnotatedSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class BoundAsSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class EagerSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
interface LinkedSingleton {}
@Singleton
static class RealLinkedSingleton implements LinkedSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class DependsOnJustInTimeSingleton {
@Inject JustInTimeSingleton justInTimeSingleton;
}
@Singleton
static class JustInTimeSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class NotASingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
}