1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.war;
20
21 import java.io.File;
22 import java.util.LinkedList;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.handler.ArtifactHandler;
26 import org.apache.maven.plugins.war.overlay.DefaultOverlay;
27 import org.apache.maven.plugins.war.stub.MavenZipProject;
28 import org.apache.maven.plugins.war.stub.WarArtifactStub;
29 import org.apache.maven.plugins.war.stub.ZipArtifactStub;
30 import org.codehaus.plexus.util.FileUtils;
31
32
33
34
35
36 public class WarZipTest extends AbstractWarMojoTest {
37 WarMojo mojo;
38
39 private static File pomFile = new File(getBasedir(), "src/test/resources/unit/warziptest/war-with-zip.xml");
40
41 protected File getTestDirectory() {
42 return new File(getBasedir(), "target/test-classes/unit/warziptest");
43 }
44
45 public void setUp() throws Exception {
46 super.setUp();
47 mojo = (WarMojo) lookupMojo("war", pomFile);
48 }
49
50 private Artifact buildZipArtifact() throws Exception {
51 ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE, "jar");
52 File zipFile = new File(getTestDirectory(), "foobar.zip");
53 return new ZipArtifactStub("src/test/resources/unit/warziptest", artifactHandler, zipFile);
54 }
55
56 private File configureMojo(String testId) throws Exception {
57 MavenZipProject project = new MavenZipProject();
58 String outputDir = getTestDirectory().getAbsolutePath() + File.separatorChar + testId + "-output";
59
60 File outputDirFile = new File(outputDir);
61 if (outputDirFile.exists()) {
62 FileUtils.deleteDirectory(outputDirFile);
63 }
64 File webAppDirectory = new File(getTestDirectory(), testId);
65 WarArtifactStub warArtifact = new WarArtifactStub(getBasedir());
66 String warName = "simple";
67 File webAppSource = createWebAppSource(testId);
68 File classesDir = createClassesDir(testId, true);
69 File xmlSource = createXMLConfigDir(testId, new String[] {"web.xml"});
70 project.setArtifact(warArtifact);
71
72 this.configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
73 setVariableValueToObject(mojo, "outputDirectory", outputDir);
74 setVariableValueToObject(mojo, "warName", warName);
75 setVariableValueToObject(mojo, "workDirectory", new File(getTestDirectory(), "work"));
76 mojo.setWebXml(new File(xmlSource, "web.xml"));
77
78 project.getArtifacts().add(buildZipArtifact());
79
80 return webAppDirectory;
81 }
82
83 public void testOneZipWithNoSkip() throws Exception {
84 File webAppDirectory = configureMojo("one-zip");
85
86 Overlay overlay = new DefaultOverlay(buildZipArtifact());
87
88 overlay.setType("zip");
89 mojo.addOverlay(overlay);
90 mojo.execute();
91
92 File foo = new File(webAppDirectory, "foo.txt");
93 assertTrue("foo.txt not exists", foo.exists());
94 assertTrue("foo.txt not a file", foo.isFile());
95
96 File barDirectory = new File(webAppDirectory, "bar");
97 assertTrue("bar directory not exists", barDirectory.exists());
98 assertTrue("bar not a directory", barDirectory.isDirectory());
99
100 File bar = new File(barDirectory, "bar.txt");
101 assertTrue("bar/bar.txt not exists", bar.exists());
102 assertTrue("bar/bar.txt not a file", bar.isFile());
103 }
104
105 public void testOneZipWithTargetPathOverlay() throws Exception {
106 File webAppDirectory = configureMojo("one-zip-overlay-targetPath");
107
108 Overlay overlay = new DefaultOverlay(buildZipArtifact());
109 overlay.setSkip(false);
110 overlay.setType("zip");
111 overlay.setTargetPath("overridePath");
112 mojo.addOverlay(overlay);
113
114 mojo.execute();
115
116 File foo = new File(webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt");
117 assertTrue("foo.txt not exists", foo.exists());
118 assertTrue("foo.txt not a file", foo.isFile());
119
120 File barDirectory = new File(webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar");
121 assertTrue("bar directory not exists", barDirectory.exists());
122 assertTrue("bar not a directory", barDirectory.isDirectory());
123
124 File bar = new File(barDirectory, "bar.txt");
125 assertTrue("bar/bar.txt not exists", bar.exists());
126 assertTrue("bar/bar.txt not a file", bar.isFile());
127 }
128
129 public void testOneZipDefaultSkip() throws Exception {
130 File webAppDirectory = configureMojo("one-zip-overlay-skip");
131
132 mojo.execute();
133
134 assertZipContentNotHere(webAppDirectory);
135 }
136
137 public void testOneZipWithForceSkip() throws Exception {
138 File webAppDirectory = configureMojo("one-zip-overlay-skip");
139 Overlay overlay = new DefaultOverlay(buildZipArtifact());
140 overlay.setSkip(true);
141 overlay.setType("zip");
142 mojo.addOverlay(overlay);
143
144 mojo.execute();
145 assertZipContentNotHere(webAppDirectory);
146 }
147
148 protected void assertZipContentNotHere(File webAppDirectory) {
149 File foo = new File(webAppDirectory.getPath() + File.separatorChar + "overridePath", "foo.txt");
150 assertFalse("foo.txt exists", foo.exists());
151 assertFalse("foo.txt a file", foo.isFile());
152
153 File barDirectory = new File(webAppDirectory.getPath() + File.separatorChar + "overridePath", "bar");
154 assertFalse("bar directory exists", barDirectory.exists());
155 assertFalse("bar is a directory", barDirectory.isDirectory());
156
157 File bar = new File(barDirectory, "bar.txt");
158 assertFalse("bar/bar.txt exists", bar.exists());
159 assertFalse("bar/bar.txt is a file", bar.isFile());
160 }
161 }