CheckMojo extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { getLog().info("skipping plugin execution since missinglink.skip=true"); return; } final Consumer<String> log = verbose ? msg -> getLog().info(msg) : msg -> getLog().debug(msg); logDependencies(log); final Set<ConflictCategory> categoriesToInclude; try { categoriesToInclude = includeCategories.stream() .map(ConflictCategory::valueOf) .collect(Collectors.toSet()); } catch (IllegalArgumentException e) { getLog().error(e); throw new MojoExecutionException( "Invalid value(s) for 'includeCategories': " + includeCategories + ". " + "Valid choices are: " + Joiner.on(", ").join(ConflictCategory.values())); } Collection<Conflict> conflicts = loadArtifactsAndCheckConflicts(); final int initialCount = conflicts.size(); conflicts = filterConflicts(conflicts, categoriesToInclude); if (conflicts.isEmpty()) { getLog().info("No conflicts found"); } else { String warning = conflicts.size() + " conflicts found!"; if (initialCount != conflicts.size()) { warning += " (" + initialCount + " conflicts were found before applying filters)"; } getLog().warn(warning); outputConflicts(conflicts); if (failOnConflicts) { final String message = conflicts.size() + " class/method conflicts found between source " + "code in this project and the runtime dependencies from the Maven" + " project. Look above for specific descriptions of each conflict"; throw new MojoFailureException(message); } } }  void execute();  }
@Test public void noConflictsFound() throws Exception { getMojo("simple-test").execute(); }
@Test public void shouldIgnoreProvidedByDefault() throws Exception { CheckMojo mojo = getMojo("simple-test"); setupProvidedArtifact(mojo); mojo.execute(); verify(artifactLoader, never()).load(argThat(hasId("boobaz")), any(File.class)); }
@Test public void shouldCheckProvidedIfConfigured() throws Exception { CheckMojo mojo = getMojo("simple-test"); setupProvidedArtifact(mojo); mojo.includeScopes.add(Scope.provided); mojo.execute(); verify(artifactLoader).load(argThat(hasId("boobaz")), any(File.class)); }
@Test public void doesNotFailOnConflictByDefault() throws Exception { final CheckMojo mojo = getMojo("simple-test"); setMockConflictResults(mockConflicts(ConflictCategory.CLASS_NOT_FOUND)); mojo.execute(); }
@Test public void failsOnConflictWhenOptionIsSet() throws Exception { setMockConflictResults(mockConflicts( ConflictCategory.CLASS_NOT_FOUND, ConflictCategory.METHOD_SIGNATURE_NOT_FOUND)); exception.expect(MojoFailureException.class); exception.expectMessage("conflicts found"); getMojo("fail-on-warning").execute(); }
@Test public void testBadValuesForIncludeCategories() throws Exception { when(conflictChecker.check(any(Artifact.class), anyListOf(Artifact.class), anyListOf(Artifact.class)) ).thenThrow(new RuntimeException("Mojo should not get as far as checking conflicts if the " + "configuration is bad!")); exception.expect(MojoExecutionException.class); exception.expectMessage("Invalid value(s) for 'includeCategories'"); getMojo("include-categories-bad-values").execute(); }
@Test public void testIncludeCategoriesActuallyFilters() throws Exception { setMockConflictResults(mockConflicts(ConflictCategory.METHOD_SIGNATURE_NOT_FOUND)); getMojo("include-categories").execute(); }
@Test public void testExcludeArtifacts() throws Exception { final CheckMojo mojo = getMojo("exclude-dependencies"); mojo.project.setArtifacts(ImmutableSet.of( new DefaultArtifact("com.foobar", "bizbat", "1.2.3", "compile", "jar", "", null) )); mojo.execute(); ArgumentCaptor<ImmutableList> toCheck = ArgumentCaptor.forClass(ImmutableList.class); verify(conflictChecker).check( any(Artifact.class), toCheck.capture(), anyListOf(Artifact.class) ); assertThat(toCheck.getValue()).isEmpty(); }
@Test public void testIgnoreSourcePackages() throws Exception { final CheckMojo mojo = getMojo("ignore-source-packages"); assertThat(mojo.ignoreSourcePackages) .hasSize(2) .contains( new IgnoredPackage("groovy.lang", true), new IgnoredPackage("org.codehaus.janino", false) ); setMockConflictResults( mockConflicts(TypeDescriptors.fromClassName("groovy.lang.foo.Bar"), ConflictCategory.CLASS_NOT_FOUND) ); mojo.execute(); }
@Test public void testIgnoreDestinationPackages() throws Exception { final CalledMethod callee = new CalledMethodBuilder() .owner(TypeDescriptors.fromClassName("com/foo/Bar")) .descriptor(new MethodDescriptorBuilder() .returnType(TypeDescriptors.fromRaw("Ljava/lang/String;")) .name("bat") .parameterTypes(ImmutableList.of()) .build()) .build(); final DeclaredMethod caller = new DeclaredMethodBuilder() .methodCalls(ImmutableSet.of()) .fieldAccesses(ImmutableSet.of()) .descriptor(new MethodDescriptorBuilder() .returnType(TypeDescriptors.fromRaw("Ljava/lang/String;")) .name("bat") .parameterTypes(ImmutableList.of(TypeDescriptors.fromRaw("I"))) .build()) .build(); setMockConflictResults(ImmutableList.of( conflict(ConflictCategory.CLASS_NOT_FOUND, TypeDescriptors.fromClassName("com/Whatever"), caller, callee, "class com/foo/Bar not found") )); final CheckMojo mojo = getMojo("ignore-destination-packages"); assertThat(mojo.ignoreDestinationPackages) .hasSize(1) .contains(new IgnoredPackage("com.foo", true)); mojo.execute(); }
@Test public void testCustomBootstrapPath() throws Exception { final String testPath = File.createTempFile("boot", ".jar").getAbsolutePath(); final CheckMojo mojo = getMojo("simple-test"); mojo.bootClasspath = testPath; mojo.execute(); verify(artifactLoader).load(new File(testPath)); }
@Test public void testSkip() throws Exception { getMojo("skip").execute(); verifyNoMoreInteractions(conflictChecker, artifactLoader); }