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