001/*
002 *   Copyright (C) Christian Schulte <cs@schulte.it>, 2014-043
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: TemplateParameterTypeTest.java 5043 2015-05-27 07:03:39Z schulte $
029 *
030 */
031package org.jomc.tools.model.test;
032
033import java.lang.reflect.InvocationTargetException;
034import org.jomc.model.ModelObjectException;
035import org.jomc.tools.model.TemplateParameterType;
036import org.junit.Test;
037import static org.junit.Assert.assertEquals;
038import static org.junit.Assert.assertNotNull;
039import static org.junit.Assert.assertNull;
040import static org.junit.Assert.assertTrue;
041import static org.junit.Assert.fail;
042
043/**
044 * Test cases for class {@code org.jomc.tools.model.TemplateParameterType}.
045 *
046 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
047 * @version $JOMC: TemplateParameterTypeTest.java 5043 2015-05-27 07:03:39Z schulte $
048 */
049public class TemplateParameterTypeTest
050{
051
052    public abstract static class AbstractJavaValue
053    {
054
055        public AbstractJavaValue( final String value )
056        {
057            super();
058        }
059
060    }
061
062    public static class UnsupportedJavaValue
063    {
064
065        public UnsupportedJavaValue()
066        {
067            super();
068        }
069
070        public UnsupportedJavaValue( final String value )
071        {
072            super();
073            throw new UnsupportedOperationException();
074        }
075
076        public Object getJavaValue( final ClassLoader classLoader )
077        {
078            throw new UnsupportedOperationException();
079        }
080
081    }
082
083    public static class ObjectJavaValue
084    {
085
086        public Object getJavaValue( final ClassLoader classLoader )
087        {
088            return new Object();
089        }
090
091    }
092
093    private static class InaccessibleJavaValue
094    {
095
096        private InaccessibleJavaValue( final String value )
097        {
098            super();
099        }
100
101        private Object getJavaValue( final ClassLoader classLoader )
102        {
103            return null;
104        }
105
106    }
107
108    private static class FactoryMethodTestClass
109    {
110
111        public static Object valueOf( final String string )
112        {
113            return null;
114        }
115
116    }
117
118    /**
119     * Creates a new {@code TemplateParameterTypeTest} instance.
120     */
121    public TemplateParameterTypeTest()
122    {
123        super();
124    }
125
126    @Test
127    public final void testGetJavaValue() throws Exception
128    {
129        final TemplateParameterType p = new TemplateParameterType();
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 ModelObjectException not thrown for missing mandatory type." );
138        }
139        catch ( final ModelObjectException 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 ModelObjectException not thrown for unsupported getJavaValue operation." );
152        }
153        catch ( final ModelObjectException 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 ModelObjectException not thrown for inaccessible getJavaValue method." );
176        }
177        catch ( final ModelObjectException 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 ModelObjectException not thrown for incompatible getJavaValue method." );
191        }
192        catch ( final ModelObjectException 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 ModelObjectException not thrown for mandatory primitive value." );
206        }
207        catch ( final ModelObjectException 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 ModelObjectException not thrown for missing class." );
221        }
222        catch ( final ModelObjectException 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 ModelObjectException not thrown for illegal char value." );
235        }
236        catch ( final ModelObjectException 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 ModelObjectException not thrown for non-instantiable class." );
250        }
251        catch ( final ModelObjectException 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 ModelObjectException not thrown for missing constructor." );
265        }
266        catch ( final ModelObjectException 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 ModelObjectException not thrown for unsupported constructor." );
280        }
281        catch ( final ModelObjectException 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 ModelObjectException not thrown for inaccessible constructor." );
295        }
296        catch ( final ModelObjectException e )
297        {
298            assertNotNull( e.getMessage() );
299            System.out.println( e );
300        }
301
302        p.setType( Thread.State.class.getName() );
303        p.setAny( null );
304        p.setValue( "RUNNABLE" );
305        assertEquals( Thread.State.RUNNABLE, p.getJavaValue( this.getClass().getClassLoader() ) );
306
307        p.setType( FactoryMethodTestClass.class.getName() );
308        p.setAny( null );
309        p.setValue( "TEST" );
310
311        try
312        {
313            p.getJavaValue( this.getClass().getClassLoader() );
314            fail( "Expected ModelObjectException not thrown for illegal factory method." );
315        }
316        catch ( final ModelObjectException e )
317        {
318            assertNotNull( e.getMessage() );
319            System.out.println( e );
320        }
321    }
322
323}