001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 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 5299 2016-08-30 01:50:13Z schulte $ 029 * 030 */ 031package org.jomc.tools.test; 032 033import java.io.File; 034import java.io.FileNotFoundException; 035import java.io.FileOutputStream; 036import java.io.IOException; 037import java.io.OutputStream; 038import java.net.URL; 039import java.text.ParseException; 040import java.util.Calendar; 041import java.util.Locale; 042import java.util.Properties; 043import java.util.concurrent.ExecutorService; 044import java.util.concurrent.atomic.AtomicInteger; 045import java.util.logging.Level; 046import org.apache.commons.io.FileUtils; 047import org.jomc.model.Argument; 048import org.jomc.model.Dependency; 049import org.jomc.model.Implementation; 050import org.jomc.model.JavaIdentifier; 051import org.jomc.model.Message; 052import org.jomc.model.ModelObject; 053import org.jomc.model.Module; 054import org.jomc.model.Modules; 055import org.jomc.model.Property; 056import org.jomc.model.Specification; 057import org.jomc.model.SpecificationReference; 058import org.jomc.model.Text; 059import org.jomc.model.Texts; 060import org.jomc.model.modlet.DefaultModelProvider; 061import org.jomc.model.modlet.ModelHelper; 062import org.jomc.modlet.Model; 063import org.jomc.modlet.ModelContext; 064import org.jomc.modlet.ModelContextFactory; 065import org.jomc.modlet.ModelException; 066import org.jomc.modlet.ModelValidationReport; 067import org.jomc.tools.JomcTool; 068import org.junit.After; 069import org.junit.Test; 070import static org.junit.Assert.assertEquals; 071import static org.junit.Assert.assertNotNull; 072import static org.junit.Assert.assertNull; 073import static org.junit.Assert.assertTrue; 074import static org.junit.Assert.fail; 075 076/** 077 * Test cases for class {@code org.jomc.tools.JomcTool}. 078 * 079 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 080 * @version $JOMC: JomcToolTest.java 5299 2016-08-30 01:50:13Z schulte $ 081 */ 082public class JomcToolTest 083{ 084 085 /** 086 * Constant for the name of the system property holding the name of the encoding of resources backing the test. 087 */ 088 private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding"; 089 090 /** 091 * Constant for the name of the system property holding the output directory for the test. 092 */ 093 private static final String OUTPUT_DIRECTORY_PROPERTY_NAME = "jomc.test.outputDirectory"; 094 095 /** 096 * The {@code JomcTool} instance tests are performed with. 097 */ 098 private volatile JomcTool jomcTool; 099 100 /** 101 * The {@code ModelContext} of the instance. 102 */ 103 private volatile ModelContext modelContext; 104 105 /** 106 * The {@code ExecutorService} backing the tests. 107 */ 108 private volatile ExecutorService executorService; 109 110 /** 111 * The {@code Model} of the instance. 112 */ 113 private volatile Model model; 114 115 /** 116 * The name of the encoding to use when reading or writing resources. 117 */ 118 private volatile String resourceEncoding; 119 120 /** 121 * The output directory of the instance. 122 */ 123 private volatile File outputDirectory; 124 125 /** 126 * Serial number of next output directories. 127 */ 128 private final AtomicInteger outputDirectoryId = new AtomicInteger( 0 ); 129 130 /** 131 * Creates a new {@code JomcToolTest} instance. 132 */ 133 public JomcToolTest() 134 { 135 super(); 136 } 137 138 /** 139 * Gets the name of the encoding used when reading resources. 140 * 141 * @return The name of the encoding used when reading resources. 142 * 143 * @see #setResourceEncoding(java.lang.String) 144 */ 145 public final String getResourceEncoding() 146 { 147 if ( this.resourceEncoding == null ) 148 { 149 this.resourceEncoding = System.getProperty( RESOURCE_ENCODING_PROPERTY_NAME ); 150 assertNotNull( "Expected '" + RESOURCE_ENCODING_PROPERTY_NAME + "' system property not found.", 151 this.resourceEncoding ); 152 153 } 154 155 return this.resourceEncoding; 156 } 157 158 /** 159 * Sets the name of the encoding to use when reading resources. 160 * 161 * @param value The new name of the encoding to use when reading resources or {@code null}. 162 * 163 * @see #getResourceEncoding() 164 */ 165 public final void setResourceEncoding( final String value ) 166 { 167 this.resourceEncoding = value; 168 } 169 170 /** 171 * Gets the output directory of instance. 172 * 173 * @return The output directory of instance. 174 * 175 * @see #setOutputDirectory(java.io.File) 176 */ 177 public final File getOutputDirectory() 178 { 179 if ( this.outputDirectory == null ) 180 { 181 final String name = System.getProperty( OUTPUT_DIRECTORY_PROPERTY_NAME ); 182 assertNotNull( "Expected '" + OUTPUT_DIRECTORY_PROPERTY_NAME + "' system property not found.", name ); 183 this.outputDirectory = new File( new File( name ), this.getClass().getSimpleName() ); 184 assertTrue( "Expected '" + OUTPUT_DIRECTORY_PROPERTY_NAME + "' system property to hold an absolute path.", 185 this.outputDirectory.isAbsolute() ); 186 187 if ( !this.outputDirectory.exists() ) 188 { 189 assertTrue( this.outputDirectory.mkdirs() ); 190 } 191 } 192 193 return this.outputDirectory; 194 } 195 196 /** 197 * Sets the output directory of instance. 198 * 199 * @param value The new output directory of instance or {@code null}. 200 * 201 * @see #getOutputDirectory() 202 */ 203 public final void setOutputDirectory( final File value ) 204 { 205 if ( value != null ) 206 { 207 assertTrue( "Expected absolute 'outputDirectory'.", value.isAbsolute() ); 208 } 209 210 this.outputDirectory = value; 211 } 212 213 /** 214 * Gets the next output directory of the instance. 215 * 216 * @return The next output directory of the instance. 217 */ 218 public final File getNextOutputDirectory() 219 { 220 try 221 { 222 final File nextOutputDirectory = 223 new File( this.getOutputDirectory(), Integer.toString( this.outputDirectoryId.incrementAndGet() ) ); 224 225 assertTrue( nextOutputDirectory.isAbsolute() ); 226 if ( nextOutputDirectory.exists() ) 227 { 228 FileUtils.deleteDirectory( nextOutputDirectory ); 229 } 230 231 return nextOutputDirectory; 232 } 233 catch ( final IOException e ) 234 { 235 throw new AssertionError( e ); 236 } 237 } 238 239 /** 240 * Gets the {@code JomcTool} instance tests are performed with. 241 * 242 * @return The {@code JomcTool} instance tests are performed with. 243 * 244 * @see #newJomcTool() 245 */ 246 public JomcTool getJomcTool() 247 { 248 if ( this.jomcTool == null ) 249 { 250 this.jomcTool = this.newJomcTool(); 251 this.jomcTool.setExecutorService( this.getExecutorService() ); 252 this.jomcTool.setModel( this.getModel() ); 253 this.jomcTool.getListeners().add( new JomcTool.Listener() 254 { 255 256 @Override 257 public void onLog( final Level level, final String message, final Throwable throwable ) 258 { 259 super.onLog( level, message, throwable ); 260 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 261 262 if ( throwable != null ) 263 { 264 throwable.printStackTrace( System.out ); 265 } 266 } 267 268 } ); 269 270 } 271 272 return this.jomcTool; 273 } 274 275 /** 276 * Creates a new {@code JomcTool} instance to test. 277 * 278 * @return A new {@code JomcTool} instance to test. 279 * 280 * @see #getJomcTool() 281 */ 282 protected JomcTool newJomcTool() 283 { 284 return new JomcTool(); 285 } 286 287 /** 288 * Gets the {@code ModelContext} instance backing the test. 289 * 290 * @return The {@code ModelContext} instance backing the test. 291 * 292 * @see #newModelContext() 293 */ 294 public ModelContext getModelContext() 295 { 296 if ( this.modelContext == null ) 297 { 298 this.modelContext = this.newModelContext(); 299 this.modelContext.setExecutorService( this.getExecutorService() ); 300 this.modelContext.getListeners().add( new ModelContext.Listener() 301 { 302 303 @Override 304 public void onLog( final Level level, String message, Throwable t ) 305 { 306 super.onLog( level, message, t ); 307 System.out.println( "[" + level.getLocalizedName() + "] " + message ); 308 309 if ( t != null ) 310 { 311 t.printStackTrace( System.out ); 312 } 313 } 314 315 } ); 316 317 } 318 319 return this.modelContext; 320 } 321 322 /** 323 * Creates a new {@code ModelContext} instance backing the test. 324 * 325 * @return A new {@code ModelContext} instance backing the test. 326 * 327 * @see #getModelContext() 328 */ 329 protected ModelContext newModelContext() 330 { 331 return ModelContextFactory.newInstance().newModelContext(); 332 } 333 334 /** 335 * Gets the {@code ExecutorService} backing the tests. 336 * 337 * @return The {@code ExecutorService} backing the tests. 338 * 339 * @see #newExecutorService() 340 * @since 1.10 341 */ 342 public final ExecutorService getExecutorService() 343 { 344 if ( this.executorService == null ) 345 { 346 this.executorService = this.newExecutorService(); 347 } 348 349 return this.executorService; 350 } 351 352 /** 353 * Creates a new {@code ExecutorService} backing the tests. 354 * 355 * @return A new {@code ExecutorService} backing the tests, or {@code null}. 356 * 357 * @see #getExecutorService() 358 * @since 1.10 359 */ 360 protected ExecutorService newExecutorService() 361 { 362 return null; 363 } 364 365 /** 366 * Shuts down the {@code ExecutorService} backing the tests, if not {@code null}. 367 */ 368 @After 369 public final void shutdown() 370 { 371 if ( this.executorService != null ) 372 { 373 this.executorService.shutdown(); 374 this.executorService = null; 375 } 376 } 377 378 /** 379 * Gets the {@code Model} instance backing the test. 380 * 381 * @return The {@code Model} instance backing the test. 382 * 383 * @see #newModel() 384 */ 385 public Model getModel() 386 { 387 if ( this.model == null ) 388 { 389 this.model = this.newModel(); 390 } 391 392 return this.model; 393 } 394 395 /** 396 * Creates a new {@code Model} instance backing the test. 397 * 398 * @return A new {@code Model} instance backing the test. 399 * 400 * @see #getModel() 401 */ 402 protected Model newModel() 403 { 404 try 405 { 406 DefaultModelProvider.setDefaultModuleLocation( this.getClass().getPackage().getName().replace( '.', '/' ) 407 + "/jomc.xml" ); 408 409 Model m = this.getModelContext().findModel( ModelObject.MODEL_PUBLIC_ID ); 410 411 if ( m != null ) 412 { 413 final Modules modules = ModelHelper.getModules( m ); 414 415 if ( modules != null ) 416 { 417 final Module cp = modules.getClasspathModule( Modules.getDefaultClasspathModuleName(), 418 this.getClass().getClassLoader() ); 419 420 if ( cp != null ) 421 { 422 modules.getModule().add( cp ); 423 } 424 } 425 426 m = this.getModelContext().processModel( m ); 427 428 if ( m != null ) 429 { 430 final ModelValidationReport validationReport = this.getModelContext().validateModel( m ); 431 432 for ( int i = 0, s0 = validationReport.getDetails().size(); i < s0; i++ ) 433 { 434 System.out.println( validationReport.getDetails().get( i ) ); 435 } 436 437 assertTrue( "Unexpected invalid '" + m.getIdentifier() + "' model.", 438 validationReport.isModelValid() ); 439 440 } 441 } 442 443 return m; 444 } 445 catch ( final ModelException e ) 446 { 447 throw new AssertionError( e ); 448 } 449 finally 450 { 451 DefaultModelProvider.setDefaultModuleLocation( null ); 452 } 453 } 454 455 @Test 456 @SuppressWarnings( "deprecation" ) 457 public final void testJomcToolNullPointerException() throws Exception 458 { 459 assertNotNull( this.getJomcTool() ); 460 461 try 462 { 463 this.getJomcTool().getDisplayLanguage( null ); 464 fail( "Expected NullPointerException not thrown." ); 465 } 466 catch ( final NullPointerException e ) 467 { 468 assertNullPointerException( e ); 469 } 470 471 try 472 { 473 this.getJomcTool().getImplementedJavaTypeNames( null, false ); 474 fail( "Expected NullPointerException not thrown." ); 475 } 476 catch ( final NullPointerException e ) 477 { 478 assertNullPointerException( e ); 479 } 480 481 try 482 { 483 this.getJomcTool().getJavaClasspathLocation( (Implementation) null ); 484 fail( "Expected NullPointerException not thrown." ); 485 } 486 catch ( final NullPointerException e ) 487 { 488 assertNullPointerException( e ); 489 } 490 491 try 492 { 493 this.getJomcTool().getJavaClasspathLocation( (Specification) null ); 494 fail( "Expected NullPointerException not thrown." ); 495 } 496 catch ( final NullPointerException e ) 497 { 498 assertNullPointerException( e ); 499 } 500 501 try 502 { 503 this.getJomcTool().getJavaGetterMethodName( (Dependency) null ); 504 fail( "Expected NullPointerException not thrown." ); 505 } 506 catch ( final NullPointerException e ) 507 { 508 assertNullPointerException( e ); 509 } 510 511 try 512 { 513 this.getJomcTool().getJavaGetterMethodName( (Message) null ); 514 fail( "Expected NullPointerException not thrown." ); 515 } 516 catch ( final NullPointerException e ) 517 { 518 assertNullPointerException( e ); 519 } 520 521 try 522 { 523 this.getJomcTool().getJavaGetterMethodName( (Property) null ); 524 fail( "Expected NullPointerException not thrown." ); 525 } 526 catch ( final NullPointerException e ) 527 { 528 assertNullPointerException( e ); 529 } 530 531 try 532 { 533 this.getJomcTool().getJavaMethodParameterName( (Argument) null ); 534 fail( "Expected NullPointerException not thrown." ); 535 } 536 catch ( final NullPointerException e ) 537 { 538 assertNullPointerException( e ); 539 } 540 541 try 542 { 543 this.getJomcTool().getJavaMethodParameterName( (Dependency) null ); 544 fail( "Expected NullPointerException not thrown." ); 545 } 546 catch ( final NullPointerException e ) 547 { 548 assertNullPointerException( e ); 549 } 550 551 try 552 { 553 this.getJomcTool().getJavaMethodParameterName( (Message) null ); 554 fail( "Expected NullPointerException not thrown." ); 555 } 556 catch ( final NullPointerException e ) 557 { 558 assertNullPointerException( e ); 559 } 560 561 try 562 { 563 this.getJomcTool().getJavaMethodParameterName( (Property) null ); 564 fail( "Expected NullPointerException not thrown." ); 565 } 566 catch ( final NullPointerException e ) 567 { 568 assertNullPointerException( e ); 569 } 570 571 try 572 { 573 this.getJomcTool().getJavaInterfaceNames( null, false ); 574 fail( "Expected NullPointerException not thrown." ); 575 } 576 catch ( final NullPointerException e ) 577 { 578 assertNullPointerException( e ); 579 } 580 581 try 582 { 583 this.getJomcTool().getJavaModifierName( null, (Dependency) null ); 584 fail( "Expected NullPointerException not thrown." ); 585 } 586 catch ( final NullPointerException e ) 587 { 588 assertNullPointerException( e ); 589 } 590 try 591 { 592 this.getJomcTool().getJavaModifierName( new Implementation(), (Dependency) null ); 593 fail( "Expected NullPointerException not thrown." ); 594 } 595 catch ( final NullPointerException e ) 596 { 597 assertNullPointerException( e ); 598 } 599 600 try 601 { 602 this.getJomcTool().getJavaModifierName( null, (Message) null ); 603 fail( "Expected NullPointerException not thrown." ); 604 } 605 catch ( final NullPointerException e ) 606 { 607 assertNullPointerException( e ); 608 } 609 try 610 { 611 this.getJomcTool().getJavaModifierName( new Implementation(), (Message) null ); 612 fail( "Expected NullPointerException not thrown." ); 613 } 614 catch ( final NullPointerException e ) 615 { 616 assertNullPointerException( e ); 617 } 618 619 try 620 { 621 this.getJomcTool().getJavaModifierName( null, (Property) null ); 622 fail( "Expected NullPointerException not thrown." ); 623 } 624 catch ( final NullPointerException e ) 625 { 626 assertNullPointerException( e ); 627 } 628 try 629 { 630 this.getJomcTool().getJavaModifierName( new Implementation(), (Property) null ); 631 fail( "Expected NullPointerException not thrown." ); 632 } 633 catch ( final NullPointerException e ) 634 { 635 assertNullPointerException( e ); 636 } 637 638 try 639 { 640 this.getJomcTool().getJavaPackageName( (Implementation) null ); 641 fail( "Expected NullPointerException not thrown." ); 642 } 643 catch ( final NullPointerException e ) 644 { 645 assertNullPointerException( e ); 646 } 647 648 try 649 { 650 this.getJomcTool().getJavaPackageName( (Specification) null ); 651 fail( "Expected NullPointerException not thrown." ); 652 } 653 catch ( final NullPointerException e ) 654 { 655 assertNullPointerException( e ); 656 } 657 658 try 659 { 660 this.getJomcTool().getJavaPackageName( (SpecificationReference) null ); 661 fail( "Expected NullPointerException not thrown." ); 662 } 663 catch ( final NullPointerException e ) 664 { 665 assertNullPointerException( e ); 666 } 667 668 try 669 { 670 this.getJomcTool().getJavaSetterMethodName( (Dependency) null ); 671 fail( "Expected NullPointerException not thrown." ); 672 } 673 catch ( final NullPointerException e ) 674 { 675 assertNullPointerException( e ); 676 } 677 678 try 679 { 680 this.getJomcTool().getJavaSetterMethodName( (Message) null ); 681 fail( "Expected NullPointerException not thrown." ); 682 } 683 catch ( final NullPointerException e ) 684 { 685 assertNullPointerException( e ); 686 } 687 688 try 689 { 690 this.getJomcTool().getJavaSetterMethodName( (Property) null ); 691 fail( "Expected NullPointerException not thrown." ); 692 } 693 catch ( final NullPointerException e ) 694 { 695 assertNullPointerException( e ); 696 } 697 698 try 699 { 700 this.getJomcTool().getJavaTypeName( (Argument) null ); 701 fail( "Expected NullPointerException not thrown." ); 702 } 703 catch ( final NullPointerException e ) 704 { 705 assertNullPointerException( e ); 706 } 707 708 try 709 { 710 this.getJomcTool().getJavaTypeName( (Dependency) null ); 711 fail( "Expected NullPointerException not thrown." ); 712 } 713 catch ( final NullPointerException e ) 714 { 715 assertNullPointerException( e ); 716 } 717 718 try 719 { 720 this.getJomcTool().getJavaTypeName( (Implementation) null, true ); 721 fail( "Expected NullPointerException not thrown." ); 722 } 723 catch ( final NullPointerException e ) 724 { 725 assertNullPointerException( e ); 726 } 727 728 try 729 { 730 this.getJomcTool().getJavaTypeName( (Property) null, true ); 731 fail( "Expected NullPointerException not thrown." ); 732 } 733 catch ( final NullPointerException e ) 734 { 735 assertNullPointerException( e ); 736 } 737 738 try 739 { 740 this.getJomcTool().getJavaTypeName( (Specification) null, true ); 741 fail( "Expected NullPointerException not thrown." ); 742 } 743 catch ( final NullPointerException e ) 744 { 745 assertNullPointerException( e ); 746 } 747 748 try 749 { 750 this.getJomcTool().getJavaTypeName( (SpecificationReference) null, true ); 751 fail( "Expected NullPointerException not thrown." ); 752 } 753 catch ( final NullPointerException e ) 754 { 755 assertNullPointerException( e ); 756 } 757 758 try 759 { 760 this.getJomcTool().getJavadocComment( (Text) null, 0, "\n" ); 761 fail( "Expected NullPointerException not thrown." ); 762 } 763 catch ( final NullPointerException e ) 764 { 765 assertNullPointerException( e ); 766 } 767 try 768 { 769 this.getJomcTool().getJavadocComment( new Text(), 0, null ); 770 fail( "Expected NullPointerException not thrown." ); 771 } 772 catch ( final NullPointerException e ) 773 { 774 assertNullPointerException( e ); 775 } 776 try 777 { 778 this.getJomcTool().getJavadocComment( new Text(), Integer.MIN_VALUE, "\n" ); 779 fail( "Expected IllegalArgumentException not thrown." ); 780 } 781 catch ( final IllegalArgumentException e ) 782 { 783 assertIllegalArgumentException( e ); 784 } 785 786 try 787 { 788 this.getJomcTool().getJavadocComment( (Texts) null, 0, "\n" ); 789 fail( "Expected NullPointerException not thrown." ); 790 } 791 catch ( final NullPointerException e ) 792 { 793 assertNullPointerException( e ); 794 } 795 try 796 { 797 this.getJomcTool().getJavadocComment( new Texts(), 0, null ); 798 fail( "Expected NullPointerException not thrown." ); 799 } 800 catch ( final NullPointerException e ) 801 { 802 assertNullPointerException( e ); 803 } 804 try 805 { 806 this.getJomcTool().getJavadocComment( new Texts(), Integer.MIN_VALUE, "\n" ); 807 fail( "Expected IllegalArgumentException not thrown." ); 808 } 809 catch ( final IllegalArgumentException e ) 810 { 811 assertIllegalArgumentException( e ); 812 } 813 814 try 815 { 816 this.getJomcTool().getLongDate( null ); 817 fail( "Expected NullPointerException not thrown." ); 818 } 819 catch ( final NullPointerException e ) 820 { 821 assertNullPointerException( e ); 822 } 823 824 try 825 { 826 this.getJomcTool().getLongDateTime( null ); 827 fail( "Expected NullPointerException not thrown." ); 828 } 829 catch ( final NullPointerException e ) 830 { 831 assertNullPointerException( e ); 832 } 833 834 try 835 { 836 this.getJomcTool().getLongTime( null ); 837 fail( "Expected NullPointerException not thrown." ); 838 } 839 catch ( final NullPointerException e ) 840 { 841 assertNullPointerException( e ); 842 } 843 844 try 845 { 846 this.getJomcTool().getMediumDate( null ); 847 fail( "Expected NullPointerException not thrown." ); 848 } 849 catch ( final NullPointerException e ) 850 { 851 assertNullPointerException( e ); 852 } 853 854 try 855 { 856 this.getJomcTool().getMediumDateTime( null ); 857 fail( "Expected NullPointerException not thrown." ); 858 } 859 catch ( final NullPointerException e ) 860 { 861 assertNullPointerException( e ); 862 } 863 864 try 865 { 866 this.getJomcTool().getMediumTime( null ); 867 fail( "Expected NullPointerException not thrown." ); 868 } 869 catch ( final NullPointerException e ) 870 { 871 assertNullPointerException( e ); 872 } 873 874 try 875 { 876 this.getJomcTool().getShortDate( null ); 877 fail( "Expected NullPointerException not thrown." ); 878 } 879 catch ( final NullPointerException e ) 880 { 881 assertNullPointerException( e ); 882 } 883 884 try 885 { 886 this.getJomcTool().getShortDateTime( null ); 887 fail( "Expected NullPointerException not thrown." ); 888 } 889 catch ( final NullPointerException e ) 890 { 891 assertNullPointerException( e ); 892 } 893 894 try 895 { 896 this.getJomcTool().getShortTime( null ); 897 fail( "Expected NullPointerException not thrown." ); 898 } 899 catch ( final NullPointerException e ) 900 { 901 assertNullPointerException( e ); 902 } 903 904 try 905 { 906 this.getJomcTool().getVelocityTemplate( null ); 907 fail( "Expected NullPointerException not thrown." ); 908 } 909 catch ( final NullPointerException e ) 910 { 911 assertNullPointerException( e ); 912 } 913 914 try 915 { 916 this.getJomcTool().getYears( null, Calendar.getInstance() ); 917 fail( "Expected NullPointerException not thrown." ); 918 } 919 catch ( final NullPointerException e ) 920 { 921 assertNullPointerException( e ); 922 } 923 924 try 925 { 926 this.getJomcTool().getYears( Calendar.getInstance(), null ); 927 fail( "Expected NullPointerException not thrown." ); 928 } 929 catch ( final NullPointerException e ) 930 { 931 assertNullPointerException( e ); 932 } 933 934 try 935 { 936 this.getJomcTool().isJavaDefaultPackage( (Implementation) null ); 937 fail( "Expected NullPointerException not thrown." ); 938 } 939 catch ( final NullPointerException e ) 940 { 941 assertNullPointerException( e ); 942 } 943 944 try 945 { 946 this.getJomcTool().isJavaDefaultPackage( (Specification) null ); 947 fail( "Expected NullPointerException not thrown." ); 948 } 949 catch ( final NullPointerException e ) 950 { 951 assertNullPointerException( e ); 952 } 953 954 try 955 { 956 this.getJomcTool().isJavaPrimitiveType( null ); 957 fail( "Expected NullPointerException not thrown." ); 958 } 959 catch ( final NullPointerException e ) 960 { 961 assertNullPointerException( e ); 962 } 963 964 try 965 { 966 this.getJomcTool().isLoggable( null ); 967 fail( "Expected NullPointerException not thrown." ); 968 } 969 catch ( final NullPointerException e ) 970 { 971 assertNullPointerException( e ); 972 } 973 974 try 975 { 976 this.getJomcTool().getIsoDate( null ); 977 fail( "Expected NullPointerException not thrown." ); 978 } 979 catch ( final NullPointerException e ) 980 { 981 assertNullPointerException( e ); 982 } 983 984 try 985 { 986 this.getJomcTool().getIsoTime( null ); 987 fail( "Expected NullPointerException not thrown." ); 988 } 989 catch ( final NullPointerException e ) 990 { 991 assertNullPointerException( e ); 992 } 993 994 try 995 { 996 this.getJomcTool().getIsoDateTime( null ); 997 fail( "Expected NullPointerException not thrown." ); 998 } 999 catch ( final NullPointerException e ) 1000 { 1001 assertNullPointerException( e ); 1002 } 1003 1004 try 1005 { 1006 this.getJomcTool().getTemplateEncoding( null ); 1007 fail( "Expected NullPointerException not thrown." ); 1008 } 1009 catch ( final NullPointerException e ) 1010 { 1011 assertNullPointerException( e ); 1012 } 1013 1014 try 1015 { 1016 this.getJomcTool().getParentTemplateProfile( null ); 1017 fail( "Expected NullPointerException not thrown." ); 1018 } 1019 catch ( final NullPointerException e ) 1020 { 1021 assertNullPointerException( e ); 1022 } 1023 } 1024 1025 @Test 1026 @SuppressWarnings( "deprecation" ) 1027 public final void testJomcToolNotNull() throws Exception 1028 { 1029 final Specification specification = new Specification(); 1030 specification.setClazz( "java.lang.Object" ); 1031 specification.setIdentifier( "java.lang.Object" ); 1032 1033 final Specification defaultPackageSpecification = new Specification(); 1034 defaultPackageSpecification.setClazz( "Object" ); 1035 defaultPackageSpecification.setIdentifier( "Object" ); 1036 1037 final Implementation implementation = new Implementation(); 1038 implementation.setIdentifier( "java.lang.Object" ); 1039 implementation.setName( "java.lang.Object" ); 1040 implementation.setClazz( "java.lang.Object" ); 1041 1042 final Implementation defaultPackageImplementation = new Implementation(); 1043 defaultPackageImplementation.setIdentifier( "Object" ); 1044 defaultPackageImplementation.setName( "Object" ); 1045 defaultPackageImplementation.setClazz( "Object" ); 1046 1047 final Dependency d = new Dependency(); 1048 d.setIdentifier( "java.util.Locale" ); 1049 d.setName( "locale" ); 1050 d.setImplementationName( "default" ); 1051 1052 final Property p = new Property(); 1053 p.setName( "property" ); 1054 p.setValue( "Test" ); 1055 1056 final Message m = new Message(); 1057 m.setName( "message" ); 1058 1059 final Calendar now = Calendar.getInstance(); 1060 final Calendar nextYear = Calendar.getInstance(); 1061 nextYear.set( Calendar.YEAR, nextYear.get( Calendar.YEAR ) + 1 ); 1062 1063 assertNotNull( this.getJomcTool().getListeners() ); 1064 assertNotNull( this.getJomcTool().getInputEncoding() ); 1065 assertNotNull( this.getJomcTool().getModel() ); 1066 assertNotNull( this.getJomcTool().getModules() ); 1067 assertNotNull( this.getJomcTool().getOutputEncoding() ); 1068 assertNotNull( this.getJomcTool().getTemplateProfile() ); 1069 assertNotNull( this.getJomcTool().getTemplateEncoding() ); 1070 assertNotNull( this.getJomcTool().getDefaultTemplateEncoding() ); 1071 assertNotNull( this.getJomcTool().getTemplateParameters() ); 1072 assertNotNull( this.getJomcTool().getIndentation() ); 1073 assertNotNull( this.getJomcTool().getLineSeparator() ); 1074 assertNotNull( this.getJomcTool().getVelocityContext() ); 1075 assertNotNull( this.getJomcTool().getVelocityEngine() ); 1076 assertNotNull( JomcTool.getDefaultLogLevel() ); 1077 assertNotNull( this.getJomcTool().getLongDate( now ) ); 1078 assertNotNull( this.getJomcTool().getLongDateTime( now ) ); 1079 assertNotNull( this.getJomcTool().getLongTime( now ) ); 1080 assertNotNull( this.getJomcTool().getMediumDate( now ) ); 1081 assertNotNull( this.getJomcTool().getMediumDateTime( now ) ); 1082 assertNotNull( this.getJomcTool().getMediumTime( now ) ); 1083 assertNotNull( this.getJomcTool().getShortDate( now ) ); 1084 assertNotNull( this.getJomcTool().getShortDateTime( now ) ); 1085 assertNotNull( this.getJomcTool().getShortTime( now ) ); 1086 assertNotNull( this.getJomcTool().getIsoDate( now ) ); 1087 assertNotNull( this.getJomcTool().getIsoDateTime( now ) ); 1088 assertNotNull( this.getJomcTool().getIsoTime( now ) ); 1089 assertNotNull( this.getJomcTool().getYears( now, now ) ); 1090 assertNotNull( this.getJomcTool().getYears( now, nextYear ) ); 1091 assertNotNull( this.getJomcTool().getYears( nextYear, now ) ); 1092 assertNotNull( this.getJomcTool().getDisplayLanguage( "en" ) ); 1093 1094 assertEquals( this.getJomcTool().getYears( now, nextYear ), this.getJomcTool().getYears( nextYear, now ) ); 1095 assertEquals( Locale.getDefault().getDisplayLanguage(), 1096 this.getJomcTool().getDisplayLanguage( Locale.getDefault().getLanguage() ) ); 1097 1098 assertEquals( "java/lang/Object", this.getJomcTool().getJavaClasspathLocation( implementation ) ); 1099 assertEquals( "Object", this.getJomcTool().getJavaClasspathLocation( defaultPackageImplementation ) ); 1100 assertEquals( "java/lang/Object", this.getJomcTool().getJavaClasspathLocation( specification ) ); 1101 assertEquals( "Object", this.getJomcTool().getJavaClasspathLocation( defaultPackageSpecification ) ); 1102 assertEquals( "getLocale", this.getJomcTool().getJavaGetterMethodName( d ) ); 1103 assertEquals( "getMessage", this.getJomcTool().getJavaGetterMethodName( m ) ); 1104 assertEquals( "getProperty", this.getJomcTool().getJavaGetterMethodName( p ) ); 1105 assertEquals( 0, this.getJomcTool().getJavaInterfaceNames( implementation, true ).size() ); 1106 assertEquals( 0, this.getJomcTool().getImplementedJavaTypeNames( implementation, true ).size() ); 1107 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, d ) ); 1108 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, m ) ); 1109 assertEquals( "private", this.getJomcTool().getJavaModifierName( implementation, p ) ); 1110 assertEquals( "java.lang", this.getJomcTool().getJavaPackageName( implementation ) ); 1111 assertEquals( "", this.getJomcTool().getJavaPackageName( defaultPackageImplementation ) ); 1112 assertEquals( "java.lang", this.getJomcTool().getJavaPackageName( specification ) ); 1113 assertEquals( "", this.getJomcTool().getJavaPackageName( defaultPackageSpecification ) ); 1114 assertEquals( "java.util", this.getJomcTool().getJavaPackageName( d ) ); 1115 assertEquals( "", this.getJomcTool().getJavaString( "" ) ); 1116 assertEquals( this.getJomcTool().getIndentation(), this.getJomcTool().getIndentation( 1 ) ); 1117 assertEquals( this.getJomcTool().getDefaultTemplateEncoding(), 1118 this.getJomcTool().getTemplateEncoding( "DOES_NOT_EXIST" ) ); 1119 1120 assertEquals( this.getJomcTool().getDefaultTemplateProfile(), 1121 this.getJomcTool().getParentTemplateProfile( "DOES_NOT_EXIST" ) ); 1122 1123 } 1124 1125 @Test 1126 public final void testVelocityTemplates() throws Exception 1127 { 1128 assertNotNull( this.getJomcTool().getVelocityTemplate( "Implementation.java.vm" ) ); 1129 this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" ); 1130 assertNotNull( this.getJomcTool().getVelocityTemplate( "Implementation.java.vm" ) ); 1131 this.getJomcTool().setTemplateProfile( null ); 1132 1133 try 1134 { 1135 this.getJomcTool().getVelocityTemplate( "DOES_NOT_EXIST" ); 1136 fail( "Expected FileNotFoundException not thrown." ); 1137 } 1138 catch ( final FileNotFoundException e ) 1139 { 1140 assertNotNull( e.getMessage() ); 1141 System.out.println( e.toString() ); 1142 } 1143 1144 try 1145 { 1146 this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" ); 1147 this.getJomcTool().getVelocityTemplate( "DOES_NOT_EXIST" ); 1148 fail( "Expected FileNotFoundException not thrown." ); 1149 } 1150 catch ( final FileNotFoundException e ) 1151 { 1152 assertNotNull( e.getMessage() ); 1153 System.out.println( e.toString() ); 1154 } 1155 } 1156 1157 @Test 1158 public final void testDefaultLogLevel() throws Exception 1159 { 1160 final String testLogLevel = System.getProperty( "org.jomc.tools.JomcTool.defaultLogLevel" ); 1161 1162 assertNotNull( JomcTool.getDefaultLogLevel() ); 1163 JomcTool.setDefaultLogLevel( null ); 1164 System.setProperty( "org.jomc.tools.JomcTool.defaultLogLevel", "OFF" ); 1165 assertEquals( Level.OFF, JomcTool.getDefaultLogLevel() ); 1166 1167 if ( testLogLevel != null ) 1168 { 1169 System.setProperty( "org.jomc.tools.JomcTool.defaultLogLevel", testLogLevel ); 1170 } 1171 else 1172 { 1173 System.clearProperty( "org.jomc.tools.JomcTool.defaultLogLevel" ); 1174 } 1175 1176 JomcTool.setDefaultLogLevel( null ); 1177 } 1178 1179 @Test 1180 public final void testLogLevel() throws Exception 1181 { 1182 JomcTool.setDefaultLogLevel( null ); 1183 this.getJomcTool().setLogLevel( null ); 1184 assertNotNull( this.getJomcTool().getLogLevel() ); 1185 1186 JomcTool.setDefaultLogLevel( Level.OFF ); 1187 this.getJomcTool().setLogLevel( null ); 1188 assertEquals( Level.OFF, this.getJomcTool().getLogLevel() ); 1189 1190 JomcTool.setDefaultLogLevel( null ); 1191 this.getJomcTool().setLogLevel( null ); 1192 } 1193 1194 @Test 1195 @SuppressWarnings( "deprecation" ) 1196 public final void testDefaultTemplateProfile() throws Exception 1197 { 1198 assertNotNull( JomcTool.getDefaultTemplateProfile() ); 1199 System.setProperty( "org.jomc.tools.JomcTool.defaultTemplateProfile", "TEST" ); 1200 JomcTool.setDefaultTemplateProfile( null ); 1201 assertEquals( "TEST", JomcTool.getDefaultTemplateProfile() ); 1202 System.clearProperty( "org.jomc.tools.JomcTool.defaultTemplateProfile" ); 1203 JomcTool.setDefaultTemplateProfile( null ); 1204 } 1205 1206 @Test 1207 @SuppressWarnings( "deprecation" ) 1208 public final void testTemplateProfile() throws Exception 1209 { 1210 JomcTool.setDefaultTemplateProfile( null ); 1211 this.getJomcTool().setTemplateProfile( null ); 1212 assertNotNull( this.getJomcTool().getTemplateProfile() ); 1213 1214 JomcTool.setDefaultTemplateProfile( "TEST" ); 1215 this.getJomcTool().setTemplateProfile( null ); 1216 assertEquals( "TEST", this.getJomcTool().getTemplateProfile() ); 1217 1218 JomcTool.setDefaultTemplateProfile( null ); 1219 this.getJomcTool().setTemplateProfile( null ); 1220 } 1221 1222 @Test 1223 public final void testIndentation() throws Exception 1224 { 1225 assertEquals( "", this.getJomcTool().getIndentation( 0 ) ); 1226 assertEquals( this.getJomcTool().getIndentation(), this.getJomcTool().getIndentation( 1 ) ); 1227 1228 try 1229 { 1230 this.getJomcTool().getIndentation( Integer.MIN_VALUE ); 1231 fail( "Expected IllegalArgumentException not thrown." ); 1232 } 1233 catch ( final IllegalArgumentException e ) 1234 { 1235 assertIllegalArgumentException( e ); 1236 } 1237 1238 this.getJomcTool().setIndentation( " TEST " ); 1239 assertEquals( " TEST ", this.getJomcTool().getIndentation() ); 1240 assertEquals( " TEST ", this.getJomcTool().getIndentation( 1 ) ); 1241 this.getJomcTool().setIndentation( null ); 1242 } 1243 1244 @Test 1245 public final void testModel() throws Exception 1246 { 1247 final Model m = this.getJomcTool().getModel(); 1248 this.getJomcTool().setModel( null ); 1249 assertNotNull( this.getJomcTool().getModel() ); 1250 this.getJomcTool().setModel( m ); 1251 } 1252 1253 @Test 1254 public final void testVelocityEngine() throws Exception 1255 { 1256 this.getJomcTool().setVelocityEngine( null ); 1257 assertNotNull( this.getJomcTool().getVelocityEngine() ); 1258 this.getJomcTool().setVelocityEngine( null ); 1259 } 1260 1261 @Test 1262 public final void testVelocityContext() throws Exception 1263 { 1264 assertNotNull( this.getJomcTool().getVelocityContext() ); 1265 this.getJomcTool().setTemplateProfile( "test" ); 1266 assertNotNull( this.getJomcTool().getVelocityContext() ); 1267 assertNotNull( this.getJomcTool().getVelocityContext().get( "test-object" ) ); 1268 assertTrue( this.getJomcTool().getVelocityContext().get( "test-object" ) instanceof JomcTool ); 1269 assertNotNull( this.getJomcTool().getVelocityContext().get( "test-url" ) ); 1270 assertTrue( this.getJomcTool().getVelocityContext().get( "test-url" ) instanceof URL ); 1271 assertEquals( new URL( "file:///tmp" ), this.getJomcTool().getVelocityContext().get( "test-url" ) ); 1272 assertNotNull( this.getJomcTool().getVelocityContext().get( "test-string" ) ); 1273 assertTrue( this.getJomcTool().getVelocityContext().get( "test-string" ) instanceof String ); 1274 assertEquals( "Test", this.getJomcTool().getVelocityContext().get( "test-string" ) ); 1275 this.getJomcTool().setTemplateProfile( null ); 1276 } 1277 1278 @Test 1279 public final void testDefaultTemplateEncoding() throws Exception 1280 { 1281 this.getJomcTool().setDefaultTemplateEncoding( null ); 1282 assertNotNull( this.getJomcTool().getDefaultTemplateEncoding() ); 1283 this.getJomcTool().setDefaultTemplateEncoding( null ); 1284 } 1285 1286 @Test 1287 public final void testTemplateEncoding() throws Exception 1288 { 1289 final File templateLocation = this.getNextOutputDirectory(); 1290 File templatesDir = new File( templateLocation, "org" ); 1291 templatesDir = new File( templatesDir, "jomc" ); 1292 templatesDir = new File( templatesDir, "tools" ); 1293 templatesDir = new File( templatesDir, "templates" ); 1294 templatesDir = new File( templatesDir, "tmp" ); 1295 1296 assertTrue( templatesDir.mkdirs() ); 1297 1298 final Properties p = new Properties(); 1299 p.setProperty( "template-encoding", "ISO-8859-1" ); 1300 1301 final OutputStream profileProperties = new FileOutputStream( new File( templatesDir, "profile.properties" ) ); 1302 p.store( profileProperties, this.getClass().getName() ); 1303 profileProperties.close(); 1304 1305 this.getJomcTool().setDefaultTemplateEncoding( null ); 1306 this.getJomcTool().setTemplateLocation( templateLocation.toURI().toURL() ); 1307 1308 assertEquals( "ISO-8859-1", this.getJomcTool().getTemplateEncoding( "tmp" ) ); 1309 assertEquals( "US-ASCII", this.getJomcTool().getTemplateEncoding( "test" ) ); 1310 assertEquals( this.getJomcTool().getDefaultTemplateEncoding(), 1311 this.getJomcTool().getTemplateEncoding( "jomc-java-bundles" ) ); 1312 1313 this.getJomcTool().setTemplateLocation( null ); 1314 this.getJomcTool().setDefaultTemplateEncoding( null ); 1315 } 1316 1317 @Test 1318 public final void testInputEncoding() throws Exception 1319 { 1320 this.getJomcTool().setInputEncoding( null ); 1321 assertNotNull( this.getJomcTool().getInputEncoding() ); 1322 this.getJomcTool().setInputEncoding( null ); 1323 } 1324 1325 @Test 1326 public final void testOutputEncoding() throws Exception 1327 { 1328 this.getJomcTool().setOutputEncoding( null ); 1329 assertNotNull( this.getJomcTool().getOutputEncoding() ); 1330 this.getJomcTool().setOutputEncoding( null ); 1331 } 1332 1333 @Test 1334 public final void testLineSeparator() throws Exception 1335 { 1336 this.getJomcTool().setLineSeparator( null ); 1337 assertNotNull( this.getJomcTool().getLineSeparator() ); 1338 this.getJomcTool().setLineSeparator( null ); 1339 } 1340 1341 @Test 1342 @SuppressWarnings( "deprecation" ) 1343 public final void testJomcToolModelObjectsNotFound() throws Exception 1344 { 1345 final SpecificationReference ref = new SpecificationReference(); 1346 ref.setIdentifier( "DOES_NOT_EXIST" ); 1347 1348 final Implementation i = new Implementation(); 1349 i.setIdentifier( "DOES_NOT_EXSIST" ); 1350 1351 final Dependency d = new Dependency(); 1352 d.setIdentifier( "DOES_NOT_EXIST" ); 1353 1354 final Property p = new Property(); 1355 p.setName( "DOES_NOT_EXIST" ); 1356 1357 assertNull( this.getJomcTool().getJavaPackageName( ref ) ); 1358 assertNull( this.getJomcTool().getJavaTypeName( ref, false ) ); 1359 assertNull( this.getJomcTool().getJavaTypeName( d ) ); 1360 1361 final Model oldModel = this.getJomcTool().getModel(); 1362 this.getJomcTool().setModel( null ); 1363 assertTrue( this.getJomcTool().getImplementedJavaTypeNames( i, true ).isEmpty() ); 1364 assertEquals( "private", this.getJomcTool().getJavaModifierName( i, p ) ); 1365 this.getJomcTool().setModel( oldModel ); 1366 } 1367 1368 @Test 1369 @SuppressWarnings( "deprecation" ) 1370 public final void testJavaIdentifier() throws Exception 1371 { 1372 assertEquals( "", this.getJomcTool().getJavaIdentifier( "", true ) ); 1373 assertEquals( "", this.getJomcTool().getJavaIdentifier( "", false ) ); 1374 assertEquals( "", this.getJomcTool().getJavaIdentifier( " ", true ) ); 1375 assertEquals( "", this.getJomcTool().getJavaIdentifier( " ", false ) ); 1376 assertEquals( "testTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", false ) ); 1377 assertEquals( "testTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", false ) ); 1378 assertEquals( "TestTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", true ) ); 1379 assertEquals( "TestTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", true ) ); 1380 assertEquals( "testTestTest", this.getJomcTool().getJavaIdentifier( " Test test test ", false ) ); 1381 assertEquals( "testTestTest", this.getJomcTool().getJavaIdentifier( " Test test test ", false ) ); 1382 assertEquals( "TestTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", true ) ); 1383 assertEquals( "TestTestTest", this.getJomcTool().getJavaIdentifier( " test test test ", true ) ); 1384 } 1385 1386 @Test 1387 @SuppressWarnings( "deprecation" ) 1388 public final void testJavaConstantName() throws Exception 1389 { 1390 assertEquals( "", this.getJomcTool().getJavaConstantName( "" ) ); 1391 assertEquals( "", this.getJomcTool().getJavaConstantName( " " ) ); 1392 assertEquals( "TEST_TEST_TEST", this.getJomcTool().getJavaConstantName( " test test test " ) ); 1393 assertEquals( "TEST_TEST_TEST", this.getJomcTool().getJavaConstantName( " test test test " ) ); 1394 assertEquals( "TEST_T_EST_TE_ST_TES_T", 1395 this.getJomcTool().getJavaConstantName( " Test tEst teSt tesT " ) ); 1396 1397 } 1398 1399 @Test 1400 @SuppressWarnings( "deprecation" ) 1401 public final void testJavaFieldName() throws Exception 1402 { 1403 assertEquals( "", this.getJomcTool().getJavaFieldName( "" ) ); 1404 assertEquals( "", this.getJomcTool().getJavaFieldName( " " ) ); 1405 assertEquals( "testTestTest", this.getJomcTool().getJavaFieldName( " test test test " ) ); 1406 assertEquals( "testTestTest", this.getJomcTool().getJavaFieldName( " test test test " ) ); 1407 assertEquals( "testTEstTeStTesT", this.getJomcTool().getJavaFieldName( " Test tEst teSt tesT " ) ); 1408 assertEquals( "testTEstTeStTesT", this.getJomcTool().getJavaFieldName( " Test tEst teSt tesT " ) ); 1409 assertEquals( "_package", this.getJomcTool().getJavaFieldName( " Package " ) ); 1410 assertEquals( "_new", this.getJomcTool().getJavaFieldName( " New " ) ); 1411 } 1412 1413 @Test 1414 @SuppressWarnings( "deprecation" ) 1415 public final void testJavaMethodParameterName() throws Exception 1416 { 1417 assertEquals( "", this.getJomcTool().getJavaMethodParameterName( "" ) ); 1418 assertEquals( "", this.getJomcTool().getJavaMethodParameterName( " " ) ); 1419 assertEquals( "testTestTest", this.getJomcTool().getJavaMethodParameterName( " test test test " ) ); 1420 assertEquals( "testTEstTeStTesT", this.getJomcTool().getJavaMethodParameterName( " Test tEst teSt tesT " ) ); 1421 assertEquals( "testTEstTeStTesT", 1422 this.getJomcTool().getJavaMethodParameterName( " Test tEst teSt tesT " ) ); 1423 1424 assertEquals( "_package", this.getJomcTool().getJavaMethodParameterName( " Package " ) ); 1425 assertEquals( "_new", this.getJomcTool().getJavaMethodParameterName( " New " ) ); 1426 } 1427 1428 @Test 1429 public final void testToJavaConstantName() throws Exception 1430 { 1431 try 1432 { 1433 this.getJomcTool().toJavaConstantName( "" ); 1434 fail( "Expected 'ParseException' not thrown." ); 1435 } 1436 catch ( final ParseException e ) 1437 { 1438 System.out.println( e.toString() ); 1439 assertNotNull( e.getMessage() ); 1440 } 1441 try 1442 { 1443 this.getJomcTool().toJavaConstantName( " " ); 1444 fail( "Expected 'ParseException' not thrown." ); 1445 } 1446 catch ( final ParseException e ) 1447 { 1448 System.out.println( e.toString() ); 1449 assertNotNull( e.getMessage() ); 1450 } 1451 1452 assertEquals( JavaIdentifier.valueOf( "TEST_TEST_TEST" ), 1453 this.getJomcTool().toJavaConstantName( " test test test " ) ); 1454 1455 assertEquals( JavaIdentifier.valueOf( "TEST_TEST_TEST_TEST" ), 1456 this.getJomcTool().toJavaConstantName( " Test tEst teSt tesT " ) ); 1457 1458 assertEquals( JavaIdentifier.valueOf( "TEST_TEST_TEST_TEST" ), 1459 this.getJomcTool().toJavaConstantName( " Test tEst teSt tesT " ) ); 1460 1461 assertEquals( JavaIdentifier.valueOf( "PACKAGE" ), this.getJomcTool().toJavaConstantName( " Package " ) ); 1462 assertEquals( JavaIdentifier.valueOf( "NEW" ), this.getJomcTool().toJavaConstantName( " New " ) ); 1463 } 1464 1465 @Test 1466 public final void testToJavaMethodName() throws Exception 1467 { 1468 try 1469 { 1470 this.getJomcTool().toJavaMethodName( "" ); 1471 fail( "Expected 'ParseException' not thrown." ); 1472 } 1473 catch ( final ParseException e ) 1474 { 1475 System.out.println( e.toString() ); 1476 assertNotNull( e.getMessage() ); 1477 } 1478 try 1479 { 1480 this.getJomcTool().toJavaMethodName( " " ); 1481 fail( "Expected 'ParseException' not thrown." ); 1482 } 1483 catch ( final ParseException e ) 1484 { 1485 System.out.println( e.toString() ); 1486 assertNotNull( e.getMessage() ); 1487 } 1488 1489 assertEquals( JavaIdentifier.valueOf( "testTestTest" ), 1490 this.getJomcTool().toJavaMethodName( " test test test " ) ); 1491 1492 assertEquals( JavaIdentifier.valueOf( "testTestTestTest" ), 1493 this.getJomcTool().toJavaMethodName( " Test tEst teSt tesT " ) ); 1494 1495 assertEquals( JavaIdentifier.valueOf( "testTestTestTest" ), 1496 this.getJomcTool().toJavaMethodName( " Test tEst teSt tesT " ) ); 1497 1498 assertEquals( JavaIdentifier.valueOf( "_package" ), this.getJomcTool().toJavaMethodName( " Package " ) ); 1499 assertEquals( JavaIdentifier.valueOf( "_new" ), this.getJomcTool().toJavaMethodName( " New " ) ); 1500 } 1501 1502 @Test 1503 public final void testToJavaVariableName() throws Exception 1504 { 1505 try 1506 { 1507 this.getJomcTool().toJavaVariableName( "" ); 1508 fail( "Expected 'ParseException' not thrown." ); 1509 } 1510 catch ( final ParseException e ) 1511 { 1512 System.out.println( e.toString() ); 1513 assertNotNull( e.getMessage() ); 1514 } 1515 try 1516 { 1517 this.getJomcTool().toJavaVariableName( " " ); 1518 fail( "Expected 'ParseException' not thrown." ); 1519 } 1520 catch ( final ParseException e ) 1521 { 1522 System.out.println( e.toString() ); 1523 assertNotNull( e.getMessage() ); 1524 } 1525 1526 assertEquals( JavaIdentifier.valueOf( "testTestTest" ), 1527 this.getJomcTool().toJavaVariableName( " test test test " ) ); 1528 1529 assertEquals( JavaIdentifier.valueOf( "testTestTestTest" ), 1530 this.getJomcTool().toJavaVariableName( " Test tEst teSt tesT " ) ); 1531 1532 assertEquals( JavaIdentifier.valueOf( "testTestTestTest" ), 1533 this.getJomcTool().toJavaVariableName( " Test tEst teSt tesT " ) ); 1534 1535 assertEquals( JavaIdentifier.valueOf( "_package" ), this.getJomcTool().toJavaVariableName( " Package " ) ); 1536 assertEquals( JavaIdentifier.valueOf( "_new" ), this.getJomcTool().toJavaVariableName( " New " ) ); 1537 } 1538 1539 @Test 1540 @SuppressWarnings( "deprecation" ) 1541 public final void testParentTemplateProfile() throws Exception 1542 { 1543 final File templateLocation = this.getNextOutputDirectory(); 1544 File templatesDir = new File( templateLocation, "org" ); 1545 templatesDir = new File( templatesDir, "jomc" ); 1546 templatesDir = new File( templatesDir, "tools" ); 1547 templatesDir = new File( templatesDir, "templates" ); 1548 templatesDir = new File( templatesDir, "tmp" ); 1549 1550 assertTrue( templatesDir.mkdirs() ); 1551 1552 final Properties p = new Properties(); 1553 p.setProperty( "parent-template-profile", "test" ); 1554 1555 final OutputStream profileProperties = new FileOutputStream( new File( templatesDir, "profile.properties" ) ); 1556 p.store( profileProperties, this.getClass().getName() ); 1557 profileProperties.close(); 1558 1559 this.getJomcTool().setDefaultTemplateProfile( null ); 1560 this.getJomcTool().setTemplateLocation( templateLocation.toURI().toURL() ); 1561 1562 assertEquals( "test", this.getJomcTool().getParentTemplateProfile( "tmp" ) ); 1563 assertEquals( "jomc-java-bundles", this.getJomcTool().getParentTemplateProfile( "test" ) ); 1564 assertEquals( this.getJomcTool().getDefaultTemplateProfile(), 1565 this.getJomcTool().getParentTemplateProfile( "jomc-java-bundles" ) ); 1566 1567 assertNull( this.getJomcTool().getParentTemplateProfile( this.getJomcTool().getDefaultTemplateProfile() ) ); 1568 this.getJomcTool().setTemplateLocation( null ); 1569 this.getJomcTool().setDefaultTemplateEncoding( null ); 1570 } 1571 1572 @Test 1573 public final void testHtmlString() throws Exception 1574 { 1575 assertEquals( "<>"∗&", this.getJomcTool().getHtmlString( "<>\"*&" ) ); 1576 } 1577 1578 public static void assertNullPointerException( final NullPointerException e ) 1579 { 1580 assertNotNull( e ); 1581 assertNotNull( e.getMessage() ); 1582 System.out.println( e.toString() ); 1583 } 1584 1585 public static void assertIllegalArgumentException( final IllegalArgumentException e ) 1586 { 1587 assertNotNull( e ); 1588 assertNotNull( e.getMessage() ); 1589 System.out.println( e.toString() ); 1590 } 1591 1592}