1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.tools.model.test;
32
33 import java.lang.reflect.InvocationTargetException;
34 import org.jomc.model.ModelObjectException;
35 import org.jomc.tools.model.TemplateParameterType;
36 import org.junit.Test;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertNull;
40 import static org.junit.Assert.assertTrue;
41 import static org.junit.Assert.fail;
42
43
44
45
46
47
48
49 public class TemplateParameterTypeTest
50 {
51
52 public abstract static class AbstractJavaValue
53 {
54
55 public AbstractJavaValue( final String value )
56 {
57 super();
58 }
59
60 }
61
62 public static class UnsupportedJavaValue
63 {
64
65 public UnsupportedJavaValue()
66 {
67 super();
68 }
69
70 public UnsupportedJavaValue( final String value )
71 {
72 super();
73 throw new UnsupportedOperationException();
74 }
75
76 public Object getJavaValue( final ClassLoader classLoader )
77 {
78 throw new UnsupportedOperationException();
79 }
80
81 }
82
83 public static class ObjectJavaValue
84 {
85
86 public Object getJavaValue( final ClassLoader classLoader )
87 {
88 return new Object();
89 }
90
91 }
92
93 private static class InaccessibleJavaValue
94 {
95
96 private InaccessibleJavaValue( final String value )
97 {
98 super();
99 }
100
101 private Object getJavaValue( final ClassLoader classLoader )
102 {
103 return null;
104 }
105
106 }
107
108 private static class FactoryMethodTestClass
109 {
110
111 public static Object valueOf( final String string )
112 {
113 return null;
114 }
115
116 }
117
118
119
120
121 public TemplateParameterTypeTest()
122 {
123 super();
124 }
125
126 @Test
127 public final void testGetJavaValue() throws Exception
128 {
129 final TemplateParameterType p = new TemplateParameterType();
130 assertNull( p.getJavaValue( this.getClass().getClassLoader() ) );
131
132 p.setAny( new Object() );
133
134 try
135 {
136 p.getJavaValue( this.getClass().getClassLoader() );
137 fail( "Expected ModelObjectException not thrown for missing mandatory type." );
138 }
139 catch ( final ModelObjectException e )
140 {
141 assertNotNull( e.getMessage() );
142 System.out.println( e );
143 }
144
145 p.setType( UnsupportedJavaValue.class.getName() );
146 p.setAny( new UnsupportedJavaValue() );
147
148 try
149 {
150 p.getJavaValue( this.getClass().getClassLoader() );
151 fail( "Expected ModelObjectException not thrown for unsupported getJavaValue operation." );
152 }
153 catch ( final ModelObjectException e )
154 {
155 assertNotNull( e.getMessage() );
156 assertTrue( e.getCause() instanceof InvocationTargetException );
157 System.out.println( e );
158 System.out.println( e.getCause() );
159 }
160
161 p.setType( Object.class.getName() );
162 p.setAny( new Object()
163 {
164
165 public Object getJavaValue( final ClassLoader classLoader )
166 {
167 return new Object();
168 }
169
170 } );
171
172 try
173 {
174 p.getJavaValue( this.getClass().getClassLoader() );
175 fail( "Expected ModelObjectException not thrown for inaccessible getJavaValue method." );
176 }
177 catch ( final ModelObjectException e )
178 {
179 assertNotNull( e.getMessage() );
180 System.out.println( e );
181 System.out.println( e.getCause() );
182 }
183
184 p.setType( "java.lang.String" );
185 p.setAny( new ObjectJavaValue() );
186
187 try
188 {
189 p.getJavaValue( this.getClass().getClassLoader() );
190 fail( "Expected ModelObjectException not thrown for incompatible getJavaValue method." );
191 }
192 catch ( final ModelObjectException e )
193 {
194 assertNotNull( e.getMessage() );
195 System.out.println( e );
196 }
197
198 p.setType( "int" );
199 p.setAny( null );
200 p.setValue( null );
201
202 try
203 {
204 p.getJavaValue( this.getClass().getClassLoader() );
205 fail( "Expected ModelObjectException not thrown for mandatory primitive value." );
206 }
207 catch ( final ModelObjectException e )
208 {
209 assertNotNull( e.getMessage() );
210 System.out.println( e );
211 }
212
213 p.setType( "DOES_NOT_EXIST" );
214 p.setAny( null );
215 p.setValue( "STRING VALUE" );
216
217 try
218 {
219 p.getJavaValue( this.getClass().getClassLoader() );
220 fail( "Expected ModelObjectException not thrown for missing class." );
221 }
222 catch ( final ModelObjectException e )
223 {
224 assertNotNull( e.getMessage() );
225 System.out.println( e );
226 }
227
228 p.setType( "char" );
229 p.setValue( "NO CHAR VALUE" );
230
231 try
232 {
233 p.getJavaValue( this.getClass().getClassLoader() );
234 fail( "Expected ModelObjectException not thrown for illegal char value." );
235 }
236 catch ( final ModelObjectException e )
237 {
238 assertNotNull( e.getMessage() );
239 System.out.println( e );
240 }
241
242 p.setType( AbstractJavaValue.class.getName() );
243 p.setAny( null );
244 p.setValue( "STRING VALUE" );
245
246 try
247 {
248 p.getJavaValue( this.getClass().getClassLoader() );
249 fail( "Expected ModelObjectException not thrown for non-instantiable class." );
250 }
251 catch ( final ModelObjectException e )
252 {
253 assertNotNull( e.getMessage() );
254 System.out.println( e );
255 }
256
257 p.setType( ObjectJavaValue.class.getName() );
258 p.setAny( null );
259 p.setValue( "STRING VALUE" );
260
261 try
262 {
263 p.getJavaValue( this.getClass().getClassLoader() );
264 fail( "Expected ModelObjectException not thrown for missing constructor." );
265 }
266 catch ( final ModelObjectException e )
267 {
268 assertNotNull( e.getMessage() );
269 System.out.println( e );
270 }
271
272 p.setType( UnsupportedJavaValue.class.getName() );
273 p.setAny( null );
274 p.setValue( "STRING VALUE" );
275
276 try
277 {
278 p.getJavaValue( this.getClass().getClassLoader() );
279 fail( "Expected ModelObjectException not thrown for unsupported constructor." );
280 }
281 catch ( final ModelObjectException e )
282 {
283 assertNotNull( e.getMessage() );
284 System.out.println( e );
285 }
286
287 p.setType( InaccessibleJavaValue.class.getName() );
288 p.setAny( null );
289 p.setValue( "STRING VALUE" );
290
291 try
292 {
293 p.getJavaValue( this.getClass().getClassLoader() );
294 fail( "Expected ModelObjectException not thrown for inaccessible constructor." );
295 }
296 catch ( final ModelObjectException e )
297 {
298 assertNotNull( e.getMessage() );
299 System.out.println( e );
300 }
301
302 p.setType( Thread.State.class.getName() );
303 p.setAny( null );
304 p.setValue( "RUNNABLE" );
305 assertEquals( Thread.State.RUNNABLE, p.getJavaValue( this.getClass().getClassLoader() ) );
306
307 p.setType( FactoryMethodTestClass.class.getName() );
308 p.setAny( null );
309 p.setValue( "TEST" );
310
311 try
312 {
313 p.getJavaValue( this.getClass().getClassLoader() );
314 fail( "Expected ModelObjectException not thrown for illegal factory method." );
315 }
316 catch ( final ModelObjectException e )
317 {
318 assertNotNull( e.getMessage() );
319 System.out.println( e );
320 }
321 }
322
323 }