001/* 002 * Copyright (C) Christian Schulte, 2005-206 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: ModelContextTest.java 4613 2012-09-22 10:07:08Z schulte $ 029 * 030 */ 031package org.jomc.modlet.test; 032 033import java.io.IOException; 034import java.io.InputStream; 035import java.io.StringWriter; 036import java.net.URI; 037import java.util.Properties; 038import java.util.logging.Level; 039import javax.xml.bind.JAXBElement; 040import javax.xml.bind.Marshaller; 041import javax.xml.bind.util.JAXBSource; 042import org.jomc.modlet.DefaultModletProvider; 043import org.jomc.modlet.Model; 044import org.jomc.modlet.ModelContext; 045import org.jomc.modlet.ModelContextFactory; 046import org.jomc.modlet.ModelException; 047import org.jomc.modlet.ModletObject; 048import org.jomc.modlet.ModletProvider; 049import org.jomc.modlet.Modlets; 050import org.jomc.modlet.Property; 051import org.jomc.modlet.Service; 052import org.jomc.modlet.test.support.ClassCastExceptionModelContext; 053import org.jomc.modlet.test.support.IllegalAccessExceptionModelContext; 054import org.jomc.modlet.test.support.InstantiationExceptionModelContext; 055import org.jomc.modlet.test.support.InvocationTargetExceptionModelContext; 056import org.jomc.modlet.test.support.NoSuchMethodExceptionModelContext; 057import org.jomc.modlet.test.support.TestModletProvider; 058import org.junit.Test; 059import org.w3c.dom.ls.LSInput; 060import org.w3c.dom.ls.LSResourceResolver; 061import org.xml.sax.EntityResolver; 062import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 063import static org.junit.Assert.assertEquals; 064import static org.junit.Assert.assertFalse; 065import static org.junit.Assert.assertNotNull; 066import static org.junit.Assert.assertNull; 067import static org.junit.Assert.assertTrue; 068import static org.junit.Assert.fail; 069 070/** 071 * Test cases for {@code org.jomc.modlet.ModelContext} implementations. 072 * 073 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0 074 * @version $JOMC: ModelContextTest.java 4613 2012-09-22 10:07:08Z schulte $ 075 */ 076public class ModelContextTest 077{ 078 079 /** Constant for the name of a modlet provided without {@code ModletProvider}. */ 080 public static final String DEFAULT_MODLET_NAME; 081 082 /** Constant for the absolute location of an existing test resource. */ 083 private static final String TEST_RESOURCE_LOCATION = 084 ModelContextTest.class.getName().replace( '.', '/' ) + ".properties"; 085 086 static 087 { 088 InputStream in = null; 089 boolean suppressExceptionOnClose = true; 090 091 try 092 { 093 final Properties p = new Properties(); 094 in = ModelContextTest.class.getResourceAsStream( "/" + TEST_RESOURCE_LOCATION ); 095 assertNotNull( "Expected '" + TEST_RESOURCE_LOCATION + "' not found.", in ); 096 p.load( in ); 097 098 final String defaultModletName = p.getProperty( "defaultModletName" ); 099 assertNotNull( "Expected 'defaultModletName' property not found.", defaultModletName ); 100 DEFAULT_MODLET_NAME = defaultModletName; 101 102 suppressExceptionOnClose = false; 103 } 104 catch ( final IOException e ) 105 { 106 throw new AssertionError( e ); 107 } 108 finally 109 { 110 try 111 { 112 if ( in != null ) 113 { 114 in.close(); 115 } 116 } 117 catch ( final IOException e ) 118 { 119 if ( !suppressExceptionOnClose ) 120 { 121 throw new AssertionError( e ); 122 } 123 } 124 } 125 } 126 127 /** The {@code ModelContext} instance tests are performed with. */ 128 private ModelContext modelContext; 129 130 /** Creates a new {@code ModelContextTest} instance. */ 131 public ModelContextTest() 132 { 133 super(); 134 } 135 136 /** 137 * Gets the {@code ModelContext} instance tests are performed with. 138 * 139 * @return The {@code ModelContext} instance tests are performed with. 140 * 141 * @throws ModelException if creating a new instance fails. 142 * 143 * @see #newModelContext() 144 */ 145 public ModelContext getModelContext() throws ModelException 146 { 147 if ( this.modelContext == null ) 148 { 149 this.modelContext = this.newModelContext(); 150 this.modelContext.getListeners().add( new ModelContext.Listener() 151 { 152 153 @Override 154 public void onLog( final Level level, final String message, final Throwable t ) 155 { 156 super.onLog( level, message, t ); 157 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 158 } 159 160 } ); 161 162 } 163 164 return this.modelContext; 165 } 166 167 /** 168 * Creates a new {@code ModelContext} instance to test. 169 * 170 * @return A new {@code ModelContext} instance to test. 171 * 172 * @see #getModelContext() 173 */ 174 protected ModelContext newModelContext() 175 { 176 return ModelContextFactory.newInstance().newModelContext(); 177 } 178 179 @Test 180 public final void testAttributes() throws Exception 181 { 182 try 183 { 184 this.getModelContext().getAttribute( null ); 185 fail( "Expected NullPointerException not thrown." ); 186 } 187 catch ( final NullPointerException e ) 188 { 189 assertNotNull( e.getMessage() ); 190 System.out.println( e.toString() ); 191 } 192 193 try 194 { 195 this.getModelContext().getAttribute( null, null ); 196 fail( "Expected NullPointerException not thrown." ); 197 } 198 catch ( final NullPointerException e ) 199 { 200 assertNotNull( e.getMessage() ); 201 System.out.println( e.toString() ); 202 } 203 204 try 205 { 206 this.getModelContext().setAttribute( "TEST", null ); 207 fail( "Expected NullPointerException not thrown." ); 208 } 209 catch ( final NullPointerException e ) 210 { 211 assertNotNull( e.getMessage() ); 212 System.out.println( e.toString() ); 213 } 214 215 try 216 { 217 this.getModelContext().setAttribute( null, "TEST" ); 218 fail( "Expected NullPointerException not thrown." ); 219 } 220 catch ( final NullPointerException e ) 221 { 222 assertNotNull( e.getMessage() ); 223 System.out.println( e.toString() ); 224 } 225 226 try 227 { 228 this.getModelContext().clearAttribute( null ); 229 fail( "Expected NullPointerException not thrown." ); 230 } 231 catch ( final NullPointerException e ) 232 { 233 assertNotNull( e.getMessage() ); 234 System.out.println( e.toString() ); 235 } 236 237 assertNull( this.getModelContext().getAttribute( "ATTRIBUTE" ) ); 238 assertEquals( "TEST", this.getModelContext().getAttribute( "ATTRIBUTE", "TEST" ) ); 239 assertNull( this.getModelContext().setAttribute( "ATTRIBUTE", "VALUE" ) ); 240 assertEquals( "VALUE", this.getModelContext().getAttribute( "ATTRIBUTE", "TEST" ) ); 241 assertEquals( "VALUE", this.getModelContext().setAttribute( "ATTRIBUTE", "TEST" ) ); 242 assertEquals( "TEST", this.getModelContext().getAttribute( "ATTRIBUTE" ) ); 243 this.getModelContext().clearAttribute( "ATTRIBUTE" ); 244 assertNull( this.getModelContext().getAttribute( "ATTRIBUTE" ) ); 245 assertEquals( "TEST", this.getModelContext().getAttribute( "ATTRIBUTE", "TEST" ) ); 246 } 247 248 @Test 249 public final void testFindClass() throws Exception 250 { 251 try 252 { 253 this.getModelContext().findClass( null ); 254 fail( "Expected NullPointerException not thrown." ); 255 } 256 catch ( final NullPointerException e ) 257 { 258 assertNotNull( e.getMessage() ); 259 System.out.println( e.toString() ); 260 } 261 262 assertEquals( Object.class, this.getModelContext().findClass( "java.lang.Object" ) ); 263 assertNull( this.getModelContext().findClass( "DOES_NOT_EXIST" ) ); 264 } 265 266 @Test 267 public final void testFindResource() throws Exception 268 { 269 try 270 { 271 this.getModelContext().findResource( null ); 272 fail( "Expected NullPointerException not thrown." ); 273 } 274 catch ( final NullPointerException e ) 275 { 276 assertNotNull( e.getMessage() ); 277 System.out.println( e.toString() ); 278 } 279 280 assertNotNull( this.getModelContext().findResource( TEST_RESOURCE_LOCATION ) ); 281 } 282 283 @Test 284 public final void testFindResources() throws Exception 285 { 286 try 287 { 288 this.getModelContext().findResources( null ); 289 fail( "Expected NullPointerException not thrown." ); 290 } 291 catch ( final NullPointerException e ) 292 { 293 assertNotNull( e.getMessage() ); 294 System.out.println( e.toString() ); 295 } 296 297 assertTrue( this.getModelContext().findResources( TEST_RESOURCE_LOCATION ).hasMoreElements() ); 298 } 299 300 @Test 301 public final void testGetModlets() throws Exception 302 { 303 this.getModelContext().setModlets( null ); 304 DefaultModletProvider.setDefaultEnabled( Boolean.FALSE ); 305 final Modlets modlets = this.getModelContext().getModlets(); 306 assertNotNull( modlets ); 307 assertNotNull( modlets.getModlet( DEFAULT_MODLET_NAME ) ); 308 DefaultModletProvider.setDefaultEnabled( null ); 309 } 310 311 @Test 312 public final void testGetInvalidModlets() throws Exception 313 { 314 DefaultModletProvider.setDefaultEnabled( Boolean.TRUE ); 315 DefaultModletProvider.setDefaultModletLocation( "META-INF/invalid-modlet/jomc-modlet.xml" ); 316 this.getModelContext().setModlets( null ); 317 318 try 319 { 320 this.getModelContext().createContext( ModletObject.MODEL_PUBLIC_ID ); 321 fail( "Expected 'ModelException' not thrown." ); 322 } 323 catch ( final ModelException e ) 324 { 325 assertNotNull( e.getMessage() ); 326 System.out.println( e.toString() ); 327 } 328 329 try 330 { 331 this.getModelContext().createContext( ModletObject.PUBLIC_ID ); 332 fail( "Expected 'ModelException' not thrown." ); 333 } 334 catch ( final ModelException e ) 335 { 336 assertNotNull( e.getMessage() ); 337 System.out.println( e.toString() ); 338 } 339 340 try 341 { 342 this.getModelContext().createMarshaller( ModletObject.MODEL_PUBLIC_ID ); 343 fail( "Expected 'ModelException' not thrown." ); 344 } 345 catch ( final ModelException e ) 346 { 347 assertNotNull( e.getMessage() ); 348 System.out.println( e.toString() ); 349 } 350 351 try 352 { 353 this.getModelContext().createMarshaller( ModletObject.PUBLIC_ID ); 354 fail( "Expected 'ModelException' not thrown." ); 355 } 356 catch ( final ModelException e ) 357 { 358 assertNotNull( e.getMessage() ); 359 System.out.println( e.toString() ); 360 } 361 362 try 363 { 364 this.getModelContext().createSchema( ModletObject.MODEL_PUBLIC_ID ); 365 fail( "Expected 'ModelException' not thrown." ); 366 } 367 catch ( final ModelException e ) 368 { 369 assertNotNull( e.getMessage() ); 370 System.out.println( e.toString() ); 371 } 372 373 try 374 { 375 this.getModelContext().createSchema( ModletObject.PUBLIC_ID ); 376 fail( "Expected 'ModelException' not thrown." ); 377 } 378 catch ( final ModelException e ) 379 { 380 assertNotNull( e.getMessage() ); 381 System.out.println( e.toString() ); 382 } 383 384 try 385 { 386 this.getModelContext().createUnmarshaller( ModletObject.MODEL_PUBLIC_ID ); 387 fail( "Expected 'ModelException' not thrown." ); 388 } 389 catch ( final ModelException e ) 390 { 391 assertNotNull( e.getMessage() ); 392 System.out.println( e.toString() ); 393 } 394 395 try 396 { 397 this.getModelContext().createUnmarshaller( ModletObject.PUBLIC_ID ); 398 fail( "Expected 'ModelException' not thrown." ); 399 } 400 catch ( final ModelException e ) 401 { 402 assertNotNull( e.getMessage() ); 403 System.out.println( e.toString() ); 404 } 405 406 try 407 { 408 this.getModelContext().createEntityResolver( ModletObject.MODEL_PUBLIC_ID ); 409 fail( "Expected 'ModelException' not thrown." ); 410 } 411 catch ( final ModelException e ) 412 { 413 assertNotNull( e.getMessage() ); 414 System.out.println( e.toString() ); 415 } 416 417 try 418 { 419 this.getModelContext().createEntityResolver( ModletObject.PUBLIC_ID ); 420 fail( "Expected 'ModelException' not thrown." ); 421 } 422 catch ( final ModelException e ) 423 { 424 assertNotNull( e.getMessage() ); 425 System.out.println( e.toString() ); 426 } 427 428 try 429 { 430 this.getModelContext().createResourceResolver( ModletObject.MODEL_PUBLIC_ID ); 431 fail( "Expected 'ModelException' not thrown." ); 432 } 433 catch ( final ModelException e ) 434 { 435 assertNotNull( e.getMessage() ); 436 System.out.println( e.toString() ); 437 } 438 439 try 440 { 441 this.getModelContext().createResourceResolver( ModletObject.PUBLIC_ID ); 442 fail( "Expected 'ModelException' not thrown." ); 443 } 444 catch ( final ModelException e ) 445 { 446 assertNotNull( e.getMessage() ); 447 System.out.println( e.toString() ); 448 } 449 450 DefaultModletProvider.setDefaultEnabled( null ); 451 DefaultModletProvider.setDefaultModletLocation( null ); 452 this.getModelContext().setModlets( null ); 453 } 454 455 @Test 456 @SuppressWarnings( "deprecation" ) 457 public final void testCreateModelContext() throws Exception 458 { 459 ModelContext.setModelContextClassName( null ); 460 assertNotNull( ModelContext.createModelContext( null ) ); 461 assertNotNull( ModelContext.createModelContext( this.getClass().getClassLoader() ) ); 462 463 ModelContext.setModelContextClassName( "DOES_NOT_EXIST" ); 464 465 try 466 { 467 ModelContext.createModelContext( null ); 468 fail( "Expected ModelException not thrown." ); 469 } 470 catch ( final ModelException e ) 471 { 472 assertNotNull( e.getMessage() ); 473 System.out.println( e ); 474 } 475 476 try 477 { 478 ModelContext.createModelContext( this.getClass().getClassLoader() ); 479 fail( "Expected ModelException not thrown." ); 480 } 481 catch ( final ModelException e ) 482 { 483 assertNotNull( e.getMessage() ); 484 System.out.println( e ); 485 } 486 487 ModelContext.setModelContextClassName( InstantiationExceptionModelContext.class.getName() ); 488 489 try 490 { 491 ModelContext.createModelContext( this.getClass().getClassLoader() ); 492 fail( "Expected ModelException not thrown." ); 493 } 494 catch ( final ModelException e ) 495 { 496 assertNotNull( e.getMessage() ); 497 System.out.println( e ); 498 } 499 500 ModelContext.setModelContextClassName( IllegalAccessExceptionModelContext.class.getName() ); 501 502 try 503 { 504 ModelContext.createModelContext( this.getClass().getClassLoader() ); 505 fail( "Expected ModelException not thrown." ); 506 } 507 catch ( final ModelException e ) 508 { 509 assertNotNull( e.getMessage() ); 510 System.out.println( e ); 511 } 512 513 ModelContext.setModelContextClassName( ClassCastExceptionModelContext.class.getName() ); 514 515 try 516 { 517 ModelContext.createModelContext( this.getClass().getClassLoader() ); 518 fail( "Expected ModelException not thrown." ); 519 } 520 catch ( final ModelException e ) 521 { 522 assertNotNull( e.getMessage() ); 523 System.out.println( e ); 524 } 525 526 ModelContext.setModelContextClassName( NoSuchMethodExceptionModelContext.class.getName() ); 527 528 try 529 { 530 ModelContext.createModelContext( this.getClass().getClassLoader() ); 531 fail( "Expected ModelException not thrown." ); 532 } 533 catch ( final ModelException e ) 534 { 535 assertNotNull( e.getMessage() ); 536 System.out.println( e ); 537 } 538 539 ModelContext.setModelContextClassName( InvocationTargetExceptionModelContext.class.getName() ); 540 541 try 542 { 543 ModelContext.createModelContext( this.getClass().getClassLoader() ); 544 fail( "Expected ModelException not thrown." ); 545 } 546 catch ( final ModelException e ) 547 { 548 assertNotNull( e.getMessage() ); 549 System.out.println( e ); 550 } 551 552 ModelContext.setModelContextClassName( null ); 553 } 554 555 @Test 556 public final void testCreateEntityResolver() throws Exception 557 { 558 this.getModelContext().setModlets( null ); 559 560 try 561 { 562 this.getModelContext().createEntityResolver( (String) null ); 563 fail( "Expected NullPointerException not thrown." ); 564 } 565 catch ( final NullPointerException e ) 566 { 567 assertNotNull( e.getMessage() ); 568 System.out.println( e.toString() ); 569 } 570 571 try 572 { 573 this.getModelContext().createEntityResolver( (URI) null ); 574 fail( "Expected NullPointerException not thrown." ); 575 } 576 catch ( final NullPointerException e ) 577 { 578 assertNotNull( e.getMessage() ); 579 System.out.println( e.toString() ); 580 } 581 582 EntityResolver r = this.getModelContext().createEntityResolver( ModletObject.MODEL_PUBLIC_ID ); 583 assertNotNull( r ); 584 585 try 586 { 587 r.resolveEntity( null, null ); 588 fail( "Expected NullPointerException not thrown." ); 589 } 590 catch ( final NullPointerException e ) 591 { 592 assertNotNull( e.getMessage() ); 593 System.out.println( e.toString() ); 594 } 595 596 assertNull( r.resolveEntity( null, "DOES_NOT_EXIST" ) ); 597 assertNotNull( r.resolveEntity( "http://jomc.org/modlet", "DOES_NOT_EXIST" ) ); 598 assertNull( r.resolveEntity( ":", "DOES_NOT_EXIST" ) ); 599 assertNull( r.resolveEntity( null, ":" ) ); 600 601 r = this.getModelContext().createEntityResolver( ModletObject.PUBLIC_ID ); 602 assertNotNull( r ); 603 604 try 605 { 606 r.resolveEntity( null, null ); 607 fail( "Expected NullPointerException not thrown." ); 608 } 609 catch ( final NullPointerException e ) 610 { 611 assertNotNull( e.getMessage() ); 612 System.out.println( e.toString() ); 613 } 614 615 assertNull( r.resolveEntity( null, "DOES_NOT_EXIST" ) ); 616 assertNotNull( r.resolveEntity( "http://jomc.org/modlet", "DOES_NOT_EXIST" ) ); 617 assertNull( r.resolveEntity( ":", "DOES_NOT_EXIST" ) ); 618 assertNull( r.resolveEntity( null, ":" ) ); 619 } 620 621 @Test 622 public final void testCreateResourceResolver() throws Exception 623 { 624 this.getModelContext().setModlets( null ); 625 626 try 627 { 628 this.getModelContext().createResourceResolver( (String) null ); 629 fail( "Expected NullPointerException not thrown." ); 630 } 631 catch ( final NullPointerException e ) 632 { 633 assertNotNull( e.getMessage() ); 634 System.out.println( e.toString() ); 635 } 636 637 try 638 { 639 this.getModelContext().createResourceResolver( (URI) null ); 640 fail( "Expected NullPointerException not thrown." ); 641 } 642 catch ( final NullPointerException e ) 643 { 644 assertNotNull( e.getMessage() ); 645 System.out.println( e.toString() ); 646 } 647 648 LSResourceResolver r = this.getModelContext().createResourceResolver( ModletObject.MODEL_PUBLIC_ID ); 649 650 assertNotNull( r ); 651 assertNull( r.resolveResource( null, null, null, null, null ) ); 652 assertNull( r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, null, "DOES_NOT_EXIST", null ) ); 653 assertNull( r.resolveResource( "UNSUPPORTED", null, ModletObject.MODEL_PUBLIC_ID, null, null ) ); 654 assertNotNull( r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, ModletObject.MODEL_PUBLIC_ID, 655 ModelContext.getDefaultModletSchemaSystemId(), null ) ); 656 657 LSInput input = r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, null, 658 ModelContext.getDefaultModletSchemaSystemId(), null ); 659 660 assertNotNull( input ); 661 662 input.getBaseURI(); 663 input.getByteStream(); 664 input.getCertifiedText(); 665 input.getCharacterStream(); 666 input.getEncoding(); 667 input.getPublicId(); 668 input.getStringData(); 669 input.getSystemId(); 670 671 input.setBaseURI( null ); 672 input.setByteStream( null ); 673 input.setCertifiedText( false ); 674 input.setCharacterStream( null ); 675 input.setEncoding( null ); 676 input.setPublicId( null ); 677 input.setStringData( null ); 678 input.setSystemId( null ); 679 680 r = this.getModelContext().createResourceResolver( ModletObject.PUBLIC_ID ); 681 682 assertNotNull( r ); 683 assertNull( r.resolveResource( null, null, null, null, null ) ); 684 assertNull( r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, null, "DOES_NOT_EXIST", null ) ); 685 assertNull( r.resolveResource( "UNSUPPORTED", null, ModletObject.MODEL_PUBLIC_ID, null, null ) ); 686 assertNotNull( r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, ModletObject.MODEL_PUBLIC_ID, 687 ModelContext.getDefaultModletSchemaSystemId(), null ) ); 688 689 input = r.resolveResource( W3C_XML_SCHEMA_NS_URI, null, null, 690 ModelContext.getDefaultModletSchemaSystemId(), null ); 691 692 assertNotNull( input ); 693 694 input.getBaseURI(); 695 input.getByteStream(); 696 input.getCertifiedText(); 697 input.getCharacterStream(); 698 input.getEncoding(); 699 input.getPublicId(); 700 input.getStringData(); 701 input.getSystemId(); 702 703 input.setBaseURI( null ); 704 input.setByteStream( null ); 705 input.setCertifiedText( false ); 706 input.setCharacterStream( null ); 707 input.setEncoding( null ); 708 input.setPublicId( null ); 709 input.setStringData( null ); 710 input.setSystemId( null ); 711 } 712 713 @Test 714 public final void testCreateContext() throws Exception 715 { 716 this.getModelContext().setModlets( null ); 717 718 try 719 { 720 this.getModelContext().createContext( (String) null ); 721 fail( "Expected NullPointerException not thrown." ); 722 } 723 catch ( final NullPointerException e ) 724 { 725 assertNotNull( e.getMessage() ); 726 System.out.println( e.toString() ); 727 } 728 729 try 730 { 731 this.getModelContext().createContext( (URI) null ); 732 fail( "Expected NullPointerException not thrown." ); 733 } 734 catch ( final NullPointerException e ) 735 { 736 assertNotNull( e.getMessage() ); 737 System.out.println( e.toString() ); 738 } 739 740 this.getModelContext().setModlets( new Modlets() ); 741 742 try 743 { 744 this.getModelContext().createContext( "" ); 745 fail( "Expected ModelException not thrown." ); 746 } 747 catch ( final ModelException e ) 748 { 749 assertNotNull( e.getMessage() ); 750 System.out.println( e.toString() ); 751 } 752 753 try 754 { 755 this.getModelContext().createContext( new URI( "DOES_NOT_EXIST" ) ); 756 fail( "Expected ModelException not thrown." ); 757 } 758 catch ( final ModelException e ) 759 { 760 assertNotNull( e.getMessage() ); 761 System.out.println( e.toString() ); 762 } 763 764 this.getModelContext().setModlets( null ); 765 assertNotNull( this.getModelContext().createContext( ModletObject.MODEL_PUBLIC_ID ) ); 766 assertNotNull( this.getModelContext().createContext( ModletObject.PUBLIC_ID ) ); 767 } 768 769 @Test 770 public final void testCreateMarshaller() throws Exception 771 { 772 this.getModelContext().setModlets( null ); 773 774 try 775 { 776 this.getModelContext().createMarshaller( (String) null ); 777 fail( "Expected NullPointerException not thrown." ); 778 } 779 catch ( final NullPointerException e ) 780 { 781 assertNotNull( e.getMessage() ); 782 System.out.println( e.toString() ); 783 } 784 785 try 786 { 787 this.getModelContext().createMarshaller( (URI) null ); 788 fail( "Expected NullPointerException not thrown." ); 789 } 790 catch ( final NullPointerException e ) 791 { 792 assertNotNull( e.getMessage() ); 793 System.out.println( e.toString() ); 794 } 795 796 this.getModelContext().setModlets( new Modlets() ); 797 798 try 799 { 800 this.getModelContext().createMarshaller( "" ); 801 fail( "Expected ModelException not thrown." ); 802 } 803 catch ( final ModelException e ) 804 { 805 assertNotNull( e.getMessage() ); 806 System.out.println( e.toString() ); 807 } 808 809 try 810 { 811 this.getModelContext().createMarshaller( new URI( "DOES_NOT_EXIST" ) ); 812 fail( "Expected ModelException not thrown." ); 813 } 814 catch ( final ModelException e ) 815 { 816 assertNotNull( e.getMessage() ); 817 System.out.println( e.toString() ); 818 } 819 820 this.getModelContext().setModlets( null ); 821 assertNotNull( this.getModelContext().createMarshaller( ModletObject.MODEL_PUBLIC_ID ) ); 822 assertNotNull( this.getModelContext().createMarshaller( ModletObject.PUBLIC_ID ) ); 823 } 824 825 @Test 826 public final void testCreateUnmarshaller() throws Exception 827 { 828 this.getModelContext().setModlets( null ); 829 830 try 831 { 832 this.getModelContext().createUnmarshaller( (String) null ); 833 fail( "Expected NullPointerException not thrown." ); 834 } 835 catch ( final NullPointerException e ) 836 { 837 assertNotNull( e.getMessage() ); 838 System.out.println( e.toString() ); 839 } 840 841 try 842 { 843 this.getModelContext().createUnmarshaller( (URI) null ); 844 fail( "Expected NullPointerException not thrown." ); 845 } 846 catch ( final NullPointerException e ) 847 { 848 assertNotNull( e.getMessage() ); 849 System.out.println( e.toString() ); 850 } 851 852 this.getModelContext().setModlets( new Modlets() ); 853 854 try 855 { 856 this.getModelContext().createUnmarshaller( "" ); 857 fail( "Expected ModelException not thrown." ); 858 } 859 catch ( final ModelException e ) 860 { 861 assertNotNull( e.getMessage() ); 862 System.out.println( e.toString() ); 863 } 864 865 try 866 { 867 this.getModelContext().createUnmarshaller( new URI( "DOES_NOT_EXIST" ) ); 868 fail( "Expected ModelException not thrown." ); 869 } 870 catch ( final ModelException e ) 871 { 872 assertNotNull( e.getMessage() ); 873 System.out.println( e.toString() ); 874 } 875 876 this.getModelContext().setModlets( null ); 877 assertNotNull( this.getModelContext().createUnmarshaller( ModletObject.MODEL_PUBLIC_ID ) ); 878 assertNotNull( this.getModelContext().createUnmarshaller( ModletObject.PUBLIC_ID ) ); 879 } 880 881 @Test 882 public final void testCreateSchema() throws Exception 883 { 884 this.getModelContext().setModlets( null ); 885 886 try 887 { 888 this.getModelContext().createSchema( (String) null ); 889 fail( "Expected NullPointerException not thrown." ); 890 } 891 catch ( final NullPointerException e ) 892 { 893 assertNotNull( e.getMessage() ); 894 System.out.println( e.toString() ); 895 } 896 897 try 898 { 899 this.getModelContext().createSchema( (URI) null ); 900 fail( "Expected NullPointerException not thrown." ); 901 } 902 catch ( final NullPointerException e ) 903 { 904 assertNotNull( e.getMessage() ); 905 System.out.println( e.toString() ); 906 } 907 908 this.getModelContext().setModlets( new Modlets() ); 909 910 try 911 { 912 this.getModelContext().createSchema( "" ); 913 fail( "Expected ModelException not thrown." ); 914 } 915 catch ( final ModelException e ) 916 { 917 assertNotNull( e.getMessage() ); 918 System.out.println( e.toString() ); 919 } 920 921 try 922 { 923 this.getModelContext().createSchema( new URI( "DOES_NOT_EXIST" ) ); 924 fail( "Expected ModelException not thrown." ); 925 } 926 catch ( final ModelException e ) 927 { 928 assertNotNull( e.getMessage() ); 929 System.out.println( e.toString() ); 930 } 931 932 this.getModelContext().setModlets( null ); 933 assertNotNull( this.getModelContext().createSchema( ModletObject.MODEL_PUBLIC_ID ) ); 934 assertNotNull( this.getModelContext().createSchema( ModletObject.PUBLIC_ID ) ); 935 } 936 937 @Test 938 public final void testCreateServiceObject() throws Exception 939 { 940 try 941 { 942 this.getModelContext().createServiceObject( null, Object.class ); 943 fail( "Expected 'NullPointerException' not thrown." ); 944 } 945 catch ( final NullPointerException e ) 946 { 947 assertNotNull( e.getMessage() ); 948 System.out.println( e.toString() ); 949 } 950 951 try 952 { 953 this.getModelContext().createServiceObject( new Service(), null ); 954 fail( "Expected 'NullPointerException' not thrown." ); 955 } 956 catch ( final NullPointerException e ) 957 { 958 assertNotNull( e.getMessage() ); 959 System.out.println( e.toString() ); 960 } 961 962 final Service illegalAccessService = new Service(); 963 illegalAccessService.setIdentifier( ModelContext.class.getName() ); 964 illegalAccessService.setClazz( IllegalAccessExceptionModelContext.class.getName() ); 965 966 try 967 { 968 this.getModelContext().createServiceObject( illegalAccessService, ModelContext.class ); 969 fail( "Expected 'ModelException' not thrown." ); 970 } 971 catch ( final ModelException e ) 972 { 973 assertNotNull( e.getMessage() ); 974 System.out.println( e.toString() ); 975 } 976 977 final Service instantiationExceptionService = new Service(); 978 instantiationExceptionService.setIdentifier( ModelContext.class.getName() ); 979 instantiationExceptionService.setClazz( InstantiationExceptionModelContext.class.getName() ); 980 981 try 982 { 983 this.getModelContext().createServiceObject( instantiationExceptionService, ModelContext.class ); 984 fail( "Expected 'ModelException' not thrown." ); 985 } 986 catch ( final ModelException e ) 987 { 988 assertNotNull( e.getMessage() ); 989 System.out.println( e.toString() ); 990 } 991 992 final Service legalService = new Service(); 993 legalService.setIdentifier( ModletProvider.class.getName() ); 994 legalService.setClazz( TestModletProvider.class.getName() ); 995 assertNotNull( this.getModelContext().createServiceObject( legalService, ModletProvider.class ) ); 996 997 final Property unsupportedTypeProperty = new Property(); 998 unsupportedTypeProperty.setName( "unsupportedPropertyType" ); 999 unsupportedTypeProperty.setValue( "UNSUPPORTED TYPE" ); 1000 1001 final Property instantiationExceptionProperty = new Property(); 1002 instantiationExceptionProperty.setName( "instantiationExceptionProperty" ); 1003 instantiationExceptionProperty.setValue( "UNSUPPORTED TYPE" ); 1004 1005 final Property invocationTargetExceptionProperty = new Property(); 1006 invocationTargetExceptionProperty.setName( "invocationTargetExceptionProperty" ); 1007 invocationTargetExceptionProperty.setValue( "UNSUPPORTED TYPE" ); 1008 1009 legalService.getProperty().add( unsupportedTypeProperty ); 1010 1011 try 1012 { 1013 this.getModelContext().createServiceObject( legalService, ModletProvider.class ); 1014 fail( "Expected 'ModelException' not thrown." ); 1015 } 1016 catch ( final ModelException e ) 1017 { 1018 assertNotNull( e.getMessage() ); 1019 System.out.println( e.toString() ); 1020 } 1021 1022 legalService.getProperty().remove( unsupportedTypeProperty ); 1023 legalService.getProperty().add( instantiationExceptionProperty ); 1024 1025 try 1026 { 1027 this.getModelContext().createServiceObject( legalService, ModletProvider.class ); 1028 fail( "Expected 'ModelException' not thrown." ); 1029 } 1030 catch ( final ModelException e ) 1031 { 1032 assertNotNull( e.getMessage() ); 1033 System.out.println( e.toString() ); 1034 } 1035 1036 legalService.getProperty().remove( instantiationExceptionProperty ); 1037 legalService.getProperty().add( invocationTargetExceptionProperty ); 1038 1039 try 1040 { 1041 this.getModelContext().createServiceObject( legalService, ModletProvider.class ); 1042 fail( "Expected 'ModelException' not thrown." ); 1043 } 1044 catch ( final ModelException e ) 1045 { 1046 assertNotNull( e.getMessage() ); 1047 System.out.println( e.toString() ); 1048 } 1049 } 1050 1051 @Test 1052 public final void testDefaultLogLevel() throws Exception 1053 { 1054 final String testLogLevel = System.getProperty( "org.jomc.modlet.ModelContext.defaultLogLevel" ); 1055 1056 System.clearProperty( "org.jomc.modlet.ModelContext.defaultLogLevel" ); 1057 ModelContext.setDefaultLogLevel( null ); 1058 assertNotNull( ModelContext.getDefaultLogLevel() ); 1059 ModelContext.setDefaultLogLevel( null ); 1060 System.setProperty( "org.jomc.modlet.ModelContext.defaultLogLevel", "OFF" ); 1061 assertEquals( Level.OFF, ModelContext.getDefaultLogLevel() ); 1062 1063 if ( testLogLevel != null ) 1064 { 1065 System.setProperty( "org.jomc.modlet.ModelContext.defaultLogLevel", testLogLevel ); 1066 } 1067 else 1068 { 1069 System.clearProperty( "org.jomc.modlet.ModelContext.defaultLogLevel" ); 1070 } 1071 1072 ModelContext.setDefaultLogLevel( null ); 1073 } 1074 1075 @Test 1076 public final void testDefaultModletSchemaSystemId() throws Exception 1077 { 1078 System.clearProperty( "org.jomc.modlet.ModelContext.defaultModletSchemaSystemId" ); 1079 ModelContext.setDefaultModletSchemaSystemId( null ); 1080 assertNotNull( ModelContext.getDefaultModletSchemaSystemId() ); 1081 ModelContext.setDefaultModletSchemaSystemId( null ); 1082 System.setProperty( "org.jomc.modlet.ModelContext.defaultModletSchemaSystemId", "TEST" ); 1083 assertEquals( "TEST", ModelContext.getDefaultModletSchemaSystemId() ); 1084 System.clearProperty( "org.jomc.modlet.ModelContext.defaultModletSchemaSystemId" ); 1085 ModelContext.setDefaultModletSchemaSystemId( null ); 1086 assertNotNull( ModelContext.getDefaultModletSchemaSystemId() ); 1087 } 1088 1089 @Test 1090 public final void testLogLevel() throws Exception 1091 { 1092 ModelContext.setDefaultLogLevel( null ); 1093 this.getModelContext().setLogLevel( null ); 1094 assertNotNull( this.getModelContext().getLogLevel() ); 1095 1096 ModelContext.setDefaultLogLevel( Level.OFF ); 1097 this.getModelContext().setLogLevel( null ); 1098 assertEquals( Level.OFF, this.getModelContext().getLogLevel() ); 1099 1100 ModelContext.setDefaultLogLevel( null ); 1101 this.getModelContext().setLogLevel( null ); 1102 } 1103 1104 @Test 1105 public final void testLogging() throws Exception 1106 { 1107 try 1108 { 1109 this.getModelContext().isLoggable( null ); 1110 fail( "Expected NullPointerException not thrown." ); 1111 } 1112 catch ( final NullPointerException e ) 1113 { 1114 assertNotNull( e.getMessage() ); 1115 System.out.println( e.toString() ); 1116 } 1117 1118 try 1119 { 1120 this.getModelContext().log( null, "Message", new Throwable() ); 1121 fail( "Expected NullPointerException not thrown." ); 1122 } 1123 catch ( final NullPointerException e ) 1124 { 1125 assertNotNull( e.getMessage() ); 1126 System.out.println( e.toString() ); 1127 } 1128 1129 this.getModelContext().log( Level.ALL, null, null ); 1130 this.getModelContext().setLogLevel( Level.SEVERE ); 1131 assertFalse( this.getModelContext().isLoggable( Level.WARNING ) ); 1132 assertTrue( this.getModelContext().isLoggable( Level.SEVERE ) ); 1133 this.getModelContext().setLogLevel( null ); 1134 } 1135 1136 @Test 1137 public final void testModletSchemaSystemId() throws Exception 1138 { 1139 ModelContext.setDefaultModletSchemaSystemId( null ); 1140 this.getModelContext().setModletSchemaSystemId( null ); 1141 assertNotNull( this.getModelContext().getModletSchemaSystemId() ); 1142 1143 ModelContext.setDefaultModletSchemaSystemId( "TEST" ); 1144 this.getModelContext().setModletSchemaSystemId( null ); 1145 assertEquals( "TEST", this.getModelContext().getModletSchemaSystemId() ); 1146 1147 ModelContext.setDefaultModletSchemaSystemId( null ); 1148 this.getModelContext().setModletSchemaSystemId( null ); 1149 } 1150 1151 @Test 1152 public final void testFindModel() throws Exception 1153 { 1154 this.getModelContext().setModlets( null ); 1155 1156 try 1157 { 1158 this.getModelContext().findModel( (String) null ); 1159 fail( "Expected NullPointerException not thrown." ); 1160 } 1161 catch ( final NullPointerException e ) 1162 { 1163 assertNotNull( e.getMessage() ); 1164 System.out.println( e.toString() ); 1165 } 1166 1167 try 1168 { 1169 this.getModelContext().findModel( (Model) null ); 1170 fail( "Expected NullPointerException not thrown." ); 1171 } 1172 catch ( final NullPointerException e ) 1173 { 1174 assertNotNull( e.getMessage() ); 1175 System.out.println( e.toString() ); 1176 } 1177 1178 assertNotNull( this.getModelContext().findModel( ModletObject.MODEL_PUBLIC_ID ) ); 1179 1180 final Model model = new Model(); 1181 model.setIdentifier( ModletObject.MODEL_PUBLIC_ID ); 1182 1183 assertNotNull( this.getModelContext().findModel( model ) ); 1184 } 1185 1186 @Test 1187 public final void testProcessModel() throws Exception 1188 { 1189 this.getModelContext().setModlets( null ); 1190 1191 try 1192 { 1193 this.getModelContext().processModel( null ); 1194 fail( "Expected NullPointerException not thrown." ); 1195 } 1196 catch ( final NullPointerException e ) 1197 { 1198 assertNotNull( e.getMessage() ); 1199 System.out.println( e.toString() ); 1200 } 1201 } 1202 1203 @Test 1204 public final void testValidateModel() throws Exception 1205 { 1206 this.getModelContext().setModlets( null ); 1207 1208 try 1209 { 1210 this.getModelContext().validateModel( null, null ); 1211 fail( "Expected NullPointerException not thrown." ); 1212 } 1213 catch ( final NullPointerException e ) 1214 { 1215 assertNotNull( e.getMessage() ); 1216 System.out.println( e.toString() ); 1217 } 1218 1219 try 1220 { 1221 this.getModelContext().validateModel( "TEST", null ); 1222 fail( "Expected NullPointerException not thrown." ); 1223 } 1224 catch ( final NullPointerException e ) 1225 { 1226 assertNotNull( e.getMessage() ); 1227 System.out.println( e.toString() ); 1228 } 1229 1230 try 1231 { 1232 this.getModelContext().validateModel( null ); 1233 fail( "Expected NullPointerException not thrown." ); 1234 } 1235 catch ( final NullPointerException e ) 1236 { 1237 assertNotNull( e.getMessage() ); 1238 System.out.println( e.toString() ); 1239 } 1240 1241 final Model invalidModel = new Model(); 1242 final Model validModel = new Model(); 1243 validModel.setIdentifier( ModletObject.MODEL_PUBLIC_ID ); 1244 1245 final Marshaller m = this.getModelContext().createMarshaller( ModletObject.MODEL_PUBLIC_ID ); 1246 final JAXBElement<Model> invalid = new org.jomc.modlet.ObjectFactory().createModel( invalidModel ); 1247 final JAXBElement<Model> valid = new org.jomc.modlet.ObjectFactory().createModel( validModel ); 1248 1249 StringWriter stringWriter = new StringWriter(); 1250 m.marshal( invalid, stringWriter ); 1251 System.out.println( stringWriter ); 1252 1253 stringWriter = new StringWriter(); 1254 m.marshal( valid, stringWriter ); 1255 System.out.println( stringWriter ); 1256 1257 assertFalse( this.getModelContext().validateModel( ModletObject.MODEL_PUBLIC_ID, 1258 new JAXBSource( m, invalid ) ).isModelValid() ); 1259 1260 assertTrue( this.getModelContext().validateModel( ModletObject.MODEL_PUBLIC_ID, 1261 new JAXBSource( m, valid ) ).isModelValid() ); 1262 1263 } 1264 1265}