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: ToolsModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 029 * 030 */ 031 package org.jomc.tools.modlet.test; 032 033 import org.jomc.model.Implementation; 034 import org.jomc.model.Implementations; 035 import org.jomc.model.ModelObject; 036 import org.jomc.model.Module; 037 import org.jomc.model.Modules; 038 import org.jomc.model.Specification; 039 import org.jomc.model.Specifications; 040 import org.jomc.model.modlet.ModelHelper; 041 import org.jomc.modlet.Model; 042 import org.jomc.modlet.ModelContext; 043 import org.jomc.modlet.ModelContextFactory; 044 import org.jomc.tools.model.SourceFilesType; 045 import org.jomc.tools.modlet.ToolsModelProvider; 046 import org.junit.Test; 047 import static org.junit.Assert.assertFalse; 048 import static org.junit.Assert.assertNotNull; 049 import static org.junit.Assert.assertNull; 050 import static org.junit.Assert.assertTrue; 051 import static org.junit.Assert.fail; 052 053 /** 054 * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProvider}. 055 * 056 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 057 * @version $JOMC: ToolsModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 058 */ 059 public class ToolsModelProviderTest 060 { 061 062 /** The {@code ToolsModelProvider} instance tests are performed with. */ 063 private ToolsModelProvider toolsModelProvider; 064 065 /** Creates a new {@code ToolsModelProviderTest} instance. */ 066 public ToolsModelProviderTest() 067 { 068 super(); 069 } 070 071 /** 072 * Gets the {@code ToolsModelProvider} instance tests are performed with. 073 * 074 * @return The {@code ToolsModelProvider} instance tests are performed with. 075 * 076 * @see #newModelProvider() 077 */ 078 public ToolsModelProvider getModelProvider() 079 { 080 if ( this.toolsModelProvider == null ) 081 { 082 this.toolsModelProvider = this.newModelProvider(); 083 } 084 085 return this.toolsModelProvider; 086 } 087 088 /** 089 * Creates a new {@code ToolsModelProvider} instance to test. 090 * 091 * @return A new {@code ToolsModelProvider} instance to test. 092 * 093 * @see #getModelProvider() 094 */ 095 protected ToolsModelProvider newModelProvider() 096 { 097 return new ToolsModelProvider(); 098 } 099 100 @Test 101 public final void testFindModel() throws Exception 102 { 103 final ModelContext context = ModelContextFactory.newInstance().newModelContext(); 104 Model model = new Model(); 105 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 106 107 Modules modules = new Modules(); 108 Module module = new Module(); 109 module.setName( this.getClass().getName() ); 110 module.setSpecifications( new Specifications() ); 111 module.setImplementations( new Implementations() ); 112 113 Specification specification = new Specification(); 114 specification.setClassDeclaration( true ); 115 specification.setClazz( this.getClass().getName() ); 116 specification.setIdentifier( this.getClass().getName() + " Specification" ); 117 118 Implementation implementation = new Implementation(); 119 implementation.setClassDeclaration( true ); 120 implementation.setClazz( this.getClass().getName() ); 121 implementation.setIdentifier( this.getClass().getName() + " Implementation" ); 122 implementation.setName( this.getClass().getName() + " Implementation" ); 123 124 module.getSpecifications().getSpecification().add( specification ); 125 module.getImplementations().getImplementation().add( implementation ); 126 modules.getModule().add( module ); 127 128 ModelHelper.setModules( model, modules ); 129 130 try 131 { 132 this.getModelProvider().findModel( null, model ); 133 fail( "Expected NullPointerException not thrown." ); 134 } 135 catch ( final NullPointerException e ) 136 { 137 assertNotNull( e.getMessage() ); 138 System.out.println( e.toString() ); 139 } 140 141 try 142 { 143 this.getModelProvider().findModel( context, null ); 144 fail( "Expected NullPointerException not thrown." ); 145 } 146 catch ( final NullPointerException e ) 147 { 148 assertNotNull( e.getMessage() ); 149 System.out.println( e.toString() ); 150 } 151 152 Model found = this.getModelProvider().findModel( context, model ); 153 assertNotNull( found ); 154 155 modules = ModelHelper.getModules( found ); 156 assertNotNull( modules ); 157 158 specification = modules.getSpecification( this.getClass().getName() + " Specification" ); 159 assertNotNull( specification ); 160 161 implementation = modules.getImplementation( this.getClass().getName() + " Implementation" ); 162 assertNotNull( implementation ); 163 164 assertNotNull( specification.getAnyObject( SourceFilesType.class ) ); 165 assertNotNull( implementation.getAnyObject( SourceFilesType.class ) ); 166 167 this.getModelProvider().setEnabled( false ); 168 169 found = this.getModelProvider().findModel( context, model ); 170 assertNull( found ); 171 172 this.getModelProvider().setEnabled( true ); 173 } 174 175 @Test 176 public final void testDefaultEnabled() throws Exception 177 { 178 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 179 ToolsModelProvider.setDefaultEnabled( null ); 180 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 181 182 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( false ) ); 183 ToolsModelProvider.setDefaultEnabled( null ); 184 assertFalse( ToolsModelProvider.isDefaultEnabled() ); 185 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 186 ToolsModelProvider.setDefaultEnabled( null ); 187 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 188 189 System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( true ) ); 190 ToolsModelProvider.setDefaultEnabled( null ); 191 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 192 System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" ); 193 ToolsModelProvider.setDefaultEnabled( null ); 194 assertTrue( ToolsModelProvider.isDefaultEnabled() ); 195 } 196 197 @Test 198 public final void testEnabled() throws Exception 199 { 200 final Model model = new Model(); 201 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID ); 202 203 ToolsModelProvider.setDefaultEnabled( null ); 204 this.getModelProvider().setEnabled( null ); 205 assertTrue( this.getModelProvider().isEnabled() ); 206 207 this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model ); 208 ToolsModelProvider.setDefaultEnabled( false ); 209 this.getModelProvider().setEnabled( null ); 210 assertFalse( this.getModelProvider().isEnabled() ); 211 212 this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model ); 213 ToolsModelProvider.setDefaultEnabled( null ); 214 this.getModelProvider().setEnabled( null ); 215 } 216 217 }