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: SourceFileProcessorTest.java 5299 2016-08-30 01:50:13Z schulte $
029 *
030 */
031package org.jomc.tools.test;
032
033import java.io.File;
034import java.io.FileInputStream;
035import java.io.FileOutputStream;
036import java.io.IOException;
037import java.io.InputStream;
038import java.io.OutputStream;
039import org.apache.commons.io.IOUtils;
040import org.jomc.model.Implementation;
041import org.jomc.model.Module;
042import org.jomc.model.Specification;
043import org.jomc.modlet.Model;
044import org.jomc.tools.SourceFileProcessor;
045import org.jomc.util.SectionEditor;
046import org.junit.Test;
047import static org.junit.Assert.assertEquals;
048import static org.junit.Assert.assertFalse;
049import static org.junit.Assert.assertNotNull;
050import static org.junit.Assert.assertNull;
051import static org.junit.Assert.assertTrue;
052import static org.junit.Assert.fail;
053
054/**
055 * Test cases for class {@code org.jomc.tools.SourceFileProcessor}.
056 *
057 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
058 * @version $JOMC: SourceFileProcessorTest.java 5299 2016-08-30 01:50:13Z schulte $
059 */
060public class SourceFileProcessorTest extends JomcToolTest
061{
062
063    /**
064     * Constant to prefix relative resource names with.
065     */
066    private static final String ABSOLUTE_RESOURCE_NAME_PREFIX =
067        "/" + SourceFileProcessorTest.class.getPackage().getName().replace( '.', '/' ) + "/";
068
069    /**
070     * Creates a new {@code SourceFileProcessorTest} instance.
071     */
072    public SourceFileProcessorTest()
073    {
074        super();
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public SourceFileProcessor getJomcTool()
082    {
083        return (SourceFileProcessor) super.getJomcTool();
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    protected SourceFileProcessor newJomcTool()
091    {
092        return new SourceFileProcessor();
093    }
094
095    @Test
096    @SuppressWarnings( "deprecation" )
097    public final void testSourceFileProcessorNullPointerException() throws Exception
098    {
099        try
100        {
101            this.getJomcTool().getSourceFileType( (Specification) null );
102            fail( "Expected NullPointerException not thrown." );
103        }
104        catch ( final NullPointerException e )
105        {
106            assertNullPointerException( e );
107        }
108
109        try
110        {
111            this.getJomcTool().getSourceFileType( (Implementation) null );
112            fail( "Expected NullPointerException not thrown." );
113        }
114        catch ( final NullPointerException e )
115        {
116            assertNullPointerException( e );
117        }
118
119        try
120        {
121            this.getJomcTool().getSourceFilesType( (Specification) null );
122            fail( "Expected NullPointerException not thrown." );
123        }
124        catch ( final NullPointerException e )
125        {
126            assertNullPointerException( e );
127        }
128
129        try
130        {
131            this.getJomcTool().getSourceFilesType( (Implementation) null );
132            fail( "Expected NullPointerException not thrown." );
133        }
134        catch ( final NullPointerException e )
135        {
136            assertNullPointerException( e );
137        }
138
139        try
140        {
141            this.getJomcTool().getSourceFileEditor( (Specification) null );
142            fail( "Expected NullPointerException not thrown." );
143        }
144        catch ( final NullPointerException e )
145        {
146            assertNullPointerException( e );
147        }
148
149        try
150        {
151            this.getJomcTool().getSourceFileEditor( (Implementation) null );
152            fail( "Expected NullPointerException not thrown." );
153        }
154        catch ( final NullPointerException e )
155        {
156            assertNullPointerException( e );
157        }
158
159        try
160        {
161            this.getJomcTool().manageSourceFiles( null );
162            fail( "Expected NullPointerException not thrown." );
163        }
164        catch ( final NullPointerException e )
165        {
166            assertNullPointerException( e );
167        }
168
169        try
170        {
171            this.getJomcTool().manageSourceFiles( (Implementation) null, new File( "/" ) );
172            fail( "Expected NullPointerException not thrown." );
173        }
174        catch ( final NullPointerException e )
175        {
176            assertNullPointerException( e );
177        }
178
179        try
180        {
181            this.getJomcTool().manageSourceFiles( new Implementation(), null );
182            fail( "Expected NullPointerException not thrown." );
183        }
184        catch ( final NullPointerException e )
185        {
186            assertNullPointerException( e );
187        }
188
189        try
190        {
191            this.getJomcTool().manageSourceFiles( (Module) null, new File( "/" ) );
192            fail( "Expected NullPointerException not thrown." );
193        }
194        catch ( final NullPointerException e )
195        {
196            assertNullPointerException( e );
197        }
198
199        try
200        {
201            this.getJomcTool().manageSourceFiles( new Module(), null );
202            fail( "Expected NullPointerException not thrown." );
203        }
204        catch ( final NullPointerException e )
205        {
206            assertNullPointerException( e );
207        }
208
209        try
210        {
211            this.getJomcTool().manageSourceFiles( (Specification) null, new File( "/" ) );
212            fail( "Expected NullPointerException not thrown." );
213        }
214        catch ( final NullPointerException e )
215        {
216            assertNullPointerException( e );
217        }
218
219        try
220        {
221            this.getJomcTool().manageSourceFiles( new Specification(), null );
222            fail( "Expected NullPointerException not thrown." );
223        }
224        catch ( final NullPointerException e )
225        {
226            assertNullPointerException( e );
227        }
228    }
229
230    @Test
231    public final void testSourceFileProcessorNotNull() throws Exception
232    {
233        assertNotNull( this.getJomcTool().getSourceFilesType(
234            this.getJomcTool().getModules().getImplementation( "Implementation" ) ) );
235
236        assertNotNull( this.getJomcTool().getSourceFilesType(
237            this.getJomcTool().getModules().getSpecification( "Specification" ) ) );
238
239        assertNotNull( this.getJomcTool().getSourceFileEditor() );
240    }
241
242    @Test
243    public final void testManageSources() throws Exception
244    {
245        this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
246        this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
247
248        final File nonExistingDirectory = this.getNextOutputDirectory();
249
250        try
251        {
252            this.getJomcTool().manageSourceFiles( nonExistingDirectory );
253            fail( "Expected IOException not thrown." );
254        }
255        catch ( final IOException e )
256        {
257            assertNotNull( e.getMessage() );
258            System.out.println( e );
259        }
260
261        try
262        {
263            this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
264                                                  nonExistingDirectory );
265
266            fail( "Expected IOException not thrown." );
267        }
268        catch ( final IOException e )
269        {
270            assertNotNull( e.getMessage() );
271            System.out.println( e );
272        }
273
274        try
275        {
276            this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
277                                                  nonExistingDirectory );
278
279            fail( "Expected IOException not thrown." );
280        }
281        catch ( final IOException e )
282        {
283            assertNotNull( e.getMessage() );
284            System.out.println( e );
285        }
286
287        try
288        {
289            this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
290                                                  nonExistingDirectory );
291
292            fail( "Expected IOException not thrown." );
293        }
294        catch ( final IOException e )
295        {
296            assertNotNull( e.getMessage() );
297            System.out.println( e );
298        }
299
300        File sourcesDirectory = this.getNextOutputDirectory();
301        assertTrue( sourcesDirectory.mkdirs() );
302        this.getJomcTool().manageSourceFiles( sourcesDirectory );
303        this.getJomcTool().manageSourceFiles( sourcesDirectory );
304
305        sourcesDirectory = this.getNextOutputDirectory();
306        assertTrue( sourcesDirectory.mkdirs() );
307        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
308                                              sourcesDirectory );
309
310        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
311                                              sourcesDirectory );
312
313        final File implementationDirectory = this.getNextOutputDirectory();
314        final File implementationSourceFile = new File( implementationDirectory, "Implementation.java" );
315        assertTrue( implementationDirectory.mkdirs() );
316        long implementationSourceFileLength;
317
318        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
319                                              implementationDirectory );
320
321        implementationSourceFileLength = implementationSourceFile.length();
322        assertTrue( implementationSourceFile.exists() );
323
324        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
325                                              implementationDirectory );
326
327        assertTrue( implementationSourceFile.exists() );
328        assertEquals( implementationSourceFileLength, implementationSourceFile.length() );
329
330        this.getJomcTool().getTemplateParameters().put( "with-assertions", Boolean.FALSE );
331        this.getJomcTool().getTemplateParameters().put( "with-author-copyright", Boolean.FALSE );
332        this.getJomcTool().getTemplateParameters().put( "with-editor-fold", Boolean.FALSE );
333        this.getJomcTool().getTemplateParameters().put( "with-javadoc", Boolean.FALSE );
334        this.getJomcTool().getTemplateParameters().put( "with-javadoc-author", Boolean.FALSE );
335        this.getJomcTool().getTemplateParameters().put( "with-javadoc-version", Boolean.FALSE );
336        this.getJomcTool().getTemplateParameters().put( "with-jsr-250", Boolean.FALSE );
337        this.getJomcTool().getTemplateParameters().put( "with-project-name", null );
338        this.getJomcTool().getTemplateParameters().put( "with-revision-keyword", null );
339        this.getJomcTool().getTemplateParameters().put( "with-suppress-warnings", null );
340        this.getJomcTool().getTemplateParameters().put( "with-vendor-copyright", null );
341
342        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
343                                              implementationDirectory );
344
345        assertTrue( implementationSourceFile.exists() );
346        assertTrue( implementationSourceFile.length() < implementationSourceFileLength );
347
348        this.getJomcTool().getTemplateParameters().clear();
349
350        this.getJomcTool().setTemplateProfile( "jomc-java-bundles" );
351
352        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
353                                              implementationDirectory );
354
355        this.getJomcTool().getTemplateParameters().put( "with-assertions", Boolean.FALSE );
356        this.getJomcTool().getTemplateParameters().put( "with-author-copyright", Boolean.FALSE );
357        this.getJomcTool().getTemplateParameters().put( "with-editor-fold", Boolean.FALSE );
358        this.getJomcTool().getTemplateParameters().put( "with-javadoc", Boolean.FALSE );
359        this.getJomcTool().getTemplateParameters().put( "with-javadoc-author", Boolean.FALSE );
360        this.getJomcTool().getTemplateParameters().put( "with-javadoc-version", Boolean.FALSE );
361        this.getJomcTool().getTemplateParameters().put( "with-jsr-250", Boolean.FALSE );
362        this.getJomcTool().getTemplateParameters().put( "with-project-name", null );
363        this.getJomcTool().getTemplateParameters().put( "with-revision-keyword", null );
364        this.getJomcTool().getTemplateParameters().put( "with-suppress-warnings", null );
365        this.getJomcTool().getTemplateParameters().put( "with-vendor-copyright", null );
366
367        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
368                                              implementationDirectory );
369
370        this.getJomcTool().getTemplateParameters().clear();
371
372        this.getJomcTool().setTemplateProfile( null );
373
374        this.getJomcTool().manageSourceFiles(
375            this.getJomcTool().getModules().getImplementation( "ImplementationWithSourceFilesModel" ),
376            implementationDirectory );
377
378        this.getJomcTool().manageSourceFiles(
379            this.getJomcTool().getModules().getImplementation( "ImplementationWithSourceFilesModel" ),
380            implementationDirectory );
381
382        final File specificationDirectory = this.getNextOutputDirectory();
383        assertTrue( specificationDirectory.mkdirs() );
384        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
385                                              specificationDirectory );
386
387        this.getJomcTool().getTemplateParameters().put( "with-assertions", Boolean.FALSE );
388        this.getJomcTool().getTemplateParameters().put( "with-author-copyright", Boolean.FALSE );
389        this.getJomcTool().getTemplateParameters().put( "with-editor-fold", Boolean.FALSE );
390        this.getJomcTool().getTemplateParameters().put( "with-javadoc", Boolean.FALSE );
391        this.getJomcTool().getTemplateParameters().put( "with-javadoc-author", Boolean.FALSE );
392        this.getJomcTool().getTemplateParameters().put( "with-javadoc-version", Boolean.FALSE );
393        this.getJomcTool().getTemplateParameters().put( "with-jsr-250", Boolean.FALSE );
394        this.getJomcTool().getTemplateParameters().put( "with-project-name", null );
395        this.getJomcTool().getTemplateParameters().put( "with-revision-keyword", null );
396        this.getJomcTool().getTemplateParameters().put( "with-suppress-warnings", null );
397        this.getJomcTool().getTemplateParameters().put( "with-vendor-copyright", null );
398
399        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
400                                              specificationDirectory );
401
402        this.getJomcTool().getTemplateParameters().clear();
403
404        this.getJomcTool().manageSourceFiles(
405            this.getJomcTool().getModules().getSpecification( "SpecificationWithSourceFilesModel" ),
406            specificationDirectory );
407
408        this.getJomcTool().manageSourceFiles(
409            this.getJomcTool().getModules().getSpecification( "SpecificationWithSourceFilesModel" ),
410            specificationDirectory );
411
412        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "IllegalImplementationSource.java.txt",
413                           new File( implementationDirectory, "Implementation.java" ) );
414
415        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "IllegalSpecificationSource.java.txt",
416                           new File( specificationDirectory, "Specification.java" ) );
417
418        try
419        {
420            this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
421                                                  implementationDirectory );
422
423            fail( "Expected IOException not thrown." );
424        }
425        catch ( final IOException e )
426        {
427            assertNotNull( e.getMessage() );
428            System.out.println( e.toString() );
429        }
430
431        try
432        {
433            this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
434                                                  specificationDirectory );
435
436            fail( "Expected IOException not thrown." );
437        }
438        catch ( final IOException e )
439        {
440            assertNotNull( e.getMessage() );
441            System.out.println( e.toString() );
442        }
443
444        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "EmptyImplementationSource.java.txt",
445                           new File( implementationDirectory, "Implementation.java" ) );
446
447        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "EmptySpecificationSource.java.txt",
448                           new File( specificationDirectory, "Specification.java" ) );
449
450        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
451                                              implementationDirectory );
452
453        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
454                                              specificationDirectory );
455
456        this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" );
457
458        sourcesDirectory = this.getNextOutputDirectory();
459        assertTrue( sourcesDirectory.mkdirs() );
460        this.getJomcTool().manageSourceFiles( sourcesDirectory );
461
462        sourcesDirectory = this.getNextOutputDirectory();
463        assertTrue( sourcesDirectory.mkdirs() );
464        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ), sourcesDirectory );
465
466        sourcesDirectory = this.getNextOutputDirectory();
467        assertTrue( sourcesDirectory.mkdirs() );
468        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
469                                              sourcesDirectory );
470
471        sourcesDirectory = this.getNextOutputDirectory();
472        assertTrue( sourcesDirectory.mkdirs() );
473        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
474                                              sourcesDirectory );
475
476        this.getJomcTool().setInputEncoding( null );
477        this.getJomcTool().setOutputEncoding( null );
478    }
479
480    @Test
481    public final void testMandatorySections() throws Exception
482    {
483        final SectionEditor editor = new SectionEditor();
484        final File specificationDirectory = this.getNextOutputDirectory();
485        final File implementationDirectory = this.getNextOutputDirectory();
486
487        assertTrue( specificationDirectory.mkdirs() );
488        assertTrue( implementationDirectory.mkdirs() );
489
490        this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
491        this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
492
493        File f = new File( implementationDirectory, "Implementation.java" );
494        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutAnnotationsSection.java.txt", f );
495        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
496                                              implementationDirectory );
497
498        String edited = this.toString( f );
499        editor.edit( edited );
500        assertTrue( editor.isSectionPresent( "Annotations" ) );
501
502        f = new File( implementationDirectory, "Implementation.java" );
503        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDependenciesSection.java.txt", f );
504        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
505                                              implementationDirectory );
506
507        edited = this.toString( f );
508        editor.edit( edited );
509        assertTrue( editor.isSectionPresent( "Dependencies" ) );
510
511        f = new File( implementationDirectory, "Implementation.java" );
512        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutMessagesSection.java.txt", f );
513        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
514                                              implementationDirectory );
515
516        edited = this.toString( f );
517        editor.edit( edited );
518        assertTrue( editor.isSectionPresent( "Messages" ) );
519
520        f = new File( implementationDirectory, "Implementation.java" );
521        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutPropertiesSection.java.txt", f );
522        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
523                                              implementationDirectory );
524
525        edited = this.toString( f );
526        editor.edit( edited );
527        assertTrue( editor.isSectionPresent( "Properties" ) );
528
529        f = new File( implementationDirectory, "ImplementationOfSpecification.java" );
530        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX
531                               + "ImplementationOfSpecificationWithoutConstructorsSection.java.txt", f );
532
533        this.getJomcTool().manageSourceFiles(
534            this.getJomcTool().getModules().getImplementation( "ImplementationOfSpecification" ),
535            implementationDirectory );
536
537        edited = this.toString( f );
538        editor.edit( edited );
539        assertTrue( editor.isSectionPresent( "Constructors" ) );
540
541        f = new File( specificationDirectory, "Specification.java" );
542        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutAnnotationsSection.java.txt", f );
543        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
544                                              specificationDirectory );
545
546        edited = this.toString( f );
547        editor.edit( edited );
548        assertTrue( editor.isSectionPresent( "Annotations" ) );
549
550        this.getJomcTool().setInputEncoding( null );
551        this.getJomcTool().setOutputEncoding( null );
552    }
553
554    @Test
555    public final void testOptionalSections() throws Exception
556    {
557        final SectionEditor editor = new SectionEditor();
558        final File implementationDirectory = this.getNextOutputDirectory();
559        final File specificationDirectory = this.getNextOutputDirectory();
560
561        assertTrue( specificationDirectory.mkdirs() );
562        assertTrue( implementationDirectory.mkdirs() );
563
564        this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
565        this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
566
567        File f = new File( implementationDirectory, "Implementation.java" );
568        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutConstructorsSection.java.txt", f );
569        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
570                                              implementationDirectory );
571
572        String edited = this.toString( f );
573        editor.edit( edited );
574        assertFalse( editor.isSectionPresent( "Constructors" ) );
575        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDefaultConstructorSection.java.txt",
576                           f );
577
578        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
579                                              implementationDirectory );
580
581        edited = this.toString( f );
582        editor.edit( edited );
583        assertTrue( editor.isSectionPresent( "Constructors" ) );
584        assertTrue( editor.isSectionPresent( "Default Constructor" ) );
585        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDocumentationSection.java.txt", f );
586        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
587                                              implementationDirectory );
588
589        edited = this.toString( f );
590        editor.edit( edited );
591        assertFalse( editor.isSectionPresent( "Documentation" ) );
592        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutLicenseSection.java.txt", f );
593        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
594                                              implementationDirectory );
595
596        edited = this.toString( f );
597        editor.edit( edited );
598        assertFalse( editor.isSectionPresent( "License Header" ) );
599
600        f = new File( specificationDirectory, "Specification.java" );
601        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutDocumentationSection.java.txt", f );
602        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
603                                              specificationDirectory );
604
605        edited = this.toString( f );
606        editor.edit( edited );
607        assertFalse( editor.isSectionPresent( "Documentation" ) );
608        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutLicenseSection.java.txt", f );
609        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
610                                              specificationDirectory );
611
612        edited = this.toString( f );
613        editor.edit( edited );
614        assertFalse( editor.isSectionPresent( "License Header" ) );
615
616        this.getJomcTool().setInputEncoding( null );
617        this.getJomcTool().setOutputEncoding( null );
618    }
619
620    @Test
621    public final void testSourceFileEditor() throws Exception
622    {
623        assertNotNull( this.getJomcTool().getSourceFileEditor() );
624        this.getJomcTool().setSourceFileEditor( null );
625        assertNotNull( this.getJomcTool().getSourceFileEditor() );
626    }
627
628    @Test
629    public final void testCopyConstructor() throws Exception
630    {
631        try
632        {
633            new SourceFileProcessor( null );
634            fail( "Expected NullPointerException not thrown." );
635        }
636        catch ( final NullPointerException e )
637        {
638            assertNotNull( e.getMessage() );
639            System.out.println( e.toString() );
640        }
641
642        new SourceFileProcessor( this.getJomcTool() );
643    }
644
645    @Test
646    public final void testSourceFileProcessorModelObjectsNotFound() throws Exception
647    {
648        final File tmpDir = new File( System.getProperty( "java.io.tmpdir", "/tmp" ) );
649        final Module m = new Module();
650        m.setName( "DOES_NOT_EXIST" );
651
652        final Specification s = new Specification();
653        s.setIdentifier( "DOES_NOT_EXIST)" );
654
655        final Implementation i = new Implementation();
656        i.setIdentifier( "DOES_NOT_EXIST" );
657
658        final Model oldModel = this.getJomcTool().getModel();
659        this.getJomcTool().setModel( null );
660
661        assertNull( this.getJomcTool().getSourceFilesType( s ) );
662        assertNull( this.getJomcTool().getSourceFilesType( i ) );
663
664        this.getJomcTool().manageSourceFiles( tmpDir );
665        this.getJomcTool().manageSourceFiles( m, tmpDir );
666        this.getJomcTool().manageSourceFiles( s, tmpDir );
667        this.getJomcTool().manageSourceFiles( i, tmpDir );
668
669        this.getJomcTool().setModel( oldModel );
670    }
671
672    private void copyResource( final String resourceName, final File file ) throws IOException
673    {
674        assertTrue( resourceName.startsWith( "/" ) );
675
676        InputStream in = null;
677        OutputStream out = null;
678
679        try
680        {
681            in = this.getClass().getResourceAsStream( resourceName );
682            assertNotNull( "Resource '" + resourceName + "' not found.", in );
683            out = new FileOutputStream( file );
684            IOUtils.copy( in, out );
685
686            out.close();
687            out = null;
688
689            in.close();
690            in = null;
691        }
692        finally
693        {
694            try
695            {
696                if ( out != null )
697                {
698                    out.close();
699                }
700            }
701            catch ( final IOException e )
702            {
703                // Suppressed.
704            }
705            finally
706            {
707                try
708                {
709                    if ( in != null )
710                    {
711                        in.close();
712                    }
713                }
714                catch ( final IOException e )
715                {
716                    // Suppressed.
717                }
718            }
719        }
720    }
721
722    private String toString( final File f ) throws IOException
723    {
724        InputStream in = null;
725
726        try
727        {
728            in = new FileInputStream( f );
729            final String str = IOUtils.toString( in, this.getResourceEncoding() );
730            in.close();
731            in = null;
732            return str;
733        }
734        finally
735        {
736            try
737            {
738                if ( in != null )
739                {
740                    in.close();
741                }
742            }
743            catch ( final IOException e )
744            {
745                // Suppressed.
746            }
747        }
748    }
749
750}