KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > PreloadingTest


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.inject;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author crazybob@google.com (Bob Lee)
23  */

24 public class PreloadingTest extends TestCase {
25
26   protected void tearDown() throws Exception JavaDoc {
27     Foo.count = 0;
28     Bar.count = 0;
29   }
30
31   public void testPreloadSome() throws CreationException {
32     BinderImpl builder = createBinder(Stage.DEVELOPMENT);
33     builder.createInjector();
34     assertEquals(1, Foo.count);
35     assertEquals(0, Bar.count);
36   }
37
38   public void testPreloadAll() throws CreationException {
39     BinderImpl builder = createBinder(Stage.PRODUCTION);
40     builder.createInjector();
41     assertEquals(1, Foo.count);
42     assertEquals(1, Bar.count);
43   }
44
45   private BinderImpl createBinder(Stage stage) {
46     BinderImpl builder = new BinderImpl(stage);
47     builder.bind(Foo.class).asEagerSingleton();
48     builder.bind(Bar.class);
49     return builder;
50   }
51
52   static class Foo {
53     static int count = 0;
54     public Foo() {
55       count++;
56     }
57   }
58
59   @Singleton
60   static class Bar {
61     static int count = 0;
62     public Bar() {
63       count++;
64     }
65   }
66 }
67
Popular Tags