View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: PropertyTest.java 3851 2011-10-10 16:25:09Z schulte2005 $
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   * Test cases for class {@code org.jomc.model.Property}.
44   *
45   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
46   * @version $JOMC: PropertyTest.java 3851 2011-10-10 16:25:09Z schulte2005 $
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     /** Creates a new {@code PropertyTest} instance. */
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 }