Rename packages etc

This commit is contained in:
Dave Syer
2014-04-14 10:38:16 -07:00
parent 46c3d2bccd
commit 8460ea0425
12 changed files with 28 additions and 29 deletions

View File

@@ -0,0 +1,82 @@
package org.springframework.guice;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Injector;
public abstract class AbstractCompleteWiringTests {
private Injector injector;
@Before
public void init() {
injector = createInjector();
}
protected abstract Injector createInjector();
@Test
public void injectInstance() {
Bar bar = new Bar();
injector.injectMembers(bar);
assertNotNull(bar.service);
}
@Test
public void memberInjector() {
Bar bar = new Bar();
injector.getMembersInjector(Bar.class).injectMembers(bar);
assertNotNull(bar.service);
}
@Test
public void getInstanceUnbound() {
assertNotNull(injector.getInstance(Foo.class));
}
@Test
public void getInstanceBound() {
assertNotNull(injector.getInstance(Service.class));
}
@Test
public void getProviderUnbound() {
assertNotNull(injector.getProvider(Foo.class).get());
}
@Test
public void getProviderBound() {
assertNotNull(injector.getProvider(Service.class).get());
}
interface Service {
}
protected static class MyService implements Service {
}
public static class Foo {
@Inject
public Foo(Service service) {
}
}
public static class Bar {
private Service service;
@Inject
public void setService(Service service) {
this.service = service;
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.guice;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class GuiceWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
Injector app = Guice.createInjector(new TestConfig());
return app;
}
public static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class, ModuleRegistryConfiguration.class);
context.refresh();
return new SpringInjector(context);
}
public static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.guice;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.inject.Binding;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
@Configuration
public class ModuleRegistryConfiguration {
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Autowired(required=false)
private List<Module> modules = Collections.emptyList();
private Injector injector;
@Bean
public Injector injector() {
return injector;
}
@PostConstruct
public void init() {
injector = Guice.createInjector(modules);
for (Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet()) {
if (entry.getKey().getTypeLiteral().getRawType().equals(Injector.class)) {
continue;
}
final Provider<?> provider = entry.getValue().getProvider();
beanFactory.registerResolvableDependency(entry.getKey().getTypeLiteral().getRawType(), new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
return provider.get();
}
});
}
}
}

View File

@@ -0,0 +1,59 @@
package org.springframework.guice;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import org.junit.Test;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class SimpleWiringTests {
@Test
public void guiceyFoo() {
Injector app = Guice.createInjector(new TestConfig());
assertNotNull(app.getInstance(Foo.class));
}
@Test
public void springyFoo() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, MyService.class);
context.getDefaultListableBeanFactory().registerBeanDefinition(Foo.class.getSimpleName(), new RootBeanDefinition(Foo.class));
assertNotNull(context.getBean(Foo.class));
}
@Test
public void hybridFoo() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, ModuleRegistryConfiguration.class);
Injector app = new SpringInjector(context);
assertNotNull(app.getInstance(Foo.class));
}
protected static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
interface Service {
}
protected static class MyService implements Service {
}
protected static class Foo {
@Inject
public Foo(Service service) {
}
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class SpringModuleWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class);
context.refresh();
return Guice.createInjector(new SpringModule(context));
}
@Configuration
public static class TestConfig {
@Bean
public Service service() {
return new MyService();
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class SpringWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class);
context.refresh();
return new SpringInjector(context);
}
@Configuration
public static class TestConfig {
@Bean
public Service service() {
return new MyService();
}
}
}