001package org.apache.maven.tools.plugin.scanner; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.model.Build; 023import org.apache.maven.model.Model; 024import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 025import org.apache.maven.plugin.descriptor.MojoDescriptor; 026import org.apache.maven.plugin.descriptor.PluginDescriptor; 027import org.apache.maven.project.MavenProject; 028import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; 029import org.apache.maven.tools.plugin.extractor.ExtractionException; 030import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor; 031import org.junit.Before; 032import org.junit.Test; 033 034import java.io.File; 035import java.util.Arrays; 036import java.util.Collection; 037import java.util.Collections; 038import java.util.HashMap; 039import java.util.HashSet; 040import java.util.List; 041import java.util.Map; 042import java.util.Set; 043 044import static org.junit.Assert.assertEquals; 045import static org.junit.Assert.assertTrue; 046import static org.junit.Assert.fail; 047 048/** 049 * @author jdcasey 050 */ 051public class DefaultMojoScannerTest 052{ 053 private Map<String, MojoDescriptorExtractor> extractors; 054 055 private Build build; 056 057 private Model model; 058 059 private MojoScanner scanner; 060 061 private MavenProject project; 062 063 @Before 064 public void setUp() 065 { 066 extractors = new HashMap<>(); 067 extractors.put( "one", new ScannerTestExtractor( "one" ) ); 068 extractors.put( "two", new ScannerTestExtractor( "two" ) ); 069 extractors.put( "three", new ScannerTestExtractor( "three" ) ); 070 071 scanner = new DefaultMojoScanner( extractors ); 072 073 build = new Build(); 074 build.setSourceDirectory( "testdir" ); 075 076 model = new Model(); 077 model.setBuild( build ); 078 079 project = new MavenProject( model ); 080 project.setFile( new File( "." ) ); 081 } 082 083 @Test 084 public void testUnspecifiedExtractors() 085 throws Exception 086 { 087 PluginDescriptor pluginDescriptor = createPluginDescriptor(); 088 089 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 090 091 checkResult( pluginDescriptor, extractors.keySet() ); 092 } 093 094 @Test 095 public void testSpecifiedExtractors() 096 throws Exception 097 { 098 Set<String> activeExtractors = new HashSet<>(); 099 activeExtractors.add( "one" ); 100 activeExtractors.add( "" ); 101 activeExtractors.add( null ); 102 activeExtractors.add( "three" ); 103 104 PluginDescriptor pluginDescriptor = createPluginDescriptor(); 105 106 scanner.setActiveExtractors( activeExtractors ); 107 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 108 109 checkResult( pluginDescriptor, Arrays.asList( "one", "three" ) ); 110 } 111 112 @Test 113 public void testAllExtractorsThroughNull() 114 throws Exception 115 { 116 PluginDescriptor pluginDescriptor = createPluginDescriptor(); 117 118 scanner.setActiveExtractors( null ); 119 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 120 121 checkResult( pluginDescriptor, extractors.keySet() ); 122 } 123 124 @Test 125 public void testNoExtractorsThroughEmptySet() 126 throws Exception 127 { 128 PluginDescriptor pluginDescriptor = createPluginDescriptor(); 129 130 scanner.setActiveExtractors( Collections.<String>emptySet() ); 131 try 132 { 133 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 134 fail( "Expected exception" ); 135 } 136 catch (InvalidPluginDescriptorException e) 137 { 138 // Ok 139 } 140 141 checkResult( pluginDescriptor, Collections.<String>emptySet() ); 142 } 143 144 @Test 145 public void testUnknownExtractor() 146 throws Exception 147 { 148 Set<String> activeExtractors = new HashSet<>(); 149 activeExtractors.add( "four" ); 150 151 PluginDescriptor pluginDescriptor = createPluginDescriptor(); 152 153 scanner.setActiveExtractors( activeExtractors ); 154 155 try 156 { 157 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 158 fail( "No error for unknown extractor" ); 159 } 160 catch ( ExtractionException e ) 161 { 162 // Ok 163 } 164 165 checkResult( pluginDescriptor, Collections.<String>emptySet() ); 166 } 167 168 private PluginDescriptor createPluginDescriptor() 169 { 170 PluginDescriptor pluginDescriptor = new PluginDescriptor(); 171 pluginDescriptor.setGroupId( "groupId" ); 172 pluginDescriptor.setArtifactId( "artifactId" ); 173 pluginDescriptor.setVersion( "version" ); 174 pluginDescriptor.setGoalPrefix( "testId" ); 175 return pluginDescriptor; 176 } 177 178 /** 179 * Checks if the {@link PluginDescriptor} contains exactly the {@link MojoDescriptor}s with the 180 * supplied goal names. 181 * 182 * @param pluginDescriptor The {@link PluginDescriptor} to check. 183 * @param expectedGoals The goal names of the {@link MojoDescriptor}s. 184 */ 185 protected void checkResult( PluginDescriptor pluginDescriptor, Collection<String> expectedGoals ) 186 { 187 Set<String> remainingGoals = new HashSet<>( expectedGoals ); 188 List<MojoDescriptor> descriptors = pluginDescriptor.getMojos(); 189 190 if ( descriptors == null ) 191 { 192 // TODO Maybe getMojos should be more user friendly and not return null 193 descriptors = Collections.emptyList(); 194 } 195 196 for ( MojoDescriptor desc : descriptors ) 197 { 198 assertEquals( pluginDescriptor, desc.getPluginDescriptor() ); 199 assertTrue( "Unexpected goal in PluginDescriptor: " + desc.getGoal(), 200 remainingGoals.remove( desc.getGoal() ) ); 201 } 202 203 assertEquals( "Expected goals missing from PluginDescriptor: " + remainingGoals, 0, remainingGoals.size() ); 204 } 205 206}