PageRenderTime 629ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mycila-testing/tags/mycila-testing-1.0-rc1/mycila-testing-plugins/mycila-testing-guice/src/main/java/com/mycila/testing/plugin/guice/Guice1TestPlugin.java

http://mycila.googlecode.com/
Java | 128 lines | 96 code | 13 blank | 19 comment | 12 complexity | e6c7237f880c83832a53c3a9c63b4c0b MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (C) 2008 Mathieu Carbou <mathieu.carbou@gmail.com>
  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, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mycila.testing.plugin.guice;
  17. import com.google.inject.*;
  18. import com.google.inject.binder.AnnotatedBindingBuilder;
  19. import com.google.inject.binder.LinkedBindingBuilder;
  20. import com.google.inject.binder.ScopedBindingBuilder;
  21. import com.mycila.testing.core.AbstractTestPlugin;
  22. import com.mycila.testing.core.Context;
  23. import com.mycila.testing.core.TestPluginException;
  24. import java.lang.reflect.Field;
  25. import java.lang.reflect.Method;
  26. import java.lang.reflect.Type;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. /**
  31. * @author Mathieu Carbou (mathieu.carbou@gmail.com)
  32. */
  33. public final class Guice1TestPlugin extends AbstractTestPlugin {
  34. public void prepareTestInstance(Context context) {
  35. GuiceContext ctx = context.getTest().getTargetClass().getAnnotation(GuiceContext.class);
  36. List<Module> modules = new ArrayList<Module>();
  37. modules.addAll(contextualModules(ctx));
  38. modules.addAll(providedModules(context));
  39. modules.add(bindings(context));
  40. modules.add(providedBindings(context));
  41. if (context.getTest().getTarget() instanceof Module) {
  42. modules.add((Module) context.getTest().getTarget());
  43. }
  44. Injector injector = Guice.createInjector(findStage(ctx), modules);
  45. context.setAttribute("com.google.inject.Injector", injector);
  46. injector.injectMembers(context.getTest().getTarget());
  47. }
  48. private Module bindings(final Context context) {
  49. return new Module() {
  50. public void configure(Binder binder) {
  51. for (final Field field : context.getTest().getFieldsAnnotatedWith(Bind.class)) {
  52. Guice1TestPlugin.this.configure(binder, field.getGenericType(), field.getAnnotation(Bind.class), new Provider<Object>() {
  53. public Object get() {
  54. return context.getTest().get(field);
  55. }
  56. });
  57. }
  58. }
  59. };
  60. }
  61. private Module providedBindings(final Context context) {
  62. return new Module() {
  63. public void configure(Binder binder) {
  64. for (final Method method : context.getTest().getMethodsAnnotatedWith(Bind.class)) {
  65. Guice1TestPlugin.this.configure(binder, method.getGenericReturnType(), method.getAnnotation(Bind.class), new Provider<Object>() {
  66. public Object get() {
  67. return context.getTest().invoke(method);
  68. }
  69. });
  70. }
  71. }
  72. };
  73. }
  74. @SuppressWarnings({"unchecked"})
  75. private <T> void configure(Binder binder, Type type, Bind annotation, Provider<T> provider) {
  76. AnnotatedBindingBuilder<T> builder1 = (AnnotatedBindingBuilder<T>) binder.bind(TypeLiteral.get(type));
  77. LinkedBindingBuilder<T> builder2 = annotation.annotatedBy().equals(NoAnnotation.class) ? builder1 : builder1.annotatedWith(annotation.annotatedBy());
  78. ScopedBindingBuilder builder3 = builder2.toProvider(provider);
  79. if (!annotation.scope().equals(NoAnnotation.class)) {
  80. builder3.in(annotation.scope());
  81. }
  82. }
  83. private List<Module> providedModules(Context ctx) {
  84. List<Module> modules = new ArrayList<Module>();
  85. for (Method method : ctx.getTest().getMethodsOfTypeAnnotatedWith(Module.class, ModuleProvider.class)) {
  86. modules.add((Module) ctx.getTest().invoke(method));
  87. }
  88. for (Method method : ctx.getTest().getMethodsOfTypeAnnotatedWith(Module[].class, ModuleProvider.class)) {
  89. modules.addAll(Arrays.asList((Module[]) ctx.getTest().invoke(method)));
  90. }
  91. for (Method method : ctx.getTest().getMethodsOfTypeAnnotatedWith(Iterable.class, ModuleProvider.class)) {
  92. //noinspection unchecked
  93. for (Module module : (Iterable<Module>) ctx.getTest().invoke(method)) {
  94. modules.add(module);
  95. }
  96. }
  97. return modules;
  98. }
  99. private Stage findStage(GuiceContext ctx) {
  100. return ctx == null ? Stage.DEVELOPMENT : ctx.stage();
  101. }
  102. private List<Module> contextualModules(GuiceContext context) {
  103. List<Module> modules = new ArrayList<Module>();
  104. if (context != null) {
  105. for (Class<? extends Module> moduleClass : context.value()) {
  106. try {
  107. modules.add(moduleClass.newInstance());
  108. } catch (Exception e) {
  109. throw new TestPluginException(e, "Error instanciating module class '%s': %s", moduleClass.getName(), e.getMessage());
  110. }
  111. }
  112. }
  113. return modules;
  114. }
  115. }