crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
| 19 | import junit.framework.TestCase; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.HashMap; |
| 23 | |
| 24 | /** |
| 25 | * @author crazybob@google.com (Bob Lee) |
| 26 | */ |
| 27 | public 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 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 52 | 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 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 64 | public void testConstantInjection() throws ContainerCreationException { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 65 | 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(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 73 | b.bindProperties(properties); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 74 | Container c = b.create(false); |
crazyboblee | f1ba2b5 | 2007-01-29 21:19:53 +0000 | [diff] [blame] | 75 | Foo foo = c.getCreator(Foo.class).get(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 76 | |
| 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 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 102 | public void testInvalidInteger() throws ContainerCreationException { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 103 | ContainerBuilder b = new ContainerBuilder(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 104 | b.bind("#").to("invalid"); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 105 | Container c = b.create(false); |
| 106 | try { |
crazyboblee | f1ba2b5 | 2007-01-29 21:19:53 +0000 | [diff] [blame] | 107 | c.getCreator(InvalidInteger.class).get(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 108 | fail(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 109 | } catch (ConfigurationException e) { |
| 110 | assertTrue(e.getMessage().contains( |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 111 | "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 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 120 | public void testInvalidCharacter() throws ContainerCreationException { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 121 | ContainerBuilder b = new ContainerBuilder(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 122 | b.bind("foo").to("invalid"); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 123 | Container c = b.create(false); |
| 124 | try { |
crazyboblee | f1ba2b5 | 2007-01-29 21:19:53 +0000 | [diff] [blame] | 125 | c.getCreator(InvalidCharacter.class).get(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 126 | fail(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 127 | } catch (ConfigurationException e) { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 128 | assertTrue(e.getMessage().contains( |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 129 | "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 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 138 | public void testInvalidEnum() throws ContainerCreationException { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 139 | ContainerBuilder b = new ContainerBuilder(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 140 | b.bind("foo").to("invalid"); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 141 | Container c = b.create(false); |
| 142 | try { |
crazyboblee | f1ba2b5 | 2007-01-29 21:19:53 +0000 | [diff] [blame] | 143 | c.getCreator(InvalidEnum.class).get(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 144 | fail(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 145 | } catch (ConfigurationException e) { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 146 | 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 | } |