PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/com/google/inject/name/NamesTest.java

https://gitlab.com/metamorphiccode/guice
Java | 112 lines | 72 code | 22 blank | 18 comment | 0 complexity | b93adcea9da896b98282bd705c9ee405 MD5 | raw file
  1. /**
  2. * Copyright (C) 2008 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.name;
  17. import static com.google.inject.Asserts.assertEqualWhenReserialized;
  18. import static com.google.inject.Asserts.assertEqualsBothWays;
  19. import com.google.common.collect.ImmutableMap;
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Key;
  24. import junit.framework.TestCase;
  25. import java.io.IOException;
  26. import java.util.Map;
  27. import java.util.Properties;
  28. /**
  29. * @author jessewilson@google.com (Jesse Wilson)
  30. */
  31. public class NamesTest extends TestCase {
  32. @Named("foo") private String foo;
  33. private Named namedFoo;
  34. protected void setUp() throws Exception {
  35. super.setUp();
  36. namedFoo = getClass().getDeclaredField("foo").getAnnotation(Named.class);
  37. }
  38. public void testConsistentEqualsAndHashcode() {
  39. Named actual = Names.named("foo");
  40. assertEqualsBothWays(namedFoo, actual);
  41. assertEquals(namedFoo.toString(), actual.toString());
  42. }
  43. public void testNamedIsSerializable() throws IOException {
  44. assertEqualWhenReserialized(Names.named("foo"));
  45. }
  46. public void testBindPropertiesUsingProperties() {
  47. final Properties teams = new Properties();
  48. teams.setProperty("SanJose", "Sharks");
  49. teams.setProperty("Edmonton", "Oilers");
  50. Injector injector = Guice.createInjector(new AbstractModule() {
  51. protected void configure() {
  52. Names.bindProperties(binder(), teams);
  53. }
  54. });
  55. assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
  56. assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
  57. }
  58. public void testBindPropertiesUsingMap() {
  59. final Map<String, String> properties = ImmutableMap.of(
  60. "SanJose", "Sharks", "Edmonton", "Oilers");
  61. Injector injector = Guice.createInjector(new AbstractModule() {
  62. protected void configure() {
  63. Names.bindProperties(binder(), properties);
  64. }
  65. });
  66. assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
  67. assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
  68. }
  69. public void testBindPropertiesIncludesInheritedProperties() {
  70. Properties defaults = new Properties();
  71. defaults.setProperty("Edmonton", "Eskimos");
  72. defaults.setProperty("Regina", "Pats");
  73. final Properties teams = new Properties(defaults);
  74. teams.setProperty("SanJose", "Sharks");
  75. teams.setProperty("Edmonton", "Oilers");
  76. Injector injector = Guice.createInjector(new AbstractModule() {
  77. protected void configure() {
  78. Names.bindProperties(binder(), teams);
  79. }
  80. });
  81. assertEquals("Pats", injector.getInstance(Key.get(String.class, Names.named("Regina"))));
  82. assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
  83. assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
  84. try {
  85. injector.getInstance(Key.get(String.class, Names.named("Calgary")));
  86. fail();
  87. } catch (RuntimeException expected) {
  88. }
  89. }
  90. }