RequirePrerequisite extends AbstractNonCacheableEnforcerRule { @Override public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException { try { MavenProject project = (MavenProject) helper.evaluate( "${project}" ); if ( "pom".equals( project.getPackaging() ) ) { helper.getLog().debug( "Packaging is pom, skipping requirePrerequisite rule" ); return; } if ( packagings != null && !packagings.contains( project.getPackaging() ) ) { helper.getLog().debug( "Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule" ); return; } Prerequisites prerequisites = project.getPrerequisites(); if ( prerequisites == null ) { throw new EnforcerRuleException( "Requires prerequisite not set" ); } if ( mavenVersion != null ) { VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion ); if ( !requiredVersionRange.hasRestrictions() ) { requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" ); } VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() ); VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion ); if ( restrictedVersionRange.getRecommendedVersion() == null ) { throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion + " ) doesn't match the required version: " + mavenVersion ); } } } catch ( ExpressionEvaluationException e ) { throw new EnforcerRuleException( e.getMessage(), e ); } catch ( InvalidVersionSpecificationException e ) { throw new EnforcerRuleException( e.getMessage(), e ); } }  void setMavenVersion( String mavenVersion ); void setPackagings( List<String> packagings ); @Override void execute( EnforcerRuleHelper helper );  }
@Test( expected = EnforcerRuleException.class ) public void testNoPrerequisite() throws Exception { RequirePrerequisite rule = new RequirePrerequisite(); rule.execute( helper ); }
@Test public void testNoSpecifiedPrerequisite() throws Exception { when( project.getPrerequisites() ).thenReturn( new Prerequisites() ); RequirePrerequisite rule = new RequirePrerequisite(); rule.execute( helper ); }
@Test public void testPomPackaging() throws Exception { when( project.getPackaging() ).thenReturn( "pom" ); Log log = mock( Log.class ); when( helper.getLog() ).thenReturn( log ); RequirePrerequisite rule = new RequirePrerequisite(); rule.execute( helper ); verify( log ).debug( "Packaging is pom, skipping requirePrerequisite rule" ); }