PageRenderTime 132ms CodeModel.GetById 8ms RepoModel.GetById 2ms app.codeStats 0ms

/jdk8-tests/test/com/google/inject/jdk8/Java8LanguageFeatureBindingTest.java

https://gitlab.com/metamorphiccode/guice
Java | 168 lines | 120 code | 26 blank | 22 comment | 3 complexity | 8519860d6378fe5d3f1a02958eeffb14 MD5 | raw file
  1. /**
  2. * Copyright (C) 2014 Google Inc.
  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.google.inject.jdk8;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.CreationException;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Inject;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Key;
  23. import com.google.inject.Provider;
  24. import com.google.inject.Provides;
  25. import com.google.inject.ProvisionException;
  26. import com.google.inject.TypeLiteral;
  27. import junit.framework.TestCase;
  28. import java.util.Collections;
  29. import java.util.UUID;
  30. import java.util.concurrent.Callable;
  31. import java.util.concurrent.atomic.AtomicInteger;
  32. import java.util.function.Predicate;
  33. /**
  34. * Test bindings to lambdas, method references, etc.
  35. *
  36. * @author cgdecker@google.com (Colin Decker)
  37. */
  38. public class Java8LanguageFeatureBindingTest extends TestCase {
  39. // Some of these tests are kind of weird.
  40. // See https://github.com/google/guice/issues/757 for more on why they exist.
  41. public void testBinding_lambdaToInterface() {
  42. Injector injector = Guice.createInjector(new AbstractModule() {
  43. @Override
  44. protected void configure() {
  45. bind(new TypeLiteral<Predicate<Object>>() {}).toInstance(o -> o != null);
  46. }
  47. });
  48. Predicate<Object> predicate = injector.getInstance(new Key<Predicate<Object>>() {});
  49. assertTrue(predicate.test(new Object()));
  50. assertFalse(predicate.test(null));
  51. }
  52. public void testProviderMethod_returningLambda() throws Exception {
  53. Injector injector = Guice.createInjector(new AbstractModule() {
  54. @Override
  55. protected void configure() {}
  56. @Provides
  57. public Callable<String> provideCallable() {
  58. return () -> "foo";
  59. }
  60. });
  61. Callable<String> callable = injector.getInstance(new Key<Callable<String>>() {});
  62. assertEquals("foo", callable.call());
  63. }
  64. public void testProviderMethod_containingLambda_throwingException() throws Exception {
  65. Injector injector = Guice.createInjector(new AbstractModule() {
  66. @Override
  67. protected void configure() {}
  68. @Provides
  69. public Callable<String> provideCallable() {
  70. if (Boolean.parseBoolean("false")) { // avoid dead code warnings
  71. return () -> "foo";
  72. } else {
  73. throw new RuntimeException("foo");
  74. }
  75. }
  76. });
  77. try {
  78. injector.getInstance(new Key<Callable<String>>() {});
  79. } catch (ProvisionException expected) {
  80. assertTrue(expected.getCause() instanceof RuntimeException);
  81. assertEquals("foo", expected.getCause().getMessage());
  82. }
  83. }
  84. public void testProvider_usingJdk8Features() {
  85. try {
  86. Guice.createInjector(new AbstractModule() {
  87. @Override
  88. protected void configure() {
  89. bind(String.class).toProvider(StringProvider.class);
  90. }
  91. });
  92. fail();
  93. } catch (CreationException expected) {
  94. }
  95. UUID uuid = UUID.randomUUID();
  96. Injector injector = Guice.createInjector(new AbstractModule() {
  97. @Override
  98. protected void configure() {
  99. bind(UUID.class).toInstance(uuid);
  100. bind(String.class).toProvider(StringProvider.class);
  101. }
  102. });
  103. assertEquals(uuid.toString(), injector.getInstance(String.class));
  104. }
  105. private static final class StringProvider implements Provider<String> {
  106. private final UUID uuid;
  107. @Inject
  108. StringProvider(UUID uuid) {
  109. this.uuid = uuid;
  110. }
  111. @Override
  112. public String get() {
  113. return Collections.singleton(uuid).stream()
  114. .map(UUID::toString)
  115. .findFirst().get();
  116. }
  117. }
  118. public void testBinding_toProvider_lambda() {
  119. Injector injector = Guice.createInjector(new AbstractModule() {
  120. @Override
  121. protected void configure() {
  122. AtomicInteger i = new AtomicInteger();
  123. bind(String.class).toProvider(() -> "Hello" + i.incrementAndGet());
  124. }
  125. });
  126. assertEquals("Hello1", injector.getInstance(String.class));
  127. assertEquals("Hello2", injector.getInstance(String.class));
  128. }
  129. public void testBinding_toProvider_methodReference() {
  130. Injector injector = Guice.createInjector(new AbstractModule() {
  131. @Override
  132. protected void configure() {
  133. bind(String.class).toProvider(Java8LanguageFeatureBindingTest.this::provideString);
  134. }
  135. });
  136. Provider<String> provider = injector.getProvider(String.class);
  137. assertEquals("Hello", provider.get());
  138. }
  139. private String provideString() {
  140. return "Hello";
  141. }
  142. }