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 final Injector injector = Guice
32  			.createInjector(new GuiceTestModule());
33  
34  	@Test
35  	public void getInstance() {
36  		try {
37  			injector.getInstance(Foo.class);
38  		} catch (final RuntimeException e) {
39  			if (e.getClass().getName().equals(
40  					"com.google.inject.ConfigurationException")) {
41  				System.out.println(e);
42  			} else {
43  				throw e;
44  			}
45  		}
46  	}
47  
48  	@Test(expected = ConfigurationException.class)
49  	public void getBinding1() {
50  		final Key<Foo> key = Key.get(Foo.class);
51  		injector.getBinding(key);
52  		// assertNull(binding);
53  	}
54  
55  	@Test
56  	public void getBinding2() {
57  		final Key<Bar> key = Key.get(Bar.class);
58  		final Binding<Bar> binding = injector.getBinding(key);
59  		assertNotNull(binding);
60  	}
61  
62  	@Test
63  	public void getBinding3() {
64  		final Key<Baz> key = Key.get(Baz.class);
65  		final Binding<Baz> binding = injector.getBinding(key);
66  		// assertNull(binding);
67  		assertNotNull(binding);
68  	}
69  
70  	private static class GuiceTestModule extends AbstractModule {
71  
72  		@Override
73  		protected void configure() {
74  			bind(Bar.class).to(BarImpl.class);
75  		}
76  
77  	}
78  
79  	private static interface Foo {
80  	}
81  
82  	private static interface Bar {
83  	}
84  
85  	private static class BarImpl implements Bar {
86  	}
87  
88  	private static class Baz {
89  	}
90  
91  }