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: ToolsModelProcessorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 029 * 030 */ 031package org.jomc.tools.modlet.test; 032 033import org.jomc.model.Implementation; 034import org.jomc.model.Implementations; 035import org.jomc.model.ModelObject; 036import org.jomc.model.Module; 037import org.jomc.model.Modules; 038import org.jomc.model.Specification; 039import org.jomc.model.Specifications; 040import org.jomc.model.modlet.ModelHelper; 041import org.jomc.modlet.Model; 042import org.jomc.modlet.ModelContext; 043import org.jomc.modlet.ModelContextFactory; 044import org.jomc.tools.model.SourceFileType; 045import org.jomc.tools.model.SourceFilesType; 046import org.jomc.tools.model.SourceSectionType; 047import org.jomc.tools.model.SourceSectionsType; 048import org.jomc.tools.modlet.ToolsModelProcessor; 049import org.junit.Test; 050import static org.junit.Assert.assertEquals; 051import static org.junit.Assert.assertFalse; 052import static org.junit.Assert.assertNotNull; 053import static org.junit.Assert.assertNull; 054import static org.junit.Assert.assertTrue; 055import static org.junit.Assert.fail; 056 057/** 058 * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProcessor}. 059 * 060 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 061 * @version $JOMC: ToolsModelProcessorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 062 */ 063public class ToolsModelProcessorTest 064{ 065 066 /** The {@code ToolsModelProcessor} instance tests are performed with. */ 067 private ToolsModelProcessor toolsModelProcessor; 068 069 /** Creates a new {@code ToolsModelProcessorTest} instance. */ 070 public ToolsModelProcessorTest() 071 { 072 super(); 073 } 074 075 /** 076 * Gets the {@code ToolsModelProcessor} instance tests are performed with. 077 * 078 * @return The {@code ToolsModelProcessor} instance tests are performed with. 079 * 080 * @see #newModelProcessor() 081 */ 082 public ToolsModelProcessor getModelProcessor() 083 { 084 if ( this.toolsModelProcessor == null ) 085 { 086 this.toolsModelProcessor = this.newModelProcessor(); 087 } 088 089 return this.toolsModelProcessor; 090 } 091 092 /** 093 * Creates a new {@code ToolsModelProcessor} instance to test. 094 * 095 * @return A new {@code ToolsModelProcessor} instance to test. 096 * 097 * @see #getModelProcessor() 098 */ 099 protected ToolsModelProcessor newModelProcessor() 100 { 101 return new ToolsModelProcessor(); 102 } 103 104 @Test 105 public final void testProcessModel() throws Exception 106 { 107 final ModelContext context = ModelContextFactory.newInstance().newModelContext(); 108 Model model = new Model(); 109 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 110 111 Modules modules = new Modules(); 112 Module module = new Module(); 113 module.setName( this.getClass().getName() ); 114 module.setSpecifications( new Specifications() ); 115 module.setImplementations( new Implementations() ); 116 117 Specification specification = new Specification(); 118 specification.setClassDeclaration( true ); 119 specification.setClazz( this.getClass().getName() ); 120 specification.setIdentifier( this.getClass().getName() + " Specification" ); 121 122 Implementation implementation = new Implementation(); 123 implementation.setClassDeclaration( true ); 124 implementation.setClazz( this.getClass().getName() ); 125 implementation.setIdentifier( this.getClass().getName() + " Implementation" ); 126 implementation.setName( this.getClass().getName() + " Implementation" ); 127 128 module.getSpecifications().getSpecification().add( specification ); 129 module.getImplementations().getImplementation().add( implementation ); 130 modules.getModule().add( module ); 131 132 ModelHelper.setModules( model, modules ); 133 134 try 135 { 136 this.getModelProcessor().processModel( null, model ); 137 fail( "Expected NullPointerException not thrown." ); 138 } 139 catch ( final NullPointerException e ) 140 { 141 assertNotNull( e.getMessage() ); 142 System.out.println( e.toString() ); 143 } 144 145 try 146 { 147 this.getModelProcessor().processModel( context, null ); 148 fail( "Expected NullPointerException not thrown." ); 149 } 150 catch ( final NullPointerException e ) 151 { 152 assertNotNull( e.getMessage() ); 153 System.out.println( e.toString() ); 154 } 155 156 Model processed = this.getModelProcessor().processModel( context, model ); 157 assertNotNull( processed ); 158 159 modules = ModelHelper.getModules( processed ); 160 assertNotNull( modules ); 161 162 specification = modules.getSpecification( this.getClass().getName() + " Specification" ); 163 assertNotNull( specification ); 164 165 implementation = modules.getImplementation( this.getClass().getName() + " Implementation" ); 166 assertNotNull( implementation ); 167 168 SourceFileType ss = specification.getAnyObject( SourceFileType.class ); 169 assertNull( ss ); 170 171 SourceFileType is = implementation.getAnyObject( SourceFileType.class ); 172 assertNull( is ); 173 174 ss = new SourceFileType(); 175 ss.setIdentifier( this.getClass().getName() + " Specification" ); 176 177 is = new SourceFileType(); 178 is.setIdentifier( this.getClass().getName() + " Implementation" ); 179 180 specification.getAny().add( ss ); 181 implementation.getAny().add( is ); 182 183 processed = this.getModelProcessor().processModel( context, processed ); 184 assertNotNull( processed ); 185 186 modules = ModelHelper.getModules( processed ); 187 assertNotNull( modules ); 188 189 specification = modules.getSpecification( this.getClass().getName() + " Specification" ); 190 assertNotNull( specification ); 191 192 implementation = modules.getImplementation( this.getClass().getName() + " Implementation" ); 193 assertNotNull( implementation ); 194 195 ss = specification.getAnyObject( SourceFileType.class ); 196 assertNotNull( ss ); 197 assertNotNull( ss.getLocation() ); 198 assertNotNull( ss.getHeadComment() ); 199 200 is = implementation.getAnyObject( SourceFileType.class ); 201 assertNotNull( is ); 202 assertNotNull( is.getLocation() ); 203 assertNotNull( is.getHeadComment() ); 204 205 specification.getAny().clear(); 206 implementation.getAny().clear(); 207 208 SourceFilesType specificationSourceFiles = new SourceFilesType(); 209 ss = new SourceFileType(); 210 ss.setIdentifier( this.getClass().getSimpleName() ); 211 ss.setSourceSections( new SourceSectionsType() ); 212 specificationSourceFiles.getSourceFile().add( ss ); 213 specification.getAny().add( specificationSourceFiles ); 214 215 SourceFilesType implementationSourceFiles = new SourceFilesType(); 216 is = new SourceFileType(); 217 is.setIdentifier( this.getClass().getSimpleName() ); 218 is.setSourceSections( new SourceSectionsType() ); 219 implementationSourceFiles.getSourceFile().add( is ); 220 implementation.getAny().add( implementationSourceFiles ); 221 222 SourceSectionType sourceSection = new SourceSectionType(); 223 sourceSection.setName( "License Header" ); 224 225 ss.getSourceSections().getSourceSection().add( sourceSection ); 226 is.getSourceSections().getSourceSection().add( sourceSection ); 227 228 sourceSection = new SourceSectionType(); 229 sourceSection.setName( "Annotations" ); 230 231 ss.getSourceSections().getSourceSection().add( sourceSection ); 232 is.getSourceSections().getSourceSection().add( sourceSection ); 233 234 sourceSection = new SourceSectionType(); 235 sourceSection.setName( "Documentation" ); 236 237 ss.getSourceSections().getSourceSection().add( sourceSection ); 238 is.getSourceSections().getSourceSection().add( sourceSection ); 239 240 sourceSection = new SourceSectionType(); 241 sourceSection.setName( this.getClass().getSimpleName() ); 242 243 ss.getSourceSections().getSourceSection().add( sourceSection ); 244 is.getSourceSections().getSourceSection().add( sourceSection ); 245 246 sourceSection = new SourceSectionType(); 247 sourceSection.setName( "Constructors" ); 248 249 is.getSourceSections().getSourceSection().add( sourceSection ); 250 251 sourceSection = new SourceSectionType(); 252 sourceSection.setName( "Default Constructor" ); 253 254 is.getSourceSections().getSourceSection().add( sourceSection ); 255 256 sourceSection = new SourceSectionType(); 257 sourceSection.setName( "Dependencies" ); 258 259 is.getSourceSections().getSourceSection().add( sourceSection ); 260 261 sourceSection = new SourceSectionType(); 262 sourceSection.setName( "Properties" ); 263 264 is.getSourceSections().getSourceSection().add( sourceSection ); 265 266 sourceSection = new SourceSectionType(); 267 sourceSection.setName( "Messages" ); 268 269 is.getSourceSections().getSourceSection().add( sourceSection ); 270 271 processed = this.getModelProcessor().processModel( context, processed ); 272 assertNotNull( processed ); 273 274 modules = ModelHelper.getModules( processed ); 275 assertNotNull( modules ); 276 277 specification = modules.getSpecification( this.getClass().getName() + " Specification" ); 278 assertNotNull( specification ); 279 280 implementation = modules.getImplementation( this.getClass().getName() + " Implementation" ); 281 assertNotNull( implementation ); 282 283 specificationSourceFiles = specification.getAnyObject( SourceFilesType.class ); 284 assertNotNull( specificationSourceFiles ); 285 286 ss = specificationSourceFiles.getSourceFile( this.getClass().getSimpleName() ); 287 assertNotNull( ss ); 288 assertNotNull( ss.getHeadComment() ); 289 assertNotNull( ss.getLocation() ); 290 assertNotNull( ss.getTemplate() ); 291 292 implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class ); 293 assertNotNull( implementationSourceFiles ); 294 is = implementationSourceFiles.getSourceFile( this.getClass().getSimpleName() ); 295 assertNotNull( is ); 296 assertNotNull( is.getHeadComment() ); 297 assertNotNull( is.getLocation() ); 298 assertNotNull( is.getTemplate() ); 299 300 sourceSection = ss.getSourceSections().getSourceSection( "License Header" ); 301 assertNotNull( sourceSection ); 302 assertTrue( sourceSection.isOptional() ); 303 assertNotNull( sourceSection.getHeadTemplate() ); 304 305 sourceSection = is.getSourceSections().getSourceSection( "License Header" ); 306 assertNotNull( sourceSection ); 307 assertTrue( sourceSection.isOptional() ); 308 assertNotNull( sourceSection.getHeadTemplate() ); 309 310 sourceSection = ss.getSourceSections().getSourceSection( "Annotations" ); 311 assertNotNull( sourceSection ); 312 assertNotNull( sourceSection.getHeadTemplate() ); 313 314 sourceSection = is.getSourceSections().getSourceSection( "Annotations" ); 315 assertNotNull( sourceSection ); 316 assertNotNull( sourceSection.getHeadTemplate() ); 317 318 sourceSection = ss.getSourceSections().getSourceSection( "Documentation" ); 319 assertNotNull( sourceSection ); 320 assertTrue( sourceSection.isOptional() ); 321 assertNotNull( sourceSection.getHeadTemplate() ); 322 323 sourceSection = is.getSourceSections().getSourceSection( "Documentation" ); 324 assertNotNull( sourceSection ); 325 assertTrue( sourceSection.isOptional() ); 326 assertNotNull( sourceSection.getHeadTemplate() ); 327 328 sourceSection = ss.getSourceSections().getSourceSection( this.getClass().getSimpleName() ); 329 assertNotNull( sourceSection ); 330 assertTrue( sourceSection.isEditable() ); 331 assertEquals( 1, sourceSection.getIndentationLevel() ); 332 333 sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() ); 334 assertNotNull( sourceSection ); 335 assertTrue( sourceSection.isEditable() ); 336 assertEquals( 1, sourceSection.getIndentationLevel() ); 337 338 sourceSection = is.getSourceSections().getSourceSection( "Constructors" ); 339 assertNotNull( sourceSection ); 340 assertNotNull( sourceSection.getHeadTemplate() ); 341 assertNotNull( sourceSection.getTailTemplate() ); 342 assertEquals( 1, sourceSection.getIndentationLevel() ); 343 assertTrue( sourceSection.isOptional() ); 344 345 sourceSection = is.getSourceSections().getSourceSection( "Default Constructor" ); 346 assertNotNull( sourceSection ); 347 assertNotNull( sourceSection.getHeadTemplate() ); 348 assertEquals( 2, sourceSection.getIndentationLevel() ); 349 assertTrue( sourceSection.isEditable() ); 350 351 sourceSection = is.getSourceSections().getSourceSection( "Dependencies" ); 352 assertNotNull( sourceSection ); 353 assertNotNull( sourceSection.getHeadTemplate() ); 354 assertEquals( 1, sourceSection.getIndentationLevel() ); 355 assertTrue( sourceSection.isOptional() ); 356 357 sourceSection = is.getSourceSections().getSourceSection( "Properties" ); 358 assertNotNull( sourceSection ); 359 assertNotNull( sourceSection.getHeadTemplate() ); 360 assertEquals( 1, sourceSection.getIndentationLevel() ); 361 assertTrue( sourceSection.isOptional() ); 362 363 sourceSection = is.getSourceSections().getSourceSection( "Messages" ); 364 assertNotNull( sourceSection ); 365 assertNotNull( sourceSection.getHeadTemplate() ); 366 assertEquals( 1, sourceSection.getIndentationLevel() ); 367 assertTrue( sourceSection.isOptional() ); 368 369 sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() ); 370 assertNotNull( sourceSection ); 371 assertEquals( 1, sourceSection.getIndentationLevel() ); 372 assertTrue( sourceSection.isEditable() ); 373 } 374 375 @Test 376 public final void testDefaultEnabled() throws Exception 377 { 378 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" ); 379 ToolsModelProcessor.setDefaultEnabled( null ); 380 assertTrue( ToolsModelProcessor.isDefaultEnabled() ); 381 382 System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( false ) ); 383 ToolsModelProcessor.setDefaultEnabled( null ); 384 assertFalse( ToolsModelProcessor.isDefaultEnabled() ); 385 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" ); 386 ToolsModelProcessor.setDefaultEnabled( null ); 387 assertTrue( ToolsModelProcessor.isDefaultEnabled() ); 388 389 System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( true ) ); 390 ToolsModelProcessor.setDefaultEnabled( null ); 391 assertTrue( ToolsModelProcessor.isDefaultEnabled() ); 392 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" ); 393 ToolsModelProcessor.setDefaultEnabled( null ); 394 assertTrue( ToolsModelProcessor.isDefaultEnabled() ); 395 } 396 397 @Test 398 public final void testEnabled() throws Exception 399 { 400 final Model model = new Model(); 401 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 402 403 ToolsModelProcessor.setDefaultEnabled( null ); 404 this.getModelProcessor().setEnabled( null ); 405 assertTrue( this.getModelProcessor().isEnabled() ); 406 407 this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model ); 408 ToolsModelProcessor.setDefaultEnabled( false ); 409 this.getModelProcessor().setEnabled( null ); 410 assertFalse( this.getModelProcessor().isEnabled() ); 411 412 this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model ); 413 ToolsModelProcessor.setDefaultEnabled( null ); 414 this.getModelProcessor().setEnabled( null ); 415 } 416 417}