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
36
37 public class WarDependenciesAnalysisTest
38 extends AbstractWarExplodedMojoTest
39 {
40 protected File getPomFile()
41 {
42 return new File( getBasedir(), "/target/test-classes/unit/dependencies/default.xml" );
43 }
44
45 protected File getTestDirectory()
46 {
47 return new File( getBasedir(), "target/test-classes/unit/dependenciesanalysis/test-dir" );
48 }
49
50
51 public void testNoChange()
52 throws Exception
53 {
54
55 final String testId = "no-change";
56 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
57 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
58 jarArtifact.setArtifactId( "lib-test" );
59 jarArtifact.setVersion( "1.0" );
60
61 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, new ArtifactStub[]{jarArtifact},
62 new String[]{"WEB-INF/lib/lib-test-1.0.jar"},
63 new String[]{"WEB-INF/lib/lib-test-1.0.jar"} );
64
65 }
66
67 public void testRemovedDependency()
68 throws Exception
69 {
70
71 final String testId = "remove-dependency";
72 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
73 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
74 jarArtifact.setArtifactId( "lib-test" );
75 jarArtifact.setVersion( "1.0" );
76
77 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, null,
78 new String[]{"WEB-INF/lib/lib-test-1.0.jar"}, null );
79
80 }
81
82 public void testDependencyWithUpdatedVersion()
83 throws Exception
84 {
85
86 final String testId = "dependency-update-version";
87 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
88 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
89 jarArtifact.setArtifactId( "lib-test" );
90 jarArtifact.setVersion( "1.0" );
91
92 ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
93 jarArtifact2.setArtifactId( "lib-test" );
94 jarArtifact2.setVersion( "2.0" );
95
96 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, new ArtifactStub[]{jarArtifact2},
97 new String[]{"WEB-INF/lib/lib-test-1.0.jar"},
98 new String[]{"WEB-INF/lib/lib-test-2.0.jar"} );
99
100 }
101
102 public void testDependencyNowProvided()
103 throws Exception
104 {
105
106 final String testId = "dependency-now-provided";
107 final ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE, "jar" );
108 ArtifactStub jarArtifact = new JarArtifactStub( getBasedir(), artifactHandler );
109 jarArtifact.setArtifactId( "lib-test" );
110 jarArtifact.setVersion( "1.0" );
111
112 ArtifactStub jarArtifact2 = new JarArtifactStub( getBasedir(), artifactHandler );
113 jarArtifact2.setArtifactId( "lib-test" );
114 jarArtifact2.setVersion( "1.0" );
115 jarArtifact2.setScope( Artifact.SCOPE_PROVIDED );
116
117 doTestTwiceWithUpdatedDependency( testId, new ArtifactStub[]{jarArtifact}, 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 assertedFiles = new ArrayList();
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 assertedFiles2 = new ArrayList();
150 assertedFiles2.addAll( assertDefaultContent( webAppDirectory ) );
151 assertedFiles2.addAll( assertWebXml( webAppDirectory ) );
152 if ( secondCustomContent != null )
153 {
154 assertedFiles2.addAll(
155 assertCustomContent( webAppDirectory, secondCustomContent, "library not found" ) );
156
157 }
158 assertWebAppContent( webAppDirectory, assertedFiles2, filter );
159
160 }
161 finally
162 {
163 cleanDirectory( webAppDirectory );
164 cleanDirectory( mojo.getWorkDirectory() );
165
166 }
167 }
168
169
170
171
172
173
174
175
176
177 protected File setUpMojoWithCache( final String testId, ArtifactStub[] artifactStubs )
178 throws Exception
179 {
180 final File webappDir = setUpMojo( testId, artifactStubs, null );
181 setVariableValueToObject( mojo, "useCache", Boolean.TRUE );
182 final File cacheFile = new File( mojo.getWorkDirectory(), "webapp-cache.xml" );
183 setVariableValueToObject( mojo, "cacheFile", cacheFile );
184
185 return webappDir;
186 }
187
188
189 }