blob: 2f7bf520767f47ec2a0b0797041d456225ae9550 [file] [log] [blame]
crazyboblee07e41822006-11-21 01:27:08 +00001/**
2 * Copyright (C) 2006 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
17package com.google.inject;
18
19import junit.framework.TestCase;
20
21import java.util.Map;
22import java.util.HashMap;
23
24/**
25 * @author crazybob@google.com (Bob Lee)
26 */
27public class ConstantConversionTest extends TestCase {
28
29 public static class Foo {
30 @Inject("#") Integer integerField;
31 @Inject("#") int primitiveIntField;
32 @Inject("#") Long longField;
33 @Inject("#") long primitiveLongField;
34 @Inject("boolean") Boolean booleanField;
35 @Inject("boolean") boolean primitiveBooleanField;
36 @Inject("#") Byte byteField;
37 @Inject("#") byte primitiveByteField;
38 @Inject("#") Short shortField;
39 @Inject("#") short primitiveShortField;
40 @Inject("#") Float floatField;
41 @Inject("#") float primitiveFloatField;
42 @Inject("#") Double doubleField;
43 @Inject("#") short primitiveDoubleField;
44 @Inject("enum") Bar enumField;
45 @Inject("class") Class<?> classField;
46 }
47
48 public enum Bar {
49 TEE, BAZ, BOB;
50 }
51
crazybobleea6e73982007-02-02 00:21:07 +000052 public void testOneConstantInjection() throws ContainerCreationException {
53 ContainerBuilder builder = new ContainerBuilder();
54 builder.bind("#").to("5");
55 Container container = builder.create(false);
56 Simple simple = container.getCreator(Simple.class).get();
57 assertEquals(5, simple.i);
58 }
59
60 static class Simple {
61 @Inject("#") int i;
62 }
63
crazyboblee9bb62022007-02-01 00:06:53 +000064 public void testConstantInjection() throws ContainerCreationException {
crazyboblee07e41822006-11-21 01:27:08 +000065 Map<String, String> properties = new HashMap<String, String>() {{
66 put("#", "5");
67 put("boolean", "true");
68 put("enum", "TEE");
69 put("class", Foo.class.getName());
70 }};
71
72 ContainerBuilder b = new ContainerBuilder();
crazyboblee7c5b2c42007-01-20 02:05:20 +000073 b.bindProperties(properties);
crazyboblee07e41822006-11-21 01:27:08 +000074 Container c = b.create(false);
crazybobleef1ba2b52007-01-29 21:19:53 +000075 Foo foo = c.getCreator(Foo.class).get();
crazyboblee07e41822006-11-21 01:27:08 +000076
77 checkNumbers(
78 foo.integerField,
79 foo.primitiveIntField,
80 foo.longField,
81 foo.primitiveLongField,
82 foo.byteField,
83 foo.primitiveByteField,
84 foo.shortField,
85 foo.primitiveShortField,
86 foo.floatField,
87 foo.primitiveFloatField,
88 foo.doubleField,
89 foo.primitiveDoubleField
90 );
91
92 assertEquals(Bar.TEE, foo.enumField);
93 assertEquals(Foo.class, foo.classField);
94 }
95
96 void checkNumbers(Number... ns) {
97 for (Number n : ns) {
98 assertEquals(5, n.intValue());
99 }
100 }
101
crazyboblee9bb62022007-02-01 00:06:53 +0000102 public void testInvalidInteger() throws ContainerCreationException {
crazyboblee07e41822006-11-21 01:27:08 +0000103 ContainerBuilder b = new ContainerBuilder();
crazyboblee7c5b2c42007-01-20 02:05:20 +0000104 b.bind("#").to("invalid");
crazyboblee07e41822006-11-21 01:27:08 +0000105 Container c = b.create(false);
106 try {
crazybobleef1ba2b52007-01-29 21:19:53 +0000107 c.getCreator(InvalidInteger.class).get();
crazyboblee07e41822006-11-21 01:27:08 +0000108 fail();
crazyboblee4727ee22007-01-30 03:13:38 +0000109 } catch (ConfigurationException e) {
110 assertTrue(e.getMessage().contains(
crazyboblee07e41822006-11-21 01:27:08 +0000111 "Error converting 'invalid' to Integer while injecting integerField "
112 + "with dependency named '#' in InvalidInteger. Reason:"));
113 }
114 }
115
116 public static class InvalidInteger {
117 @Inject("#") Integer integerField;
118 }
119
crazyboblee9bb62022007-02-01 00:06:53 +0000120 public void testInvalidCharacter() throws ContainerCreationException {
crazyboblee07e41822006-11-21 01:27:08 +0000121 ContainerBuilder b = new ContainerBuilder();
crazyboblee7c5b2c42007-01-20 02:05:20 +0000122 b.bind("foo").to("invalid");
crazyboblee07e41822006-11-21 01:27:08 +0000123 Container c = b.create(false);
124 try {
crazybobleef1ba2b52007-01-29 21:19:53 +0000125 c.getCreator(InvalidCharacter.class).get();
crazyboblee07e41822006-11-21 01:27:08 +0000126 fail();
crazyboblee4727ee22007-01-30 03:13:38 +0000127 } catch (ConfigurationException e) {
crazyboblee4727ee22007-01-30 03:13:38 +0000128 assertTrue(e.getMessage().contains(
crazyboblee07e41822006-11-21 01:27:08 +0000129 "Error converting 'invalid' to char while injecting foo "
130 + "with dependency named 'foo' in InvalidCharacter. Reason:"));
131 }
132 }
133
134 public static class InvalidCharacter {
135 @Inject("foo") char foo;
136 }
137
crazyboblee9bb62022007-02-01 00:06:53 +0000138 public void testInvalidEnum() throws ContainerCreationException {
crazyboblee07e41822006-11-21 01:27:08 +0000139 ContainerBuilder b = new ContainerBuilder();
crazyboblee7c5b2c42007-01-20 02:05:20 +0000140 b.bind("foo").to("invalid");
crazyboblee07e41822006-11-21 01:27:08 +0000141 Container c = b.create(false);
142 try {
crazybobleef1ba2b52007-01-29 21:19:53 +0000143 c.getCreator(InvalidEnum.class).get();
crazyboblee07e41822006-11-21 01:27:08 +0000144 fail();
crazyboblee4727ee22007-01-30 03:13:38 +0000145 } catch (ConfigurationException e) {
crazyboblee07e41822006-11-21 01:27:08 +0000146 assertTrue(e.getMessage().startsWith(
147 "Error converting 'invalid' to Bar while injecting foo "
148 + "with dependency named 'foo' in InvalidEnum. Reason:"));
149 }
150 }
151
152 public static class InvalidEnum {
153 @Inject("foo") Bar foo;
154 }
155}