001/* 002 * Copyright (C) Christian Schulte, 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 4861 2014-02-13 23:23:29Z 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 4861 2014-02-13 23:23:29Z 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 /** Creates a new {@code TemplateParameterTypeTest} instance. */ 119 public TemplateParameterTypeTest() 120 { 121 super(); 122 } 123 124 @Test 125 public final void testGetJavaValue() throws Exception 126 { 127 final TemplateParameterType p = new TemplateParameterType(); 128 assertNull( p.getJavaValue( this.getClass().getClassLoader() ) ); 129 130 p.setAny( new Object() ); 131 132 try 133 { 134 p.getJavaValue( this.getClass().getClassLoader() ); 135 fail( "Expected ModelObjectException not thrown for missing mandatory type." ); 136 } 137 catch ( final ModelObjectException e ) 138 { 139 assertNotNull( e.getMessage() ); 140 System.out.println( e ); 141 } 142 143 p.setType( UnsupportedJavaValue.class.getName() ); 144 p.setAny( new UnsupportedJavaValue() ); 145 146 try 147 { 148 p.getJavaValue( this.getClass().getClassLoader() ); 149 fail( "Expected ModelObjectException not thrown for unsupported getJavaValue operation." ); 150 } 151 catch ( final ModelObjectException e ) 152 { 153 assertNotNull( e.getMessage() ); 154 assertTrue( e.getCause() instanceof InvocationTargetException ); 155 System.out.println( e ); 156 System.out.println( e.getCause() ); 157 } 158 159 p.setType( Object.class.getName() ); 160 p.setAny( new Object() 161 { 162 163 public Object getJavaValue( final ClassLoader classLoader ) 164 { 165 return new Object(); 166 } 167 168 } ); 169 170 try 171 { 172 p.getJavaValue( this.getClass().getClassLoader() ); 173 fail( "Expected ModelObjectException not thrown for inaccessible getJavaValue method." ); 174 } 175 catch ( final ModelObjectException e ) 176 { 177 assertNotNull( e.getMessage() ); 178 System.out.println( e ); 179 System.out.println( e.getCause() ); 180 } 181 182 p.setType( "java.lang.String" ); 183 p.setAny( new ObjectJavaValue() ); 184 185 try 186 { 187 p.getJavaValue( this.getClass().getClassLoader() ); 188 fail( "Expected ModelObjectException not thrown for incompatible getJavaValue method." ); 189 } 190 catch ( final ModelObjectException e ) 191 { 192 assertNotNull( e.getMessage() ); 193 System.out.println( e ); 194 } 195 196 p.setType( "int" ); 197 p.setAny( null ); 198 p.setValue( null ); 199 200 try 201 { 202 p.getJavaValue( this.getClass().getClassLoader() ); 203 fail( "Expected ModelObjectException not thrown for mandatory primitive value." ); 204 } 205 catch ( final ModelObjectException e ) 206 { 207 assertNotNull( e.getMessage() ); 208 System.out.println( e ); 209 } 210 211 p.setType( "DOES_NOT_EXIST" ); 212 p.setAny( null ); 213 p.setValue( "STRING VALUE" ); 214 215 try 216 { 217 p.getJavaValue( this.getClass().getClassLoader() ); 218 fail( "Expected ModelObjectException not thrown for missing class." ); 219 } 220 catch ( final ModelObjectException e ) 221 { 222 assertNotNull( e.getMessage() ); 223 System.out.println( e ); 224 } 225 226 p.setType( "char" ); 227 p.setValue( "NO CHAR VALUE" ); 228 229 try 230 { 231 p.getJavaValue( this.getClass().getClassLoader() ); 232 fail( "Expected ModelObjectException not thrown for illegal char value." ); 233 } 234 catch ( final ModelObjectException e ) 235 { 236 assertNotNull( e.getMessage() ); 237 System.out.println( e ); 238 } 239 240 p.setType( AbstractJavaValue.class.getName() ); 241 p.setAny( null ); 242 p.setValue( "STRING VALUE" ); 243 244 try 245 { 246 p.getJavaValue( this.getClass().getClassLoader() ); 247 fail( "Expected ModelObjectException not thrown for non-instantiable class." ); 248 } 249 catch ( final ModelObjectException e ) 250 { 251 assertNotNull( e.getMessage() ); 252 System.out.println( e ); 253 } 254 255 p.setType( ObjectJavaValue.class.getName() ); 256 p.setAny( null ); 257 p.setValue( "STRING VALUE" ); 258 259 try 260 { 261 p.getJavaValue( this.getClass().getClassLoader() ); 262 fail( "Expected ModelObjectException not thrown for missing constructor." ); 263 } 264 catch ( final ModelObjectException e ) 265 { 266 assertNotNull( e.getMessage() ); 267 System.out.println( e ); 268 } 269 270 p.setType( UnsupportedJavaValue.class.getName() ); 271 p.setAny( null ); 272 p.setValue( "STRING VALUE" ); 273 274 try 275 { 276 p.getJavaValue( this.getClass().getClassLoader() ); 277 fail( "Expected ModelObjectException not thrown for unsupported constructor." ); 278 } 279 catch ( final ModelObjectException e ) 280 { 281 assertNotNull( e.getMessage() ); 282 System.out.println( e ); 283 } 284 285 p.setType( InaccessibleJavaValue.class.getName() ); 286 p.setAny( null ); 287 p.setValue( "STRING VALUE" ); 288 289 try 290 { 291 p.getJavaValue( this.getClass().getClassLoader() ); 292 fail( "Expected ModelObjectException not thrown for inaccessible constructor." ); 293 } 294 catch ( final ModelObjectException e ) 295 { 296 assertNotNull( e.getMessage() ); 297 System.out.println( e ); 298 } 299 300 p.setType( Thread.State.class.getName() ); 301 p.setAny( null ); 302 p.setValue( "RUNNABLE" ); 303 assertEquals( Thread.State.RUNNABLE, p.getJavaValue( this.getClass().getClassLoader() ) ); 304 305 p.setType( FactoryMethodTestClass.class.getName() ); 306 p.setAny( null ); 307 p.setValue( "TEST" ); 308 309 try 310 { 311 p.getJavaValue( this.getClass().getClassLoader() ); 312 fail( "Expected ModelObjectException not thrown for illegal factory method." ); 313 } 314 catch ( final ModelObjectException e ) 315 { 316 assertNotNull( e.getMessage() ); 317 System.out.println( e ); 318 } 319 } 320 321}