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