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.model.modlet.test;
32
33 import java.util.List;
34 import java.util.logging.Level;
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.JAXBElement;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.util.JAXBSource;
39 import org.jomc.model.ModelObject;
40 import org.jomc.model.Modules;
41 import org.jomc.model.modlet.DefaultModelValidator;
42 import org.jomc.model.modlet.ModelHelper;
43 import org.jomc.model.test.ModelValidationReportDetail;
44 import org.jomc.model.test.ModulesConstraintsTestType;
45 import org.jomc.model.test.SchemaConstraintsTestType;
46 import org.jomc.model.test.TestSuite;
47 import org.jomc.modlet.Model;
48 import org.jomc.modlet.ModelContext;
49 import org.jomc.modlet.ModelContextFactory;
50 import org.jomc.modlet.ModelException;
51 import org.jomc.modlet.ModelValidationReport;
52 import org.junit.Test;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertNotNull;
55 import static org.junit.Assert.assertTrue;
56 import static org.junit.Assert.fail;
57
58
59
60
61
62
63
64 public class DefaultModelValidatorTest
65 {
66
67
68 private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/model/modlet/test/";
69
70
71 private DefaultModelValidator defaultModelValidator;
72
73
74 private TestSuite testSuite;
75
76
77 private ModelContext modelContext;
78
79
80 public DefaultModelValidatorTest()
81 {
82 super();
83 }
84
85
86
87
88
89
90
91
92 public DefaultModelValidator getModelValidator()
93 {
94 if ( this.defaultModelValidator == null )
95 {
96 this.defaultModelValidator = this.newModelValidator();
97 }
98
99 return this.defaultModelValidator;
100 }
101
102
103
104
105
106
107
108
109 protected DefaultModelValidator newModelValidator()
110 {
111 return new DefaultModelValidator();
112 }
113
114
115
116
117
118
119
120
121 public ModelContext getModelContext()
122 {
123 if ( this.modelContext == null )
124 {
125 this.modelContext = this.newModelContext();
126 this.modelContext.getListeners().add( new ModelContext.Listener()
127 {
128
129 @Override
130 public void onLog( final Level level, String message, Throwable t )
131 {
132 super.onLog( level, message, t );
133 System.out.println( "[" + level.getLocalizedName() + "] " + message );
134 if ( t != null )
135 {
136 t.printStackTrace( System.out );
137 }
138 }
139
140 } );
141 }
142
143 return this.modelContext;
144 }
145
146
147
148
149
150
151 protected ModelContext newModelContext()
152 {
153 return ModelContextFactory.newInstance().newModelContext();
154 }
155
156
157
158
159
160
161
162
163 public TestSuite getTestSuite()
164 {
165 if ( this.testSuite == null )
166 {
167 this.testSuite = this.newTestSuite();
168 }
169
170 return this.testSuite;
171 }
172
173
174
175
176
177
178 protected TestSuite newTestSuite()
179 {
180 try
181 {
182 return ( (JAXBElement<TestSuite>) this.getModelContext().createUnmarshaller(
183 ModelObject.MODEL_PUBLIC_ID ).unmarshal( this.getClass().getResource(
184 ABSOLUTE_RESOURCE_NAME_PREFIX + "DefaultModelValidatorTestSuite.xml" ) ) ).getValue();
185
186 }
187 catch ( final JAXBException e )
188 {
189 throw new AssertionError( e );
190 }
191 catch ( final ModelException e )
192 {
193 throw new AssertionError( e );
194 }
195 }
196
197 @Test
198 public final void testIllegalArguments() throws Exception
199 {
200 try
201 {
202 this.getModelValidator().validateModel( this.getModelContext(), null );
203 fail( "Expected NullPointerException not thrown." );
204 }
205 catch ( final NullPointerException e )
206 {
207 assertNotNull( e.getMessage() );
208 System.out.println( e.toString() );
209 }
210
211 try
212 {
213 this.getModelValidator().validateModel( null, new Model() );
214 fail( "Expected NullPointerException not thrown." );
215 }
216 catch ( final NullPointerException e )
217 {
218 assertNotNull( e.getMessage() );
219 System.out.println( e.toString() );
220 }
221 }
222
223 @Test
224 public final void testLegalArguments() throws Exception
225 {
226 assertNotNull( this.getModelValidator().validateModel(
227 this.getModelContext(), this.getModelContext().findModel( ModelObject.MODEL_PUBLIC_ID ) ) );
228
229 }
230
231
232
233
234
235
236
237
238 public final void testSchemaConstraints( final String identifier ) throws Exception
239 {
240 SchemaConstraintsTestType test = null;
241
242 for ( SchemaConstraintsTestType candidate : this.getTestSuite().getSchemaConstraintsTest() )
243 {
244 if ( identifier.equals( candidate.getIdentifier() ) )
245 {
246 test = candidate;
247 break;
248 }
249 }
250
251 assertNotNull( "Schema constraints test '" + identifier + "' not found.", test );
252
253 final ModelContext context = this.getModelContext();
254 final JAXBContext jaxbContext = context.createContext( ModelObject.MODEL_PUBLIC_ID );
255
256 System.out.println( "SchemaConstraintsTest: " + test.getIdentifier() );
257
258 final JAXBElement<? extends ModelObject> modelObject =
259 (JAXBElement<? extends ModelObject>) test.getModelObject().getAny();
260
261 final JAXBSource source = new JAXBSource( jaxbContext, modelObject );
262 final ModelValidationReport report = context.validateModel( ModelObject.MODEL_PUBLIC_ID, source );
263
264 log( report );
265
266 assertEquals( "[" + test.getIdentifier() + "]", test.getModelObject().isValid(), report.isModelValid() );
267 }
268
269
270
271
272
273
274
275
276 public final void testModulesConstraints( final String identifier ) throws Exception
277 {
278 ModulesConstraintsTestType test = null;
279
280 for ( ModulesConstraintsTestType candidate : this.getTestSuite().getModulesConstraintsTest() )
281 {
282 if ( identifier.equals( candidate.getIdentifier() ) )
283 {
284 test = candidate;
285 break;
286 }
287 }
288
289 assertNotNull( "Modules constraints test '" + identifier + "' not found.", test );
290
291 final ModelContext context = this.getModelContext();
292 System.out.println( "ModulesConstraintsTest: " + test.getIdentifier() );
293
294 final JAXBElement<Modules> modules = (JAXBElement<Modules>) test.getModules().getAny();
295 final Model model = new Model();
296 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
297 ModelHelper.setModules( model, modules.getValue() );
298
299 final ModelValidationReport report = this.getModelValidator().validateModel( context, model );
300
301 log( report );
302
303 assertEquals( "[" + test.getIdentifier() + "] Unexpected model validity.",
304 test.getModules().isValid(), report.isModelValid() );
305
306 for ( ModelValidationReportDetail expectedDetail : test.getDetail() )
307 {
308 final List<ModelValidationReport.Detail> reportedDetails =
309 report.getDetails( expectedDetail.getIdentifier() );
310
311 assertTrue( "[" + test.getIdentifier() + "] Expected " + expectedDetail.getCount() + " "
312 + expectedDetail.getIdentifier() + " details but got " + reportedDetails.size()
313 + ".", expectedDetail.getCount() == reportedDetails.size() );
314
315 report.getDetails().removeAll( reportedDetails );
316 }
317
318 if ( !report.getDetails().isEmpty() )
319 {
320 for ( ModelValidationReport.Detail d : report.getDetails() )
321 {
322 fail( "[" + test.getIdentifier() + "] Unexpected " + d.getIdentifier() + " detail." );
323 }
324 }
325 }
326
327 private static void log( final ModelValidationReport report )
328 {
329 for ( ModelValidationReport.Detail d : report.getDetails() )
330 {
331 System.out.println( "\t" + d.toString() );
332 }
333 }
334
335 }