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