1 package org.apache.maven.plugin.war;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.handler.ArtifactHandler;
24 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
25 import org.apache.maven.plugin.war.stub.JarArtifactStub;
26
27 import java.io.File;
28 import java.io.FileFilter;
29 import java.util.ArrayList;
30 import java.util.List;
31
32
33
34
35 public class WarDependenciesAnalysisTest
36 extends AbstractWarExplodedMojoTest
37 {
38 protected File getPomFile()
39 {
40 return new File( getBasedir(), "/target/test-classes/unit/dependencies/default.xml" );
41 }
42
43 protected File getTestDirectory()
44 {
45 return new File( getBasedir(), "target/test-classes/unit/dependenciesanalysis/test-dir" );
46 }
47
48 public void testNoChange()
49 throws Exception
50 {
51
52 final String testId = "no-change";
53 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
54 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
55 jarArtifact.setArtifactId( "lib-test" );
56 jarArtifact.setVersion( "1.0" );
57
58 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
59 new ArtifactStub[] { jarArtifact },
60 new String[] { "WEB-INF/lib/lib-test-1.0.jar" },
61 new String[] { "WEB-INF/lib/lib-test-1.0.jar" } );
62
63 }
64
65 public void testRemovedDependency()
66 throws Exception
67 {
68
69 final String testId = "remove-dependency";
70 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
71 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
72 jarArtifact.setArtifactId( "lib-test" );
73 jarArtifact.setVersion( "1.0" );
74
75 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact }, null,
76 new String[] { "WEB-INF/lib/lib-test-1.0.jar" }, null );
77
78 }
79
80 public void testDependencyWithUpdatedVersion()
81 throws Exception
82 {
83
84 final String testId = "dependency-update-version";
85 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
86 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
87 jarArtifact.setArtifactId( "lib-test" );
88 jarArtifact.setVersion( "1.0" );
89
90 ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
91 jarArtifact2.setArtifactId( "lib-test" );
92 jarArtifact2.setVersion( "2.0" );
93
94 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
95 new ArtifactStub[] { jarArtifact2 },
96 new String[] { "WEB-INF/lib/lib-test-1.0.jar" },
97 new String[] { "WEB-INF/lib/lib-test-2.0.jar" } );
98
99 }
100
101 public void testDependencyNowProvided()
102 throws Exception
103 {
104
105 final String testId = "dependency-now-provided";
106 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
107 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
108 jarArtifact.setArtifactId( "lib-test" );
109 jarArtifact.setVersion( "1.0" );
110
111 ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
112 jarArtifact2.setArtifactId( "lib-test" );
113 jarArtifact2.setVersion( "1.0" );
114 jarArtifact2.setScope( Artifact.SCOPE_PROVIDED );
115
116 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[] { jarArtifact },
117 new ArtifactStub[] { jarArtifact2 },
118 new String[] { "WEB-INF/lib/lib-test-1.0.jar" }, null );
119
120 }
121
122 protected void doTestTwiceWithUpdatedDependency( String testId, ArtifactStub[] firstStubs,
123 ArtifactStub[] secondStubs, String[] firstCustomContent,
124 String[] secondCustomContent )
125 throws Exception
126 {
127
128 final File xmlSource = createXMLConfigDir( testId, new String[] { "web.xml" } );
129 final File webAppDirectory = setUpMojoWithCache( testId, firstStubs );
130 try
131 {
132 mojo.setWebXml( new File( xmlSource, "web.xml" ) );
133 mojo.execute();
134
135 final List<File> assertedFiles = new ArrayList<File>();
136 assertedFiles.addAll( assertDefaultContent( webAppDirectory ) );
137 assertedFiles.addAll( assertWebXml( webAppDirectory ) );
138 assertedFiles.addAll( assertCustomContent( webAppDirectory, firstCustomContent, "library not found" ) );
139
140
141 final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[] { MANIFEST_PATH } );
142 assertWebAppContent( webAppDirectory, assertedFiles, filter );
143
144
145 setUpMojoWithCache( testId, secondStubs );
146
147 mojo.execute();
148
149 final List<File> assertedFiles2 = new ArrayList<File>();
150 assertedFiles2.addAll( assertDefaultContent( webAppDirectory ) );
151 assertedFiles2.addAll( assertWebXml( webAppDirectory ) );
152 if ( secondCustomContent != null )
153 {
154 assertedFiles2.addAll( assertCustomContent( webAppDirectory, secondCustomContent, "library not found" ) );
155
156 }
157 assertWebAppContent( webAppDirectory, assertedFiles2, filter );
158
159 }
160 finally
161 {
162 cleanDirectory( webAppDirectory );
163 cleanDirectory( mojo.getWorkDirectory() );
164
165 }
166 }
167
168
169
170
171
172
173
174
175
176 protected File setUpMojoWithCache( final String testId, ArtifactStub[] artifactStubs )
177 throws Exception
178 {
179 final File webappDir = setUpMojo( testId, artifactStubs, null );
180 setVariableValueToObject( mojo, "useCache", Boolean.TRUE );
181 final File cacheFile = new File( mojo.getWorkDirectory(), "webapp-cache.xml" );
182 setVariableValueToObject( mojo, "cacheFile", cacheFile );
183
184 return webappDir;
185 }
186
187 }