View Javadoc
1   package org.codehaus.plexus.archiver.war;
2   
3   import java.io.File;
4   import org.codehaus.plexus.PlexusTestCase;
5   import org.codehaus.plexus.archiver.ArchiveEntry;
6   import org.codehaus.plexus.archiver.Archiver;
7   import org.codehaus.plexus.archiver.ResourceIterator;
8   import org.codehaus.plexus.util.FileUtils;
9   
10  /**
11   * @author Kristian Rosenvold
12   */
13  public class WarArchiverTest
14      extends PlexusTestCase
15  {
16  
17      private final int expected = 8;
18  
19      public void testReAddingPlatformSpecificEncoding()
20          throws Exception
21      {
22          WarArchiver archiver = (WarArchiver) lookup( Archiver.ROLE, "war" );
23          archiver.setDestFile( new File( getTargetRarFolder(), "test.war" ) );
24  
25          File dummyContent = getTestFile( "src/test/resources/folders" );
26          archiver.addDirectory( dummyContent );
27  
28          assertEquals( expected, count( archiver.getResources() ) ); // I wonder what the first entry is
29  
30          File file = getTestFile( "src/test/resources/folders/WEB-INF/web.xml" );
31          archiver.setWebxml( file );
32  
33          assertEquals( expected, count( archiver.getResources() ) ); // I wonder what the first entry is
34  
35          archiver.createArchive();
36          assertTrue( new File( getTargetRarFolder(), "test.war" ).exists() );
37      }
38  
39      public void testInfiniteRecursion()
40          throws Exception
41      {
42          WarArchiver archiver = (WarArchiver) lookup( Archiver.ROLE, "war" );
43          archiver.setDestFile( new File( getTargetRarFolder(), "test.war" ) );
44  
45          // Easy to produce infinite recursion if you just add existing files again and again
46          File dummyContent = getTestFile( "src/test/resources/folders", "File.txt" );
47          final int INFINITY = 10;
48          for ( int i = 0; i < INFINITY; i++ )
49          {
50              archiver.addFile( dummyContent, "testZ" );
51          }
52          assertEquals( 1, count( archiver.getResources() ) ); // I wonder what the first entry is
53      }
54  
55      public File getTargetRarFolder()
56      {
57          return new File( getBasedir(), "/target/wartest/" );
58      }
59  
60      @Override
61      protected void setUp()
62          throws Exception
63      {
64          super.setUp();
65          // clean output directory and re create it
66          if ( getTargetRarFolder().exists() )
67          {
68              FileUtils.deleteDirectory( getTargetRarFolder() );
69          }
70      }
71  
72      private int count( ResourceIterator resourceIterator )
73      {
74          int i = 0;
75          while ( resourceIterator.hasNext() )
76          {
77              i++;
78              ArchiveEntry next = resourceIterator.next();
79              System.out.print( next.getMode() );
80              System.out.println( next.getName() );
81          }
82          return i;
83      }
84  
85  }