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.model.test;
32
33 import java.lang.reflect.InvocationTargetException;
34 import org.jomc.model.JavaIdentifier;
35 import org.jomc.model.ModelObjectException;
36 import org.jomc.model.Property;
37 import org.jomc.model.PropertyException;
38 import org.junit.Test;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.assertNull;
42 import static org.junit.Assert.assertTrue;
43 import static org.junit.Assert.fail;
44
45
46
47
48
49
50
51 public class PropertyTest
52 {
53
54 public abstract static class AbstractJavaValue
55 {
56
57 public AbstractJavaValue( final String value )
58 {
59 super();
60 }
61
62 }
63
64 public static class UnsupportedJavaValue
65 {
66
67 public UnsupportedJavaValue()
68 {
69 super();
70 }
71
72 public UnsupportedJavaValue( final String value )
73 {
74 super();
75 throw new UnsupportedOperationException();
76 }
77
78 public Object getJavaValue( final ClassLoader classLoader )
79 {
80 throw new UnsupportedOperationException();
81 }
82
83 }
84
85 public static class ObjectJavaValue
86 {
87
88 public Object getJavaValue( final ClassLoader classLoader )
89 {
90 return new Object();
91 }
92
93 }
94
95 private static class InaccessibleJavaValue
96 {
97
98 private InaccessibleJavaValue( final String value )
99 {
100 super();
101 }
102
103 private Object getJavaValue( final ClassLoader classLoader )
104 {
105 return null;
106 }
107
108 }
109
110 private static class FactoryMethodTestClass
111 {
112
113 public static Object valueOf( final String string )
114 {
115 return null;
116 }
117
118 }
119
120
121 public PropertyTest()
122 {
123 super();
124 }
125
126 @Test
127 public final void testGetJavaValue() throws Exception
128 {
129 final Property p = new Property();
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 PropertyException not thrown for missing mandatory type." );
138 }
139 catch ( final PropertyException 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 PropertyException not thrown for unsupported getJavaValue operation." );
152 }
153 catch ( final PropertyException 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 PropertyException not thrown for inaccessible getJavaValue method." );
176 }
177 catch ( final PropertyException 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 PropertyException not thrown for incompatible getJavaValue method." );
191 }
192 catch ( final PropertyException 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 PropertyException not thrown for mandatory primitive value." );
206 }
207 catch ( final PropertyException 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 PropertyException not thrown for missing class." );
221 }
222 catch ( final PropertyException 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 PropertyException not thrown for illegal char value." );
235 }
236 catch ( final PropertyException 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 PropertyException not thrown for non-instantiable class." );
250 }
251 catch ( final PropertyException 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 PropertyException not thrown for missing constructor." );
265 }
266 catch ( final PropertyException 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 PropertyException not thrown for unsupported constructor." );
280 }
281 catch ( final PropertyException 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 PropertyException not thrown for inaccessible constructor." );
295 }
296 catch ( final PropertyException e )
297 {
298 assertNotNull( e.getMessage() );
299 System.out.println( e );
300 }
301
302
303 p.setType( Thread.State.class.getName() );
304 p.setAny( null );
305 p.setValue( "RUNNABLE" );
306 assertEquals( Thread.State.RUNNABLE, p.getJavaValue( this.getClass().getClassLoader() ) );
307
308 p.setType( FactoryMethodTestClass.class.getName() );
309 p.setAny( null );
310 p.setValue( "TEST" );
311
312 try
313 {
314 p.getJavaValue( this.getClass().getClassLoader() );
315 fail( "Expected PropertyException not thrown for illegal factory method." );
316 }
317 catch ( final PropertyException e )
318 {
319 assertNotNull( e.getMessage() );
320 System.out.println( e );
321 }
322 }
323
324 @Test
325 public final void JavaConstantName() throws Exception
326 {
327 final Property p = new Property();
328
329 try
330 {
331 p.getJavaConstantName();
332 fail( "Expected 'ModelObjectException' not thrown." );
333 }
334 catch ( final ModelObjectException e )
335 {
336 assertNotNull( e.getMessage() );
337 System.out.println( e.toString() );
338 }
339
340 p.setName( "test test" );
341 assertEquals( JavaIdentifier.valueOf( "TEST_TEST" ), p.getJavaConstantName() );
342 }
343
344 @Test
345 public final void JavaGetterMethodName() throws Exception
346 {
347 final Property p = new Property();
348
349 try
350 {
351 p.getJavaGetterMethodName();
352 fail( "Expected 'ModelObjectException' not thrown." );
353 }
354 catch ( final ModelObjectException e )
355 {
356 assertNotNull( e.getMessage() );
357 System.out.println( e.toString() );
358 }
359
360 p.setName( "TEST TEST" );
361 assertEquals( JavaIdentifier.valueOf( "getTestTest" ), p.getJavaGetterMethodName() );
362 }
363
364 @Test
365 public final void JavaSetterMethodName() throws Exception
366 {
367 final Property p = new Property();
368
369 try
370 {
371 p.getJavaSetterMethodName();
372 fail( "Expected 'ModelObjectException' not thrown." );
373 }
374 catch ( final ModelObjectException e )
375 {
376 assertNotNull( e.getMessage() );
377 System.out.println( e.toString() );
378 }
379
380 p.setName( "TEST TEST" );
381 assertEquals( JavaIdentifier.valueOf( "setTestTest" ), p.getJavaSetterMethodName() );
382 }
383
384 @Test
385 public final void JavaVariableName() throws Exception
386 {
387 final Property p = new Property();
388
389 try
390 {
391 p.getJavaVariableName();
392 fail( "Expected 'ModelObjectException' not thrown." );
393 }
394 catch ( final ModelObjectException e )
395 {
396 assertNotNull( e.getMessage() );
397 System.out.println( e.toString() );
398 }
399
400 p.setName( "TEST TEST" );
401 assertEquals( JavaIdentifier.valueOf( "testTest" ), p.getJavaVariableName() );
402 }
403
404 }