View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2012-22
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: ModelContextFactoryTest.java 4209 2012-01-27 19:51:47Z schulte2005 $
29   *
30   */
31  package org.jomc.modlet.test;
32  
33  import org.jomc.modlet.ModelContextFactory;
34  import org.jomc.modlet.ModelContextFactoryError;
35  import org.jomc.modlet.test.support.ClassCastExceptionModelContextFactory;
36  import org.jomc.modlet.test.support.IllegalAccessExceptionModelContextFactory;
37  import org.jomc.modlet.test.support.InstantiationExceptionModelContextFactory;
38  import org.junit.Test;
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertNotNull;
41  import static org.junit.Assert.fail;
42  
43  /**
44   * Test cases for class {@code org.jomc.modlet.ModelContextFactory}.
45   *
46   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
47   * @version $JOMC: ModelContextFactoryTest.java 4209 2012-01-27 19:51:47Z schulte2005 $
48   */
49  public class ModelContextFactoryTest
50  {
51  
52      /** Constant for the name of the default {@code ModelContextFactory} implementation. */
53      private static final String DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME = "org.jomc.modlet.DefaultModelContextFactory";
54  
55      /** Constant for the name of the system property controlling {@code ModelContextFactory} implementations. */
56      private static final String MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY =
57          "org.jomc.modlet.ModelContextFactory";
58  
59      /** Creates a new {@code ModelContextFactoryTest} instance. */
60      public ModelContextFactoryTest()
61      {
62          super();
63      }
64  
65      @Test
66      public final void testModelContextFactoryDefaultInstance() throws Exception
67      {
68          assertNotNull( ModelContextFactory.newInstance() );
69          assertEquals( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME,
70                        ModelContextFactory.newInstance().getClass().getName() );
71  
72          assertNotNull( ModelContextFactory.newInstance( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME ) );
73  
74          assertEquals(
75              DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME,
76              ModelContextFactory.newInstance( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME ).getClass().getName() );
77  
78      }
79  
80      @Test
81      public final void testModelContextFactoryClassNotFound() throws Exception
82      {
83          try
84          {
85              System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY, "DOES_NOT_EXIST" );
86              ModelContextFactory.newInstance();
87              fail( "Expected 'ModelContextFactoryError' not thrown." );
88          }
89          catch ( final ModelContextFactoryError e )
90          {
91              assertNotNull( e.getMessage() );
92              System.out.println( e.toString() );
93          }
94          finally
95          {
96              System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY );
97          }
98  
99          try
100         {
101             ModelContextFactory.newInstance( "DOES_NOT_EXIST" );
102             fail( "Expected 'ModelContextFactoryError' not thrown." );
103         }
104         catch ( final ModelContextFactoryError e )
105         {
106             assertNotNull( e.getMessage() );
107             System.out.println( e.toString() );
108         }
109     }
110 
111     @Test
112     public final void testModelContextFactoryIllegalClass() throws Exception
113     {
114         try
115         {
116             System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY,
117                                 ClassCastExceptionModelContextFactory.class.getName() );
118 
119             ModelContextFactory.newInstance();
120             fail( "Expected 'ModelContextFactoryError' not thrown." );
121         }
122         catch ( final ModelContextFactoryError e )
123         {
124             assertNotNull( e.getMessage() );
125             System.out.println( e.toString() );
126         }
127         finally
128         {
129             System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY );
130         }
131 
132         try
133         {
134             ModelContextFactory.newInstance( ClassCastExceptionModelContextFactory.class.getName() );
135             fail( "Expected 'ModelContextFactoryError' not thrown." );
136         }
137         catch ( final ModelContextFactoryError e )
138         {
139             assertNotNull( e.getMessage() );
140             System.out.println( e.toString() );
141         }
142     }
143 
144     @Test
145     public final void testModelContextFactoryInstantiationException() throws Exception
146     {
147         try
148         {
149             System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY,
150                                 InstantiationExceptionModelContextFactory.class.getName() );
151 
152             ModelContextFactory.newInstance();
153             fail( "Expected 'ModelContextFactoryError' not thrown." );
154         }
155         catch ( final ModelContextFactoryError e )
156         {
157             assertNotNull( e.getMessage() );
158             System.out.println( e.toString() );
159         }
160         finally
161         {
162             System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY );
163         }
164 
165         try
166         {
167             ModelContextFactory.newInstance( InstantiationExceptionModelContextFactory.class.getName() );
168             fail( "Expected 'ModelContextFactoryError' not thrown." );
169         }
170         catch ( final ModelContextFactoryError e )
171         {
172             assertNotNull( e.getMessage() );
173             System.out.println( e.toString() );
174         }
175     }
176 
177     @Test
178     public final void testModelContextFactoryIllegalAccessException() throws Exception
179     {
180         try
181         {
182             System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY,
183                                 IllegalAccessExceptionModelContextFactory.class.getName() );
184 
185             ModelContextFactory.newInstance();
186             fail( "Expected 'ModelContextFactoryError' not thrown." );
187         }
188         catch ( final ModelContextFactoryError e )
189         {
190             assertNotNull( e.getMessage() );
191             System.out.println( e.toString() );
192         }
193         finally
194         {
195             System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY );
196         }
197 
198         try
199         {
200             ModelContextFactory.newInstance( IllegalAccessExceptionModelContextFactory.class.getName() );
201             fail( "Expected 'ModelContextFactoryError' not thrown." );
202         }
203         catch ( final ModelContextFactoryError e )
204         {
205             assertNotNull( e.getMessage() );
206             System.out.println( e.toString() );
207         }
208     }
209 
210     @Test
211     public final void testModelContextNotNull() throws Exception
212     {
213         assertNotNull( ModelContextFactory.newInstance().newModelContext() );
214         assertNotNull( ModelContextFactory.newInstance().newModelContext( null ) );
215         assertNotNull( ModelContextFactory.newInstance().newModelContext( this.getClass().getClassLoader() ) );
216     }
217 
218 }