001: /**
002: * Copyright (C) 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package com.google.inject;
016:
017: import java.lang.annotation.Retention;
018: import static java.lang.annotation.RetentionPolicy.RUNTIME;
019: import junit.framework.TestCase;
020:
021: /**
022: * @author crazybob@google.com (Bob Lee)
023: */
024: public class ConstantConversionTest extends TestCase {
025:
026: @Retention(RUNTIME)
027: @BindingAnnotation
028: @interface NumericValue {
029: }
030:
031: @Retention(RUNTIME)
032: @BindingAnnotation
033: @interface BooleanValue {
034: }
035:
036: @Retention(RUNTIME)
037: @BindingAnnotation
038: @interface EnumValue {
039: }
040:
041: @Retention(RUNTIME)
042: @BindingAnnotation
043: @interface ClassName {
044: }
045:
046: public static class Foo {
047: @Inject
048: @NumericValue
049: Integer integerField;
050: @Inject
051: @NumericValue
052: int primitiveIntField;
053: @Inject
054: @NumericValue
055: Long longField;
056: @Inject
057: @NumericValue
058: long primitiveLongField;
059: @Inject
060: @BooleanValue
061: Boolean booleanField;
062: @Inject
063: @BooleanValue
064: boolean primitiveBooleanField;
065: @Inject
066: @NumericValue
067: Byte byteField;
068: @Inject
069: @NumericValue
070: byte primitiveByteField;
071: @Inject
072: @NumericValue
073: Short shortField;
074: @Inject
075: @NumericValue
076: short primitiveShortField;
077: @Inject
078: @NumericValue
079: Float floatField;
080: @Inject
081: @NumericValue
082: float primitiveFloatField;
083: @Inject
084: @NumericValue
085: Double doubleField;
086: @Inject
087: @NumericValue
088: short primitiveDoubleField;
089: @Inject
090: @EnumValue
091: Bar enumField;
092: @Inject
093: @ClassName
094: Class<?> classField;
095: }
096:
097: public enum Bar {
098: TEE, BAZ, BOB;
099: }
100:
101: public void testOneConstantInjection() throws CreationException {
102: BinderImpl builder = new BinderImpl();
103: builder.bindConstant().annotatedWith(NumericValue.class)
104: .to("5");
105: builder.bind(Simple.class);
106: Injector injector = builder.createInjector();
107: Simple simple = injector.getInstance(Simple.class);
108: assertEquals(5, simple.i);
109: }
110:
111: static class Simple {
112: @Inject
113: @NumericValue
114: int i;
115: }
116:
117: public void testConstantInjection() throws CreationException {
118: BinderImpl b = new BinderImpl();
119: b.bindConstant().annotatedWith(NumericValue.class).to("5");
120: b.bindConstant().annotatedWith(BooleanValue.class).to("true");
121: b.bindConstant().annotatedWith(EnumValue.class).to("TEE");
122: b.bindConstant().annotatedWith(ClassName.class).to(
123: Foo.class.getName());
124: Injector c = b.createInjector();
125: Foo foo = c.getInstance(Foo.class);
126:
127: checkNumbers(foo.integerField, foo.primitiveIntField,
128: foo.longField, foo.primitiveLongField, foo.byteField,
129: foo.primitiveByteField, foo.shortField,
130: foo.primitiveShortField, foo.floatField,
131: foo.primitiveFloatField, foo.doubleField,
132: foo.primitiveDoubleField);
133:
134: assertEquals(Bar.TEE, foo.enumField);
135: assertEquals(Foo.class, foo.classField);
136: }
137:
138: void checkNumbers(Number... ns) {
139: for (Number n : ns) {
140: assertEquals(5, n.intValue());
141: }
142: }
143:
144: public void testInvalidInteger() throws CreationException {
145: BinderImpl b = new BinderImpl();
146: b.bindConstant().annotatedWith(NumericValue.class)
147: .to("invalid");
148: Injector c = b.createInjector();
149: try {
150: c.getInstance(InvalidInteger.class);
151: fail();
152: } catch (ConfigurationException e) { /* expected */
153: }
154: }
155:
156: public static class InvalidInteger {
157: @Inject
158: @NumericValue
159: Integer integerField;
160: }
161:
162: public void testInvalidCharacter() throws CreationException {
163: BinderImpl b = new BinderImpl();
164: b.bindConstant().annotatedWith(NumericValue.class)
165: .to("invalid");
166: Injector c = b.createInjector();
167: try {
168: c.getInstance(InvalidCharacter.class);
169: fail();
170: } catch (ConfigurationException e) { /* expected */
171: }
172: }
173:
174: public static class InvalidCharacter {
175: @Inject
176: @NumericValue
177: char foo;
178: }
179:
180: public void testInvalidEnum() throws CreationException {
181: BinderImpl b = new BinderImpl();
182: b.bindConstant().annotatedWith(NumericValue.class)
183: .to("invalid");
184: Injector c = b.createInjector();
185: try {
186: c.getInstance(InvalidEnum.class);
187: fail();
188: } catch (ConfigurationException e) { /* expected */
189: }
190: }
191:
192: public static class InvalidEnum {
193: @Inject
194: @NumericValue
195: Bar foo;
196: }
197:
198: public void testToInstanceIsTreatedLikeConstant()
199: throws CreationException {
200: BinderImpl b = new BinderImpl();
201: b.bind(String.class).toInstance("5");
202: b.bind(LongHolder.class);
203: Injector c = b.createInjector();
204: }
205:
206: static class LongHolder {
207: @Inject
208: LongHolder(long foo) {
209: }
210: }
211: }
|