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  
20  import org.junit.Test;
21  
22  import com.google.inject.AbstractModule;
23  import com.google.inject.Binding;
24  import com.google.inject.ConfigurationException;
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(
39  					"com.google.inject.ConfigurationException")) {
40  				System.out.println(e);
41  			} else {
42  				throw e;
43  			}
44  		}
45  	}
46  
47  	@Test(expected = ConfigurationException.class)
48  	public void getBinding1() {
49  		Key<Foo> key = Key.get(Foo.class);
50  		injector.getBinding(key);
51  		// assertNull(binding);
52  	}
53  
54  	@Test
55  	public void getBinding2() {
56  		Key<Bar> key = Key.get(Bar.class);
57  		Binding<Bar> binding = injector.getBinding(key);
58  		assertNotNull(binding);
59  	}
60  
61  	@Test
62  	public void getBinding3() {
63  		Key<Baz> key = Key.get(Baz.class);
64  		Binding<Baz> binding = injector.getBinding(key);
65  		// assertNull(binding);
66  		assertNotNull(binding);
67  	}
68  
69  	private static class GuiceTestModule extends AbstractModule {
70  
71  		@Override
72  		protected void configure() {
73  			bind(Bar.class).to(BarImpl.class);
74  		}
75  
76  	}
77  
78  	private static interface Foo {
79  	}
80  
81  	private static interface Bar {
82  	}
83  
84  	private static class BarImpl implements Bar {
85  	}
86  
87  	private static class Baz {
88  	}
89  
90  }