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