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.apache.maven.shared.utils.io.Java7Support;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.rules.TemporaryFolder;
42  import org.mockito.ArgumentCaptor;
43  
44  public class MinijarFilterTest
45  {
46  
47      private File emptyFile;
48  
49      @Before
50      public void init()
51          throws IOException
52      {
53          TemporaryFolder tempFolder = new TemporaryFolder();
54          tempFolder.create();
55          this.emptyFile = tempFolder.newFile();
56  
57      }
58  
59      /**
60       * This test will fail on JDK 7 cause the used jdependency needs at least 
61       * JDK 8.
62       */
63      @Test
64      public void testWithMockProject()
65          throws IOException
66      {
67          assumeFalse( "Expected to run under JDK8+", Java7Support.isJava7() );
68  
69          ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
70  
71          MavenProject mavenProject = mockProject( emptyFile );
72  
73          Log log = mock( Log.class );
74  
75          MinijarFilter mf = new MinijarFilter( mavenProject, log );
76  
77          mf.finished();
78  
79          verify( log, times( 1 ) ).info( logCaptor.capture() );
80  
81          assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
82  
83      }
84  
85      @Test
86      public void testWithPomProject()
87          throws IOException
88      {
89          ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
90  
91          // project with pom packaging and no artifact.
92          MavenProject mavenProject = mockProject( null );
93          mavenProject.setPackaging( "pom" );
94  
95          Log log = mock( Log.class );
96  
97          MinijarFilter mf = new MinijarFilter( mavenProject, log );
98  
99          mf.finished();
100 
101         verify( log, times( 1 ) ).info( logCaptor.capture() );
102 
103         // verify no access to project's artifacts
104         verify( mavenProject, times( 0 ) ).getArtifacts();
105 
106         assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
107 
108     }
109 
110     private MavenProject mockProject( File file )
111     {
112         MavenProject mavenProject = mock( MavenProject.class );
113 
114         Artifact artifact = mock( Artifact.class );
115         when( artifact.getGroupId() ).thenReturn( "com" );
116         when( artifact.getArtifactId() ).thenReturn( "aid" );
117         when( artifact.getVersion() ).thenReturn( "1.9" );
118         when( artifact.getClassifier() ).thenReturn( "classifier1" );
119         when( artifact.getScope() ).thenReturn( Artifact.SCOPE_COMPILE );
120 
121         when( mavenProject.getArtifact() ).thenReturn( artifact );
122 
123         DefaultArtifact dependencyArtifact =
124             new DefaultArtifact( "dep.com", "dep.aid", "1.0", "compile", "jar", "classifier2", null );
125         dependencyArtifact.setFile( file );
126 
127         Set<Artifact> artifacts = new TreeSet<>();
128         artifacts.add( dependencyArtifact );
129 
130         when( mavenProject.getArtifacts() ).thenReturn( artifacts );
131 
132         when( mavenProject.getArtifact().getFile() ).thenReturn( file );
133 
134         return mavenProject;
135 
136     }
137 
138     @Test
139     public void finsishedShouldProduceMessageForClassesTotalNonZero()
140     {
141         ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
142 
143         Log log = mock( Log.class );
144 
145         MinijarFilter m = new MinijarFilter( 1, 50, log );
146 
147         m.finished();
148 
149         verify( log, times( 1 ) ).info( logCaptor.capture() );
150 
151         assertEquals( "Minimized 51 -> 1 (1%)", logCaptor.getValue() );
152 
153     }
154 
155     @Test
156     public void finishedShouldProduceMessageForClassesTotalZero()
157     {
158         ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( CharSequence.class );
159 
160         Log log = mock( Log.class );
161 
162         MinijarFilter m = new MinijarFilter( 0, 0, log );
163 
164         m.finished();
165 
166         verify( log, times( 1 ) ).info( logCaptor.capture() );
167 
168         assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
169 
170     }
171 }