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.Property;
35 import org.jomc.model.PropertyException;
36 import org.junit.Test;
37 import static org.junit.Assert.assertNotNull;
38 import static org.junit.Assert.assertNull;
39 import static org.junit.Assert.assertTrue;
40 import static org.junit.Assert.fail;
41
42
43
44
45
46
47
48 public class PropertyTest
49 {
50
51 public abstract static class AbstractJavaValue
52 {
53
54 public AbstractJavaValue( final String value )
55 {
56 super();
57 }
58
59 }
60
61 public static class UnsupportedJavaValue
62 {
63
64 public UnsupportedJavaValue()
65 {
66 super();
67 }
68
69 public UnsupportedJavaValue( final String value )
70 {
71 super();
72 throw new UnsupportedOperationException();
73 }
74
75 public Object getJavaValue( final ClassLoader classLoader )
76 {
77 throw new UnsupportedOperationException();
78 }
79
80 }
81
82 public static class ObjectJavaValue
83 {
84
85 public Object getJavaValue( final ClassLoader classLoader )
86 {
87 return new Object();
88 }
89
90 }
91
92 private static class InaccessibleJavaValue
93 {
94
95 private InaccessibleJavaValue( final String value )
96 {
97 super();
98 }
99
100 private Object getJavaValue( final ClassLoader classLoader )
101 {
102 return null;
103 }
104
105 }
106
107
108 public PropertyTest()
109 {
110 super();
111 }
112
113 @Test
114 public final void testGetJavaValue() throws Exception
115 {
116 final Property p = new Property();
117 assertNull( p.getJavaValue( this.getClass().getClassLoader() ) );
118
119 p.setAny( new Object() );
120
121 try
122 {
123 p.getJavaValue( this.getClass().getClassLoader() );
124 fail( "Expected ModelException not thrown for missing mandatory type." );
125 }
126 catch ( final PropertyException e )
127 {
128 assertNotNull( e.getMessage() );
129 System.out.println( e );
130 }
131
132 p.setType( UnsupportedJavaValue.class.getName() );
133 p.setAny( new UnsupportedJavaValue() );
134
135 try
136 {
137 p.getJavaValue( this.getClass().getClassLoader() );
138 fail( "Expected ModelException not thrown for unsupported getJavaValue operation." );
139 }
140 catch ( final PropertyException e )
141 {
142 assertNotNull( e.getMessage() );
143 assertTrue( e.getCause() instanceof InvocationTargetException );
144 System.out.println( e );
145 System.out.println( e.getCause() );
146 }
147
148 p.setType( Object.class.getName() );
149 p.setAny( new Object()
150 {
151
152 public Object getJavaValue( final ClassLoader classLoader )
153 {
154 return new Object();
155 }
156
157 } );
158
159 try
160 {
161 p.getJavaValue( this.getClass().getClassLoader() );
162 fail( "Expected ModelException not thrown for inaccessible getJavaValue method." );
163 }
164 catch ( final PropertyException e )
165 {
166 assertNotNull( e.getMessage() );
167 System.out.println( e );
168 System.out.println( e.getCause() );
169 }
170
171 p.setType( "java.lang.String" );
172 p.setAny( new ObjectJavaValue() );
173
174 try
175 {
176 p.getJavaValue( this.getClass().getClassLoader() );
177 fail( "Expected ModelException not thrown for incompatible getJavaValue method." );
178 }
179 catch ( final PropertyException e )
180 {
181 assertNotNull( e.getMessage() );
182 System.out.println( e );
183 }
184
185 p.setType( "int" );
186 p.setAny( null );
187 p.setValue( null );
188
189 try
190 {
191 p.getJavaValue( this.getClass().getClassLoader() );
192 fail( "Expected ModelException not thrown for mandatory primitive value." );
193 }
194 catch ( final PropertyException e )
195 {
196 assertNotNull( e.getMessage() );
197 System.out.println( e );
198 }
199
200 p.setType( "DOES_NOT_EXIST" );
201 p.setAny( null );
202 p.setValue( "STRING VALUE" );
203
204 try
205 {
206 p.getJavaValue( this.getClass().getClassLoader() );
207 fail( "Expected ModelException not thrown for missing class." );
208 }
209 catch ( final PropertyException e )
210 {
211 assertNotNull( e.getMessage() );
212 System.out.println( e );
213 }
214
215 p.setType( "char" );
216 p.setValue( "NO CHAR VALUE" );
217
218 try
219 {
220 p.getJavaValue( this.getClass().getClassLoader() );
221 fail( "Expected ModelException not thrown for illegal char value." );
222 }
223 catch ( final PropertyException e )
224 {
225 assertNotNull( e.getMessage() );
226 System.out.println( e );
227 }
228
229 p.setType( AbstractJavaValue.class.getName() );
230 p.setAny( null );
231 p.setValue( "STRING VALUE" );
232
233 try
234 {
235 p.getJavaValue( this.getClass().getClassLoader() );
236 fail( "Expected ModelException not thrown for non-instantiable class." );
237 }
238 catch ( final PropertyException e )
239 {
240 assertNotNull( e.getMessage() );
241 System.out.println( e );
242 }
243
244 p.setType( ObjectJavaValue.class.getName() );
245 p.setAny( null );
246 p.setValue( "STRING VALUE" );
247
248 try
249 {
250 p.getJavaValue( this.getClass().getClassLoader() );
251 fail( "Expected ModelException not thrown for missing constructor." );
252 }
253 catch ( final PropertyException e )
254 {
255 assertNotNull( e.getMessage() );
256 System.out.println( e );
257 }
258
259 p.setType( UnsupportedJavaValue.class.getName() );
260 p.setAny( null );
261 p.setValue( "STRING VALUE" );
262
263 try
264 {
265 p.getJavaValue( this.getClass().getClassLoader() );
266 fail( "Expected ModelException not thrown for unsupported constructor." );
267 }
268 catch ( final PropertyException e )
269 {
270 assertNotNull( e.getMessage() );
271 System.out.println( e );
272 }
273
274 p.setType( InaccessibleJavaValue.class.getName() );
275 p.setAny( null );
276 p.setValue( "STRING VALUE" );
277
278 try
279 {
280 p.getJavaValue( this.getClass().getClassLoader() );
281 fail( "Expected ModelException not thrown for inaccessible constructor." );
282 }
283 catch ( final PropertyException e )
284 {
285 assertNotNull( e.getMessage() );
286 System.out.println( e );
287 }
288 }
289
290 }