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 3851 2011-10-10 16:25:09Z schulte2005 $
029 *
030 */
031package org.jomc.model.test;
032
033import java.lang.reflect.InvocationTargetException;
034import org.jomc.model.Property;
035import org.jomc.model.PropertyException;
036import org.junit.Test;
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertNull;
039import static org.junit.Assert.assertTrue;
040import static org.junit.Assert.fail;
041
042/**
043 * Test cases for class {@code org.jomc.model.Property}.
044 *
045 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
046 * @version $JOMC: PropertyTest.java 3851 2011-10-10 16:25:09Z schulte2005 $
047 */
048public class PropertyTest
049{
050
051    public abstract static class AbstractJavaValue
052    {
053
054        public AbstractJavaValue( final String value )
055        {
056            super();
057        }
058
059    }
060
061    public static class UnsupportedJavaValue
062    {
063
064        public UnsupportedJavaValue()
065        {
066            super();
067        }
068
069        public UnsupportedJavaValue( final String value )
070        {
071            super();
072            throw new UnsupportedOperationException();
073        }
074
075        public Object getJavaValue( final ClassLoader classLoader )
076        {
077            throw new UnsupportedOperationException();
078        }
079
080    }
081
082    public static class ObjectJavaValue
083    {
084
085        public Object getJavaValue( final ClassLoader classLoader )
086        {
087            return new Object();
088        }
089
090    }
091
092    private static class InaccessibleJavaValue
093    {
094
095        private InaccessibleJavaValue( final String value )
096        {
097            super();
098        }
099
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}