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