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.tools.plugin.util;
20  
21  import java.util.Collections;
22  
23  import org.apache.maven.plugin.descriptor.PluginDescriptor;
24  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertFalse;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  
31  /**
32   * @author jdcasey
33   */
34  class PluginUtilsTest {
35      @Test
36      void testShouldTrimArtifactIdToFindPluginId() {
37          assertEquals("artifactId", PluginDescriptor.getGoalPrefixFromArtifactId("maven-artifactId-plugin"));
38          assertEquals("artifactId", PluginDescriptor.getGoalPrefixFromArtifactId("maven-plugin-artifactId"));
39          assertEquals("artifactId", PluginDescriptor.getGoalPrefixFromArtifactId("artifactId-maven-plugin"));
40          assertEquals("artifactId", PluginDescriptor.getGoalPrefixFromArtifactId("artifactId"));
41          assertEquals("artifactId", PluginDescriptor.getGoalPrefixFromArtifactId("artifactId-plugin"));
42          assertEquals("plugin", PluginDescriptor.getGoalPrefixFromArtifactId("maven-plugin-plugin"));
43      }
44  
45      @Test
46      void testShouldFindTwoScriptsWhenNoExcludesAreGiven() {
47          String testScript = "test.txt";
48  
49          String basedir = TestUtils.dirname(testScript);
50  
51          String includes = "**/*.txt";
52  
53          String[] files = PluginUtils.findSources(basedir, includes);
54          assertEquals(2, files.length);
55      }
56  
57      @Test
58      void testShouldFindOneScriptsWhenAnExcludeIsGiven() {
59          String testScript = "test.txt";
60  
61          String basedir = TestUtils.dirname(testScript);
62  
63          String includes = "**/*.txt";
64          String excludes = "**/*Excludes.txt";
65  
66          String[] files = PluginUtils.findSources(basedir, includes, excludes);
67          assertEquals(1, files.length);
68      }
69  
70      @Test
71      void testIsMavenReport() throws Exception {
72          try {
73              PluginUtils.isMavenReport(null, null);
74          } catch (IllegalArgumentException e) {
75              assertTrue(true);
76          }
77  
78          String impl = "org.apache.maven.tools.plugin.util.stubs.MavenReportStub";
79  
80          MavenProjectStub stub = new MavenProjectStub();
81          stub.setCompileSourceRoots(Collections.singletonList(System.getProperty("basedir") + "/target/classes"));
82  
83          assertTrue(PluginUtils.isMavenReport(impl, stub));
84  
85          impl = "org.apache.maven.tools.plugin.util.stubs.MojoStub";
86          assertFalse(PluginUtils.isMavenReport(impl, stub));
87      }
88  }