1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.guice.spyke;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  
21  import org.junit.Test;
22  
23  import com.google.inject.AbstractModule;
24  import com.google.inject.Binding;
25  import com.google.inject.Guice;
26  import com.google.inject.Injector;
27  import com.google.inject.Key;
28  
29  public class GuiceTest {
30  
31  	private Injector injector = Guice.createInjector(new GuiceTestModule());
32  
33  	@Test
34  	public void getInstance() {
35  		try {
36  			injector.getInstance(Foo.class);
37  		} catch (RuntimeException e) {
38  			if (e.getClass().getName().equals("com.google.inject.ConfigurationException")) {
39  				System.out.println(e);
40  			} else {
41  				throw e;
42  			}
43  		}
44  	}
45  
46  	@Test
47  	public void getBinding1() {
48  		Key<Foo> key = Key.get(Foo.class);
49  		Binding<Foo> binding = injector.getBinding(key);
50  		assertNull(binding);
51  	}
52  
53  	@Test
54  	public void getBinding2() {
55  		Key<Bar> key = Key.get(Bar.class);
56  		Binding<Bar> binding = injector.getBinding(key);
57  		assertNotNull(binding);
58  	}
59  
60  	@Test
61  	public void getBinding3() {
62  		Key<Baz> key = Key.get(Baz.class);
63  		Binding<Baz> binding = injector.getBinding(key);
64  		assertNull(binding);
65  	}
66  
67  	private static class GuiceTestModule extends AbstractModule {
68  
69  		@Override
70  		protected void configure() {
71  			bind(Bar.class).to(BarImpl.class);
72  		}
73  
74  	}
75  
76  	private static interface Foo {
77  	}
78  
79  	private static interface Bar {
80  	}
81  
82  	private static class BarImpl implements Bar {
83  	}
84  
85  	private static class Baz {
86  	}
87  
88  }