KickJava   Java API By Example, From Geeks To Geeks.

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


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 NotRequiredTest extends TestCase {
25
26   public void testProvided() throws CreationException {
27     BinderImpl builder = new BinderImpl();
28     builder.bind(Bar.class).to(BarImpl.class);
29     Injector c = builder.createInjector();
30     Foo foo = c.getInstance(Foo.class);
31     assertNotNull(foo.bar);
32     assertNotNull(foo.fromMethod);
33   }
34
35   public void testNotProvided() throws CreationException {
36     Injector c = Guice.createInjector(new AbstractModule() {
37       protected void configure() {
38         bind(Foo.class);
39       }
40     });
41     Foo foo = c.getInstance(Foo.class);
42     assertNull(foo.bar);
43     assertNull(foo.fromMethod);
44   }
45
46   static class Foo {
47     @Inject(optional=true) Bar bar;
48
49     Bar fromMethod;
50
51     @Inject(optional=true) void setBar(Bar bar) {
52       fromMethod = bar;
53     }
54   }
55
56   interface Bar {}
57
58   static class BarImpl implements Bar {}
59 }
60
Popular Tags