/core/src/test/java/org/jclouds/internal/FilterStringsBoundToInjectorByNameTest.java

https://github.com/andreisavu/jclouds · Java · 104 lines · 59 code · 23 blank · 22 comment · 0 complexity · 64ec6300b589a2df87d0c1addb78afa2 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.internal;
  20. import static org.testng.Assert.assertEquals;
  21. import javax.inject.Named;
  22. import org.testng.annotations.Test;
  23. import com.google.common.base.Predicates;
  24. import com.google.common.collect.ImmutableMap;
  25. import com.google.inject.AbstractModule;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Provides;
  28. import com.google.inject.name.Names;
  29. /**
  30. *
  31. * @author Adrian Cole
  32. */
  33. @Test(testName = "FilterStringsBoundToInjectorByNameTest")
  34. public class FilterStringsBoundToInjectorByNameTest {
  35. public void testEmptyWhenNoStringsBound() {
  36. FilterStringsBoundToInjectorByName fn = Guice.createInjector().getInstance(FilterStringsBoundToInjectorByName.class);
  37. assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of());
  38. }
  39. public void testEmptyWhenNotStringsBound() {
  40. FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
  41. @Override
  42. protected void configure() {
  43. bindConstant().annotatedWith(Names.named("foo")).to(1l);
  44. }
  45. }).getInstance(FilterStringsBoundToInjectorByName.class);
  46. assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of());
  47. }
  48. public void testReturnsGuiceNamedString() {
  49. FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
  50. @Override
  51. protected void configure() {
  52. bindConstant().annotatedWith(Names.named("foo")).to("bar");
  53. }
  54. }).getInstance(FilterStringsBoundToInjectorByName.class);
  55. assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of("foo", "bar"));
  56. }
  57. public void testReturnsJavaNamedString() {
  58. FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
  59. @SuppressWarnings("unused")
  60. @Named("foo")
  61. @Provides
  62. String provideFoo() {
  63. return "bar";
  64. }
  65. @Override
  66. protected void configure() {
  67. }
  68. }).getInstance(FilterStringsBoundToInjectorByName.class);
  69. assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of("foo", "bar"));
  70. }
  71. public void testFilterWorks() {
  72. FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
  73. @Override
  74. protected void configure() {
  75. bindConstant().annotatedWith(Names.named("foo")).to("bar");
  76. bindConstant().annotatedWith(Names.named("bing")).to("bong");
  77. }
  78. }).getInstance(FilterStringsBoundToInjectorByName.class);
  79. assertEquals(fn.apply(Predicates.<String> equalTo("bing")), ImmutableMap.<String, String> of("bing", "bong"));
  80. }
  81. }