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