1 package org.apache.maven.plugin.dependency;
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.plugin.MojoFailureException;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.dependency.testUtils.DependencyArtifactStubFactory;
27 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
28 import org.apache.maven.project.MavenProject;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.HashSet;
33 import java.util.Set;
34
35 public class TestUnpackDependenciesMojo2
36 extends AbstractDependencyMojoTestCase
37 {
38
39 private final String UNPACKABLE_FILE = "test.txt";
40
41 private final String UNPACKABLE_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
42
43 UnpackDependenciesMojo mojo;
44
45 protected void setUp()
46 throws Exception
47 {
48
49 super.setUp( "unpack-dependencies", true );
50
51 File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
52 mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
53 mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
54 mojo.useJvmChmod = true;
55
56
57
58 stubFactory.setUnpackableFile( mojo.getArchiverManager() );
59
60
61 stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + UNPACKABLE_FILE_PATH ) );
62
63 assertNotNull( mojo );
64 assertNotNull( mojo.getProject() );
65 MavenProject project = mojo.getProject();
66
67 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
68 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
69 artifacts.addAll( directArtifacts );
70
71 project.setArtifacts( artifacts );
72 project.setDependencyArtifacts( directArtifacts );
73 mojo.markersDirectory = new File( this.testDir, "markers" );
74
75 }
76
77 protected void tearDown()
78 {
79 super.tearDown();
80
81 mojo = null;
82 System.gc();
83 }
84
85 public File getUnpackedFile( Artifact artifact )
86 {
87 File destDir = DependencyUtil.getFormattedOutputDirectory( mojo.isUseSubDirectoryPerScope(),
88 mojo.isUseSubDirectoryPerType(),
89 mojo.isUseSubDirectoryPerArtifact(),
90 mojo.useRepositoryLayout, mojo.stripVersion,
91 mojo.getOutputDirectory(), artifact );
92 File unpacked = new File( destDir, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
93 assertTrue( unpacked.exists() );
94 return unpacked;
95 }
96
97 public void testDontOverWriteRelease()
98 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
99 {
100
101 Set<Artifact> artifacts = new HashSet<Artifact>();
102 Artifact release = stubFactory.getReleaseArtifact();
103 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
104
105 artifacts.add( release );
106
107 mojo.project.setArtifacts( artifacts );
108 mojo.project.setDependencyArtifacts( artifacts );
109
110 mojo.overWriteIfNewer = false;
111
112 mojo.execute();
113
114 assertUnpacked( release, false );
115 }
116
117 public void testOverWriteRelease()
118 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
119 {
120
121 Set<Artifact> artifacts = new HashSet<Artifact>();
122 Artifact release = stubFactory.getReleaseArtifact();
123 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
124
125 artifacts.add( release );
126
127 mojo.project.setArtifacts( artifacts );
128 mojo.project.setDependencyArtifacts( artifacts );
129
130 mojo.overWriteReleases = true;
131 mojo.overWriteIfNewer = false;
132
133 mojo.execute();
134
135 assertUnpacked( release, true );
136 }
137
138 public void testDontOverWriteSnap()
139 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
140 {
141
142 Set<Artifact> artifacts = new HashSet<Artifact>();
143 Artifact snap = stubFactory.getSnapshotArtifact();
144 snap.getFile().setLastModified( System.currentTimeMillis() - 2000 );
145
146 artifacts.add( snap );
147
148 mojo.project.setArtifacts( artifacts );
149 mojo.project.setDependencyArtifacts( artifacts );
150
151 mojo.overWriteReleases = false;
152 mojo.overWriteSnapshots = false;
153 mojo.overWriteIfNewer = false;
154
155 mojo.execute();
156
157 assertUnpacked( snap, false );
158 }
159
160 public void testOverWriteSnap()
161 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
162 {
163
164 Set<Artifact> artifacts = new HashSet<Artifact>();
165 Artifact snap = stubFactory.getSnapshotArtifact();
166 snap.getFile().setLastModified( System.currentTimeMillis() - 2000 );
167
168 artifacts.add( snap );
169
170 mojo.project.setArtifacts( artifacts );
171 mojo.project.setDependencyArtifacts( artifacts );
172
173 mojo.overWriteReleases = false;
174 mojo.overWriteSnapshots = true;
175 mojo.overWriteIfNewer = false;
176
177 mojo.execute();
178
179 assertUnpacked( snap, true );
180
181 }
182
183 public void testOverWriteIfNewer()
184 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException
185 {
186
187 Set<Artifact> artifacts = new HashSet<Artifact>();
188 Artifact snap = stubFactory.getSnapshotArtifact();
189 snap.getFile().setLastModified( System.currentTimeMillis() - 2000 );
190
191 artifacts.add( snap );
192
193 mojo.project.setArtifacts( artifacts );
194 mojo.project.setDependencyArtifacts( artifacts );
195
196 mojo.overWriteReleases = false;
197 mojo.overWriteSnapshots = false;
198 mojo.overWriteIfNewer = false;
199
200 mojo.execute();
201
202 File unpackedFile = getUnpackedFile( snap );
203
204
205 long time = System.currentTimeMillis();
206 time = time - ( time % 1000 );
207
208 snap.getFile().setLastModified( time + 3000 );
209 unpackedFile.setLastModified( time );
210
211
212 Thread.sleep( 1000 );
213
214 assertEquals( time, unpackedFile.lastModified() );
215 mojo.execute();
216 System.gc();
217
218 assertEquals( time, unpackedFile.lastModified() );
219
220 mojo.overWriteIfNewer = true;
221
222 mojo.execute();
223
224 assertTrue( time != unpackedFile.lastModified() );
225
226 System.gc();
227 }
228
229 public void assertUnpacked( Artifact artifact, boolean overWrite )
230 throws InterruptedException, MojoExecutionException, MojoFailureException
231 {
232 File unpackedFile = getUnpackedFile( artifact );
233
234 Thread.sleep( 100 );
235
236 long time = System.currentTimeMillis();
237 time = time - ( time % 1000 );
238 unpackedFile.setLastModified( time );
239
240
241 Thread.sleep( 1000 );
242
243 assertEquals( time, unpackedFile.lastModified() );
244 mojo.execute();
245
246 if ( overWrite )
247 {
248 assertTrue( time != unpackedFile.lastModified() );
249 }
250 else
251 {
252 assertEquals( time, unpackedFile.lastModified() );
253 }
254 }
255 }