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