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
111 public PropertyTest()
112 {
113 super();
114 }
115
116 @Test
117 public final void testGetJavaValue() throws Exception
118 {
119 final Property p = new Property();
120 assertNull( p.getJavaValue( this.getClass().getClassLoader() ) );
121
122 p.setAny( new Object() );
123
124 try
125 {
126 p.getJavaValue( this.getClass().getClassLoader() );
127 fail( "Expected ModelException not thrown for missing mandatory type." );
128 }
129 catch ( final PropertyException e )
130 {
131 assertNotNull( e.getMessage() );
132 System.out.println( e );
133 }
134
135 p.setType( UnsupportedJavaValue.class.getName() );
136 p.setAny( new UnsupportedJavaValue() );
137
138 try
139 {
140 p.getJavaValue( this.getClass().getClassLoader() );
141 fail( "Expected ModelException not thrown for unsupported getJavaValue operation." );
142 }
143 catch ( final PropertyException e )
144 {
145 assertNotNull( e.getMessage() );
146 assertTrue( e.getCause() instanceof InvocationTargetException );
147 System.out.println( e );
148 System.out.println( e.getCause() );
149 }
150
151 p.setType( Object.class.getName() );
152 p.setAny( new Object()
153 {
154
155 public Object getJavaValue( final ClassLoader classLoader )
156 {
157 return new Object();
158 }
159
160 } );
161
162 try
163 {
164 p.getJavaValue( this.getClass().getClassLoader() );
165 fail( "Expected ModelException not thrown for inaccessible getJavaValue method." );
166 }
167 catch ( final PropertyException e )
168 {
169 assertNotNull( e.getMessage() );
170 System.out.println( e );
171 System.out.println( e.getCause() );
172 }
173
174 p.setType( "java.lang.String" );
175 p.setAny( new ObjectJavaValue() );
176
177 try
178 {
179 p.getJavaValue( this.getClass().getClassLoader() );
180 fail( "Expected ModelException not thrown for incompatible getJavaValue method." );
181 }
182 catch ( final PropertyException e )
183 {
184 assertNotNull( e.getMessage() );
185 System.out.println( e );
186 }
187
188 p.setType( "int" );
189 p.setAny( null );
190 p.setValue( null );
191
192 try
193 {
194 p.getJavaValue( this.getClass().getClassLoader() );
195 fail( "Expected ModelException not thrown for mandatory primitive value." );
196 }
197 catch ( final PropertyException e )
198 {
199 assertNotNull( e.getMessage() );
200 System.out.println( e );
201 }
202
203 p.setType( "DOES_NOT_EXIST" );
204 p.setAny( null );
205 p.setValue( "STRING VALUE" );
206
207 try
208 {
209 p.getJavaValue( this.getClass().getClassLoader() );
210 fail( "Expected ModelException not thrown for missing class." );
211 }
212 catch ( final PropertyException e )
213 {
214 assertNotNull( e.getMessage() );
215 System.out.println( e );
216 }
217
218 p.setType( "char" );
219 p.setValue( "NO CHAR VALUE" );
220
221 try
222 {
223 p.getJavaValue( this.getClass().getClassLoader() );
224 fail( "Expected ModelException not thrown for illegal char value." );
225 }
226 catch ( final PropertyException e )
227 {
228 assertNotNull( e.getMessage() );
229 System.out.println( e );
230 }
231
232 p.setType( AbstractJavaValue.class.getName() );
233 p.setAny( null );
234 p.setValue( "STRING VALUE" );
235
236 try
237 {
238 p.getJavaValue( this.getClass().getClassLoader() );
239 fail( "Expected ModelException not thrown for non-instantiable class." );
240 }
241 catch ( final PropertyException e )
242 {
243 assertNotNull( e.getMessage() );
244 System.out.println( e );
245 }
246
247 p.setType( ObjectJavaValue.class.getName() );
248 p.setAny( null );
249 p.setValue( "STRING VALUE" );
250
251 try
252 {
253 p.getJavaValue( this.getClass().getClassLoader() );
254 fail( "Expected ModelException not thrown for missing constructor." );
255 }
256 catch ( final PropertyException e )
257 {
258 assertNotNull( e.getMessage() );
259 System.out.println( e );
260 }
261
262 p.setType( UnsupportedJavaValue.class.getName() );
263 p.setAny( null );
264 p.setValue( "STRING VALUE" );
265
266 try
267 {
268 p.getJavaValue( this.getClass().getClassLoader() );
269 fail( "Expected ModelException not thrown for unsupported constructor." );
270 }
271 catch ( final PropertyException e )
272 {
273 assertNotNull( e.getMessage() );
274 System.out.println( e );
275 }
276
277 p.setType( InaccessibleJavaValue.class.getName() );
278 p.setAny( null );
279 p.setValue( "STRING VALUE" );
280
281 try
282 {
283 p.getJavaValue( this.getClass().getClassLoader() );
284 fail( "Expected ModelException not thrown for inaccessible constructor." );
285 }
286 catch ( final PropertyException e )
287 {
288 assertNotNull( e.getMessage() );
289 System.out.println( e );
290 }
291 }
292
293 @Test
294 public final void JavaConstantName() throws Exception
295 {
296 final Property p = new Property();
297
298 try
299 {
300 p.getJavaConstantName();
301 fail( "Expected 'ModelObjectException' not thrown." );
302 }
303 catch ( final ModelObjectException e )
304 {
305 assertNotNull( e.getMessage() );
306 System.out.println( e.toString() );
307 }
308
309 p.setName( "test test" );
310 assertEquals( JavaIdentifier.valueOf( "TEST_TEST" ), p.getJavaConstantName() );
311 }
312
313 @Test
314 public final void JavaGetterMethodName() throws Exception
315 {
316 final Property p = new Property();
317
318 try
319 {
320 p.getJavaGetterMethodName();
321 fail( "Expected 'ModelObjectException' not thrown." );
322 }
323 catch ( final ModelObjectException e )
324 {
325 assertNotNull( e.getMessage() );
326 System.out.println( e.toString() );
327 }
328
329 p.setName( "TEST TEST" );
330 assertEquals( JavaIdentifier.valueOf( "getTestTest" ), p.getJavaGetterMethodName() );
331 }
332
333 @Test
334 public final void JavaSetterMethodName() throws Exception
335 {
336 final Property p = new Property();
337
338 try
339 {
340 p.getJavaSetterMethodName();
341 fail( "Expected 'ModelObjectException' not thrown." );
342 }
343 catch ( final ModelObjectException e )
344 {
345 assertNotNull( e.getMessage() );
346 System.out.println( e.toString() );
347 }
348
349 p.setName( "TEST TEST" );
350 assertEquals( JavaIdentifier.valueOf( "setTestTest" ), p.getJavaSetterMethodName() );
351 }
352
353 @Test
354 public final void JavaVariableName() throws Exception
355 {
356 final Property p = new Property();
357
358 try
359 {
360 p.getJavaVariableName();
361 fail( "Expected 'ModelObjectException' not thrown." );
362 }
363 catch ( final ModelObjectException e )
364 {
365 assertNotNull( e.getMessage() );
366 System.out.println( e.toString() );
367 }
368
369 p.setName( "TEST TEST" );
370 assertEquals( JavaIdentifier.valueOf( "testTest" ), p.getJavaVariableName() );
371 }
372
373 }