1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
45
46
47
48
49 public class ModelContextFactoryTest
50 {
51
52
53 private static final String DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME = "org.jomc.modlet.DefaultModelContextFactory";
54
55
56 private static final String MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY =
57 "org.jomc.modlet.ModelContextFactory";
58
59
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 }