001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 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: ToolsModelProviderTest.java 5135 2016-04-08 13:53:07Z schulte $ 029 * 030 */ 031package org.jomc.tools.modlet.test; 032 033import java.util.HashSet; 034import java.util.Set; 035import java.util.logging.Level; 036import org.jomc.model.Implementation; 037import org.jomc.model.Implementations; 038import org.jomc.model.ModelObject; 039import org.jomc.model.Module; 040import org.jomc.model.Modules; 041import org.jomc.model.Specification; 042import org.jomc.model.SpecificationReference; 043import org.jomc.model.Specifications; 044import org.jomc.model.modlet.ModelHelper; 045import org.jomc.modlet.Model; 046import org.jomc.modlet.ModelContext; 047import org.jomc.modlet.ModelContextFactory; 048import org.jomc.tools.model.SourceFileType; 049import org.jomc.tools.model.SourceFilesType; 050import org.jomc.tools.model.SourceSectionType; 051import org.jomc.tools.model.SourceSectionsType; 052import org.jomc.tools.modlet.ToolsModelProvider; 053import org.junit.Test; 054import static org.junit.Assert.assertEquals; 055import static org.junit.Assert.assertFalse; 056import static org.junit.Assert.assertNotNull; 057import static org.junit.Assert.assertNull; 058import static org.junit.Assert.assertTrue; 059import static org.junit.Assert.fail; 060 061/** 062 * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProvider}. 063 * 064 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0 065 * @version $JOMC: ToolsModelProviderTest.java 5135 2016-04-08 13:53:07Z schulte $ 066 */ 067public class ToolsModelProviderTest 068{ 069 070 /** 071 * The {@code ToolsModelProvider} instance tests are performed with. 072 */ 073 private ToolsModelProvider toolsModelProvider; 074 075 /** 076 * Creates a new {@code ToolsModelProviderTest} instance. 077 */ 078 public ToolsModelProviderTest() 079 { 080 super(); 081 } 082 083 /** 084 * Gets the {@code ToolsModelProvider} instance tests are performed with. 085 * 086 * @return The {@code ToolsModelProvider} instance tests are performed with. 087 * 088 * @see #newModelProvider() 089 */ 090 public ToolsModelProvider getModelProvider() 091 { 092 if ( this.toolsModelProvider == null ) 093 { 094 this.toolsModelProvider = this.newModelProvider(); 095 } 096 097 return this.toolsModelProvider; 098 } 099 100 /** 101 * Creates a new {@code ToolsModelProvider} instance to test. 102 * 103 * @return A new {@code ToolsModelProvider} instance to test. 104 * 105 * @see #getModelProvider() 106 */ 107 protected ToolsModelProvider newModelProvider() 108 { 109 return new ToolsModelProvider(); 110 } 111 112 @Test 113 public final void testFindModel() throws Exception 114 { 115 final ModelContext context = ModelContextFactory.newInstance().newModelContext(); 116 context.setLogLevel( Level.ALL ); 117 context.getListeners().add( new ModelContext.Listener() 118 { 119 120 @Override 121 public void onLog( final Level level, final String message, final Throwable t ) 122 { 123 super.onLog( level, message, t ); 124 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 125 126 if ( t != null ) 127 { 128 t.printStackTrace( System.out ); 129 } 130 } 131 132 } ); 133 134 Model model = new Model(); 135 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 136 137 Modules modules = new Modules(); 138 Module module = new Module(); 139 module.setName( this.getClass().getName() ); 140 module.setSpecifications( new Specifications() ); 141 module.setImplementations( new Implementations() ); 142 143 Specification specification = new Specification(); 144 specification.setClassDeclaration( true ); 145 specification.setClazz( "specification.Documentation" ); 146 specification.setIdentifier( this.getClass().getName() + " Specification" ); 147 148 Implementation implementation = new Implementation(); 149 implementation.setClassDeclaration( true ); 150 implementation.setClazz( "implementation.Documentation" ); 151 implementation.setIdentifier( this.getClass().getName() + " Implementation" ); 152 implementation.setName( this.getClass().getName() + " Implementation" ); 153 implementation.setSpecifications( new Specifications() ); 154 implementation.getSpecifications().getReference().add( new SpecificationReference() ); 155 implementation.getSpecifications().getReference().get( 0 ).setIdentifier( specification.getIdentifier() ); 156 157 module.getSpecifications().getSpecification().add( specification ); 158 module.getImplementations().getImplementation().add( implementation ); 159 modules.getModule().add( module ); 160 161 ModelHelper.setModules( model, modules ); 162 163 try 164 { 165 this.getModelProvider().findModel( null, model ); 166 fail( "Expected NullPointerException not thrown." ); 167 } 168 catch ( final NullPointerException e ) 169 { 170 assertNotNull( e.getMessage() ); 171 System.out.println( e.toString() ); 172 } 173 174 try 175 { 176 this.getModelProvider().findModel( context, null ); 177 fail( "Expected NullPointerException not thrown." ); 178 } 179 catch ( final NullPointerException e ) 180 { 181 assertNotNull( e.getMessage() ); 182 System.out.println( e.toString() ); 183 } 184 185 Model found = this.getModelProvider().findModel( context, model ); 186 assertNotNull( found ); 187 188 modules = ModelHelper.getModules( found ); 189 assertNotNull( modules ); 190 191 specification = modules.getSpecification( this.getClass().getName() + " Specification" ); 192 assertNotNull( specification ); 193 194 implementation = modules.getImplementation( this.getClass().getName() + " Implementation" ); 195 assertNotNull( implementation ); 196 197 final SourceFilesType specificationSourceFiles = specification.getAnyObject( SourceFilesType.class ); 198 final SourceFilesType implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class ); 199 200 assertNotNull( specificationSourceFiles ); 201 assertNotNull( implementationSourceFiles ); 202 203 final SourceFileType specificationSourceFile = specificationSourceFiles.getSourceFile( "Default" ); 204 final SourceFileType implementationSourceFile = implementationSourceFiles.getSourceFile( "Default" ); 205 206 assertNotNull( specificationSourceFile ); 207 assertSectionNameUniqueness( specificationSourceFile.getSourceSections() ); 208 assertNotNull( implementationSourceFile ); 209 assertSectionNameUniqueness( implementationSourceFile.getSourceSections() ); 210 211 this.getModelProvider().setEnabled( false ); 212 213 found = this.getModelProvider().findModel( context, model ); 214 assertNull( found ); 215 216 this.getModelProvider().setEnabled( true ); 217 } 218 219 @Test 220 public final void testDefaultEnabled() throws Exception 221 { 222 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 223 ToolsModelProvider.setDefaultEnabled( null ); 224 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 225 226 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( false ) ); 227 ToolsModelProvider.setDefaultEnabled( null ); 228 assertFalse( ToolsModelProvider.isDefaultEnabled() ); 229 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 230 ToolsModelProvider.setDefaultEnabled( null ); 231 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 232 233 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( true ) ); 234 ToolsModelProvider.setDefaultEnabled( null ); 235 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 236 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 237 ToolsModelProvider.setDefaultEnabled( null ); 238 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 239 } 240 241 @Test 242 public final void testEnabled() throws Exception 243 { 244 final Model model = new Model(); 245 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 246 247 ToolsModelProvider.setDefaultEnabled( null ); 248 this.getModelProvider().setEnabled( null ); 249 assertTrue( this.getModelProvider().isEnabled() ); 250 251 this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model ); 252 ToolsModelProvider.setDefaultEnabled( false ); 253 this.getModelProvider().setEnabled( null ); 254 assertFalse( this.getModelProvider().isEnabled() ); 255 256 this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model ); 257 ToolsModelProvider.setDefaultEnabled( null ); 258 this.getModelProvider().setEnabled( null ); 259 } 260 261 @Test 262 public final void testDefaultHeadComment() throws Exception 263 { 264 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" ); 265 ToolsModelProvider.setDefaultHeadComment( null ); 266 assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() ); 267 268 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment", "/*" ); 269 ToolsModelProvider.setDefaultHeadComment( null ); 270 assertEquals( "/*", ToolsModelProvider.getDefaultHeadComment() ); 271 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" ); 272 ToolsModelProvider.setDefaultHeadComment( null ); 273 assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() ); 274 } 275 276 @Test 277 public final void testHeadComment() throws Exception 278 { 279 ToolsModelProvider.setDefaultHeadComment( null ); 280 this.getModelProvider().setHeadComment( null ); 281 assertEquals( "//", this.getModelProvider().getHeadComment() ); 282 283 ToolsModelProvider.setDefaultHeadComment( "/*" ); 284 this.getModelProvider().setHeadComment( null ); 285 assertEquals( "/*", this.getModelProvider().getHeadComment() ); 286 287 ToolsModelProvider.setDefaultHeadComment( null ); 288 this.getModelProvider().setHeadComment( null ); 289 assertEquals( "//", this.getModelProvider().getHeadComment() ); 290 } 291 292 @Test 293 public final void testDefaultTailComment() throws Exception 294 { 295 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" ); 296 ToolsModelProvider.setDefaultTailComment( null ); 297 assertNull( ToolsModelProvider.getDefaultTailComment() ); 298 299 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment", "*/" ); 300 ToolsModelProvider.setDefaultTailComment( null ); 301 assertEquals( "*/", ToolsModelProvider.getDefaultTailComment() ); 302 303 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" ); 304 ToolsModelProvider.setDefaultTailComment( null ); 305 assertNull( ToolsModelProvider.getDefaultTailComment() ); 306 } 307 308 @Test 309 public final void testTailComment() throws Exception 310 { 311 ToolsModelProvider.setDefaultTailComment( null ); 312 this.getModelProvider().setTailComment( null ); 313 assertNull( this.getModelProvider().getTailComment() ); 314 315 ToolsModelProvider.setDefaultTailComment( "*/" ); 316 this.getModelProvider().setTailComment( null ); 317 assertEquals( "*/", this.getModelProvider().getTailComment() ); 318 319 ToolsModelProvider.setDefaultTailComment( null ); 320 this.getModelProvider().setTailComment( null ); 321 assertNull( this.getModelProvider().getTailComment() ); 322 } 323 324 private static void assertSectionNameUniqueness( final SourceSectionsType sections ) 325 { 326 if ( sections != null ) 327 { 328 final Set<String> sectionNames = new HashSet<String>( sections.getSourceSection().size() ); 329 330 for ( int i = 0, s0 = sections.getSourceSection().size(); i < s0; i++ ) 331 { 332 final SourceSectionType section = sections.getSourceSection().get( i ); 333 assertTrue( "Multiple '" + section.getName() + "' sections.", sectionNames.add( section.getName() ) ); 334 assertSectionNameUniqueness( section.getSourceSections() ); 335 } 336 } 337 } 338 339}