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