01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import java.lang.annotation.Retention;
18: import static java.lang.annotation.RetentionPolicy.RUNTIME;
19: import junit.framework.TestCase;
20:
21: /**
22: * @author crazybob@google.com (Bob Lee)
23: */
24: public class StaticInjectionTest extends TestCase {
25:
26: @Retention(RUNTIME)
27: @BindingAnnotation
28: @interface I {
29: }
30:
31: @Retention(RUNTIME)
32: @BindingAnnotation
33: @interface S {
34: }
35:
36: public void testInjectStatics() throws CreationException {
37: BinderImpl builder = new BinderImpl();
38: builder.bindConstant().annotatedWith(S.class).to("test");
39: builder.bindConstant().annotatedWith(I.class).to(5);
40: builder
41: .requestStaticInjection(StaticInjectionTest.Static.class);
42:
43: Injector c = builder.createInjector();
44:
45: assertEquals("test", StaticInjectionTest.Static.s);
46: assertEquals(5, StaticInjectionTest.Static.i);
47: }
48:
49: static class Static {
50:
51: @Inject
52: @I
53: static int i;
54:
55: static String s;
56:
57: @Inject
58: static void setS(@S
59: String s) {
60: StaticInjectionTest.Static.s = s;
61: }
62: }
63: }
|