/mycila-testing/tags/2.0-rc1/mycila-testing-plugins/mycila-testing-atunit/src/main/java/com/mycila/testing/plugin/atunit/container/GuiceContainer.java

http://mycila.googlecode.com/ · Java · 77 lines · 50 code · 10 blank · 17 comment · 6 complexity · 511edbb4ae23b4d4f035beecdd4a4349 MD5 · raw file

  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.atunit.container;
  17. import atunit.core.Container;
  18. import atunit.lib.com.google.common.collect.Iterables;
  19. import atunit.lib.com.google.common.collect.Multimap;
  20. import atunit.lib.com.google.common.collect.Multimaps;
  21. import com.google.inject.AbstractModule;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Injector;
  24. import com.google.inject.Module;
  25. import com.google.inject.TypeLiteral;
  26. import com.mycila.testing.core.Mycila;
  27. import java.lang.reflect.Field;
  28. import java.lang.reflect.Type;
  29. import java.util.Collection;
  30. import java.util.Map;
  31. public class GuiceContainer implements Container {
  32. public Object createTest(Class<?> testClass, Map<Field, Object> fieldValues) throws Exception {
  33. FieldModule fields = new FieldModule(fieldValues);
  34. Injector injector;
  35. if (Module.class.isAssignableFrom(testClass)) {
  36. injector = Guice.createInjector(fields, (Module) testClass.newInstance());
  37. } else {
  38. injector = Guice.createInjector(fields);
  39. }
  40. injector.injectMembers(Mycila.currentExecution().context().introspector().instance());
  41. return null;
  42. }
  43. protected class FieldModule extends AbstractModule {
  44. final Map<Field, Object> fields;
  45. public FieldModule(Map<Field, Object> fields) {
  46. this.fields = fields;
  47. }
  48. @Override
  49. @SuppressWarnings("unchecked")
  50. protected void configure() {
  51. // map field values by type
  52. Multimap<Type, Field> fieldsByType = Multimaps.newHashMultimap();
  53. for (Field field : fields.keySet()) {
  54. fieldsByType.put(field.getGenericType(), field);
  55. }
  56. // for any types that don't have duplicates, bind instances.
  57. for (Type type : fieldsByType.keySet()) {
  58. Collection<Field> fields = fieldsByType.get(type);
  59. if (fields.size() == 1) {
  60. Field field = Iterables.getOnlyElement(fields);
  61. TypeLiteral literal = TypeLiteral.get(type);
  62. bind(literal).toInstance(this.fields.get(field));
  63. }
  64. }
  65. }
  66. }
  67. }