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: JomcToolTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 029 * 030 */ 031 package org.jomc.tools.test; 032 033 import java.io.File; 034 import java.io.IOException; 035 import java.util.Calendar; 036 import java.util.Locale; 037 import java.util.logging.Level; 038 import org.apache.commons.io.FileUtils; 039 import org.jomc.model.Argument; 040 import org.jomc.model.Dependency; 041 import org.jomc.model.Implementation; 042 import org.jomc.model.Message; 043 import org.jomc.model.ModelObject; 044 import org.jomc.model.Module; 045 import org.jomc.model.Modules; 046 import org.jomc.model.Property; 047 import org.jomc.model.Specification; 048 import org.jomc.model.SpecificationReference; 049 import org.jomc.model.Text; 050 import org.jomc.model.Texts; 051 import org.jomc.model.modlet.DefaultModelProvider; 052 import org.jomc.model.modlet.ModelHelper; 053 import org.jomc.modlet.Model; 054 import org.jomc.modlet.ModelContext; 055 import org.jomc.modlet.ModelContextFactory; 056 import org.jomc.modlet.ModelException; 057 import org.jomc.tools.JomcTool; 058 import org.junit.Test; 059 import static org.junit.Assert.assertEquals; 060 import static org.junit.Assert.assertNotNull; 061 import static org.junit.Assert.assertTrue; 062 import static org.junit.Assert.fail; 063 064 /** 065 * Test cases for class {@code org.jomc.tools.JomcTool}. 066 * 067 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 068 * @version $JOMC: JomcToolTest.java 4200 2012-01-25 09:46:13Z schulte2005 $ 069 */ 070 public class JomcToolTest 071 { 072 073 /** Constant for the name of the system property holding the name of the encoding of resources backing the test. */ 074 private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding"; 075 076 /** Constant for the name of the system property holding the output directory for the test. */ 077 private static final String OUTPUT_DIRECTORY_PROPERTY_NAME = "jomc.test.outputDirectory"; 078 079 /** The {@code JomcTool} instance tests are performed with. */ 080 private JomcTool jomcTool; 081 082 /** The {@code ModelContext} of the instance. */ 083 private ModelContext modelContext; 084 085 /** The {@code Model} of the instance. */ 086 private Model model; 087 088 /** The name of the encoding to use when reading or writing resources. */ 089 private String resourceEncoding; 090 091 /** The output directory of the instance. */ 092 private File outputDirectory; 093 094 /** Serial number of next output directories. */ 095 private volatile int outputDirectoryId; 096 097 /** Creates a new {@code JomcToolTest} instance. */ 098 public JomcToolTest() 099 { 100 super(); 101 } 102 103 /** 104 * Gets the name of the encoding used when reading resources. 105 * 106 * @return The name of the encoding used when reading resources. 107 * 108 * @see #setResourceEncoding(java.lang.String) 109 */ 110 public final String getResourceEncoding() 111 { 112 if ( this.resourceEncoding == null ) 113 { 114 this.resourceEncoding = System.getProperty( RESOURCE_ENCODING_PROPERTY_NAME ); 115 assertNotNull( "Expected '" + RESOURCE_ENCODING_PROPERTY_NAME + "' system property not found.", 116 this.resourceEncoding ); 117 118 } 119 120 return this.resourceEncoding; 121 } 122 123 /** 124 * Sets the name of the encoding to use when reading resources. 125 * 126 * @param value The new name of the encoding to use when reading resources or {@code null}. 127 * 128 * @see #getResourceEncoding() 129 */ 130 public final void setResourceEncoding( final String value ) 131 { 132 this.resourceEncoding = value; 133 } 134 135 /** 136 * Gets the output directory of instance. 137 * 138 * @return The output directory of instance. 139 * 140 * @see #setOutputDirectory(java.io.File) 141 */ 142 public final File getOutputDirectory() 143 { 144 if ( this.outputDirectory == null ) 145 { 146 final String name = System.getProperty( OUTPUT_DIRECTORY_PROPERTY_NAME ); 147 assertNotNull( "Expected '" + OUTPUT_DIRECTORY_PROPERTY_NAME + "' system property not found.", name ); 148 this.outputDirectory = new File( new File( name ), this.getClass().getSimpleName() ); 149 assertTrue( "Expected '" + OUTPUT_DIRECTORY_PROPERTY_NAME + "' system property to hold an absolute path.", 150 this.outputDirectory.isAbsolute() ); 151 152 if ( !this.outputDirectory.exists() ) 153 { 154 assertTrue( this.outputDirectory.mkdirs() ); 155 } 156 } 157 158 return this.outputDirectory; 159 } 160 161 /** 162 * Sets the output directory of instance. 163 * 164 * @param value The new output directory of instance or {@code null}. 165 * 166 * @see #getOutputDirectory() 167 */ 168 public final void setOutputDirectory( final File value ) 169 { 170 if ( value != null ) 171 { 172 assertTrue( "Expected absolute 'outputDirectory'.", value.isAbsolute() ); 173 } 174 175 this.outputDirectory = value; 176 } 177 178 /** 179 * Gets the next output directory of the instance. 180 * 181 * @return The next output directory of the instance. 182 */ 183 public final File getNextOutputDirectory() 184 { 185 try 186 { 187 final File nextOutputDirectory = 188 new File( this.getOutputDirectory(), Integer.toString( this.outputDirectoryId++ ) ); 189 190 assertTrue( nextOutputDirectory.isAbsolute() ); 191 if ( nextOutputDirectory.exists() ) 192 { 193 FileUtils.deleteDirectory( nextOutputDirectory ); 194 } 195 196 return nextOutputDirectory; 197 } 198 catch ( final IOException e ) 199 { 200 throw new AssertionError( e ); 201 } 202 } 203 204 /** 205 * Gets the {@code JomcTool} instance tests are performed with. 206 * 207 * @return The {@code JomcTool} instance tests are performed with. 208 * 209 * @see #newJomcTool() 210 */ 211 public JomcTool getJomcTool() 212 { 213 if ( this.jomcTool == null ) 214 { 215 this.jomcTool = this.newJomcTool(); 216 this.jomcTool.setModel( this.getModel() ); 217 this.jomcTool.getListeners().add( new JomcTool.Listener() 218 { 219 220 @Override 221 public void onLog( final Level level, final String message, final Throwable throwable ) 222 { 223 super.onLog( level, message, throwable ); 224 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 225 226 if ( throwable != null ) 227 { 228 throwable.printStackTrace( System.out ); 229 } 230 } 231 232 } ); 233 234 } 235 236 return this.jomcTool; 237 } 238 239 /** 240 * Creates a new {@code JomcTool} instance to test. 241 * 242 * @return A new {@code JomcTool} instance to test. 243 * 244 * @see #getJomcTool() 245 */ 246 protected JomcTool newJomcTool() 247 { 248 return new JomcTool(); 249 } 250 251 /** 252 * Gets the {@code ModelContext} instance backing the test. 253 * 254 * @return The {@code ModelContext} instance backing the test. 255 * 256 * @see #newModelContext() 257 */ 258 public ModelContext getModelContext() 259 { 260 if ( this.modelContext == null ) 261 { 262 this.modelContext = this.newModelContext(); 263 this.modelContext.getListeners().add( new ModelContext.Listener() 264 { 265 266 @Override 267 public void onLog( final Level level, String message, Throwable t ) 268 { 269 super.onLog( level, message, t ); 270 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 271 272 if ( t != null ) 273 { 274 t.printStackTrace( System.out ); 275 } 276 } 277 278 } ); 279 280 } 281 282 return this.modelContext; 283 } 284 285 /** 286 * Creates a new {@code ModelContext} instance backing the test. 287 * 288 * @return A new {@code ModelContext} instance backing the test. 289 * 290 * @see #getModelContext() 291 */ 292 protected ModelContext newModelContext() 293 { 294 return ModelContextFactory.newInstance().newModelContext(); 295 } 296 297 /** 298 * Gets the {@code Model} instance backing the test. 299 * 300 * @return The {@code Model} instance backing the test. 301 * 302 * @see #newModel() 303 */ 304 public Model getModel() 305 { 306 if ( this.model == null ) 307 { 308 this.model = this.newModel(); 309 } 310 311 return this.model; 312 } 313 314 /** 315 * Creates a new {@code Model} instance backing the test. 316 * 317 * @return A new {@code Model} instance backing the test. 318 * 319 * @see #getModel() 320 */ 321 protected Model newModel() 322 { 323 try 324 { 325 DefaultModelProvider.setDefaultModuleLocation( this.getClass().getPackage().getName().replace( '.', '/' ) 326 + "/jomc.xml" ); 327 328 Model m = this.getModelContext().findModel( ModelObject.MODEL_PUBLIC_ID ); 329 330 if ( m != null ) 331 { 332 final Modules modules = ModelHelper.getModules( m ); 333 334 if ( modules != null ) 335 { 336 final Module cp = modules.getClasspathModule( Modules.getDefaultClasspathModuleName(), 337 this.getClass().getClassLoader() ); 338 339 if ( cp != null ) 340 { 341 modules.getModule().add( cp ); 342 } 343 } 344 345 m = this.getModelContext().processModel( m ); 346 } 347 348 return m; 349 } 350 catch ( final ModelException e ) 351 { 352 throw new AssertionError( e ); 353 } 354 finally 355 { 356 DefaultModelProvider.setDefaultModuleLocation( null ); 357 } 358 } 359 360 @Test 361 @SuppressWarnings( "deprecation" ) 362 public final void testJomcToolNullPointerException() throws Exception 363 { 364 assertNotNull( this.getJomcTool() ); 365 366 try 367 { 368 this.getJomcTool().getDisplayLanguage( null ); 369 fail( "Expected NullPointerException not thrown." ); 370 } 371 catch ( final NullPointerException e ) 372 { 373 assertNullPointerException( e ); 374 } 375 376 try 377 { 378 this.getJomcTool().getImplementedJavaTypeNames( null, false ); 379 fail( "Expected NullPointerException not thrown." ); 380 } 381 catch ( final NullPointerException e ) 382 { 383 assertNullPointerException( e ); 384 } 385 386 try 387 { 388 this.getJomcTool().getJavaClasspathLocation( (Implementation) null ); 389 fail( "Expected NullPointerException not thrown." ); 390 } 391 catch ( final NullPointerException e ) 392 { 393 assertNullPointerException( e ); 394 } 395 396 try 397 { 398 this.getJomcTool().getJavaClasspathLocation( (Specification) null ); 399 fail( "Expected NullPointerException not thrown." ); 400 } 401 catch ( final NullPointerException e ) 402 { 403 assertNullPointerException( e ); 404 } 405 406 try 407 { 408 this.getJomcTool().getJavaGetterMethodName( (Dependency) null ); 409 fail( "Expected NullPointerException not thrown." ); 410 } 411 catch ( final NullPointerException e ) 412 { 413 assertNullPointerException( e ); 414 } 415 416 try 417 { 418 this.getJomcTool().getJavaGetterMethodName( (Message) null ); 419 fail( "Expected NullPointerException not thrown." ); 420 } 421 catch ( final NullPointerException e ) 422 { 423 assertNullPointerException( e ); 424 } 425 426 try 427 { 428 this.getJomcTool().getJavaGetterMethodName( (Property) null ); 429 fail( "Expected NullPointerException not thrown." ); 430 } 431 catch ( final NullPointerException e ) 432 { 433 assertNullPointerException( e ); 434 } 435 436 try 437 { 438 this.getJomcTool().getJavaMethodParameterName( (Argument) null ); 439 fail( "Expected NullPointerException not thrown." ); 440 } 441 catch ( final NullPointerException e ) 442 { 443 assertNullPointerException( e ); 444 } 445 446 try 447 { 448 this.getJomcTool().getJavaMethodParameterName( (Dependency) null ); 449 fail( "Expected NullPointerException not thrown." ); 450 } 451 catch ( final NullPointerException e ) 452 { 453 assertNullPointerException( e ); 454 } 455 456 try 457 { 458 this.getJomcTool().getJavaMethodParameterName( (Message) null ); 459 fail( "Expected NullPointerException not thrown." ); 460 } 461 catch ( final NullPointerException e ) 462 { 463 assertNullPointerException( e ); 464 } 465 466 try 467 { 468 this.getJomcTool().getJavaMethodParameterName( (Property) null ); 469 fail( "Expected NullPointerException not thrown." ); 470 } 471 catch ( final NullPointerException e ) 472 { 473 assertNullPointerException( e ); 474 } 475 476 try 477 { 478 this.getJomcTool().getJavaInterfaceNames( null, false ); 479 fail( "Expected NullPointerException not thrown." ); 480 } 481 catch ( final NullPointerException e ) 482 { 483 assertNullPointerException( e ); 484 } 485 486 try 487 { 488 this.getJomcTool().getJavaModifierName( null, (Dependency) null ); 489 fail( "Expected NullPointerException not thrown." ); 490 } 491 catch ( final NullPointerException e ) 492 { 493 assertNullPointerException( e ); 494 } 495 try 496 { 497 this.getJomcTool().getJavaModifierName( new Implementation(), (Dependency) null ); 498 fail( "Expected NullPointerException not thrown." ); 499 } 500 catch ( final NullPointerException e ) 501 { 502 assertNullPointerException( e ); 503 } 504 505 try 506 { 507 this.getJomcTool().getJavaModifierName( null, (Message) null ); 508 fail( "Expected NullPointerException not thrown." ); 509 } 510 catch ( final NullPointerException e ) 511 { 512 assertNullPointerException( e ); 513 } 514 try 515 { 516 this.getJomcTool().getJavaModifierName( new Implementation(), (Message) null ); 517 fail( "Expected NullPointerException not thrown." ); 518 } 519 catch ( final NullPointerException e ) 520 { 521 assertNullPointerException( e ); 522 } 523 524 try 525 { 526 this.getJomcTool().getJavaModifierName( null, (Property) null ); 527 fail( "Expected NullPointerException not thrown." ); 528 } 529 catch ( final NullPointerException e ) 530 { 531 assertNullPointerException( e ); 532 } 533 try 534 { 535 this.getJomcTool().getJavaModifierName( new Implementation(), (Property) null ); 536 fail( "Expected NullPointerException not thrown." ); 537 } 538 catch ( final NullPointerException e ) 539 { 540 assertNullPointerException( e ); 541 } 542 543 try 544 { 545 this.getJomcTool().getJavaPackageName( (Implementation) null ); 546 fail( "Expected NullPointerException not thrown." ); 547 } 548 catch ( final NullPointerException e ) 549 { 550 assertNullPointerException( e ); 551 } 552 553 try 554 { 555 this.getJomcTool().getJavaPackageName( (Specification) null ); 556 fail( "Expected NullPointerException not thrown." ); 557 } 558 catch ( final NullPointerException e ) 559 { 560 assertNullPointerException( e ); 561 } 562 563 try 564 { 565 this.getJomcTool().getJavaPackageName( (SpecificationReference) null ); 566 fail( "Expected NullPointerException not thrown." ); 567 } 568 catch ( final NullPointerException e ) 569 { 570 assertNullPointerException( e ); 571 } 572 573 try 574 { 575 this.getJomcTool().getJavaSetterMethodName( (Dependency) null ); 576 fail( "Expected NullPointerException not thrown." ); 577 } 578 catch ( final NullPointerException e ) 579 { 580 assertNullPointerException( e ); 581 } 582 583 try 584 { 585 this.getJomcTool().getJavaSetterMethodName( (Message) null ); 586 fail( "Expected NullPointerException not thrown." ); 587 } 588 catch ( final NullPointerException e ) 589 { 590 assertNullPointerException( e ); 591 } 592 593 try 594 { 595 this.getJomcTool().getJavaSetterMethodName( (Property) null ); 596 fail( "Expected NullPointerException not thrown." ); 597 } 598 catch ( final NullPointerException e ) 599 { 600 assertNullPointerException( e ); 601 } 602 603 try 604 { 605 this.getJomcTool().getJavaTypeName( (Argument) null ); 606 fail( "Expected NullPointerException not thrown." ); 607 } 608 catch ( final NullPointerException e ) 609 { 610 assertNullPointerException( e ); 611 } 612 613 try 614 { 615 this.getJomcTool().getJavaTypeName( (Dependency) null ); 616 fail( "Expected NullPointerException not thrown." ); 617 } 618 catch ( final NullPointerException e ) 619 { 620 assertNullPointerException( e ); 621 } 622 623 try 624 { 625 this.getJomcTool().getJavaTypeName( (Implementation) null, true ); 626 fail( "Expected NullPointerException not thrown." ); 627 } 628 catch ( final NullPointerException e ) 629 { 630 assertNullPointerException( e ); 631 } 632 633 try 634 { 635 this.getJomcTool().getJavaTypeName( (Property) null, true ); 636 fail( "Expected NullPointerException not thrown." ); 637 } 638 catch ( final NullPointerException e ) 639 { 640 assertNullPointerException( e ); 641 } 642 643 try 644 { 645 this.getJomcTool().getJavaTypeName( (Specification) null, true ); 646 fail( "Expected NullPointerException not thrown." ); 647 } 648 catch ( final NullPointerException e ) 649 { 650 assertNullPointerException( e ); 651 } 652 653 try 654 { 655 this.getJomcTool().getJavaTypeName( (SpecificationReference) null, true ); 656 fail( "Expected NullPointerException not thrown." ); 657 } 658 catch ( final NullPointerException e ) 659 { 660 assertNullPointerException( e ); 661 } 662 663 try 664 { 665 this.getJomcTool().getJavadocComment( (Text) null, 0, "\n" ); 666 fail( "Expected NullPointerException not thrown." ); 667 } 668 catch ( final NullPointerException e ) 669 { 670 assertNullPointerException( e ); 671 } 672 try 673 { 674 this.getJomcTool().getJavadocComment( new Text(), 0, null ); 675 fail( "Expected NullPointerException not thrown." ); 676 } 677 catch ( final NullPointerException e ) 678 { 679 assertNullPointerException( e ); 680 } 681 try 682 { 683 this.getJomcTool().getJavadocComment( new Text(), Integer.MIN_VALUE, "\n" ); 684 fail( "Expected IllegalArgumentException not thrown." ); 685 } 686 catch ( final IllegalArgumentException e ) 687 { 688 assertIllegalArgumentException( e ); 689 } 690 691 try 692 { 693 this.getJomcTool().getJavadocComment( (Texts) null, 0, "\n" ); 694 fail( "Expected NullPointerException not thrown." ); 695 } 696 catch ( final NullPointerException e ) 697 { 698 assertNullPointerException( e ); 699 } 700 try 701 { 702 this.getJomcTool().getJavadocComment( new Texts(), 0, null ); 703 fail( "Expected NullPointerException not thrown." ); 704 } 705 catch ( final NullPointerException e ) 706 { 707 assertNullPointerException( e ); 708 } 709 try 710 { 711 this.getJomcTool().getJavadocComment( new Texts(), Integer.MIN_VALUE, "\n" ); 712 fail( "Expected IllegalArgumentException not thrown." ); 713 } 714 catch ( final IllegalArgumentException e ) 715 { 716 assertIllegalArgumentException( e ); 717 } 718 719 try 720 { 721 this.getJomcTool().getLongDate( null ); 722 fail( "Expected NullPointerException not thrown." ); 723 } 724 catch ( final NullPointerException e ) 725 { 726 assertNullPointerException( e ); 727 } 728 729 try 730 { 731 this.getJomcTool().getLongDateTime( null ); 732 fail( "Expected NullPointerException not thrown." ); 733 } 734 catch ( final NullPointerException e ) 735 { 736 assertNullPointerException( e ); 737 } 738 739 try 740 { 741 this.getJomcTool().getLongTime( null ); 742 fail( "Expected NullPointerException not thrown." ); 743 } 744 catch ( final NullPointerException e ) 745 { 746 assertNullPointerException( e ); 747 } 748 749 try 750 { 751 this.getJomcTool().getMediumDate( null ); 752 fail( "Expected NullPointerException not thrown." ); 753 } 754 catch ( final NullPointerException e ) 755 { 756 assertNullPointerException( e ); 757 } 758 759 try 760 { 761 this.getJomcTool().getMediumDateTime( null ); 762 fail( "Expected NullPointerException not thrown." ); 763 } 764 catch ( final NullPointerException e ) 765 { 766 assertNullPointerException( e ); 767 } 768 769 try 770 { 771 this.getJomcTool().getMediumTime( null ); 772 fail( "Expected NullPointerException not thrown." ); 773 } 774 catch ( final NullPointerException e ) 775 { 776 assertNullPointerException( e ); 777 } 778 779 try 780 { 781 this.getJomcTool().getShortDate( null ); 782 fail( "Expected NullPointerException not thrown." ); 783 } 784 catch ( final NullPointerException e ) 785 { 786 assertNullPointerException( e ); 787 } 788 789 try 790 { 791 this.getJomcTool().getShortDateTime( null ); 792 fail( "Expected NullPointerException not thrown." ); 793 } 794 catch ( final NullPointerException e ) 795 { 796 assertNullPointerException( e ); 797 } 798 799 try 800 { 801 this.getJomcTool().getShortTime( null ); 802 fail( "Expected NullPointerException not thrown." ); 803 } 804 catch ( final NullPointerException e ) 805 { 806 assertNullPointerException( e ); 807 } 808 809 try 810 { 811 this.getJomcTool().getVelocityTemplate( null ); 812 fail( "Expected NullPointerException not thrown." ); 813 } 814 catch ( final NullPointerException e ) 815 { 816 assertNullPointerException( e ); 817 } 818 819 try 820 { 821 this.getJomcTool().getYears( null, Calendar.getInstance() ); 822 fail( "Expected NullPointerException not thrown." ); 823 } 824 catch ( final NullPointerException e ) 825 { 826 assertNullPointerException( e ); 827 } 828 829 try 830 { 831 this.getJomcTool().getYears( Calendar.getInstance(), null ); 832 fail( "Expected NullPointerException not thrown." ); 833 } 834 catch ( final NullPointerException e ) 835 { 836 assertNullPointerException( e ); 837 } 838 839 try 840 { 841 this.getJomcTool().isJavaDefaultPackage( (Implementation) null ); 842 fail( "Expected NullPointerException not thrown." ); 843 } 844 catch ( final NullPointerException e ) 845 { 846 assertNullPointerException( e ); 847 } 848 849 try 850 { 851 this.getJomcTool().isJavaDefaultPackage( (Specification) null ); 852 fail( "Expected NullPointerException not thrown." ); 853 } 854 catch ( final NullPointerException e ) 855 { 856 assertNullPointerException( e ); 857 } 858 859 try 860 { 861 this.getJomcTool().isJavaPrimitiveType( null ); 862 fail( "Expected NullPointerException not thrown." ); 863 } 864 catch ( final NullPointerException e ) 865 { 866 assertNullPointerException( e ); 867 } 868 869 try 870 { 871 this.getJomcTool().isLoggable( null ); 872 fail( "Expected NullPointerException not thrown." ); 873 } 874 catch ( final NullPointerException e ) 875 { 876 assertNullPointerException( e ); 877 } 878 879 try 880 { 881 this.getJomcTool().getIsoDate( null ); 882 fail( "Expected NullPointerException not thrown." ); 883 } 884 catch ( final NullPointerException e ) 885 { 886 assertNullPointerException( e ); 887 } 888 889 try 890 { 891 this.getJomcTool().getIsoTime( null ); 892 fail( "Expected NullPointerException not thrown." ); 893 } 894 catch ( final NullPointerException e ) 895 { 896 assertNullPointerException( e ); 897 } 898 899 try 900 { 901 this.getJomcTool().getIsoDateTime( null ); 902 fail( "Expected NullPointerException not thrown." ); 903 } 904 catch ( final NullPointerException e ) 905 { 906 assertNullPointerException( e ); 907 } 908 } 909 910 @Test 911 @SuppressWarnings( "deprecation" ) 912 public final void testJomcToolNotNull() throws Exception 913 { 914 final Specification specification = new Specification(); 915 specification.setClazz( "java.lang.Object" ); 916 specification.setIdentifier( "java.lang.Object" ); 917 918 final Specification defaultPackageSpecification = new Specification(); 919 defaultPackageSpecification.setClazz( "Object" ); 920 defaultPackageSpecification.setIdentifier( "Object" ); 921 922 final Implementation implementation = new Implementation(); 923 implementation.setIdentifier( "java.lang.Object" ); 924 implementation.setName( "java.lang.Object" ); 925 implementation.setClazz( "java.lang.Object" ); 926 927 final Implementation defaultPackageImplementation = new Implementation(); 928 defaultPackageImplementation.setIdentifier( "Object" ); 929 defaultPackageImplementation.setName( "Object" ); 930 defaultPackageImplementation.setClazz( "Object" ); 931 932 final Dependency d = new Dependency(); 933 d.setIdentifier( "java.util.Locale" ); 934 d.setName( "locale" ); 935 d.setImplementationName( "default" ); 936 937 final Property p = new Property(); 938 p.setName( "property" ); 939 p.setValue( "Test" ); 940 941 final Message m = new Message(); 942 m.setName( "message" ); 943 944 final Calendar now = Calendar.getInstance(); 945 final Calendar nextYear = Calendar.getInstance(); 946 nextYear.set( Calendar.YEAR, nextYear.get( Calendar.YEAR ) + 1 ); 947 948 assertNotNull( this.getJomcTool().getListeners() ); 949 assertNotNull( this.getJomcTool().getInputEncoding() ); 950 assertNotNull( this.getJomcTool().getModel() ); 951 assertNotNull( this.getJomcTool().getModules() ); 952 assertNotNull( this.getJomcTool().getOutputEncoding() ); 953 assertNotNull( this.getJomcTool().getTemplateProfile() ); 954 assertNotNull( this.getJomcTool().getTemplateEncoding() ); 955 assertNotNull( this.getJomcTool().getTemplateParameters() ); 956 assertNotNull( this.getJomcTool().getIndentation() ); 957 assertNotNull( this.getJomcTool().getLineSeparator() ); 958 assertNotNull( this.getJomcTool().getVelocityContext() ); 959 assertNotNull( this.getJomcTool().getVelocityEngine() ); 960 assertNotNull( JomcTool.getDefaultLogLevel() ); 961 assertNotNull( this.getJomcTool().getLongDate( now ) ); 962 assertNotNull( this.getJomcTool().getLongDateTime( now ) ); 963 assertNotNull( this.getJomcTool().getLongTime( now ) ); 964 assertNotNull( this.getJomcTool().getMediumDate( now ) ); 965 assertNotNull( this.getJomcTool().getMediumDateTime( now ) ); 966 assertNotNull( this.getJomcTool().getMediumTime( now ) ); 967 assertNotNull( this.getJomcTool().getShortDate( now ) ); 968 assertNotNull( this.getJomcTool().getShortDateTime( now ) ); 969 assertNotNull( this.getJomcTool().getShortTime( now ) ); 970 assertNotNull( this.getJomcTool().getIsoDate( now ) ); 971 assertNotNull( this.getJomcTool().getIsoDateTime( now ) ); 972 assertNotNull( this.getJomcTool().getIsoTime( now ) ); 973 assertNotNull( this.getJomcTool().getYears( now, now ) ); 974 assertNotNull( this.getJomcTool().getYears( now, nextYear ) ); 975 assertNotNull( this.getJomcTool().getYears( nextYear, now ) ); 976 assertNotNull( this.getJomcTool().getDisplayLanguage( "en" ) ); 977 assertEquals( this.getJomcTool().getYears( now, nextYear ), this.getJomcTool().getYears( nextYear, now ) ); 978 assertEquals( Locale.getDefault().getDisplayLanguage(), 979 this.getJomcTool().getDisplayLanguage( Locale.getDefault().getLanguage() ) ); 980 981 assertEquals( "java/lang/Object", this.getJomcTool().getJavaClasspathLocation( implementation ) ); 982 assertEquals( "Object", this.getJomcTool().getJavaClasspathLocation( defaultPackageImplementation ) ); 983 assertEquals( "java/lang/Object", this.getJomcTool().getJavaClasspathLocation( specification ) ); 984 assertEquals( "Object", this.getJomcTool().getJavaClasspathLocation( defaultPackageSpecification ) ); 985 assertEquals( "getLocale", this.getJomcTool().getJavaGetterMethodName( d ) ); 986 assertEquals( "getMessage", this.getJomcTool().getJavaGetterMethodName( m ) ); 987 assertEquals( "getProperty", this.getJomcTool().getJavaGetterMethodName( p ) ); 988 assertEquals( 0, this.getJomcTool().getJavaInterfaceNames( implementation, true ).size() ); 989 assertEquals( 0, this.getJomcTool().getImplementedJavaTypeNames( implementation, true ).size() ); 990 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, d ) ); 991 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, m ) ); 992 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, p ) ); 993 assertEquals( "java.lang", this.getJomcTool().getJavaPackageName( implementation ) ); 994 assertEquals( "", this.getJomcTool().getJavaPackageName( defaultPackageImplementation ) ); 995 assertEquals( "java.lang", this.getJomcTool().getJavaPackageName( specification ) ); 996 assertEquals( "", this.getJomcTool().getJavaPackageName( defaultPackageSpecification ) ); 997 assertEquals( "java.util", this.getJomcTool().getJavaPackageName( d ) ); 998 assertEquals( "", this.getJomcTool().getJavaString( "" ) ); 999 assertEquals( this.getJomcTool().getIndentation(), this.getJomcTool().getIndentation( 1 ) ); 1000 } 1001 1002 @Test 1003 public final void testVelocityTemplates() throws Exception 1004 { 1005 assertNotNull( this.getJomcTool().getVelocityTemplate( "Implementation.java.vm" ) ); 1006 this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" ); 1007 assertNotNull( this.getJomcTool().getVelocityTemplate( "Implementation.java.vm" ) ); 1008 this.getJomcTool().setTemplateProfile( null ); 1009 1010 try 1011 { 1012 this.getJomcTool().getVelocityTemplate( "DOES_NOT_EXIST" ); 1013 fail( "Expected IOException not thrown." ); 1014 } 1015 catch ( final IOException e ) 1016 { 1017 assertNotNull( e.getMessage() ); 1018 System.out.println( e.toString() ); 1019 } 1020 1021 try 1022 { 1023 this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" ); 1024 this.getJomcTool().getVelocityTemplate( "DOES_NOT_EXIST" ); 1025 fail( "Expected IOException not thrown." ); 1026 } 1027 catch ( final IOException e ) 1028 { 1029 assertNotNull( e.getMessage() ); 1030 System.out.println( e.toString() ); 1031 } 1032 } 1033 1034 @Test 1035 public final void testDefaultLogLevel() throws Exception 1036 { 1037 final String testLogLevel = System.getProperty( "org.jomc.tools.JomcTool.defaultLogLevel" ); 1038 1039 assertNotNull( JomcTool.getDefaultLogLevel() ); 1040 JomcTool.setDefaultLogLevel( null ); 1041 System.setProperty( "org.jomc.tools.JomcTool.defaultLogLevel", "OFF" ); 1042 assertEquals( Level.OFF, JomcTool.getDefaultLogLevel() ); 1043 1044 if ( testLogLevel != null ) 1045 { 1046 System.setProperty( "org.jomc.tools.JomcTool.defaultLogLevel", testLogLevel ); 1047 } 1048 else 1049 { 1050 System.clearProperty( "org.jomc.tools.JomcTool.defaultLogLevel" ); 1051 } 1052 1053 JomcTool.setDefaultLogLevel( null ); 1054 } 1055 1056 @Test 1057 public final void testLogLevel() throws Exception 1058 { 1059 JomcTool.setDefaultLogLevel( null ); 1060 this.getJomcTool().setLogLevel( null ); 1061 assertNotNull( this.getJomcTool().getLogLevel() ); 1062 1063 JomcTool.setDefaultLogLevel( Level.OFF ); 1064 this.getJomcTool().setLogLevel( null ); 1065 assertEquals( Level.OFF, this.getJomcTool().getLogLevel() ); 1066 1067 JomcTool.setDefaultLogLevel( null ); 1068 this.getJomcTool().setLogLevel( null ); 1069 } 1070 1071 @Test 1072 @SuppressWarnings( "deprecation" ) 1073 public final void testDefaultTemplateProfile() throws Exception 1074 { 1075 assertNotNull( JomcTool.getDefaultTemplateProfile() ); 1076 System.setProperty( "org.jomc.tools.JomcTool.defaultTemplateProfile", "TEST" ); 1077 JomcTool.setDefaultTemplateProfile( null ); 1078 assertEquals( "TEST", JomcTool.getDefaultTemplateProfile() ); 1079 System.clearProperty( "org.jomc.tools.JomcTool.defaultTemplateProfile" ); 1080 JomcTool.setDefaultTemplateProfile( null ); 1081 } 1082 1083 @Test 1084 @SuppressWarnings( "deprecation" ) 1085 public final void testTemplateProfile() throws Exception 1086 { 1087 JomcTool.setDefaultTemplateProfile( null ); 1088 this.getJomcTool().setTemplateProfile( null ); 1089 assertNotNull( this.getJomcTool().getTemplateProfile() ); 1090 1091 JomcTool.setDefaultTemplateProfile( "TEST" ); 1092 this.getJomcTool().setTemplateProfile( null ); 1093 assertEquals( "TEST", this.getJomcTool().getTemplateProfile() ); 1094 1095 JomcTool.setDefaultTemplateProfile( null ); 1096 this.getJomcTool().setTemplateProfile( null ); 1097 } 1098 1099 @Test 1100 public final void testIndentation() throws Exception 1101 { 1102 assertEquals( "", this.getJomcTool().getIndentation( 0 ) ); 1103 assertEquals( this.getJomcTool().getIndentation(), this.getJomcTool().getIndentation( 1 ) ); 1104 1105 try 1106 { 1107 this.getJomcTool().getIndentation( Integer.MIN_VALUE ); 1108 fail( "Expected IllegalArgumentException not thrown." ); 1109 } 1110 catch ( final IllegalArgumentException e ) 1111 { 1112 assertIllegalArgumentException( e ); 1113 } 1114 1115 this.getJomcTool().setIndentation( " TEST " ); 1116 assertEquals( " TEST ", this.getJomcTool().getIndentation() ); 1117 assertEquals( " TEST ", this.getJomcTool().getIndentation( 1 ) ); 1118 this.getJomcTool().setIndentation( null ); 1119 } 1120 1121 @Test 1122 public final void testModel() throws Exception 1123 { 1124 final Model m = this.getJomcTool().getModel(); 1125 this.getJomcTool().setModel( null ); 1126 assertNotNull( this.getJomcTool().getModel() ); 1127 this.getJomcTool().setModel( m ); 1128 } 1129 1130 @Test 1131 public final void testVelocityEngine() throws Exception 1132 { 1133 this.getJomcTool().setVelocityEngine( null ); 1134 assertNotNull( this.getJomcTool().getVelocityEngine() ); 1135 this.getJomcTool().setVelocityEngine( null ); 1136 } 1137 1138 @Test 1139 public final void testTemplateEncoding() throws Exception 1140 { 1141 this.getJomcTool().setTemplateEncoding( null ); 1142 assertNotNull( this.getJomcTool().getTemplateEncoding() ); 1143 this.getJomcTool().setTemplateEncoding( null ); 1144 } 1145 1146 @Test 1147 public final void testInputEncoding() throws Exception 1148 { 1149 this.getJomcTool().setInputEncoding( null ); 1150 assertNotNull( this.getJomcTool().getInputEncoding() ); 1151 this.getJomcTool().setInputEncoding( null ); 1152 } 1153 1154 @Test 1155 public final void testOutputEncoding() throws Exception 1156 { 1157 this.getJomcTool().setOutputEncoding( null ); 1158 assertNotNull( this.getJomcTool().getOutputEncoding() ); 1159 this.getJomcTool().setOutputEncoding( null ); 1160 } 1161 1162 @Test 1163 public final void testLineSeparator() throws Exception 1164 { 1165 this.getJomcTool().setLineSeparator( null ); 1166 assertNotNull( this.getJomcTool().getLineSeparator() ); 1167 this.getJomcTool().setLineSeparator( null ); 1168 } 1169 1170 public static void assertNullPointerException( final NullPointerException e ) 1171 { 1172 assertNotNull( e ); 1173 assertNotNull( e.getMessage() ); 1174 System.out.println( e.toString() ); 1175 } 1176 1177 public static void assertIllegalArgumentException( final IllegalArgumentException e ) 1178 { 1179 assertNotNull( e ); 1180 assertNotNull( e.getMessage() ); 1181 System.out.println( e.toString() ); 1182 } 1183 1184 }