1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht;
21
22 import org.grouplens.grapht.annotation.DefaultDouble;
23 import org.grouplens.grapht.annotation.DefaultInteger;
24 import org.junit.Assert;
25 import org.junit.Test;
26
27 import javax.inject.Inject;
28 import javax.inject.Qualifier;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.math.BigDecimal;
32 import java.math.BigInteger;
33
34 public class ParameterAnnotationTest {
35 @Test
36 public void testBoxedIntBinding() throws InjectionException {
37 InjectorBuilder b = InjectorBuilder.create();
38 b.bind(Integer.class).withQualifier(IntParameter.class).to(50);
39 Type t = b.build().getInstance(Type.class);
40
41 Assert.assertEquals(50, t.a);
42 }
43
44 @Test
45 public void testPrimitiveIntBinding() throws InjectionException {
46 InjectorBuilder b = InjectorBuilder.create();
47 b.bind(int.class).withQualifier(IntParameter.class).to(50);
48 Type t = b.build().getInstance(Type.class);
49
50 Assert.assertEquals(50, t.a);
51 }
52
53 @Test
54 public void testBoxedDoubleBinding() throws InjectionException {
55 InjectorBuilder b = InjectorBuilder.create();
56 b.bind(Double.class).withQualifier(DoubleParameter.class).to(50.0);
57 Type t = b.build().getInstance(Type.class);
58
59 Assert.assertEquals(50.0, t.b, 0.00001);
60 }
61
62 @Test
63 public void testPrimitiveDoubleBinding() throws InjectionException {
64 InjectorBuilder b = InjectorBuilder.create();
65 b.bind(double.class).withQualifier(DoubleParameter.class).to(50.0);
66 Type t = b.build().getInstance(Type.class);
67
68 Assert.assertEquals(50.0, t.b, 0.00001);
69 }
70
71 private static void bindDouble(Context ctx, Object obj) {
72 ctx.bind((Class) Double.class).withQualifier(DoubleParameter.class).to(obj);
73 }
74 private static void bindInt(Context ctx, Object obj) {
75 ctx.bind((Class) Integer.class).withQualifier(IntParameter.class).to(obj);
76 }
77
78 @Test
79 public void testDirectBinding() throws InjectionException {
80 InjectorBuilder b = InjectorBuilder.create();
81 bindInt(b, 50);
82 bindDouble(b, 50.0);
83 Type t = b.build().getInstance(Type.class);
84
85 Assert.assertEquals(50, t.a);
86 Assert.assertEquals(50.0, t.b, 0.00001);
87 }
88
89 @Test
90 public void testFromBigIntegerCoercion() throws InjectionException {
91 InjectorBuilder b = InjectorBuilder.create();
92 bindInt(b, BigInteger.valueOf(50));
93 Type t = b.build().getInstance(Type.class);
94
95 Assert.assertEquals(50, t.a);
96 }
97
98 @Test
99 public void testFromBigDecimalCoercion() throws InjectionException {
100 InjectorBuilder b = InjectorBuilder.create();
101 bindDouble(b, new BigDecimal(50.34));
102 Type t = b.build().getInstance(Type.class);
103
104 Assert.assertEquals(50.34, t.b, 0.00001);
105 }
106
107 @Test
108 public void testFromLongCoercion() throws InjectionException {
109 InjectorBuilder b = InjectorBuilder.create();
110 bindInt(b, Long.valueOf(50));
111 Type t = b.build().getInstance(Type.class);
112
113 Assert.assertEquals(50, t.a);
114 }
115
116 @Test
117 public void testFromFloatCoercion() throws InjectionException {
118 InjectorBuilder b = InjectorBuilder.create();
119 bindDouble(b, Float.valueOf(50f));
120 Type t = b.build().getInstance(Type.class);
121
122 Assert.assertEquals(50.0, t.b, 0.00001);
123 }
124
125 @Test
126 public void testFromByteCoercion() throws InjectionException {
127 InjectorBuilder b = InjectorBuilder.create();
128 bindInt(b, Byte.valueOf((byte) 50));
129 Type t = b.build().getInstance(Type.class);
130
131 Assert.assertEquals(50, t.a);
132 }
133
134 @Test
135 public void testDiscreteToFloatCoercion() throws InjectionException {
136 InjectorBuilder b = InjectorBuilder.create();
137 bindDouble(b, Integer.valueOf(50));
138 Type t = b.build().getInstance(Type.class);
139
140 Assert.assertEquals(50.0, t.b, 0.00001);
141 }
142
143 @DefaultInteger(0)
144 @Qualifier
145 @Retention(RetentionPolicy.RUNTIME)
146 public static @interface IntParameter { }
147
148 @DefaultDouble(0.0)
149 @Qualifier
150 @Retention(RetentionPolicy.RUNTIME)
151 public static @interface DoubleParameter { }
152
153 public static class Type {
154 final int a;
155 final double b;
156
157 @Inject
158 public Type(@IntParameter int a,
159 @DoubleParameter double b) {
160 this.a = a;
161 this.b = b;
162 }
163 }
164 }