1 /*
2 * Copyright The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE.txt file.
7 */
8 package org.codehaus.spice.configkit;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.net.URLClassLoader;
14 import java.util.ArrayList;
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import junit.framework.TestCase;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Node;
20 import org.xml.sax.ContentHandler;
21 import org.xml.sax.EntityResolver;
22 import org.xml.sax.ErrorHandler;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.SAXParseException;
26 import org.xml.sax.helpers.DefaultHandler;
27
28 /***
29 * Basic unit tests for the info objects.
30 *
31 * @author Peter Donald
32 */
33 public final class ConfigValidatorTestCase
34 extends TestCase
35 implements ErrorHandler
36 {
37 private static final boolean DEBUG = false;
38 private static final ContentHandler HANDLER = new NoopContentHandler();
39
40 public void testValidationIssue()
41 throws Exception
42 {
43 final SAXParseException spe = new SAXParseException( "", null );
44
45 final ValidationIssue issue1 = new ValidationIssue(
46 ValidationIssue.TYPE_WARNING, spe );
47 assertTrue( "issue3.isWarning", issue1.isWarning() );
48 assertTrue( "!issue3.isError()", !issue1.isError() );
49 assertTrue( "!issue3.isFatalError()", !issue1.isFatalError() );
50 assertNotNull( "issue3.exception not null", issue1.getException() );
51
52 final ValidationIssue issue2 = new ValidationIssue(
53 ValidationIssue.TYPE_ERROR, spe );
54 assertTrue( "issue3.isWarning", !issue2.isWarning() );
55 assertTrue( "!issue3.isError()", issue2.isError() );
56 assertTrue( "!issue3.isFatalError()", !issue2.isFatalError() );
57 assertNotNull( "issue3.exception not null", issue2.getException() );
58
59 final ValidationIssue issue3 = new ValidationIssue(
60 ValidationIssue.TYPE_FATAL_ERROR, spe );
61 assertTrue( "issue3.isWarning", !issue3.isWarning() );
62 assertTrue( "!issue3.isError()", !issue3.isError() );
63 assertTrue( "!issue3.isFatalError()", issue3.isFatalError() );
64 assertNotNull( "issue3.exception not null", issue3.getException() );
65 }
66
67 public void testIssueColector()
68 throws Exception
69 {
70 final SAXParseException spe = new SAXParseException( "", null );
71
72 final ArrayList issues = new ArrayList();
73 final IssueCollector collector = new IssueCollector( issues );
74 collector.warning( spe );
75 collector.error( spe );
76 collector.fatalError( spe );
77
78 assertEquals( "issues.length", 3, issues.size() );
79
80 final ValidationIssue issue1 = (ValidationIssue)issues.get( 0 );
81 final ValidationIssue issue2 = (ValidationIssue)issues.get( 1 );
82 final ValidationIssue issue3 = (ValidationIssue)issues.get( 2 );
83
84 assertTrue( "issue1.isWarning()", issue1.isWarning() );
85 assertTrue( "!issue1.isError()", !issue1.isError() );
86 assertTrue( "!issue1.isFatalError()", !issue1.isFatalError() );
87 assertEquals( "issue1.exception()", spe, issue1.getException() );
88
89 assertTrue( "issue2.isWarning()", !issue2.isWarning() );
90 assertTrue( "!issue2.isError()", issue2.isError() );
91 assertTrue( "!issue2.isFatalError()", !issue2.isFatalError() );
92 assertEquals( "issue2.exception()", spe, issue2.getException() );
93
94 assertTrue( "issue3.isWarning()", !issue3.isWarning() );
95 assertTrue( "!issue3.isError()", !issue3.isError() );
96 assertTrue( "!issue3.isFatalError()", issue3.isFatalError() );
97 assertEquals( "issue3.exception()", spe, issue3.getException() );
98 }
99
100 public void testNullStreamForSchema()
101 throws Exception
102 {
103 try
104 {
105 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
106 (InputStream)null,
107 new NoopEntityResolver() );
108 fail( "Expected Null pointer due to null schema" );
109 }
110 catch( final NullPointerException npe )
111 {
112 assertEquals( npe.getMessage(), "inputStream" );
113 }
114 }
115
116 public void testNullCTorInIssue()
117 throws Exception
118 {
119 try
120 {
121 new ValidationIssue( ValidationIssue.TYPE_WARNING, null );
122 fail( "Expected Null pointer due to null schema" );
123 }
124 catch( final NullPointerException npe )
125 {
126 assertEquals( npe.getMessage(), "exception" );
127 }
128 }
129
130 public void testNullCTorInIssueColector()
131 throws Exception
132 {
133 try
134 {
135 new IssueCollector( null );
136 fail( "Expected Null pointer due to null schema" );
137 }
138 catch( final NullPointerException npe )
139 {
140 assertEquals( npe.getMessage(), "issues" );
141 }
142 }
143
144 public void testNullCTorInResult()
145 throws Exception
146 {
147 try
148 {
149 new ValidationResult( null, null );
150 fail( "Expected Null pointer due to null issues" );
151 }
152 catch( final NullPointerException npe )
153 {
154 assertEquals( npe.getMessage(), "issues" );
155 }
156
157 try
158 {
159 final ValidationIssue issue = new ValidationIssue(
160 ValidationIssue.TYPE_WARNING,
161 new SAXParseException( "test", null ) );
162 final ValidationIssue[] issues = new ValidationIssue[]{issue,
163 null};
164 new ValidationResult( null, issues );
165 fail( "Expected Null pointer due to null issues" );
166 }
167 catch( final NullPointerException npe )
168 {
169 assertEquals( npe.getMessage(), "issues[1]" );
170 }
171 }
172
173 public void testNullCTor()
174 throws Exception
175 {
176 try
177 {
178 new ConfigValidator( null, null );
179 fail( "Expected Null pointer due to null schema" );
180 }
181 catch( final NullPointerException npe )
182 {
183 assertEquals( npe.getMessage(), "schema" );
184 }
185 }
186
187 public void testNullHandler()
188 throws Exception
189 {
190 try
191 {
192 final ConfigValidator validator =
193 ConfigValidatorFactory.create( TestData.SCHEMA_PUBLIC_ID,
194 TestData.SCHEMA_SYSTEM_ID,
195 createClassLoader() );
196 final InputStream inputStream = getClass()
197 .getClassLoader()
198 .getResourceAsStream( TestData.SCHEMA );
199 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR,
200 inputStream );
201 validator.validate( inputStream, null, null );
202 fail( "Expected Null pointer due to null contentHandler" );
203 }
204 catch( final NullPointerException npe )
205 {
206 assertEquals( npe.getMessage(), "contentHandler" );
207 }
208 }
209
210 public void testNullSource()
211 throws Exception
212 {
213 try
214 {
215 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
216 (InputSource)null );
217 fail( "Expected Null pointer due to null source" );
218 }
219 catch( final NullPointerException npe )
220 {
221 assertEquals( npe.getMessage(), "inputSource" );
222 }
223 }
224
225 public void testNullStream()
226 throws Exception
227 {
228 try
229 {
230 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
231 (InputStream)null );
232 fail( "Expected Null pointer due to null stream" );
233 }
234 catch( final NullPointerException npe )
235 {
236 assertEquals( npe.getMessage(), "inputStream" );
237 }
238 }
239
240 public void testNullIDs()
241 throws Exception
242 {
243 try
244 {
245 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
246 null,
247 null,
248 createClassLoader() );
249 fail( "Expected Null pointer due to null schema" );
250 }
251 catch( final NullPointerException npe )
252 {
253 assertEquals( npe.getMessage(), "publicID" );
254 }
255 }
256
257 public void testNullClassLoader()
258 throws Exception
259 {
260 try
261 {
262 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
263 TestData.SCHEMA_PUBLIC_ID,
264 null,
265 null );
266 fail( "Expected Null pointer due to null schema" );
267 }
268 catch( final NullPointerException npe )
269 {
270 assertEquals( npe.getMessage(), "classLoader" );
271 }
272 }
273
274 public void testNoExist()
275 throws Exception
276 {
277 try
278 {
279 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
280 "noexist",
281 null,
282 createClassLoader() );
283 fail( "Expected Null pointer due to null schema" );
284 }
285 catch( final Exception npe )
286 {
287 assertTrue( -1 != npe.getMessage().indexOf( "noexist" ) );
288 }
289 }
290
291 public void testLoadViaPublicID()
292 throws Exception
293 {
294 final ConfigValidator configValidator =
295 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
296 TestData.SCHEMA_PUBLIC_ID,
297 null,
298 createClassLoader() );
299 doValidate( configValidator );
300 }
301
302 public void testLoadViaPublicIDNoSchema()
303 throws Exception
304 {
305 final ConfigValidator configValidator =
306 ConfigValidatorFactory.create( TestData.SCHEMA_PUBLIC_ID,
307 null,
308 createClassLoader() );
309 doValidate( configValidator );
310 }
311
312 public void testLoadViaSystemID()
313 throws Exception
314 {
315 final ConfigValidator configValidator =
316 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
317 null,
318 TestData.SCHEMA_SYSTEM_ID,
319 createClassLoader() );
320 doValidate( configValidator );
321 }
322
323 public void testLoadViaSystemIDNoSchema()
324 throws Exception
325 {
326 final ConfigValidator configValidator =
327 ConfigValidatorFactory.create( null,
328 TestData.SCHEMA_SYSTEM_ID,
329 createClassLoader() );
330 doValidate( configValidator );
331 }
332
333 public void testLoadViaInputStream()
334 throws Exception
335 {
336 final InputStream inputStream = getClass()
337 .getClassLoader()
338 .getResourceAsStream( TestData.SCHEMA );
339 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR,
340 inputStream );
341 final ConfigValidator configValidator =
342 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
343 new InputSource( inputStream ) );
344 doValidate( configValidator );
345 }
346
347 public void testLoadViaInputStreamNoSchema()
348 throws Exception
349 {
350 final InputStream inputStream = getClass()
351 .getClassLoader()
352 .getResourceAsStream( TestData.SCHEMA );
353 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR,
354 inputStream );
355 final ConfigValidator configValidator =
356 ConfigValidatorFactory.create( new InputSource( inputStream ) );
357 doValidate( configValidator );
358 }
359
360 public void testLoadSchemaWithResolver()
361 throws Exception
362 {
363 final InputStream inputStream = getClass()
364 .getClassLoader()
365 .getResourceAsStream( TestData.SCHEMA );
366 assertNotNull( "ResourcePresent: " + TestData.SCHEMA, inputStream );
367 final EntityResolver resolver = ResolverFactory.createResolver(
368 createClassLoader() );
369 final ConfigValidator validator =
370 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
371 inputStream,
372 resolver );
373 doValidate( validator );
374 }
375
376 public void testLoadDTD()
377 throws Exception
378 {
379 final InputStream inputStream = getClass()
380 .getClassLoader()
381 .getResourceAsStream( TestData.DTD );
382 assertNotNull( "ResourcePresent: " + TestData.DTD, inputStream );
383 final EntityResolver resolver = ResolverFactory.createResolver(
384 createClassLoader() );
385 final InputSource inputSource = new InputSource( inputStream );
386 inputSource.setPublicId( TestData.PUBLIC_ID );
387 inputSource.setSystemId( TestData.SYSTEM_ID );
388 final ConfigValidator validator =
389 ConfigValidatorFactory.create( inputSource, resolver );
390 final ClassLoader classLoader = getClass().getClassLoader();
391
392 final InputStream dataStream = classLoader.getResourceAsStream(
393 TestData.ASSEMBLY_DATA );
394 final ValidationResult result = validator.validate( dataStream );
395 verifyResult( result, true );
396 }
397
398 public void testLoadViaInputSource()
399 throws Exception
400 {
401 final InputStream inputStream = getClass()
402 .getClassLoader()
403 .getResourceAsStream( TestData.SCHEMA );
404 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR,
405 inputStream );
406 final ConfigValidator configValidator =
407 ConfigValidatorFactory.create( ConfigValidatorFactory.RELAX_NG,
408 inputStream );
409 doValidate( configValidator );
410 }
411
412 public void testLoadViaInputSourceNoSchema()
413 throws Exception
414 {
415 final InputStream inputStream = getClass()
416 .getClassLoader()
417 .getResourceAsStream( TestData.SCHEMA );
418 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR,
419 inputStream );
420 final ConfigValidator configValidator =
421 ConfigValidatorFactory.create( inputStream );
422 doValidate( configValidator );
423 }
424
425 private void doValidate( final ConfigValidator configValidator )
426 throws Exception
427 {
428 ValidationResult result = null;
429 InputStream dataStream = null;
430 final ClassLoader classLoader = getClass().getClassLoader();
431
432 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
433 configValidator.validate( dataStream, this );
434
435 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
436 configValidator.validate( dataStream, HANDLER, this );
437
438 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
439 result = configValidator.validate( dataStream );
440 verifyResult( result, true );
441
442 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
443 result = configValidator.validate( dataStream, HANDLER );
444 verifyResult( result, true );
445
446 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
447 configValidator.validate( new InputSource( dataStream ), this );
448
449 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
450 configValidator.validate( new InputSource( dataStream ),
451 HANDLER,
452 this );
453
454 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
455 result = configValidator.validate( new InputSource( dataStream ) );
456 verifyResult( result, true );
457
458 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA );
459 result =
460 configValidator.validate( new InputSource( dataStream ), HANDLER );
461 verifyResult( result, true );
462
463 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
464 try
465 {
466 configValidator.validate( dataStream, this );
467 fail( "Expected fail to not being xml" );
468 }
469 catch( ValidateException e )
470 {
471 }
472
473 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
474 try
475 {
476 configValidator.validate( dataStream, HANDLER, this );
477 fail( "Expected fail to not being xml" );
478 }
479 catch( ValidateException e )
480 {
481 }
482
483 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
484 result = configValidator.validate( dataStream );
485 verifyResult( result, false );
486
487 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
488 result = configValidator.validate( dataStream, HANDLER );
489 verifyResult( result, false );
490
491 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
492 try
493 {
494 configValidator.validate( new InputSource( dataStream ), this );
495 fail( "Expected fail to not being xml" );
496 }
497 catch( ValidateException e )
498 {
499 }
500
501 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
502 try
503 {
504 configValidator.validate( new InputSource( dataStream ),
505 HANDLER,
506 this );
507 fail( "Expected fail to not being xml" );
508 }
509 catch( ValidateException e )
510 {
511 }
512
513 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
514 result = configValidator.validate( new InputSource( dataStream ) );
515 verifyResult( result, false );
516
517 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA2 );
518 result =
519 configValidator.validate( new InputSource( dataStream ), HANDLER );
520 verifyResult( result, false );
521
522 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
523 try
524 {
525 configValidator.validate( dataStream, this );
526 fail( "Expected fail to not conforming" );
527 }
528 catch( ValidateException e )
529 {
530 }
531
532 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
533 try
534 {
535 configValidator.validate( dataStream, HANDLER, this );
536 fail( "Expected fail to not conforming" );
537 }
538 catch( ValidateException e )
539 {
540 }
541
542 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
543 result = configValidator.validate( dataStream );
544 verifyResult( result, false );
545
546 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
547 result = configValidator.validate( dataStream, HANDLER );
548 verifyResult( result, false );
549
550 try
551 {
552 configValidator.validate( new InputSource( dataStream ), this );
553 fail( "Expected fail to not conforming" );
554 }
555 catch( ValidateException e )
556 {
557 }
558
559 try
560 {
561 configValidator.validate( new InputSource( dataStream ),
562 HANDLER,
563 this );
564 fail( "Expected fail to not conforming" );
565 }
566 catch( ValidateException e )
567 {
568 }
569
570 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
571 result = configValidator.validate( new InputSource( dataStream ) );
572 verifyResult( result, false );
573
574 dataStream = classLoader.getResourceAsStream( TestData.XML_DATA3 );
575 result =
576 configValidator.validate( new InputSource( dataStream ), HANDLER );
577 verifyResult( result, false );
578
579 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
580 final DocumentBuilder builder = factory.newDocumentBuilder();
581 final Document document = builder.newDocument();
582
583 try
584 {
585 configValidator.validate( document.createElement( "root" ), this );
586 fail( "Expected fail to not conforming" );
587 }
588 catch( ValidateException e )
589 {
590 }
591 result = configValidator.validate( document.createElement( "root" ) );
592 verifyResult( result, false );
593
594 try
595 {
596 configValidator.validate( (Node)null, this );
597 fail( "Expected Null pointer due to null input" );
598 }
599 catch( final NullPointerException npe )
600 {
601 assertEquals( npe.getMessage(), "node" );
602 }
603
604 try
605 {
606 configValidator.validate( (Node)null );
607 fail( "Expected Null pointer due to null input" );
608 }
609 catch( final NullPointerException npe )
610 {
611 assertEquals( npe.getMessage(), "node" );
612 }
613
614 try
615 {
616 configValidator.validate( (InputStream)null, HANDLER, this );
617 fail( "Expected Null pointer due to null input" );
618 }
619 catch( final NullPointerException npe )
620 {
621 assertEquals( npe.getMessage(), "inputStream" );
622 }
623
624 try
625 {
626 configValidator.validate( (InputStream)null, this );
627 fail( "Expected Null pointer due to null input" );
628 }
629 catch( final NullPointerException npe )
630 {
631 assertEquals( npe.getMessage(), "inputStream" );
632 }
633
634 try
635 {
636 configValidator.validate( (InputStream)null );
637 fail( "Expected Null pointer due to null input" );
638 }
639 catch( final NullPointerException npe )
640 {
641 assertEquals( npe.getMessage(), "inputStream" );
642 }
643
644 try
645 {
646 configValidator.validate( (InputStream)null, HANDLER );
647 fail( "Expected Null pointer due to null input" );
648 }
649 catch( final NullPointerException npe )
650 {
651 assertEquals( npe.getMessage(), "inputStream" );
652 }
653
654 try
655 {
656 configValidator.validate( (InputSource)null, this );
657 fail( "Expected Null pointer due to null input" );
658 }
659 catch( final NullPointerException npe )
660 {
661 assertEquals( npe.getMessage(), "source" );
662 }
663
664 try
665 {
666 configValidator.validate( (InputSource)null, HANDLER, this );
667 fail( "Expected Null pointer due to null input" );
668 }
669 catch( final NullPointerException npe )
670 {
671 assertEquals( npe.getMessage(), "source" );
672 }
673
674 try
675 {
676 configValidator.validate( (InputSource)null );
677 fail( "Expected Null pointer due to null input" );
678 }
679 catch( final NullPointerException npe )
680 {
681 assertEquals( npe.getMessage(), "source" );
682 }
683
684 try
685 {
686 configValidator.validate( (InputSource)null, HANDLER );
687 fail( "Expected Null pointer due to null input" );
688 }
689 catch( final NullPointerException npe )
690 {
691 assertEquals( npe.getMessage(), "source" );
692 }
693 }
694
695 private void verifyResult( final ValidationResult result,
696 final boolean success )
697 {
698 assertEquals( "Success?", success, result.isValid() );
699 final ValidationIssue[] issues = result.getIssues();
700 if( success )
701 {
702 assertTrue( "issue count = 0", 0 == issues.length );
703 }
704 for( int i = 0; i < issues.length; i++ )
705 {
706 assertNotNull( "issues[ i ].getException()",
707 issues[ i ].getException() );
708 }
709 }
710
711 private ClassLoader createClassLoader()
712 {
713 final URL url = getClass().getClassLoader().getResource(
714 TestData.CATALOG_JAR );
715 assertNotNull( "ResourcePresent: " + TestData.CATALOG_JAR, url );
716 return new URLClassLoader( new URL[]{url} );
717 }
718
719 public void warning( SAXParseException exception )
720 throws SAXException
721 {
722 if( DEBUG )
723 {
724 System.out.println( "ConfigValidatorTestCase.warning" );
725 System.out.println( "exception = " + exception );
726 }
727 }
728
729 public void error( SAXParseException exception )
730 throws SAXException
731 {
732 if( DEBUG )
733 {
734 System.out.println( "ConfigValidatorTestCase.error" );
735 System.out.println( "exception = " + exception );
736 }
737 }
738
739 public void fatalError( SAXParseException exception )
740 throws SAXException
741 {
742 if( DEBUG )
743 {
744 System.out.println( "ConfigValidatorTestCase.fatalError" );
745 System.out.println( "exception = " + exception );
746 }
747 }
748
749 static final class NoopContentHandler
750 extends DefaultHandler
751 {
752 }
753
754 static final class NoopEntityResolver
755 implements EntityResolver
756 {
757 public InputSource resolveEntity( String publicId,
758 String systemId )
759 throws SAXException, IOException
760 {
761 return null;
762 }
763 }
764 }
765
This page was automatically generated by Maven