1 | package com.hammurapi.convert.tests; |
2 | |
3 | import static org.junit.Assert.assertEquals; |
4 | import static org.junit.Assert.assertNotNull; |
5 | |
6 | import java.io.File; |
7 | import java.net.MalformedURLException; |
8 | import java.net.URL; |
9 | import java.net.URLClassLoader; |
10 | import java.util.Collections; |
11 | import java.util.HashSet; |
12 | import java.util.Set; |
13 | import java.util.concurrent.Callable; |
14 | import java.util.concurrent.ExecutionException; |
15 | import java.util.concurrent.Future; |
16 | import java.util.concurrent.TimeUnit; |
17 | import java.util.concurrent.TimeoutException; |
18 | |
19 | import org.junit.Test; |
20 | |
21 | import com.hammurapi.common.Context; |
22 | import com.hammurapi.common.DefaultContext; |
23 | import com.hammurapi.convert.ConversionException; |
24 | import com.hammurapi.convert.Converter; |
25 | import com.hammurapi.convert.ConverterMethod; |
26 | import com.hammurapi.convert.ConvertingService; |
27 | |
28 | public class ConvertTests { |
29 | |
30 | public static class Source { |
31 | |
32 | Set<String> convertersInvoked = new HashSet<String>(); |
33 | |
34 | @ConverterMethod |
35 | public TargetOne toTargetOne() { |
36 | convertersInvoked.add("TargetOne"); |
37 | return new TargetOne(); |
38 | } |
39 | |
40 | @ConverterMethod |
41 | public TargetTwo toTargetTwo(Context context) { |
42 | convertersInvoked.add("TargetTwo"); |
43 | assertNotNull(context); |
44 | return new TargetTwo(); |
45 | } |
46 | |
47 | @ConverterMethod |
48 | public TargetThree toTargetThree(Converter master) { |
49 | convertersInvoked.add("TargetThree"); |
50 | assertNotNull(master); |
51 | return new TargetThree(); |
52 | } |
53 | |
54 | @ConverterMethod |
55 | public TargetFour toTargetFour(Context context, Converter master) { |
56 | convertersInvoked.add("TargetFour"); |
57 | assertNotNull(context); |
58 | assertNotNull(master); |
59 | return new TargetFour(); |
60 | } |
61 | |
62 | @ConverterMethod |
63 | public TargetFive toTargetFive(Converter master, Context context) { |
64 | convertersInvoked.add("TargetFive"); |
65 | assertNotNull(context); |
66 | assertNotNull(master); |
67 | return new TargetFive(); |
68 | } |
69 | |
70 | } |
71 | |
72 | /** |
73 | * Converted to TargetOne by reflective converter bundle |
74 | * @author Pavel Vlasov |
75 | * |
76 | */ |
77 | public static class SourceTwo { |
78 | |
79 | } |
80 | |
81 | /** |
82 | * Converted to TargetTwo by reflective converter bundle |
83 | * @author Pavel Vlasov |
84 | * |
85 | */ |
86 | public static class SourceThree { |
87 | |
88 | } |
89 | |
90 | public static class TargetOne { |
91 | |
92 | public TargetOne() { |
93 | |
94 | } |
95 | |
96 | } |
97 | |
98 | public static class TargetTwo { |
99 | |
100 | } |
101 | |
102 | public static class TargetThree { |
103 | |
104 | } |
105 | |
106 | public static class TargetFour { |
107 | |
108 | } |
109 | |
110 | public static class TargetFive { |
111 | |
112 | } |
113 | |
114 | public static class TargetSix { |
115 | |
116 | Source src; |
117 | |
118 | public TargetSix() { |
119 | |
120 | } |
121 | |
122 | public TargetSix(Source src) { |
123 | this.src = src; |
124 | } |
125 | |
126 | } |
127 | |
128 | @Test |
129 | public void testConvertMethod() { |
130 | Source source = new Source(); |
131 | Set<String> expectedTargets = new HashSet<String>(); |
132 | |
133 | TargetOne t1 = ConvertingService.convert(source, TargetOne.class); |
134 | expectedTargets.add("TargetOne"); |
135 | assertNotNull(t1); |
136 | assertEquals(expectedTargets, source.convertersInvoked); |
137 | |
138 | TargetTwo t2 = ConvertingService.convert(source, TargetTwo.class, DefaultContext.INSTANCE); |
139 | expectedTargets.add("TargetTwo"); |
140 | assertNotNull(t2); |
141 | assertEquals(expectedTargets, source.convertersInvoked); |
142 | |
143 | TargetThree t3 = ConvertingService.convert(source, TargetThree.class); |
144 | expectedTargets.add("TargetThree"); |
145 | assertNotNull(t3); |
146 | assertEquals(expectedTargets, source.convertersInvoked); |
147 | |
148 | TargetFour t4 = ConvertingService.convert(source, TargetFour.class, DefaultContext.INSTANCE); |
149 | expectedTargets.add("TargetFour"); |
150 | assertNotNull(t4); |
151 | assertEquals(expectedTargets, source.convertersInvoked); |
152 | |
153 | TargetFive t5 = ConvertingService.convert(source, TargetFive.class, DefaultContext.INSTANCE); |
154 | expectedTargets.add("TargetFive"); |
155 | assertNotNull(t5); |
156 | assertEquals(expectedTargets, source.convertersInvoked); |
157 | |
158 | TargetSix t6 = ConvertingService.convert(source, TargetSix.class); |
159 | assertNotNull(t6); |
160 | assertEquals(source, t6.src); |
161 | |
162 | } |
163 | |
164 | @Test |
165 | public void testReflectiveBundle() throws ConversionException, MalformedURLException { |
166 | SourceTwo source2 = new SourceTwo(); |
167 | |
168 | URLClassLoader classLoader = new URLClassLoader(new URL[] {new File("..\\Common\\tests").toURI().toURL()}, SourceTwo.class.getClassLoader()); |
169 | TargetOne t1 = ConvertingService.convert(source2, TargetOne.class, classLoader); |
170 | assertNotNull(t1); |
171 | |
172 | SourceThree source3 = new SourceThree(); |
173 | |
174 | TargetTwo t2 = ConvertingService.convert(source3, TargetTwo.class, DefaultContext.INSTANCE, classLoader); |
175 | assertNotNull(t2); |
176 | |
177 | } |
178 | |
179 | @Test |
180 | public void testSingletonConverter() { |
181 | Set<Integer> s = Collections.singleton(33); |
182 | Integer v = ConvertingService.convert(s, Integer.class); |
183 | assertEquals(new Integer(33), v); |
184 | |
185 | Set<String> ss = Collections.singleton("33"); |
186 | v = ConvertingService.convert(s, Integer.class); |
187 | assertEquals(new Integer(33), v); |
188 | |
189 | } |
190 | |
191 | @Test |
192 | public void testCallableSingletonConverter() { |
193 | Callable<Iterable<String>> c = new Callable<Iterable<String>>() { |
194 | |
195 | @Override |
196 | public Iterable<String> call() throws Exception { |
197 | return Collections.singleton("33"); |
198 | } |
199 | |
200 | }; |
201 | Integer v = ConvertingService.convert(c, Integer.class); |
202 | assertEquals(new Integer(33), v); |
203 | } |
204 | |
205 | @Test |
206 | public void testFutureSingletonConverter() { |
207 | Future<Iterable<String>> f = new Future<Iterable<String>>() { |
208 | |
209 | @Override |
210 | public Iterable<String> get() { |
211 | return Collections.singleton("33"); |
212 | } |
213 | |
214 | @Override |
215 | public boolean cancel(boolean mayInterruptIfRunning) { |
216 | return false; |
217 | } |
218 | |
219 | @Override |
220 | public boolean isCancelled() { |
221 | return false; |
222 | } |
223 | |
224 | @Override |
225 | public boolean isDone() { |
226 | return true; |
227 | } |
228 | |
229 | @Override |
230 | public Iterable<String> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
231 | return get(); |
232 | } |
233 | |
234 | }; |
235 | Integer v = ConvertingService.convert(f, Integer.class); |
236 | assertEquals(new Integer(33), v); |
237 | } |
238 | } |