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