19
19
* under the License.
20
20
*/
21
21
22
+ import static org .hamcrest .MatcherAssert .assertThat ;
23
+ import static org .hamcrest .Matchers .startsWith ;
22
24
import static org .junit .Assert .assertEquals ;
25
+ import static org .junit .Assert .fail ;
23
26
import static org .junit .Assume .assumeFalse ;
24
27
import static org .mockito .Mockito .mock ;
25
28
import static org .mockito .Mockito .times ;
26
29
import static org .mockito .Mockito .verify ;
27
30
import static org .mockito .Mockito .when ;
28
31
29
32
import java .io .File ;
33
+ import java .io .FileOutputStream ;
30
34
import java .io .IOException ;
35
+ import java .util .ArrayList ;
36
+ import java .util .Arrays ;
37
+ import java .util .List ;
31
38
import java .util .Set ;
32
39
import java .util .TreeSet ;
40
+ import java .util .jar .JarOutputStream ;
33
41
34
42
import org .apache .maven .artifact .Artifact ;
35
43
import org .apache .maven .artifact .DefaultArtifact ;
44
+ import org .apache .maven .artifact .DependencyResolutionRequiredException ;
45
+ import org .apache .maven .model .Build ;
36
46
import org .apache .maven .plugin .logging .Log ;
37
47
import org .apache .maven .project .MavenProject ;
38
48
import org .junit .Before ;
49
+ import org .junit .Rule ;
39
50
import org .junit .Test ;
40
51
import org .junit .rules .TemporaryFolder ;
41
52
import org .mockito .ArgumentCaptor ;
42
53
43
54
public class MinijarFilterTest
44
55
{
45
56
57
+ @ Rule
58
+ public TemporaryFolder tempFolder = TemporaryFolder .builder ().assureDeletion ().build ();
59
+
60
+ private File outputDirectory ;
46
61
private File emptyFile ;
62
+ private File jarFile ;
63
+ private Log log ;
64
+ private ArgumentCaptor <CharSequence > logCaptor ;
47
65
48
66
@ Before
49
67
public void init ()
50
68
throws IOException
51
69
{
52
- TemporaryFolder tempFolder = new TemporaryFolder ();
53
- tempFolder .create ();
70
+ this .outputDirectory = tempFolder .newFolder ();
54
71
this .emptyFile = tempFolder .newFile ();
55
-
72
+ this .jarFile = tempFolder .newFile ();
73
+ new JarOutputStream ( new FileOutputStream ( this .jarFile ) ).close ();
74
+ this .log = mock (Log .class );
75
+ logCaptor = ArgumentCaptor .forClass (CharSequence .class );
56
76
}
57
77
58
78
/**
@@ -64,11 +84,7 @@ public void testWithMockProject()
64
84
{
65
85
assumeFalse ( "Expected to run under JDK8+" , System .getProperty ("java.version" ).startsWith ("1.7" ) );
66
86
67
- ArgumentCaptor <CharSequence > logCaptor = ArgumentCaptor .forClass ( CharSequence .class );
68
-
69
- MavenProject mavenProject = mockProject ( emptyFile );
70
-
71
- Log log = mock ( Log .class );
87
+ MavenProject mavenProject = mockProject ( outputDirectory , emptyFile );
72
88
73
89
MinijarFilter mf = new MinijarFilter ( mavenProject , log );
74
90
@@ -84,14 +100,10 @@ public void testWithMockProject()
84
100
public void testWithPomProject ()
85
101
throws IOException
86
102
{
87
- ArgumentCaptor <CharSequence > logCaptor = ArgumentCaptor .forClass ( CharSequence .class );
88
-
89
103
// project with pom packaging and no artifact.
90
- MavenProject mavenProject = mockProject ( null );
104
+ MavenProject mavenProject = mockProject ( outputDirectory , null );
91
105
mavenProject .setPackaging ( "pom" );
92
106
93
- Log log = mock ( Log .class );
94
-
95
107
MinijarFilter mf = new MinijarFilter ( mavenProject , log );
96
108
97
109
mf .finished ();
@@ -105,7 +117,7 @@ public void testWithPomProject()
105
117
106
118
}
107
119
108
- private MavenProject mockProject ( File file )
120
+ private MavenProject mockProject ( File outputDirectory , File file , String ... classPathElements )
109
121
{
110
122
MavenProject mavenProject = mock ( MavenProject .class );
111
123
@@ -129,17 +141,29 @@ private MavenProject mockProject( File file )
129
141
130
142
when ( mavenProject .getArtifact ().getFile () ).thenReturn ( file );
131
143
132
- return mavenProject ;
144
+ Build build = new Build ();
145
+ build .setOutputDirectory ( outputDirectory .toString () );
146
+
147
+ List <String > classpath = new ArrayList <>();
148
+ classpath .add ( outputDirectory .toString () );
149
+ if ( file != null )
150
+ {
151
+ classpath .add (file .toString ());
152
+ }
153
+ classpath .addAll ( Arrays .asList ( classPathElements ) );
154
+ when ( mavenProject .getBuild () ).thenReturn ( build );
155
+ try {
156
+ when (mavenProject .getRuntimeClasspathElements ()).thenReturn (classpath );
157
+ } catch (DependencyResolutionRequiredException e ) {
158
+ fail ("Encountered unexpected exception: " + e .getClass ().getSimpleName () + ": " + e .getMessage ());
159
+ }
133
160
161
+ return mavenProject ;
134
162
}
135
163
136
164
@ Test
137
165
public void finsishedShouldProduceMessageForClassesTotalNonZero ()
138
166
{
139
- ArgumentCaptor <CharSequence > logCaptor = ArgumentCaptor .forClass ( CharSequence .class );
140
-
141
- Log log = mock ( Log .class );
142
-
143
167
MinijarFilter m = new MinijarFilter ( 1 , 50 , log );
144
168
145
169
m .finished ();
@@ -153,10 +177,6 @@ public void finsishedShouldProduceMessageForClassesTotalNonZero()
153
177
@ Test
154
178
public void finishedShouldProduceMessageForClassesTotalZero ()
155
179
{
156
- ArgumentCaptor <CharSequence > logCaptor = ArgumentCaptor .forClass ( CharSequence .class );
157
-
158
- Log log = mock ( Log .class );
159
-
160
180
MinijarFilter m = new MinijarFilter ( 0 , 0 , log );
161
181
162
182
m .finished ();
@@ -166,4 +186,24 @@ public void finishedShouldProduceMessageForClassesTotalZero()
166
186
assertEquals ( "Minimized 0 -> 0" , logCaptor .getValue () );
167
187
168
188
}
189
+
190
+ /**
191
+ * Verify that directories are ignored when scanning the classpath for JARs containing services,
192
+ * but warnings are logged instead
193
+ *
194
+ * @see <a href="https://issues.apache.org/jira/browse/MSHADE-366">MSHADE-366</a>
195
+ */
196
+ @ Test
197
+ public void removeServicesShouldIgnoreDirectories () throws Exception {
198
+ String classPathElementToIgnore = tempFolder .newFolder ().getAbsolutePath ();
199
+ MavenProject mockedProject = mockProject ( outputDirectory , jarFile , classPathElementToIgnore );
200
+
201
+ new MinijarFilter (mockedProject , log );
202
+
203
+ verify ( log , times ( 1 ) ).warn ( logCaptor .capture () );
204
+
205
+ assertThat ( logCaptor .getValue ().toString (), startsWith (
206
+ "Not a JAR file candidate. Ignoring classpath element '" + classPathElementToIgnore + "' (" ) );
207
+ }
208
+
169
209
}
0 commit comments