View Javadoc
1   package org.apache.maven.plugins.shade.filter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assume.assumeFalse;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.Set;
32  import java.util.TreeSet;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.DefaultArtifact;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.project.MavenProject;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.rules.TemporaryFolder;
41  import org.mockito.ArgumentCaptor;
42  
43  public class MinijarFilterTest
44  {
45  
46      private File emptyFile;
47  
48      @Before
49      public void init()
50          throws IOException
51      {
52          TemporaryFolder tempFolder = new TemporaryFolder();
53          tempFolder.create();
54          this.emptyFile = tempFolder.newFile();
55  
56      }
57  
58      /**
59       * This test will fail on JDK 7 because jdependency needs at least JDK 8.
60       */
61      @Test
62      public void testWithMockProject()
63          throws IOException
64      {
65          assumeFalse( "Expected to run under JDK8+", System.getProperty("java.version").startsWith("1.7") );
66  
67          ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
68  
69          MavenProject mavenProject = mockProject( emptyFile );
70  
71          Log log = mock( Log.class );
72  
73          MinijarFilter mf = new MinijarFilter( mavenProject, log );
74  
75          mf.finished();
76  
77          verify( log, times( 1 ) ).info( logCaptor.capture() );
78  
79          assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
80  
81      }
82  
83      @Test
84      public void testWithPomProject()
85          throws IOException
86      {
87          ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
88  
89          // project with pom packaging and no artifact.
90          MavenProject mavenProject = mockProject( null );
91          mavenProject.setPackaging( "pom" );
92  
93          Log log = mock( Log.class );
94  
95          MinijarFilter mf = new MinijarFilter( mavenProject, log );
96  
97          mf.finished();
98  
99          verify( log, times( 1 ) ).info( logCaptor.capture() );
100 
101         // verify no access to project's artifacts
102         verify( mavenProject, times( 0 ) ).getArtifacts();
103 
104         assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
105 
106     }
107 
108     private MavenProject mockProject( File file )
109     {
110         MavenProject mavenProject = mock( MavenProject.class );
111 
112         Artifact artifact = mock( Artifact.class );
113         when( artifact.getGroupId() ).thenReturn( "com" );
114         when( artifact.getArtifactId() ).thenReturn( "aid" );
115         when( artifact.getVersion() ).thenReturn( "1.9" );
116         when( artifact.getClassifier() ).thenReturn( "classifier1" );
117         when( artifact.getScope() ).thenReturn( Artifact.SCOPE_COMPILE );
118 
119         when( mavenProject.getArtifact() ).thenReturn( artifact );
120 
121         DefaultArtifact dependencyArtifact =
122             new DefaultArtifact( "dep.com", "dep.aid", "1.0", "compile", "jar", "classifier2", null );
123         dependencyArtifact.setFile( file );
124 
125         Set<Artifact> artifacts = new TreeSet<>();
126         artifacts.add( dependencyArtifact );
127 
128         when( mavenProject.getArtifacts() ).thenReturn( artifacts );
129 
130         when( mavenProject.getArtifact().getFile() ).thenReturn( file );
131 
132         return mavenProject;
133 
134     }
135 
136     @Test
137     public void finsishedShouldProduceMessageForClassesTotalNonZero()
138     {
139         ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
140 
141         Log log = mock( Log.class );
142 
143         MinijarFilter m = new MinijarFilter( 1, 50, log );
144 
145         m.finished();
146 
147         verify( log, times( 1 ) ).info( logCaptor.capture() );
148 
149         assertEquals( "Minimized 51 -> 1 (1%)", logCaptor.getValue() );
150 
151     }
152 
153     @Test
154     public void finishedShouldProduceMessageForClassesTotalZero()
155     {
156         ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
157 
158         Log log = mock( Log.class );
159 
160         MinijarFilter m = new MinijarFilter( 0, 0, log );
161 
162         m.finished();
163 
164         verify( log, times( 1 ) ).info( logCaptor.capture() );
165 
166         assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
167 
168     }
169 }