View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.filter;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Set;
28  import java.util.TreeSet;
29  import java.util.jar.JarOutputStream;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.DefaultArtifact;
33  import org.apache.maven.artifact.DependencyResolutionRequiredException;
34  import org.apache.maven.model.Build;
35  import org.apache.maven.plugin.logging.Log;
36  import org.apache.maven.project.MavenProject;
37  import org.junit.Before;
38  import org.junit.Rule;
39  import org.junit.Test;
40  import org.junit.rules.TemporaryFolder;
41  import org.mockito.ArgumentCaptor;
42  
43  import static org.junit.Assert.assertEquals;
44  import static org.junit.Assert.fail;
45  import static org.junit.Assume.assumeFalse;
46  import static org.mockito.Mockito.mock;
47  import static org.mockito.Mockito.times;
48  import static org.mockito.Mockito.verify;
49  import static org.mockito.Mockito.when;
50  
51  public class MinijarFilterTest {
52  
53      @Rule
54      public TemporaryFolder tempFolder =
55              TemporaryFolder.builder().assureDeletion().build();
56  
57      private File outputDirectory;
58      private File emptyFile;
59      private File jarFile;
60      private Log log;
61      private ArgumentCaptor<CharSequence> logCaptor;
62  
63      @Before
64      public void init() throws IOException {
65          this.outputDirectory = tempFolder.newFolder();
66          this.emptyFile = tempFolder.newFile();
67          this.jarFile = tempFolder.newFile();
68          new JarOutputStream(Files.newOutputStream(this.jarFile.toPath())).close();
69          this.log = mock(Log.class);
70          logCaptor = ArgumentCaptor.forClass(CharSequence.class);
71      }
72  
73      /**
74       * This test will fail on JDK 7 because jdependency needs at least JDK 8.
75       */
76      @Test
77      public void testWithMockProject() throws IOException {
78          assumeFalse(
79                  "Expected to run under JDK8+",
80                  System.getProperty("java.version").startsWith("1.7"));
81  
82          MavenProject mavenProject = mockProject(outputDirectory, emptyFile);
83  
84          MinijarFilter mf = new MinijarFilter(mavenProject, log);
85  
86          mf.finished();
87  
88          verify(log, times(1)).info(logCaptor.capture());
89  
90          assertEquals("Minimized 0 -> 0", logCaptor.getValue());
91      }
92  
93      @Test
94      public void testWithPomProject() throws IOException {
95          // project with pom packaging and no artifact.
96          MavenProject mavenProject = mockProject(outputDirectory, null);
97          mavenProject.setPackaging("pom");
98  
99          MinijarFilter mf = new MinijarFilter(mavenProject, log);
100 
101         mf.finished();
102 
103         verify(log, times(1)).info(logCaptor.capture());
104 
105         // verify no access to project's artifacts
106         verify(mavenProject, times(0)).getArtifacts();
107 
108         assertEquals("Minimized 0 -> 0", logCaptor.getValue());
109     }
110 
111     private MavenProject mockProject(File outputDirectory, File file, String... classPathElements) {
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         Build build = new Build();
135         build.setOutputDirectory(outputDirectory.toString());
136 
137         List<String> classpath = new ArrayList<>();
138         classpath.add(outputDirectory.toString());
139         if (file != null) {
140             classpath.add(file.toString());
141         }
142         classpath.addAll(Arrays.asList(classPathElements));
143         when(mavenProject.getBuild()).thenReturn(build);
144         try {
145             when(mavenProject.getRuntimeClasspathElements()).thenReturn(classpath);
146         } catch (DependencyResolutionRequiredException e) {
147             fail("Encountered unexpected exception: " + e.getClass().getSimpleName() + ": " + e.getMessage());
148         }
149 
150         return mavenProject;
151     }
152 
153     @Test
154     public void finsishedShouldProduceMessageForClassesTotalNonZero() {
155         MinijarFilter m = new MinijarFilter(1, 50, log);
156 
157         m.finished();
158 
159         verify(log, times(1)).info(logCaptor.capture());
160 
161         assertEquals("Minimized 51 -> 1 (1%)", logCaptor.getValue());
162     }
163 
164     @Test
165     public void finishedShouldProduceMessageForClassesTotalZero() {
166         MinijarFilter m = new MinijarFilter(0, 0, log);
167 
168         m.finished();
169 
170         verify(log, times(1)).info(logCaptor.capture());
171 
172         assertEquals("Minimized 0 -> 0", logCaptor.getValue());
173     }
174 }