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 4670 2012-12-23 01:20:36Z 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 4670 2012-12-23 01:20:36Z 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    /** Creates a new {@code PropertyTest} instance. */
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}