001/*
002 *   Copyright (C) Christian Schulte, 2005-206
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: PropertyTest.java 4762 2013-04-09 00:57:31Z schulte $
029 *
030 */
031package org.jomc.model.test;
032
033import java.lang.reflect.InvocationTargetException;
034import org.jomc.model.JavaIdentifier;
035import org.jomc.model.ModelObjectException;
036import org.jomc.model.Property;
037import org.jomc.model.PropertyException;
038import org.junit.Test;
039import static org.junit.Assert.assertEquals;
040import static org.junit.Assert.assertNotNull;
041import static org.junit.Assert.assertNull;
042import static org.junit.Assert.assertTrue;
043import static org.junit.Assert.fail;
044
045/**
046 * Test cases for class {@code org.jomc.model.Property}.
047 *
048 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
049 * @version $JOMC: PropertyTest.java 4762 2013-04-09 00:57:31Z schulte $
050 */
051public class PropertyTest
052{
053
054    public abstract static class AbstractJavaValue
055    {
056
057        public AbstractJavaValue( final String value )
058        {
059            super();
060        }
061
062    }
063
064    public static class UnsupportedJavaValue
065    {
066
067        public UnsupportedJavaValue()
068        {
069            super();
070        }
071
072        public UnsupportedJavaValue( final String value )
073        {
074            super();
075            throw new UnsupportedOperationException();
076        }
077
078        public Object getJavaValue( final ClassLoader classLoader )
079        {
080            throw new UnsupportedOperationException();
081        }
082
083    }
084
085    public static class ObjectJavaValue
086    {
087
088        public Object getJavaValue( final ClassLoader classLoader )
089        {
090            return new Object();
091        }
092
093    }
094
095    private static class InaccessibleJavaValue
096    {
097
098        private InaccessibleJavaValue( final String value )
099        {
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    /** Creates a new {@code PropertyTest} instance. */
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        // Since 1.5
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}