View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: ToolsModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
29   *
30   */
31  package org.jomc.tools.modlet.test;
32  
33  import org.jomc.model.Implementation;
34  import org.jomc.model.Implementations;
35  import org.jomc.model.ModelObject;
36  import org.jomc.model.Module;
37  import org.jomc.model.Modules;
38  import org.jomc.model.Specification;
39  import org.jomc.model.Specifications;
40  import org.jomc.model.modlet.ModelHelper;
41  import org.jomc.modlet.Model;
42  import org.jomc.modlet.ModelContext;
43  import org.jomc.modlet.ModelContextFactory;
44  import org.jomc.tools.model.SourceFilesType;
45  import org.jomc.tools.modlet.ToolsModelProvider;
46  import org.junit.Test;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  import static org.junit.Assert.assertTrue;
51  import static org.junit.Assert.fail;
52  
53  /**
54   * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProvider}.
55   *
56   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
57   * @version $JOMC: ToolsModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
58   */
59  public class ToolsModelProviderTest
60  {
61  
62      /** The {@code ToolsModelProvider} instance tests are performed with. */
63      private ToolsModelProvider toolsModelProvider;
64  
65      /** Creates a new {@code ToolsModelProviderTest} instance. */
66      public ToolsModelProviderTest()
67      {
68          super();
69      }
70  
71      /**
72       * Gets the {@code ToolsModelProvider} instance tests are performed with.
73       *
74       * @return The {@code ToolsModelProvider} instance tests are performed with.
75       *
76       * @see #newModelProvider()
77       */
78      public ToolsModelProvider getModelProvider()
79      {
80          if ( this.toolsModelProvider == null )
81          {
82              this.toolsModelProvider = this.newModelProvider();
83          }
84  
85          return this.toolsModelProvider;
86      }
87  
88      /**
89       * Creates a new {@code ToolsModelProvider} instance to test.
90       *
91       * @return A new {@code ToolsModelProvider} instance to test.
92       *
93       * @see #getModelProvider()
94       */
95      protected ToolsModelProvider newModelProvider()
96      {
97          return new ToolsModelProvider();
98      }
99  
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 }