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 4579 2012-06-03 00:28:14Z schulte2005 $
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:schulte2005@users.sourceforge.net">Christian Schulte</a>
058 * @version $JOMC: SourceFileProcessorTest.java 4579 2012-06-03 00:28:14Z schulte2005 $
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", f );
520        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
521                                              implementationDirectory );
522
523        edited = this.toString( f );
524        editor.edit( edited );
525        assertTrue( editor.isSectionPresent( "Constructors" ) );
526        assertTrue( editor.isSectionPresent( "Default Constructor" ) );
527        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDocumentationSection.java.txt", f );
528        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
529                                              implementationDirectory );
530
531        edited = this.toString( f );
532        editor.edit( edited );
533        assertFalse( editor.isSectionPresent( "Documentation" ) );
534        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutLicenseSection.java.txt", f );
535        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
536                                              implementationDirectory );
537
538        edited = this.toString( f );
539        editor.edit( edited );
540        assertFalse( editor.isSectionPresent( "License Header" ) );
541
542        f = new File( specificationDirectory, "Specification.java" );
543        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutDocumentationSection.java.txt", f );
544        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
545                                              specificationDirectory );
546
547        edited = this.toString( f );
548        editor.edit( edited );
549        assertFalse( editor.isSectionPresent( "Documentation" ) );
550        this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutLicenseSection.java.txt", f );
551        this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
552                                              specificationDirectory );
553
554        edited = this.toString( f );
555        editor.edit( edited );
556        assertFalse( editor.isSectionPresent( "License Header" ) );
557
558        this.getJomcTool().setInputEncoding( null );
559        this.getJomcTool().setOutputEncoding( null );
560    }
561
562    @Test
563    public final void testSourceFileEditor() throws Exception
564    {
565        assertNotNull( this.getJomcTool().getSourceFileEditor() );
566        this.getJomcTool().setSourceFileEditor( null );
567        assertNotNull( this.getJomcTool().getSourceFileEditor() );
568    }
569
570    @Test
571    public final void testCopyConstructor() throws Exception
572    {
573        try
574        {
575            new SourceFileProcessor( null );
576            fail( "Expected NullPointerException not thrown." );
577        }
578        catch ( final NullPointerException e )
579        {
580            assertNotNull( e.getMessage() );
581            System.out.println( e.toString() );
582        }
583
584        new SourceFileProcessor( this.getJomcTool() );
585    }
586
587    @Test
588    public final void testSourceFileProcessorModelObjectsNotFound() throws Exception
589    {
590        final File tmpDir = new File( System.getProperty( "java.io.tmpdir", "/tmp" ) );
591        final Module m = new Module();
592        m.setName( "DOES_NOT_EXIST" );
593
594        final Specification s = new Specification();
595        s.setIdentifier( "DOES_NOT_EXIST)" );
596
597        final Implementation i = new Implementation();
598        i.setIdentifier( "DOES_NOT_EXIST" );
599
600        final Model oldModel = this.getJomcTool().getModel();
601        this.getJomcTool().setModel( null );
602
603        assertNull( this.getJomcTool().getSourceFilesType( s ) );
604        assertNull( this.getJomcTool().getSourceFilesType( i ) );
605
606        this.getJomcTool().manageSourceFiles( tmpDir );
607        this.getJomcTool().manageSourceFiles( m, tmpDir );
608        this.getJomcTool().manageSourceFiles( s, tmpDir );
609        this.getJomcTool().manageSourceFiles( i, tmpDir );
610
611        this.getJomcTool().setModel( oldModel );
612    }
613
614    private void copyResource( final String resourceName, final File file ) throws IOException
615    {
616        assertTrue( resourceName.startsWith( "/" ) );
617
618        InputStream in = null;
619        boolean suppressExceptionOnClose = true;
620
621        try
622        {
623            in = this.getClass().getResourceAsStream( resourceName );
624            assertNotNull( "Resource '" + resourceName + "' not found.", in );
625            OutputStream out = null;
626
627            try
628            {
629                out = new FileOutputStream( file );
630                IOUtils.copy( in, out );
631                suppressExceptionOnClose = false;
632            }
633            finally
634            {
635                try
636                {
637                    if ( out != null )
638                    {
639                        out.close();
640                    }
641                }
642                catch ( final IOException e )
643                {
644                    if ( !suppressExceptionOnClose )
645                    {
646                        throw e;
647                    }
648                }
649            }
650        }
651        finally
652        {
653            try
654            {
655                if ( in != null )
656                {
657                    in.close();
658                }
659            }
660            catch ( final IOException e )
661            {
662                if ( !suppressExceptionOnClose )
663                {
664                    throw e;
665                }
666            }
667        }
668    }
669
670    private String toString( final File f ) throws IOException
671    {
672        InputStream in = null;
673        boolean suppressExceptionOnClose = true;
674
675        try
676        {
677            in = new FileInputStream( f );
678            final String str = IOUtils.toString( in, this.getResourceEncoding() );
679            suppressExceptionOnClose = false;
680            return str;
681        }
682        finally
683        {
684            try
685            {
686                if ( in != null )
687                {
688                    in.close();
689                }
690            }
691            catch ( final IOException e )
692            {
693                if ( !suppressExceptionOnClose )
694                {
695                    throw e;
696                }
697            }
698        }
699    }
700
701}